Mercurial > hg > MakeItSo
comparison examples/doctest.txt @ 88:712a6d358083
fixed output broke other things
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Mon, 10 Jan 2011 19:57:13 -0800 |
| parents | 3571417ef92e |
| children | 26b9c3bba04e |
comparison
equal
deleted
inserted
replaced
| 87:3571417ef92e | 88:712a6d358083 |
|---|---|
| 1 MakeItSo! | 1 MakeItSo! |
| 2 ========= | 2 ========= |
| 3 | 3 |
| 4 Boilerplate: | 4 A filesystem templating system for rapid deploy of projects |
| 5 | |
| 6 Boilerplate:: | |
| 5 | 7 |
| 6 >>> import makeitso | 8 >>> import makeitso |
| 7 >>> import os | 9 >>> import os |
| 8 >>> import shutil | 10 >>> import shutil |
| 9 >>> import tempfile | 11 >>> import tempfile |
| 13 | 15 |
| 14 >>> example = os.path.join(here, 'example.txt') | 16 >>> example = os.path.join(here, 'example.txt') |
| 15 >>> template = makeitso.PolyTemplate([example], interactive=False) | 17 >>> template = makeitso.PolyTemplate([example], interactive=False) |
| 16 >>> template.missing() | 18 >>> template.missing() |
| 17 set(['name']) | 19 set(['name']) |
| 18 >>> template.substitute(name='foo') | 20 >>> template.substitute(output=None, name='foo') |
| 19 Hello foo | 21 Hello foo |
| 20 | 22 |
| 21 Substitute to a buffer:: | 23 Substitute to a buffer:: |
| 22 | 24 |
| 23 >>> buffer = StringIO() | 25 >>> buffer = StringIO() |
| 24 >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False) | 26 >>> template = makeitso.PolyTemplate([example], interactive=False) |
| 25 >>> template.substitute(name='bar') | 27 >>> template.substitute(output=buffer, name='bar') |
| 26 >>> buffer.getvalue().strip() | 28 >>> buffer.getvalue().strip() |
| 27 'Hello bar' | 29 'Hello bar' |
| 28 | 30 |
| 29 Substitute to a file:: | 31 Substitute to a file:: |
| 30 | 32 |
| 31 >>> buffer = tempfile.mktemp() | 33 >>> buffer = tempfile.mktemp() |
| 32 >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False) | 34 >>> template = makeitso.PolyTemplate([example], interactive=False) |
| 33 >>> template.substitute(name='fleem') | 35 >>> template.substitute(buffer, name='fleem') |
| 34 >>> file(buffer).read().strip() | 36 >>> file(buffer).read().strip() |
| 35 'Hello fleem' | 37 'Hello fleem' |
| 36 >>> os.remove(buffer) | 38 >>> os.remove(buffer) |
| 37 | 39 |
| 38 Directory case:: | 40 Directory case:: |
| 39 | 41 |
| 40 >>> exampledir = os.path.join(here, 'directory-example') | 42 >>> exampledir = os.path.join(here, 'directory-example') |
| 41 >>> tempdir = tempfile.mkdtemp() | 43 >>> tempdir = tempfile.mkdtemp() |
| 42 >>> template = makeitso.PolyTemplate([exampledir], output=tempdir, interactive=False) | 44 >>> template = makeitso.PolyTemplate([exampledir], interactive=False) |
| 43 >>> sorted(template.missing()) | 45 >>> sorted(template.missing()) |
| 44 ['bar', 'foo', 'subdir'] | 46 ['bar', 'foo', 'subdir'] |
| 45 >>> template.substitute(foo='It', bar='life', subdir='mysubdir') | 47 >>> template.substitute(tempdir, foo='It', bar='life', subdir='mysubdir') |
| 46 >>> sorted(os.listdir(tempdir)) | 48 >>> sorted(os.listdir(tempdir)) |
| 47 ['foo.txt', 'mysubdir'] | 49 ['foo.txt', 'mysubdir'] |
| 48 >>> file(os.path.join(tempdir, 'foo.txt')).read().strip() | 50 >>> file(os.path.join(tempdir, 'foo.txt')).read().strip() |
| 49 'It is a wonderful life' | 51 'It is a wonderful life' |
| 50 >>> os.listdir(os.path.join(tempdir, 'mysubdir')) | 52 >>> os.listdir(os.path.join(tempdir, 'mysubdir')) |
| 62 | 64 |
| 63 You need to provide output for mixing files and directory templates:: | 65 You need to provide output for mixing files and directory templates:: |
| 64 | 66 |
| 65 >>> variables = dict([(i, i.title()) for i in variables]) | 67 >>> variables = dict([(i, i.title()) for i in variables]) |
| 66 >>> try: | 68 >>> try: |
| 67 ... template.substitute(**variables) | 69 ... template.substitute(output=None, **variables) |
| 68 ... except AssertionError, e: | 70 ... except AssertionError, e: |
| 69 ... pass | 71 ... pass |
| 70 >>> e | 72 >>> e |
| 71 AssertionError('Must specify output ... | 73 AssertionError() |
| 72 | 74 |
| 73 Provide an output:: | 75 Provide an output:: |
| 74 | 76 |
| 75 >>> template = makeitso.PolyTemplate([example, exampledir], output=tempdir, interactive=False) | 77 >>> template = makeitso.PolyTemplate([example, exampledir], interactive=False) |
| 76 >>> template.substitute(**variables) | 78 >>> template.substitute(tempdir, **variables) |
| 77 >>> sorted(os.listdir(tempdir)) | 79 >>> sorted(os.listdir(tempdir)) |
| 78 ['Subdir', 'example.txt', 'foo.txt'] | 80 ['Subdir', 'example.txt', 'foo.txt'] |
| 79 >>> shutil.rmtree(tempdir) | 81 >>> shutil.rmtree(tempdir) |
| 80 | 82 |
| 81 Test API templates:: | 83 Test API templates:: |
| 84 >>> class MyTemplate(MakeItSoTemplate): | 86 >>> class MyTemplate(MakeItSoTemplate): |
| 85 ... name = 'foo' | 87 ... name = 'foo' |
| 86 ... templates = ['example.txt'] | 88 ... templates = ['example.txt'] |
| 87 ... vars = [Variable(name='name', default='bar')] | 89 ... vars = [Variable(name='name', default='bar')] |
| 88 >>> buffer = tempfile.mktemp() | 90 >>> buffer = tempfile.mktemp() |
| 89 >>> apitemplate = MyTemplate(output=buffer) | 91 >>> apitemplate = MyTemplate(interactive=False) |
| 90 >>> apitemplate.substitute() | 92 >>> apitemplate.substitute(buffer) |
| 91 >>> file(buffer).read().strip() | 93 >>> file(buffer).read().strip() |
| 92 'Hello bar' | 94 'Hello bar' |
| 93 >>> os.remove(buffer) | 95 >>> os.remove(buffer) |
| 94 | 96 |
| 95 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:: |
| 97 >>> buffer = tempfile.mktemp() | 99 >>> buffer = tempfile.mktemp() |
| 98 >>> f = file(buffer, 'w') | 100 >>> f = file(buffer, 'w') |
| 99 >>> print >> f, '#!/bin/bash\necho foo' | 101 >>> print >> f, '#!/bin/bash\necho foo' |
| 100 >>> f.close() | 102 >>> f.close() |
| 101 >>> os.chmod(buffer, 0755) | 103 >>> os.chmod(buffer, 0755) |
| 102 >>> uritemplate = makeitso.URITemplate(example, output=buffer, interactive=False) | 104 >>> uritemplate = makeitso.URITemplate(example, interactive=False) |
| 103 >>> uritemplate.substitute(name='bar') | 105 >>> uritemplate.substitute(buffer, name='bar') |
| 104 >>> ('%o' % os.stat(buffer).st_mode).endswith('755') | 106 >>> ('%o' % os.stat(buffer).st_mode).endswith('755') |
| 105 True | 107 True |
| 106 >>> os.remove(buffer) | 108 >>> os.remove(buffer) |
