hgbook / web / texpand.py

#!/usr/bin/env python
#
# Use Django's template machinery to expand static web pages.  First
# tries the default template path for a particular installation, then
# looks for templates in the filesystem.

from django.template import Context, TemplateDoesNotExist
from django.template.loader import get_template, get_template_from_string
from django.core.management import setup_environ
import hgbook.settings as settings
import sys

setup_environ(settings)
c = Context()

if len(sys.argv) == 2:
    in_name = sys.argv[1]
    out_name = 'stdout'
    out_fp = sys.stdout
elif len(sys.argv) == 3:
    in_name = sys.argv[1]
    out_name = sys.argv[2]
    out_fp = None
else:
    print >> sys.stderr, 'Usage: %s template-file [output-file]'
    sys.exit(1)
    
try:
    t = get_template(in_name)
except TemplateDoesNotExist:
    t = get_template_from_string(open(in_name).read(), name=in_name)
if out_fp is None:
    out_fp = open(out_name, 'w')
out_fp.write(t.render(c))
out_fp.close()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.