SEESenv/web/texpand.py
author amit@thunder
Mon, 12 Apr 2010 15:12:41 +0530
changeset 49 3b5f1341d6c6
parent 2 52d12eb31c30
permissions -rwxr-xr-x
Some small changes ... bug fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
#!/usr/bin/env python
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     2
#
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     3
# Use Django's template machinery to expand static web pages.  First
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
# tries the default template path for a particular installation, then
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
# looks for templates in the filesystem.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
import sys
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
sys.path.append('/home/amit/hgbook-alqua/web/hgbook')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
import hgbook
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
from django.template import Context, TemplateDoesNotExist
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
from django.template.loader import get_template, get_template_from_string
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
from django.core.management import setup_environ
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
import hgbook.settings as settings
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
setup_environ(settings)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
c = Context()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
if len(sys.argv) == 2:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
    in_name = sys.argv[1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
    out_name = 'stdout'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
    out_fp = sys.stdout
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
elif len(sys.argv) == 3:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
    in_name = sys.argv[1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
    out_name = sys.argv[2]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
    out_fp = None
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
else:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
    print >> sys.stderr, 'Usage: %s template-file [output-file]'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
    sys.exit(1)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
    
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
try:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
    t = get_template(in_name)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
except TemplateDoesNotExist:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
    t = get_template_from_string(open(in_name).read(), name=in_name)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
if out_fp is None:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
    out_fp = open(out_name, 'w')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
out_fp.write(t.render(c))
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
out_fp.close()