2 # $Id: TemplateCmdLineIface.py,v 1.13 2006/01/10 20:34:35 tavis_rudd Exp $
4 """Provides a command line interface to compiled Cheetah template modules.
7 ================================================================================
8 Author: Tavis Rudd <tavis@damnsimple.com>
9 Version: $Revision: 1.13 $
10 Start Date: 2001/12/06
11 Last Revision Date: $Date: 2006/01/10 20:34:35 $
13 __author__
= "Tavis Rudd <tavis@damnsimple.com>"
14 __revision__
= "$Revision: 1.13 $"[11:-2]
21 from cPickle
import load
23 from pickle
import load
25 from Cheetah
.Version
import Version
27 class Error(Exception):
31 """A command line interface to compiled Cheetah template modules."""
33 def __init__(self
, templateObj
,
34 scriptName
=os
.path
.basename(sys
.argv
[0]),
35 cmdLineArgs
=sys
.argv
[1:]):
37 self
._template
= templateObj
38 self
._scriptName
= scriptName
39 self
._cmdLineArgs
= cmdLineArgs
42 """The main program controller."""
44 self
._processCmdLineArgs
()
47 def _processCmdLineArgs(self
):
49 self
._opts
, self
._args
= getopt
.getopt(
50 self
._cmdLineArgs
, 'h', ['help',
55 except getopt
.GetoptError
, v
:
56 # print help information and exit:
61 for o
, a
in self
._opts
:
62 if o
in ('-h','--help'):
66 self
._template
.searchList().insert(0, os
.environ
)
69 unpickled
= load(sys
.stdin
)
70 self
._template
.searchList().insert(0, unpickled
)
75 self
._template
.searchList().insert(0, unpickled
)
78 return """Cheetah %(Version)s template module command-line interface
82 %(scriptName)s [OPTION]
86 -h, --help Print this help information
88 --env Use shell ENVIRONMENT variables to fill the
89 $placeholders in the template.
91 --pickle <file> Use a variables from a dictionary stored in Python
92 pickle file to fill $placeholders in the template.
93 If <file> is - stdin is used:
94 '%(scriptName)s --pickle -'
99 This interface allows you to execute a Cheetah template from the command line
100 and collect the output. It can prepend the shell ENVIRONMENT or a pickled
101 Python dictionary to the template's $placeholder searchList, overriding the
102 defaults for the $placeholders.
104 """ % {'scriptName':self
._scriptName
,
108 # vim: shiftwidth=4 tabstop=4 expandtab