Package sabx10 :: Package templating :: Module generic :: Class TemplateProcessor
[hide private]
[frames] | no frames]

Class TemplateProcessor

source code

object --+
         |
        TemplateProcessor
Known Subclasses:

TemplateProcessor provides an extendable framework for working with Jinja2 templates. It allows for specifying a template file and an (optional) output file. It sets up everything necessary for Jinja2 to function, then processes the template and handles its output.

To use this class for your own purposes, just sub-class it and override the relevant functions. The process method controls the general flow, and the other methods handle specific functions within the flow.

Sample Code::

 from sabx_templating import TemplateProcessor
 TemplateProcessor().process()

Sample Usage::

 $ sample.py -o sample.out sample.jinja2
Instance Methods [hide private]
 
__init__(self, template_file=None, man=None)
Sets up handling for command-line options using the optparse module from the Python standard library.
source code
 
process_options(self, arg_count=0)
Process the options passed in on the command line.
source code
 
get_template_data(self)
Initialize the data for the template.
source code
 
process_template(self)
Setup the output file, setup the environment, then process the template file.
source code
 
process(self)
Setup for and process the template.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
list of string args
non-optional arguments specified on the command-line
string man
extended help information
options options
options specificed on the command-line
file out_file
handle to file to write processed template into
OptionParser parser
the OptionParser for handling command-line options
dict template_data
data to be processed by the template
string template_file
file name of template file
string template_path
path for template file
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, template_file=None, man=None)
(Constructor)

source code 

Sets up handling for command-line options using the optparse module from the Python standard library. Creates the option "-o" for specifying an output file and the "-m" option for printing the man-page type full help screen.

Parameters:
  • template_file (string) - (optional) path & file name of a template file
  • man (string) - (optional) extended program help information
Overrides: object.__init__

process_options(self, arg_count=0)

source code 

Process the options passed in on the command line. Make sure that a template file name is provided, either on the command line or via the constructor. If the template file is passed on the command line, it's expected to be the first non-optional argument.

Parameters:
  • arg_count (int) - count of non-optional command-line arguments expected
Raises:
  • NotEnoughArgsException - this exception is raised when there is no template file specified on the command-line

process_template(self)

source code 

Setup the output file, setup the environment, then process the template file. Put the processed template into the output file.