1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """
22 Generic framework for working with Jinja2 templates in command-line oriented
23 programs.
24 """
25
26 import os.path
27 import sys
28 from optparse import OptionParser
29 from jinja2 import Environment, FileSystemLoader
30
32 """
33 This exception is raised to indicate that not enough arguments were passed
34 in to the program.
35 """
36 pass
37
39 """
40 L{TemplateProcessor} provides an extendable framework for working with
41 Jinja2 templates. It allows for specifying a template file and an
42 (optional) output file. It sets up everything necessary for Jinja2 to
43 function, then processes the template and handles its output.
44
45 To use this class for your own purposes, just sub-class it and override the
46 relevant functions. The process method controls the general flow, and the
47 other methods handle specific functions within the flow.
48
49 Sample Code:::
50
51 from sabx_templating import TemplateProcessor
52 TemplateProcessor().process()
53
54 Sample Usage:::
55
56 $ sample.py -o sample.out sample.jinja2
57
58 @ivar parser: the C{OptionParser} for handling command-line options
59 @type parser: C{OptionParser}
60 @ivar options: options specificed on the command-line
61 @type options: C{options}
62 @ivar args: non-optional arguments specified on the command-line
63 @type args: C{list} of C{string}
64 @ivar template_path: path for template file
65 @type template_path: C{string}
66 @ivar template_file: file name of template file
67 @type template_file: C{string}
68 @ivar out_file: handle to file to write processed template into
69 @type out_file: C{file}
70 @ivar template_data: data to be processed by the template
71 @type template_data: C{dict}
72 @ivar man: extended help information
73 @type man: C{string}
74 """
75
76 - def __init__(self, template_file=None, man=None):
77 """
78 Sets up handling for command-line options using the optparse module
79 from the Python standard library. Creates the option "-o" for
80 specifying an output file and the "-m" option for printing the man-page
81 type full help screen.
82
83 @param template_file: (optional) path & file name of a template file
84 @type template_file: C{string}
85 @param man: (optional) extended program help information
86 @type man: C{string}
87 """
88 if not template_file:
89 usage = "usage: %prog [options] template_file"
90 else:
91 usage = "usage: %prog [options]"
92 self.parser = OptionParser(usage=usage)
93 self.parser.add_option("-o", "--outfile", dest="out_file",
94 help="output processed template to FILE",
95 metavar="FILE")
96
97 self.man = man
98 if man:
99 self.parser.add_option("-m", "--man-page", dest="man_page",
100 action="store_true",
101 help="display the man page and exit",
102 default=False, metavar="MANPAGE")
103
104 if template_file:
105 self.template_path, self.template_file = \
106 os.path.split(template_file)
107 else:
108 self.template_path = self.template_file = None
109
110 self.template_data = {}
111
113 """
114 Process the options passed in on the command line. Make sure that a
115 template file name is provided, either on the command line or via the
116 constructor. If the template file is passed on the command line, it's
117 expected to be the first non-optional argument.
118
119 @param arg_count: count of non-optional command-line arguments expected
120 @type arg_count: C{int}
121
122 @raise NotEnoughArgsException: this exception is raised when there
123 is no template file specified on the command-line
124 """
125 (self.options, self.args) = self.parser.parse_args()
126
127 if self.man and self.options.man_page:
128 print self.man
129 sys.exit(0)
130
131 if not self.template_file:
132 arg_count += 1
133 if len(self.args) < arg_count:
134 raise NotEnoughArgsException("NOT ENOUGH ARGUMENTS PROVIDED")
135
136 if not self.template_file:
137 self.template_path, self.template_file = os.path.split(self.args[0])
138
140 """
141 Initialize the data for the template.
142 """
143 pass
144
146 """
147 Setup the output file, setup the environment, then process the template
148 file. Put the processed template into the output file.
149 """
150 if self.options.out_file:
151 self.out_file = open(self.options.out_file, "w")
152 else:
153 self.out_file = sys.stdout
154
155 env = Environment(loader=FileSystemLoader(self.template_path),
156 trim_blocks=True)
157 template = env.get_template(self.template_file)
158 self.out_file.write(template.render(self.template_data))
159
167