1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
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