grep.py
'''\
Grep Launcher.
commands:
test - (Test function)
main - (Main function)
:::
call from code:
[module-name.]dispatch({command}, args={arguments})
invoke from system:
$PYTHON module_name {command} {arguments}
:::
Developer@Sonnack.com
November 2016
'''
from __future__ import print_function
from sys import argv, exc_info
from traceback import extract_tb
from os import path
from logger import logger, info, debug, trace
from xml_sax import parse_xml_file
from xml_grep import XmlHandlerCfgGrep
Log = logger('grep')
BasePath = r'C:\CJS\prj\Python\app'
def do_test (*args):
Log.trace('test/parameters: %s' % str(args))
cname = args[0] if 0 < len(args) else 'test'
fname = args[1] if 1 < len(args) else path.join(BasePath,'grep.xml')
oname = args[2] if 2 < len(args) else path.join(BasePath,'grep.out')
fp = open(oname, 'w')
try:
cfg_handler = XmlHandlerCfgGrep(fname, fp, Log)
parse_xml_file(fname, cfg_handler)
cfg = cfg_handler.find_configuration(cname)
if cfg:
Log.info('test/configuration: %s' % cfg)
cfg.execute()
else:
Log.error('Configuration Not Found: "%s"' % cname)
Log.info('wrote: %s' % oname)
except:
raise
finally:
fp.close()
return 'Done!'
def do_main (*args):
Log.trace('main/parameters: %s' % str(args))
cname = args[0] if 0 < len(args) else 'default'
fname = args[1] if 1 < len(args) else 'grep.xml'
oname = args[2] if 2 < len(args) else 'grep.out'
fp = open(oname, 'w')
try:
cfg_handler = XmlHandlerCfgGrep(fname, fp, Log)
parse_xml_file(fname, cfg_handler)
cfg = cfg_handler.find_configuration(cname)
if cfg:
Log.info('main/configuration: %s' % cfg)
cfg.execute()
else:
Log.error('Configuration Not Found: "%s"' % cname)
Log.info('wrote: %s' % oname)
except:
raise
finally:
fp.close()
return 'Done!'
def dispatch (cmd, *args):
Log.trace('command: %s' % cmd)
Log.trace('arguments: %d' % len(args))
if cmd == 'main': return do_main(*args)
if cmd == 'test': return do_test(*args)
return 'Nothing to do!'
if __name__ == '__main__':
print('autorun: %s' % argv[0])
Log.start(path.join(BasePath,'grep.log'))
Log.level(info())
cmd = argv[1] if 1 < len(argv) else ''
etc = argv[2:] if 2 < len(argv) else []
try:
obj = dispatch(cmd, *etc)
print(obj)
Log.info(obj)
except:
etype, evalue, tb = exc_info()
ts = extract_tb(tb)
Log.error('%s: %s' % (etype.__name__,str(evalue)))
for t in ts[-3:]:
Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
Log.error(' %s' % t[3])
raise
finally:
Log.end()
'''eof'''