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

Source Code for Module sabx10.templating.sabx

 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  Framework for processing SABX 1.0 files with Jinja2 templates using 
23  command-line programs. 
24  """ 
25   
26  import sys, os 
27  from generic import TemplateProcessor 
28  from sabx10.oxm import parse_no_def_namespaces, parse_tree 
29   
30 -class SabxProcessor(TemplateProcessor):
31 """ 32 The C{SabxProcessor} sub-classes the L{TemplateProcessor} class. It 33 provides an extendable framework for processing SABX 1.0 files with Jinja2 34 templates. It builds on the generic L{TemplateProcessor} by adding an 35 "infile" option to specify the SABX 1.0 file and by providing a routine for 36 parsing the SABX 1.0 data. 37 38 Sample Code::: 39 40 from sabx10.templating import SabxProcessor 41 SabxProcessor().process() 42 43 Sample Usage::: 44 45 $ sample.py -i sample.sabx -o sample.out sample.jinja2 46 47 @ivar in_file: handle to file to read SABX 1.0 data from 48 @type in_file: C{file} 49 @ivar sabx: C{dic} holding all the data for the given SABX 1.0 file 50 @type sabx: C{dict} 51 """
52 - def __init__(self, template_file=None, man=None):
53 """ 54 Adds the "infile" command-line option specifying the SABX 1.0 file to 55 read from to the options specified in the base class. 56 57 @param template_file: (optional) path & file name of a template file 58 @type template_file: C{string} 59 @param man: (optional) extended program help information 60 @type man: C{string} 61 """ 62 TemplateProcessor.__init__(self, template_file, man) 63 64 self.parser.add_option("-i", "--infile", dest="in_file", 65 help="input sabx data from FILE", 66 metavar="FILE")
67
68 - def get_template_data(self):
69 """ 70 Parse the specified SABX 1.0 file and save its object representation. 71 Over-ride this in sub-classes to modify the SABX data before passing it 72 to the template. 73 """ 74 if self.options.in_file: 75 self.in_file = open(self.options.in_file, "r") 76 else: 77 self.in_file = sys.stdin 78 79 tree = parse_no_def_namespaces(self.in_file) 80 self.template_data.update(parse_tree(tree)) 81 82 self.in_file.close()
83
84 - def process_template(self):
85 """ 86 Process the SABX 1.0 data into the template. First, delete the input 87 file if it is the same as the output file. This way you can read an 88 SABX 1.0 file and write the template output to the same file name, 89 effectively over-writing the file. 90 """ 91 if self.options.in_file == self.options.out_file: 92 os.unlink(self.options.in_file) 93 94 TemplateProcessor.process_template(self)
95