diff --git a/twander.py b/twander.py index 27d7e6e..c92817a 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.2 2002/10/29 00:09:22 tundra Exp $" +RCSID = "$Id: twander.py,v 1.21 2002/10/29 23:12:31 tundra Exp $" VERSION = RCSID.split()[2] @@ -46,16 +46,29 @@ FALSE = 0 == 1 # Booleans TRUE = not FALSE + +##### +# General Literals +##### + DIR_LDELIM = '[' # Directory left dsply. delimiter DIR_RDELIM = ']' # Directory left dsply. delimiter - PSEP = os.sep # Character separating path components + ##### -# Literals +# Configuration File Related Literals ##### -lCONFFILE = os.path.join(os.getenv("HOME"), "." + PROGNAME + "rc") # conf file +CONFFILE = os.path.join(os.getenv("HOME"), # Name of default config file + "." + + PROGNAME + + "rc") + +CMDKEY = r'&' # Command key delimiter +COMMENT = r"#" # Comment character +NAME = r'[NAME]' # Substitution field in config files +ENVIRO = r'$' # Introduces environment variables #----------------------------------------------------------# @@ -64,14 +77,19 @@ ##### -# Error Messages +# Error & Warning Messages ##### eBADROOT = " %s Is Not A Directory" +eDUPKEY = "Duplicate Key In Configuration File Found In Entry: \'%s\'" eERROR = "ERROR" eNOCONF = "Cannot Find Configuration File: %s" +eOPEN = "Cannot Open File: %s" eTOOMANY = "You Can Only Specify One Starting Directory." +wCMDKEY = "Configuration File Entry For: \'%s\' Has No Command Key Defined." +wWARN = "WARNING" + ##### # Informational Messages @@ -86,7 +104,7 @@ uTable = [PROGNAME + " " + VERSION + " - Copyright 2002, TundraWare Inc., All Rights Reserved\n", "usage: " + PROGNAME + " [-d dir] [-hv] [starting directory] where,\n", " startdir name of directory in which to begin (default: current dir)", - " -c file name of configuration file (default: " + lCONFFILE + ")", + " -c file name of configuration file (default: " + CONFFILE + ")", " -h print this help information", " -v print detailed version information", ] @@ -145,7 +163,7 @@ # Return Ordered List Of Directories & Files For Current Root ##### -def build_dirlist(rootdir): +def BuildDirList(rootdir): dList, fList = [], [] @@ -160,28 +178,99 @@ fList.sort() return dList + fList -# End 'build_dirlist()' +# End 'BuildDirList()' ##### # Print An Error Message ##### -def errmsg(emsg): +def ErrMsg(emsg): print PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg -# End of 'errmsg()' +# End of 'ErrMsg()' + + +##### +# Parse & Process The Configuraton File +##### + +def ParseRC(): + + try: + cf = open(conf) + except: + ErrMsg(eOPEN % conf) + sys.exit(1) + + # Process and massage the configuration file + for line in cf.read().splitlines(): + + # Lex for comment token and discard until EOL + # A line beginning with the comment token is thus + # turned into a blank line, which is discarded. + + idx = line.find(COMMENT) + if idx > -1: # found a comment character + line = line[:idx] + + # Anything which gets through the next conditional + # must be a non-blank line - i.e., Configuration information + # we care about, so process it and save for future use. + + if line != "": + fields = line.split() + for x in range(1,len(fields)): + + # Process environment variables + if fields[x][0] == ENVIRO: + fields[x] = os.getenv(fields[x][1:]) + + # Get command key value and store in dictionary + + keypos = fields[0].find(CMDKEY) # Look for key delimiter + + # No delimiter or delimiter at end of string + if (keypos < 0) or (CMDKEY == fields[0][-1]): + WrnMsg(wCMDKEY % fields[0]) + key = fields[0] # Use whole word as index + + # Found legit delimiter + else: + key = fields[0][keypos+1] # Use selected key as index + + if key in rcfile: + ErrMsg(eDUPKEY % fields[0]) # Duplicate key found + cf.close() + sys.exit(1) + + rcfile[key] = " ".join(fields[1:]) # Save entry + + cf.close() + +# End of 'ParseRC()' ##### # Print Usage Information ##### -def usage(): +def Usage(): for x in uTable: print x -# End of 'usage()' +# End of 'Usage()' + + +##### +# Print A Warning Message +##### + +def WrnMsg(wmsg): + print PROGNAME + " " + VERSION + " " + wWARN + ": " + wmsg + +# End of 'WrnMsg()' + #----------------------------------------------------------# @@ -193,16 +282,16 @@ try: opts, args = getopt.getopt(sys.argv[1:], '-c:hv') except getopt.GetoptError: - usage() + Usage() sys.exit(1) -conf = lCONFFILE +conf = CONFFILE rootdir = "." + PSEP for opt, val in opts: if opt == "-c": conf = val if opt == "-h": - usage() + Usage() sys.exit(0) if opt == "-v": print RCSID @@ -212,24 +301,30 @@ # Make sure any starting directory argument is legit if len(args) > 1: - errmsg(eTOOMANY) + ErrMsg(eTOOMANY) sys.exit(1) if len(args) == 1: rootdir = args[0] if not os.path.isdir(rootdir): - errmsg(eBADROOT % rootdir) + ErrMsg(eBADROOT % rootdir) sys.exit(1) # This program requires a config file if not os.path.exists(conf): - errmsg(eNOCONF % conf) + ErrMsg(eNOCONF % conf) sys.exit(1) +rcfile = {} +ParseRC() + +print rcfile + + # Fill the control with directory contents -DirList.items = build_dirlist(rootdir) +DirList.items = BuildDirList(rootdir) MainWin.set(title= PROGNAME + " " + VERSION + " - Examining: " + rootdir) MainApp.run()