diff --git a/tconfpy.py b/tconfpy.py index 0731bf6..46c4129 100755 --- a/tconfpy.py +++ b/tconfpy.py @@ -6,7 +6,7 @@ # Program Information PROGNAME = "tconfpy" -RCSID = "$Id: tconfpy.py,v 1.102 2004/03/09 09:50:32 tundra Exp $" +RCSID = "$Id: tconfpy.py,v 1.103 2004/03/09 23:39:33 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -17,7 +17,7 @@ RIGHTS = "All Rights Reserved" COPYRIGHT = "Copyright %s %s %s, %s." % (CPRT, DATE, OWNER, RIGHTS) PROGINFO = PROGNAME + " " + VERSION -BANNER = "%s\n%s" % (PROGINFO, COPYRIGHT) +BANNER = "%s\n%s\n%s\n" % (PROGINFO, COPYRIGHT, RCSID) #----------------------------------------------------------# @@ -25,7 +25,6 @@ #----------------------------------------------------------# - #------------------- Nothing Below Here Should Need Changing -----------------# #----------------------------------------------------------# @@ -56,16 +55,17 @@ -########## +########### # Constants -########## +########### +COMMENT = '#' # Comment introducer character MSGPOS = 10 # Where to start message output -PARSE_GOOD = True # Indicates successful parsing +PARSEOK = True # Indicates successful parsing -########## +########### # Literals -########## +########### #----------------------------------------------------------# @@ -74,31 +74,28 @@ ########## -# Error Messages +# Debug Messages ########## +dDEBUG = "DEBUG" +dNUMLINES = "Processed %d Lines In '%s'" + +########### +# Error Messages +########### + eCONFOPEN = "Cannot Open The File '%s'" eERROR = "ERROR" -########## -# Informational Messages -########## - -iERRTST = "Test Error Message - Ignore" -iINFO = "INFO" -iNUMLINES = "Processed %s Lines In '%s'" -iWARNTST = "Test Warning Message - Ignore" - - -########## +########### # Prompts -########## +########### -########## +########### # Warning Messages -########## +########### wWARNING = "WARNING" @@ -109,9 +106,15 @@ #----------------------------------------------------------# -IgnoreCase = False # Case observed by default +DEBUG = False # Control Debug output +IGNORECASE = False # Case observed by default + +DebugMsg = [] # Place to store and return debug info +ErrMsgs = [] # Place to store and return errors +WarnMsgs = [] # Place to store and return warnings + + LineNum = 0 # Keep track of the line number as we go -MsgList = [] # Place to keep any warnings and errors we find ParseOptions = {} # Options and settings for know variables SymTable = {} # Results of the parsing stored here @@ -127,30 +130,47 @@ #----------------------------------------------------------# -# Supporting Function Definitions # +# Utility Function Definitions # #----------------------------------------------------------# ########## +# Create A Debug Message +########## + +def DebugMsg(dmsg): + + global DebugMsgs + + DebugMsgs.append(mkmsg(dmsg, dDEBUG)) + +# End of 'DebugMsg()' + + +########## # Create An Error Message ########## def ErrorMsg(error): + + global ErrMsgs - return mkmsg(error + "!", eERROR) + ErrMsgs.append(mkmsg(error + "!", eERROR)) # End of 'ErrorMsg()' ########## -# Create An Informational Message +# Create A Warning Message ########## -def InfoMsg(info): +def WarningMsg(warning): - return mkmsg(info + ".", iINFO) + global WarnMsgs + + WarnMsgs.append(mkmsg(warning + "!", wWARNING)) -# End of 'InfoMsg()' +# End of 'WarningMsg()' ########## @@ -169,26 +189,14 @@ # End of 'mkmsg()' -########## -# Create A Warning Message -########## - -def WarningMsg(warning): - - return mkmsg(warning + "!", wWARNING) - -# End of 'WarningMsg()' - - #----------------------------------------------------------# # Entry Point On Direct Invocation # #----------------------------------------------------------# if __name__ == '__main__': - print BANNER + '\n' - print ErrorMsg(iERRTST) - print WarningMsg(iWARNTST) + print BANNER + #----------------------------------------------------------# @@ -196,42 +204,98 @@ #----------------------------------------------------------# -def ParseConfig(cfgfile, options, ignorecase=False): +def ParseConfig(cfgfile, Options={}, IgnoreCase=False, debug=False): - global IgnoreCase, LineNum, MsgList, ParseOptions, SymTable + global DEBUG, IGNORECASE, LineNum, ParseOptions, SymTable + global DebugMsgs, ErrMsgs, WarnMsgs # Initialize the globals - IgnoreCase = ignorecase + DEBUG = debug + IGNORECASE = IgnoreCase LineNum = 0 - MsgList = [] - ParseOptions = options + DebugMsgs = [] + ErrMsgs = [] + WarnMsgs = [] + ParseOptions = Options SymTable = {} + # Begin parsing + try: - cf = open(cfgfile) - # Successful open of config file - Begin processing it + ParseFile(cfgfile) - # Process and massage the configuration file - for line in cf.read().splitlines(): - LineNum += 1 + # Return the parsing results - # Parse this line - if line: - pass - #ParseLine(line, file, LineNum) + if DEBUG: + DebugMsg(dNUMLINES %(LineNum, cfgfile)) + + return (SymTable, ErrMsgs, WarnMsgs, DebugMsgs, PARSEOK) - # Close the config file - cf.close() - - # Return the parsing results - - MsgList.append(InfoMsg(iNUMLINES %(str(LineNum), cfgfile))) - return (SymTable, MsgList, PARSE_GOOD) - + # Something went wrong + except: - MsgList.append(ErrorMsg(eCONFOPEN % cfgfile)) - return (SymTable, MsgList, not PARSE_GOOD) + + if DEBUG: + DebugMsg(dNUMLINES %(LineNum, cfgfile)) + + ErrorMsg(eCONFOPEN % cfgfile) + return (SymTable, ErrMsgs, WarnMsgs, DebugMsgs, not PARSEOK) + + # End of 'ParseConfig()' + +#----------------------------------------------------------# +# Parser Support Functions # +#----------------------------------------------------------# + + +########## +# Condition A Line - Remove Comments & Leading/Trailing Whitespace +########## + +def ConditionLine(line): + + return line.split(COMMENT)[0].strip() + +# End of 'ConditionLine()' + + +########## +# Parse A File +########## + +def ParseFile(cfgfile): + + global IgnoreCase, LineNum, MsgList, ParseOptions, SymTable + + cf = open(cfgfile) + # Successful open of config file - Begin processing it + + # Process and massage the configuration file + for line in cf.read().splitlines(): + LineNum += 1 + + # Parse this line + ParseLine(line, cfgfile) + + # Close the config file + cf.close() + +# End of 'ParseFile()' + + +########## +# Parse A Line +########## + +def ParseLine(line, cfgfile): + + global IgnoreCase, LineNum, MsgList, ParseOptions, SymTable + + line = ConditionLine(line) + + +# End of 'ParseLine'