pf.py
'''\
Process File -- General File Processing Utilities.
commands:
/wc - Word Count
/sl - Sort Lines (of a text file)
/rl - Reverse Lines (of a text file)
main - Main function (normal use)
test - Test function (development)
usage:
module.dispatch({command}, args={arguments}) -- calling module from code
$PYTHON module_name {command} {arguments} -- executing module externally
Developer@Sonnack.com
April 2016
'''
from __future__ import print_function
from sys import argv, exc_info, stdout, stderr
from traceback import extract_tb
from os import path, listdir
from logger import logger, info, debug, trace
Log = logger('PF')
BasePath = r'C:\CJS\prj\Python\app'
def do_word_count (*args):
Log.info('count-lines: %s' % str(args))
TNB = 0
TNR = 0
TNW = 0
hdr = 'File Lines Words Bytes'
div = '---- ------ ------ ------'
fmt = '[%02d] %6d %6d %6d'
tot = 'ALL: %6d %6d %6d'
Log.info()
Log.info(hdr)
Log.info(div)
print()
print(hdr)
print(div)
for fx,iname in enumerate(args):
NB = path.getsize(iname)
NR, NW = (0,0)
fp = open(iname, 'r')
try:
for txt in fp:
NW += len(txt.split())
NR += 1
Log.debug('read[%d]: %s' % (fx,iname))
except:
raise
finally:
fp.close()
s = fmt % (1+fx, NR, NW, NB)
Log.info(s)
print(s)
TNB += NB
TNR += NR
TNW += NW
if 1 < len(args):
s = tot % (TNR, TNW, TNB)
Log.info(div)
Log.info(s)
Log.info(div)
print(div)
print(s)
print(div)
print()
Log.info()
return [TNR, TNW, TNB]
def do_sort_lines (*args):
Log.info('sort-lines: %s' % str(args))
iname = args[0]
oname = args[1] if 1 < len(args) else 'a.out'
fp = open(iname, 'r')
try:
lines = list(fp)
Log.debug('read: %s' % iname)
except:
raise
finally:
fp.close()
fp = open(oname, 'w')
try:
for line in sorted(lines):
fp.write(line)
Log.debug('wrote: %s' % oname)
except:
raise
finally:
fp.close()
return 'Done!'
def do_reverse_lines (*args):
Log.info('reverse-lines: %s' % str(args))
iname = args[0]
oname = args[1] if 1 < len(args) else 'a.out'
fp = open(iname, 'r')
try:
lines = list(fp)
Log.debug('read: %s' % iname)
except:
raise
finally:
fp.close()
fp = open(oname, 'w')
try:
for line in reversed(lines):
fp.write(line)
Log.debug('wrote: %s' % oname)
except:
raise
finally:
fp.close()
return 'Done!'
def do_test (*args):
print('test: %s' % str(args))
fname = args[0] if 0 < len(args) else ''
return 'Done!'
def do_main (*args):
print('main/parameters: %s' % str(args))
fname = args[0] if 0 < len(args) else ''
return 'Done!'
def dispatch (cmd, *args):
print('command: %s' % cmd)
print('arguments: %d' % len(args))
if cmd == '/wc': return do_word_count(*args)
if cmd == '/rl': return do_reverse_lines(*args)
if cmd == '/sl': return do_sort_lines(*args)
if cmd == 'test': return do_test(*args)
if cmd == 'main': return do_main(*args)
return 'Nothing to do!'
if __name__ == '__main__':
print('autorun: %s' % argv[0])
Log.start(path.join(BasePath,'pf.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'''