xml_config.html
001| '''\
001| XML Configuration File (SAX Parser)
001|
001| commands:
001| test - Test function
001| main - Main function
001|
001| :::
001| call from code:
001| [module-name.]dispatch({command}, args={arguments})
001|
001| invoke from system:
001| $PYTHON module_name {command} {arguments}
001|
001| :::
001| Developer@Sonnack.com
001| November 2016
001| '''
002|
003| from sys import stdout, stderr, argv
004| from os import path
005| from datetime import datetime
006| from logger import logger, info, debug, trace
007| from xml_sax import XmlHandlerConfigBase, Property
008| from xml_sax import parse_xml_file
009|
010| BasePath = r'C:\CJS\prj\Python\ws\@sandbox'
011| Log = logger('xml/sax')
012|
013|
014|
015| class XmlHandlerConfigDemo (XmlHandlerConfigBase):
016|
017| class Config (object):
018| def __init__ (self, name):
019| self.name = name
020| self.application = None
021| self.define_list = []
022| self.argument_list = []
023| self.option_list = []
024| self.filter_list = []
025| def __str__ (self): return self.name
026| def __call__ (self): return self.application
027|
028| def __init__ (self, name, fout=stdout, log=None):
029| XmlHandlerConfigBase.__init__(self, name, fout, log)
030|
031| def new_config_object (self, name):
032| return XmlHandlerConfigDemo.Config(name)
033|
034| def print_configuration (self):
035| if self.out:
036| print('Configuration: "%s"' % self.curr_config, file=self.out)
037| print('. App: %s' % str(self.curr_config()), file=self.out)
038| for prop in self.curr_config.define_list:
039| print('. def "%s"' % prop, file=self.out)
040| for prop in self.curr_config.argument_list:
041| print('. arg "%s"' % prop, file=self.out)
042| for prop in self.curr_config.option_list:
043| print('. opt "%s"' % prop, file=self.out)
044| for prop in self.curr_config.filter_list:
045| print('. flt "%s"' % prop, file=self.out)
046| print(file=self.out)
047|
048| def localStart (self, name, attrs):
049|
050| if self.state == 100:
051|
052|
053| if name == 'Application':
054| self.state = 101
055| attr_filename = self.get_attribute(attrs, 'filename')
056| attr_cwd = self.get_attribute(attrs, 'cwd')
057| g = {'fname':attr_filename, 'cwd':attr_cwd, 'text':''}
058| self.curr_config.application = g
059| return
060|
061|
062| if name == 'Defines':
063| self.state = 102
064| return
065|
066| if name == 'Arguments':
067| self.state = 103
068| return
069|
070| if name == 'Options':
071| self.state = 104
072| return
073|
074| if name == 'Filters':
075| self.state = 105
076| return
077| return
078|
079|
080| if self.state == 102:
081| if name == 'Define':
082| self.state = 112
083| attr_name = self.get_attribute(attrs,'name')
084| attr_value = self.get_attribute(attrs,'value')
085| prop = Property(attr_name, attr_value)
086| self.curr_config.define_list.append(prop)
087| return
088|
089| if self.state == 103:
090| if name == 'Argument':
091| self.state = 113
092| attr_name = self.get_attribute(attrs,'name')
093| attr_value = self.get_attribute(attrs,'value')
094| attr_type = self.get_attribute(attrs,'type')
095| prop = Property(attr_name, attr_value, attr_type)
096| self.curr_config.argument_list.append(prop)
097| return
098|
099| if self.state == 104:
100| if name == 'Option':
101| self.state = 114
102| attr_name = self.get_attribute(attrs,'name')
103| attr_value = self.get_attribute(attrs,'value')
104| attr_type = self.get_attribute(attrs,'type')
105| prop = Property(attr_name, attr_value, attr_type)
106| self.curr_config.option_list.append(prop)
107| return
108|
109| if self.state == 105:
110| if name == 'Filter':
111| self.state = 115
112| attr_name = self.get_attribute(attrs,'name')
113| attr_text = self.get_attribute(attrs,'text')
114| attr_type = self.get_attribute(attrs,'type')
115| prop = Property(attr_name, attr_text, attr_type)
116| self.curr_config.filter_list.append(prop)
117| return
118|
119| XmlHandlerConfigBase.localStart(self, name, attrs)
120|
121| def localEnd (self, name):
122|
123|
124| if self.state == 101:
125| if name == 'Application':
126| self.state = 100
127| self.curr_config.application['text'] = self.text
128| return
129|
130|
131| if self.state == 102:
132| if name == 'Defines':
133| self.state = 100
134| return
135|
136| if self.state == 103:
137| if name == 'Arguments':
138| self.state = 100
139| return
140|
141| if self.state == 104:
142| if name == 'Options':
143| self.state = 100
144| return
145|
146| if self.state == 105:
147| if name == 'Filters':
148| self.state = 100
149| return
150|
151|
152| if self.state == 112:
153| if name == 'Define':
154| self.state = 102
155| return
156|
157| if self.state == 113:
158| if name == 'Argument':
159| self.state = 103
160| return
161|
162| if self.state == 114:
163| if name == 'Option':
164| self.state = 104
165| return
166|
167| if self.state == 115:
168| if name == 'Filter':
169| self.state = 105
170| return
171|
172| XmlHandlerConfigBase.localEnd(self, name)
173|
174|
175|
176| def do_test (*args):
177| Log.info('test/parameters: %d' % len(args))
178| fname = args[0] if 0 < len(args) else path.join(BasePath, 'config.xml')
179| oname = args[1] if 1 < len(args) else path.join(BasePath, 'xml_config.out')
180| fp = open(oname, mode='w', encoding='utf-8')
181| try:
182|
183| cfg = XmlHandlerConfigDemo(fname, fp, Log)
184| parse_xml_file(fname, cfg)
185|
186|
187|
188|
189| Log.info('wrote: %s' % oname)
190| except Exception as e:
191| Log.error(e)
192| raise
193| finally:
194| fp.close()
195| return [cfg, fname, oname]
196|
197| def do_list (*args):
198| Log.info('list/parameters: %d' % len(args))
199| fname = args[0] if 0 < len(args) else path.join(BasePath, 'config.xml')
200| oname = args[1] if 1 < len(args) else path.join(BasePath, 'xml_config.out')
201|
202| cfg = XmlHandlerConfigDemo(fname, stdout, Log)
203| parse_xml_file(fname, cfg)
204|
205| fp = open(oname, mode='w', encoding='utf-8')
206| try:
207| cfg.out = fp
208| cfg.print_configurations()
209| Log.info('wrote: %s' % oname)
210| except Exception as e:
211| Log.error(e)
212| raise
213| finally:
214| fp.close()
215| return [cfg, fname, oname]
216|
217| def do_main (*args):
218| Log.info('main/parameters: %d' % len(args))
219| fname = args[0] if 0 < len(args) else path.join(BasePath, 'config.xml')
220| oname = args[1] if 1 < len(args) else path.join(BasePath, 'xml_config.out')
221|
222| return [None, fname, oname]
223|
224| def dispatch (cmd, *args):
225| Log.info('command: %s' % cmd)
226| Log.debug('arguments: %d' % len(args))
227| if cmd == 'main': return do_main(*args)
228| if cmd == 'list': return do_list(*args)
229| if cmd == 'test': return do_test(*args)
230| return [None, cmd, args]
231|
232| if __name__ == '__main__':
233| print('autorun: %s' % argv[0])
234| cmd = argv[1] if 1 < len(argv) else 'main'
235| etc = argv[2:]
236| Log.start('xml_config.log')
237| Log.level(trace())
238| try:
239| obj = dispatch(cmd, *etc)
240| Log.debug(type(obj))
241| Log.debug(repr(obj))
242| print('exit/type: %s (length: %d)' % (type(obj), len(obj)))
243| except Exception as e:
244| print('ERROR! "%s"' % e, file=stderr)
245| Log.error(e)
246| raise
247| finally:
248| Log.end()
249|
250| '''eof'''
251|