root/galaxy-central/eggs/Cheetah-2.2.2-py2.6-macosx-10.6-universal-ucs2.egg/Cheetah/TemplateCmdLineIface.py

リビジョン 3, 3.2 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
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
5Meta-Data
6================================================================================
7Author: Tavis Rudd <tavis@damnsimple.com>
8Version: $Revision: 1.13 $
9Start Date: 2001/12/06
10Last 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
15import sys
16import os
17import getopt
18import os.path
19try:
20    from cPickle import load
21except ImportError:
22    from pickle import load
23
24from Cheetah.Version import Version
25
26class Error(Exception):
27    pass
28
29class 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
79Usage
80-----
81  %(scriptName)s [OPTION]
82
83Options
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
95Description
96-----------
97
98This interface allows you to execute a Cheetah template from the command line
99and collect the output.  It can prepend the shell ENVIRONMENT or a pickled
100Python dictionary to the template's $placeholder searchList, overriding the
101defaults for the $placeholders.
102
103""" % {'scriptName':self._scriptName,
104       'Version':Version,
105       }
106
107# vim: shiftwidth=4 tabstop=4 expandtab
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。