"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/TemplateCmdLineIface.py" (20 Apr 2021, 2846 Bytes) of package /linux/www/cheetah3-3.2.6.post2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "TemplateCmdLineIface.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3-3.2.1_vs_3-3.2.2.

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