#!/usr/bin/env python # tconfpy.py # Copyright (c) 2003 TundraWare Inc. All Rights Reserved. # For Updates See: http://www.tundraware.com/Software/tconfpy # Program Information PROGNAME = "tconfpy" RCSID = "$Id: tconfpy.py,v 1.102 2004/03/09 09:50:32 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information CPRT = chr(169) DATE = "2003-2004" 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) #----------------------------------------------------------# # Variables User Might Change # #----------------------------------------------------------# #------------------- Nothing Below Here Should Need Changing -----------------# #----------------------------------------------------------# # Public Features Of This Module # #----------------------------------------------------------# __all__ = ["ParseConfig"] #----------------------------------------------------------# # Imports # #----------------------------------------------------------# import os.path #----------------------------------------------------------# # Aliases & Redefinitions # #----------------------------------------------------------# #----------------------------------------------------------# # Constants & Literals # #----------------------------------------------------------# ########## # Constants ########## MSGPOS = 10 # Where to start message output PARSE_GOOD = True # Indicates successful parsing ########## # Literals ########## #----------------------------------------------------------# # Prompts, & Application Strings # #----------------------------------------------------------# ########## # 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 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 #--------------------------- Code Begins Here ---------------------------------# #----------------------------------------------------------# # Object Base Class Definitions # #----------------------------------------------------------# #----------------------------------------------------------# # Supporting Function Definitions # #----------------------------------------------------------# ########## # Create An Error Message ########## def ErrorMsg(error): return mkmsg(error + "!", eERROR) # End of 'ErrorMsg()' ########## # Create An Informational Message ########## def InfoMsg(info): return mkmsg(info + ".", iINFO) # End of 'InfoMsg()' ########## # Construct A Standard Application Message String ########## def mkmsg(msg, msgtype=""): if msgtype: msgtype += ":" pad = " " * (MSGPOS - len(msgtype)) return "%s - %s%s%s" % (PROGINFO, msgtype, pad, msg) # 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) #----------------------------------------------------------# # Public API To Module # #----------------------------------------------------------# def ParseConfig(cfgfile, options, ignorecase=False): 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()'