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

Source Code for Module sabx10.templating.generic

  1  ############################################################################### 
  2  # 
  3  # sabx10 - an SABX file manipulation library 
  4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
  5  # 
  6  # This file is part of sabx10. 
  7  # 
  8  # sabx10 is free software: you can redistribute it and/or modify it under the 
  9  # terms of the GNU General Public License as published by the Free Software 
 10  # Foundation, either version 3 of the License, or (at your option) any later 
 11  # version. 
 12  # 
 13  # sabx10 is distributed in the hope that it will be useful, but WITHOUT ANY 
 14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 15  # A PARTICULAR PURPOSE.  See the GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License along with 
 18  # sabx10.  If not, see <http://www.gnu.org/licenses/>. 
 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   
31 -class NotEnoughArgsException(Exception):
32 """ 33 This exception is raised to indicate that not enough arguments were passed 34 in to the program. 35 """ 36 pass
37
38 -class TemplateProcessor(object):
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
112 - def process_options(self, arg_count=0):
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
139 - def get_template_data(self):
140 """ 141 Initialize the data for the template. 142 """ 143 pass
144
145 - def process_template(self):
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
160 - def process(self):
161 """ 162 Setup for and process the template. 163 """ 164 self.process_options() 165 self.get_template_data() 166 self.process_template()
167