hgbook
annotate web/texpand.py @ 1052:c8b5a06543c0
cvs section translated
author | zhaopingsun |
---|---|
date | Tue Nov 10 16:25:43 2009 -0500 (2009-11-10) |
parents | ad304b606163 |
children |
rev | line source |
---|---|
bos@574 | 1 #!/usr/bin/env python |
bos@574 | 2 # |
bos@574 | 3 # Use Django's template machinery to expand static web pages. First |
bos@574 | 4 # tries the default template path for a particular installation, then |
bos@574 | 5 # looks for templates in the filesystem. |
bos@574 | 6 |
bos@574 | 7 from django.template import Context, TemplateDoesNotExist |
bos@574 | 8 from django.template.loader import get_template, get_template_from_string |
bos@574 | 9 from django.core.management import setup_environ |
bos@598 | 10 import hgbook.settings as settings |
bos@574 | 11 import sys |
bos@574 | 12 |
bos@574 | 13 setup_environ(settings) |
bos@574 | 14 c = Context() |
bos@574 | 15 |
bos@574 | 16 if len(sys.argv) == 2: |
bos@574 | 17 in_name = sys.argv[1] |
bos@574 | 18 out_name = 'stdout' |
bos@574 | 19 out_fp = sys.stdout |
bos@574 | 20 elif len(sys.argv) == 3: |
bos@574 | 21 in_name = sys.argv[1] |
bos@574 | 22 out_name = sys.argv[2] |
bos@574 | 23 out_fp = None |
bos@574 | 24 else: |
bos@574 | 25 print >> sys.stderr, 'Usage: %s template-file [output-file]' |
bos@574 | 26 sys.exit(1) |
bos@574 | 27 |
bos@574 | 28 try: |
bos@574 | 29 t = get_template(in_name) |
bos@574 | 30 except TemplateDoesNotExist: |
bos@574 | 31 t = get_template_from_string(open(in_name).read(), name=in_name) |
bos@574 | 32 if out_fp is None: |
bos@574 | 33 out_fp = open(out_name, 'w') |
bos@574 | 34 out_fp.write(t.render(c)) |
bos@574 | 35 out_fp.close() |