Mercurial > hg > MakeItSo
comparison examples/doctest.txt @ 90:26b9c3bba04e
make the api for substitute() variables, output
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Mon, 10 Jan 2011 21:33:03 -0800 |
| parents | 712a6d358083 |
| children | 672d2d3ee322 |
comparison
equal
deleted
inserted
replaced
| 89:e055447376ab | 90:26b9c3bba04e |
|---|---|
| 15 | 15 |
| 16 >>> example = os.path.join(here, 'example.txt') | 16 >>> example = os.path.join(here, 'example.txt') |
| 17 >>> template = makeitso.PolyTemplate([example], interactive=False) | 17 >>> template = makeitso.PolyTemplate([example], interactive=False) |
| 18 >>> template.missing() | 18 >>> template.missing() |
| 19 set(['name']) | 19 set(['name']) |
| 20 >>> template.substitute(output=None, name='foo') | 20 >>> template.substitute(dict(name='foo')) |
| 21 Hello foo | 21 Hello foo |
| 22 | 22 |
| 23 Substitute to a buffer:: | 23 Substitute to a buffer:: |
| 24 | 24 |
| 25 >>> buffer = StringIO() | 25 >>> buffer = StringIO() |
| 26 >>> template = makeitso.PolyTemplate([example], interactive=False) | 26 >>> template = makeitso.PolyTemplate([example], interactive=False) |
| 27 >>> template.substitute(output=buffer, name='bar') | 27 >>> template.substitute(dict(name='bar'), output=buffer) |
| 28 >>> buffer.getvalue().strip() | 28 >>> buffer.getvalue().strip() |
| 29 'Hello bar' | 29 'Hello bar' |
| 30 | 30 |
| 31 Substitute to a file:: | 31 Substitute to a file:: |
| 32 | 32 |
| 33 >>> buffer = tempfile.mktemp() | 33 >>> buffer = tempfile.mktemp() |
| 34 >>> template = makeitso.PolyTemplate([example], interactive=False) | 34 >>> template = makeitso.PolyTemplate([example], interactive=False) |
| 35 >>> template.substitute(buffer, name='fleem') | 35 >>> template.substitute(dict(name='fleem'), buffer) |
| 36 >>> file(buffer).read().strip() | 36 >>> file(buffer).read().strip() |
| 37 'Hello fleem' | 37 'Hello fleem' |
| 38 >>> os.remove(buffer) | 38 >>> os.remove(buffer) |
| 39 | 39 |
| 40 Directory case:: | 40 Directory case:: |
| 42 >>> exampledir = os.path.join(here, 'directory-example') | 42 >>> exampledir = os.path.join(here, 'directory-example') |
| 43 >>> tempdir = tempfile.mkdtemp() | 43 >>> tempdir = tempfile.mkdtemp() |
| 44 >>> template = makeitso.PolyTemplate([exampledir], interactive=False) | 44 >>> template = makeitso.PolyTemplate([exampledir], interactive=False) |
| 45 >>> sorted(template.missing()) | 45 >>> sorted(template.missing()) |
| 46 ['bar', 'foo', 'subdir'] | 46 ['bar', 'foo', 'subdir'] |
| 47 >>> template.substitute(tempdir, foo='It', bar='life', subdir='mysubdir') | 47 >>> template.substitute(dict(foo='It', bar='life', subdir='mysubdir'), output=tempdir) |
| 48 >>> sorted(os.listdir(tempdir)) | 48 >>> sorted(os.listdir(tempdir)) |
| 49 ['foo.txt', 'mysubdir'] | 49 ['foo.txt', 'mysubdir'] |
| 50 >>> file(os.path.join(tempdir, 'foo.txt')).read().strip() | 50 >>> file(os.path.join(tempdir, 'foo.txt')).read().strip() |
| 51 'It is a wonderful life' | 51 'It is a wonderful life' |
| 52 >>> os.listdir(os.path.join(tempdir, 'mysubdir')) | 52 >>> os.listdir(os.path.join(tempdir, 'mysubdir')) |
| 64 | 64 |
| 65 You need to provide output for mixing files and directory templates:: | 65 You need to provide output for mixing files and directory templates:: |
| 66 | 66 |
| 67 >>> variables = dict([(i, i.title()) for i in variables]) | 67 >>> variables = dict([(i, i.title()) for i in variables]) |
| 68 >>> try: | 68 >>> try: |
| 69 ... template.substitute(output=None, **variables) | 69 ... template.substitute(variables) |
| 70 ... except AssertionError, e: | 70 ... except AssertionError, e: |
| 71 ... pass | 71 ... pass |
| 72 >>> e | 72 >>> e |
| 73 AssertionError() | 73 AssertionError() |
| 74 | 74 |
| 75 Provide an output:: | 75 Provide an output:: |
| 76 | 76 |
| 77 >>> template = makeitso.PolyTemplate([example, exampledir], interactive=False) | 77 >>> template = makeitso.PolyTemplate([example, exampledir], interactive=False) |
| 78 >>> template.substitute(tempdir, **variables) | 78 >>> template.substitute(variables, tempdir) |
| 79 >>> sorted(os.listdir(tempdir)) | 79 >>> sorted(os.listdir(tempdir)) |
| 80 ['Subdir', 'example.txt', 'foo.txt'] | 80 ['Subdir', 'example.txt', 'foo.txt'] |
| 81 >>> shutil.rmtree(tempdir) | 81 >>> shutil.rmtree(tempdir) |
| 82 | 82 |
| 83 Test API templates:: | 83 Test API templates:: |
| 87 ... name = 'foo' | 87 ... name = 'foo' |
| 88 ... templates = ['example.txt'] | 88 ... templates = ['example.txt'] |
| 89 ... vars = [Variable(name='name', default='bar')] | 89 ... vars = [Variable(name='name', default='bar')] |
| 90 >>> buffer = tempfile.mktemp() | 90 >>> buffer = tempfile.mktemp() |
| 91 >>> apitemplate = MyTemplate(interactive=False) | 91 >>> apitemplate = MyTemplate(interactive=False) |
| 92 >>> apitemplate.substitute(buffer) | 92 >>> apitemplate.substitute({}, buffer) |
| 93 >>> file(buffer).read().strip() | 93 >>> file(buffer).read().strip() |
| 94 'Hello bar' | 94 'Hello bar' |
| 95 >>> os.remove(buffer) | 95 >>> os.remove(buffer) |
| 96 | 96 |
| 97 Test to make sure permissions are preserved. This won't work on windows:: | 97 Test to make sure permissions are preserved. This won't work on windows:: |
| 100 >>> f = file(buffer, 'w') | 100 >>> f = file(buffer, 'w') |
| 101 >>> print >> f, '#!/bin/bash\necho foo' | 101 >>> print >> f, '#!/bin/bash\necho foo' |
| 102 >>> f.close() | 102 >>> f.close() |
| 103 >>> os.chmod(buffer, 0755) | 103 >>> os.chmod(buffer, 0755) |
| 104 >>> uritemplate = makeitso.URITemplate(example, interactive=False) | 104 >>> uritemplate = makeitso.URITemplate(example, interactive=False) |
| 105 >>> uritemplate.substitute(buffer, name='bar') | 105 >>> uritemplate.substitute(dict(name='bar'), buffer) |
| 106 >>> ('%o' % os.stat(buffer).st_mode).endswith('755') | 106 >>> ('%o' % os.stat(buffer).st_mode).endswith('755') |
| 107 True | 107 True |
| 108 >>> os.remove(buffer) | 108 >>> os.remove(buffer) |
