Cleaned up API and return data structures.
Added facility for line conditioning.
Added Debug support.
1 parent 961ad80 commit c333cab4f86017b03de9980b8231a4a14afc391f
@tundra tundra authored on 9 Mar 2004
Showing 1 changed file
View
356
tconfpy.py
 
# 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
 
OWNER = "TundraWare Inc."
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)
 
 
#----------------------------------------------------------#
# Variables User Might Change #
#----------------------------------------------------------#
 
 
 
#------------------- Nothing Below Here Should Need Changing -----------------#
 
#----------------------------------------------------------#
# Public Features Of This Module #
#----------------------------------------------------------#
 
 
 
##########
###########
# Constants
##########
 
###########
 
COMMENT = '#' # Comment introducer character
MSGPOS = 10 # Where to start message output
PARSE_GOOD = True # Indicates successful parsing
 
##########
PARSEOK = True # Indicates successful parsing
 
###########
# Literals
##########
###########
 
 
#----------------------------------------------------------#
# Prompts, & Application Strings #
#----------------------------------------------------------#
 
 
##########
# 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"
 
 
# Global Variables & Data Structures #
#----------------------------------------------------------#
 
 
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
 
 
 
 
#----------------------------------------------------------#
# 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):
return mkmsg(error + "!", eERROR)
 
global ErrMsgs
ErrMsgs.append(mkmsg(error + "!", eERROR))
 
# End of 'ErrorMsg()'
 
 
##########
# Create An Informational Message
##########
 
def InfoMsg(info):
 
return mkmsg(info + ".", iINFO)
 
# End of 'InfoMsg()'
# Create A Warning Message
##########
 
def WarningMsg(warning):
 
global WarnMsgs
WarnMsgs.append(mkmsg(warning + "!", wWARNING))
 
# End of 'WarningMsg()'
 
 
##########
# Construct A Standard Application Message String
 
# 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
 
 
 
#----------------------------------------------------------#
# Public API To Module #
#----------------------------------------------------------#
 
 
def ParseConfig(cfgfile, options, ignorecase=False):
def ParseConfig(cfgfile, Options={}, IgnoreCase=False, debug=False):
 
global DEBUG, IGNORECASE, LineNum, ParseOptions, SymTable
global DebugMsgs, ErrMsgs, WarnMsgs
# Initialize the globals
 
DEBUG = debug
IGNORECASE = IgnoreCase
LineNum = 0
DebugMsgs = []
ErrMsgs = []
WarnMsgs = []
ParseOptions = Options
SymTable = {}
 
# Begin parsing
try:
ParseFile(cfgfile)
 
# Return the parsing results
 
if DEBUG:
DebugMsg(dNUMLINES %(LineNum, cfgfile))
return (SymTable, ErrMsgs, WarnMsgs, DebugMsgs, PARSEOK)
 
# Something went wrong
except:
 
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
# Initialize the globals
 
IgnoreCase = ignorecase
LineNum = 0
MsgList = []
ParseOptions = options
SymTable = {}
 
try:
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
if line:
pass
#ParseLine(line, file, LineNum)
 
# Close the config file
cf.close()
 
# Return the parsing results
 
MsgList.append(InfoMsg(iNUMLINES %(str(LineNum), cfgfile)))
return (SymTable, MsgList, PARSE_GOOD)
 
except:
MsgList.append(ErrorMsg(eCONFOPEN % cfgfile))
return (SymTable, MsgList, not PARSE_GOOD)
 
# End of 'ParseConfig()'
 
 
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'