| 1 | # $Id: TemplateCmdLineIface.py,v 1.13 2006/01/10 20:34:35 tavis_rudd Exp $ |
|---|
| 2 | |
|---|
| 3 | """Provides a command line interface to compiled Cheetah template modules. |
|---|
| 4 | |
|---|
| 5 | Meta-Data |
|---|
| 6 | ================================================================================ |
|---|
| 7 | Author: Tavis Rudd <tavis@damnsimple.com> |
|---|
| 8 | Version: $Revision: 1.13 $ |
|---|
| 9 | Start Date: 2001/12/06 |
|---|
| 10 | Last Revision Date: $Date: 2006/01/10 20:34:35 $ |
|---|
| 11 | """ |
|---|
| 12 | __author__ = "Tavis Rudd <tavis@damnsimple.com>" |
|---|
| 13 | __revision__ = "$Revision: 1.13 $"[11:-2] |
|---|
| 14 | |
|---|
| 15 | import sys |
|---|
| 16 | import os |
|---|
| 17 | import getopt |
|---|
| 18 | import os.path |
|---|
| 19 | try: |
|---|
| 20 | from cPickle import load |
|---|
| 21 | except ImportError: |
|---|
| 22 | from pickle import load |
|---|
| 23 | |
|---|
| 24 | from Cheetah.Version import Version |
|---|
| 25 | |
|---|
| 26 | class Error(Exception): |
|---|
| 27 | pass |
|---|
| 28 | |
|---|
| 29 | class CmdLineIface: |
|---|
| 30 | """A command line interface to compiled Cheetah template modules.""" |
|---|
| 31 | |
|---|
| 32 | def __init__(self, templateObj, |
|---|
| 33 | scriptName=os.path.basename(sys.argv[0]), |
|---|
| 34 | cmdLineArgs=sys.argv[1:]): |
|---|
| 35 | |
|---|
| 36 | self._template = templateObj |
|---|
| 37 | self._scriptName = scriptName |
|---|
| 38 | self._cmdLineArgs = cmdLineArgs |
|---|
| 39 | |
|---|
| 40 | def run(self): |
|---|
| 41 | """The main program controller.""" |
|---|
| 42 | |
|---|
| 43 | self._processCmdLineArgs() |
|---|
| 44 | print self._template |
|---|
| 45 | |
|---|
| 46 | def _processCmdLineArgs(self): |
|---|
| 47 | try: |
|---|
| 48 | self._opts, self._args = getopt.getopt( |
|---|
| 49 | self._cmdLineArgs, 'h', ['help', |
|---|
| 50 | 'env', |
|---|
| 51 | 'pickle=', |
|---|
| 52 | ]) |
|---|
| 53 | |
|---|
| 54 | except getopt.GetoptError, v: |
|---|
| 55 | # print help information and exit: |
|---|
| 56 | print v |
|---|
| 57 | print self.usage() |
|---|
| 58 | sys.exit(2) |
|---|
| 59 | |
|---|
| 60 | for o, a in self._opts: |
|---|
| 61 | if o in ('-h','--help'): |
|---|
| 62 | print self.usage() |
|---|
| 63 | sys.exit() |
|---|
| 64 | if o == '--env': |
|---|
| 65 | self._template.searchList().insert(0, os.environ) |
|---|
| 66 | if o == '--pickle': |
|---|
| 67 | if a == '-': |
|---|
| 68 | unpickled = load(sys.stdin) |
|---|
| 69 | self._template.searchList().insert(0, unpickled) |
|---|
| 70 | else: |
|---|
| 71 | f = open(a) |
|---|
| 72 | unpickled = load(f) |
|---|
| 73 | f.close() |
|---|
| 74 | self._template.searchList().insert(0, unpickled) |
|---|
| 75 | |
|---|
| 76 | def usage(self): |
|---|
| 77 | return """Cheetah %(Version)s template module command-line interface |
|---|
| 78 | |
|---|
| 79 | Usage |
|---|
| 80 | ----- |
|---|
| 81 | %(scriptName)s [OPTION] |
|---|
| 82 | |
|---|
| 83 | Options |
|---|
| 84 | ------- |
|---|
| 85 | -h, --help Print this help information |
|---|
| 86 | |
|---|
| 87 | --env Use shell ENVIRONMENT variables to fill the |
|---|
| 88 | $placeholders in the template. |
|---|
| 89 | |
|---|
| 90 | --pickle <file> Use a variables from a dictionary stored in Python |
|---|
| 91 | pickle file to fill $placeholders in the template. |
|---|
| 92 | If <file> is - stdin is used: |
|---|
| 93 | '%(scriptName)s --pickle -' |
|---|
| 94 | |
|---|
| 95 | Description |
|---|
| 96 | ----------- |
|---|
| 97 | |
|---|
| 98 | This interface allows you to execute a Cheetah template from the command line |
|---|
| 99 | and collect the output. It can prepend the shell ENVIRONMENT or a pickled |
|---|
| 100 | Python dictionary to the template's $placeholder searchList, overriding the |
|---|
| 101 | defaults for the $placeholders. |
|---|
| 102 | |
|---|
| 103 | """ % {'scriptName':self._scriptName, |
|---|
| 104 | 'Version':Version, |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | # vim: shiftwidth=4 tabstop=4 expandtab |
|---|