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 argvexc_info
from traceback import extract_tb
from os import path
from logger import loggerinfodebugtrace
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[0if 0 < len(argselse 'test'
    fname = args[1if 1 < len(argselse path.join(BasePath,'grep.xml')
    oname = args[2if 2 < len(argselse path.join(BasePath,'grep.out')
    # Go...
    fp = open(oname'w')
    try:
        # Parse...
        cfg_handler = XmlHandlerCfgGrep(fnamefpLog)
        parse_xml_file(fnamecfg_handler)
        # Get a configuration...
        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[0if 0 < len(argselse 'default'
    fname = args[1if 1 < len(argselse 'grep.xml'
    oname = args[2if 2 < len(argselse 'grep.out'
    # Go...
    fp = open(oname'w')
    try:
        # Parse...
        cfg_handler = XmlHandlerCfgGrep(fnamefpLog)
        parse_xml_file(fnamecfg_handler)
        # Get a configuration...
        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[1if 1 < len(argvelse ''
    etc = argv[2:if 2 < len(argvelse []
    try:
        obj = dispatch(cmd, *etc)
        print(obj)
        Log.info(obj)
    except:
        etypeevaluetb = 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'''