diff --git a/tgrepcsv.py b/tgrepcsv.py index 49828b8..6198a9a 100755 --- a/tgrepcsv.py +++ b/tgrepcsv.py @@ -3,11 +3,11 @@ # Copyright (c) 2012 TundraWare Inc., Des Plaines, IL 60018 USA # All Rights Reserved. For Terms Of Use See: tgrepcsv-license.txt # For Program Updates See: http://www.tundraware.com/Software/tgrepcsv -# $Id: tgrepcsv.py,v 1.101 2012/09/05 18:30:41 tundra Exp $ +# $Id: tgrepcsv.py,v 1.102 2012/09/05 19:52:30 tundra Exp $ # Embed the source control ID string for use by program -CVSID='$Id: tgrepcsv.py,v 1.101 2012/09/05 18:30:41 tundra Exp $' +CVSID='$Id: tgrepcsv.py,v 1.102 2012/09/05 19:52:30 tundra Exp $' ##### # Program Information @@ -29,7 +29,7 @@ # List Of All Legal Options - Update This When You Add More!!!! ##### -OPTIONSLIST = '-hv' +OPTIONSLIST = '-c:hv' #----------------------------------------------------------# @@ -67,7 +67,8 @@ # Constants ##### - +COLUMNWIDTH = 16 +MINCOL = 8 ##### # Literals @@ -81,40 +82,23 @@ ##### -# Debug Messages -##### - -##### -# Debug Messages -##### - -DEBUGFLAG = "-d" -dDEBUG = "DEBUG" -dPROGENV = "$" + PROGENV - -##### # Error Messages ##### -eBADARG = "Invalid command line: %s!" -eERROR = "ERROR" -eFEWARGS = "Too few commmand line arguments!" - - -##### -# Informational Messages -##### - -iINFO = "INFO" - +eBADARG = "Invalid command line: %s!" +eERROR = "ERROR" +eFEWARGS = "Too few commmand line arguments!" +eMINCOL = "Minimum column width is %s!" % MINCOL +eNOTINTEGER = "%s is not an integer!" ##### # Usage Prompts ##### uTable = [PROGVER, - "usage: " + PROGNAME + " [-hv]", + "usage: " + PROGNAME + " [-c # -hv]", " where,", + " -c # column width (default: 16)", " -h print this help information", " -v print detailed version information", ] @@ -139,11 +123,11 @@ #----------------------------------------------------------# -def ColumnPad(list, padchar=" ", padwidth=20): +def ColumnPad(list, padchar=" ", padwidth=COLUMNWIDTH): retval = "" for l in list: - l = str(l) + l = str(l[:padwidth-1]) retval += l + ((padwidth - len(l)) * padchar) return retval.strip() @@ -152,35 +136,6 @@ ##### -# Print A Debug Message -##### - -def DebugMsg(msg): - PrintStderr(PROGNAME + " " + dDEBUG + ": " + msg) - -# End of 'DebugMsg()' - - -##### -# Dump The State Of The Program -##### - -def DumpState(): - - # Dump the command line - DebugMsg(ColumnPad(["Command Line", sys.argv])) - - # Names of all the state variables we want dumped - state = [ - ] - - for k in state: - DebugMsg(ColumnPad([k, eval(k)])) - -# End of 'DumpState()' - - -##### # Print An Error Message ##### @@ -191,16 +146,6 @@ ##### -# Print An Info Message -##### - -def InfoMsg(imsg): - PrintStderr(PROGNAME + " " + iINFO + ": " + imsg) - -# End of 'InfoMsg()' - - -##### # Print To stderr ##### @@ -253,6 +198,18 @@ for opt, val in opts: + if opt == "-c": + try: + COLUMNWIDTH = int(val) + + except: + ErrorMsg(eNOTINTEGER % val) + sys.exit(1) + + if COLUMNWIDTH < MINCOL: + ErrorMsg(eMINCOL) + sys.exit(1) + if opt == "-h": Usage() sys.exit(0) @@ -264,13 +221,13 @@ # Check and parse command line args -if len(sys.argv) < 3: +if len(args) < 2: - PrintStderr(eFEWARGS) + ErrorMsg(eFEWARGS) sys.exit(1) -CSVFile = sys.argv[1] -Patterns = sys.argv[2:] +CSVFile = args[0] +Patterns = args[1:] # Read the file into a local list @@ -287,7 +244,8 @@ for row in content: - line = " ".join(row) + line = ColumnPad(row, padwidth=COLUMNWIDTH) + match = False for pattern in Patterns: if pattern in line: