diff --git a/twander.py b/twander.py index 372ecd7..8036a93 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ # For Updates See: http://www.tundraware.com/Software/twander PROGNAME = "twander" -RCSID = "$Id: twander.py,v 2.58 2003/01/15 21:41:48 tundra Exp $" +RCSID = "$Id: twander.py,v 2.59 2003/01/15 23:33:58 tundra Exp $" VERSION = RCSID.split()[2] @@ -334,7 +334,7 @@ wBADEXE = "Could Not Execute Command:\n\n%s" wBADVAL = "Option Assignment Has Bad Righthand Side.\nIgnoring Line %s:\n\n%s" wCMDKEY = "Configuration File Entry For: \'%s\' Has No Command Key Defined." -wCONFOPEN = "Cannot Open Configuration File:\n%s\n\n%s" +wCONFOPEN = "Cannot Open Configuration File:\n%s" wDIRSCREDEF = "Directory Shortcut Defined More Than Once.\nIgnoring Line %s:\n\n%s" wLINKBACK = "%s Points Back To Own Directory" wNOCMDS = "Running With No Commands Defined!" @@ -464,7 +464,7 @@ # while the program is running. ##### -def ParseConfFile(event): +def ParseConfFile(event, DoOptionsProcessing=TRUE): global CONF, UI # Cleanout any old configuration data @@ -528,107 +528,25 @@ CONF = os.path.join(HOME, "." + PROGNAME) try: cf = open(CONF) + # 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: + ParseLine(line, linenum) + + # Close the config file + cf.close() + except: - WrnMsg(wCONFOPEN % (CONF, wNOCMDS)) - return + WrnMsg(wCONFOPEN % CONF) - # 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: - ParseLine(line, linenum) - - - # Rebind all the handlers - UI.BindAllHandlers() - - # Close the config file - cf.close() - - # Set the Command Menu Contents, if any, - # and enable the menu if it has entries. - # If no commands are defined, warn the user. - - if UI.CmdBtn.menu.index(END): - UI.CmdBtn['menu'] = UI.CmdBtn.menu - UI.CmdBtn.configure(state=NORMAL) - else: - WrnMsg(wNOCMDS) - - # Any user-set options have now been read, set the GUI - - for i in (UI.CmdBtn, UI.DirBtn, UI.HistBtn): - i.config(font=(FNAME, FSZ, "bold")) - i.menu.config(font=("courier", FSZ, "bold")) - - UI.DirList.config(font=(FNAME, FSZ, FWT), - foreground=FCOLOR, background=BCOLOR, - height=HEIGHT, width=WIDTH) - - # Make sure menus conform to max lengths (which may have changed). - - UpdateMenu(UI.DirBtn, UI.AllDirs, MAXDIR, LoadDirList) - UpdateMenu(UI.HistBtn, UI.CmdHist, MAXHIST, KeyRunCommand, fakeevent=TRUE) - - - ##### - # Dump requested debug information - ##### - - if int(DEBUGLEVEL) & DEBUGKEYS: - - debuginfo = [] - - # Command keys - for key in UI.KeyBindings.keys(): - debuginfo.append(key + " " * (10-len(key)) + UI.KeyBindings[key]) - debuginfo.sort() - PrintDebug(dKEYBINDS, debuginfo) - - debuginfo = [] - - # Function Keys (Directory Shortcuts) - for x in range(len(UI.FuncKeys)): - key = "F" + str(x+1) - debuginfo.append(key + " " * (10-len(key)) + UI.FuncKeys[x]) - - PrintDebug(dFUNCKEYS, debuginfo) - - if int(DEBUGLEVEL) & DEBUGSYMS: - - debuginfo = [] - for sym in UI.SymTable.keys(): - debuginfo.append(sym + " " * (16-len(sym)) + UI.SymTable[sym]) - - debuginfo.sort() - PrintDebug(dSYMTBL, debuginfo) - - if int(DEBUGLEVEL) & DEBUGCTBL: - - debuginfo = [] - for key in UI.CmdTable.keys(): - name = UI.CmdTable[key][0] - cmd = UI.CmdTable[key][1] - debuginfo.append(key + " " + name + " " * (16-len(name)) + cmd) - - PrintDebug(dCMDTBL, debuginfo) - - # Dump program variable during debug sessions - if int(DEBUGLEVEL) & DEBUGVARS: - - debuginfo = [] - for v in DebugVars: - debuginfo.append(v + " " * (12-len(v)) + (str(eval(v)) or dNULL)) - - PrintDebug(dINTVAR, debuginfo) - - # If we just wanted debug output, quit now - if int(DEBUGLEVEL) & DEBUGQUIT: - sys.exit() + # Make sure any options we've changed are implemented + if DoOptionsProcessing: + ProcessOptions() # End of 'ParseConfFile()' @@ -1846,7 +1764,17 @@ # On Unix, don't follow links pointing back to themselves if OSNAME == 'posix' and os.path.samefile(UI.CurrentDir, UI.CurrentDir + selected): - WrnMsg(wLINKBACK % (UI.CurrentDir + selected[:-1])) + + # Protect with try/except because Tk loses track of things + # if you keep hitting this selection very rapidly - i.e. Select + # the entry and lean on the Enter key. The try/except + # prevents the error message (which is benign) from ever + # appearing on stdout. + + try: + WrnMsg(wLINKBACK % (UI.CurrentDir + selected[:-1])) + except: + pass return # Build full path name @@ -2451,6 +2379,100 @@ # End of 'UpdateMenu()' +##### +# Process Options +##### + +def ProcessOptions(): + global UI + + # Rebind all the handlers + UI.BindAllHandlers() + + # Set the Command Menu Contents, if any, + # and enable the menu if it has entries. + # If no commands are defined, warn the user. + + if UI.CmdBtn.menu.index(END): + UI.CmdBtn['menu'] = UI.CmdBtn.menu + UI.CmdBtn.configure(state=NORMAL) + else: + WrnMsg(wNOCMDS) + + # Any user-set options have now been read, set the GUI + + for i in (UI.CmdBtn, UI.DirBtn, UI.HistBtn): + i.config(font=(FNAME, FSZ, "bold")) + i.menu.config(font=("courier", FSZ, "bold")) + + UI.DirList.config(font=(FNAME, FSZ, FWT), + foreground=FCOLOR, background=BCOLOR, + height=HEIGHT, width=WIDTH) + + # Make sure menus conform to max lengths (which may have changed). + + UpdateMenu(UI.DirBtn, UI.AllDirs, MAXDIR, LoadDirList) + UpdateMenu(UI.HistBtn, UI.CmdHist, MAXHIST, KeyRunCommand, fakeevent=TRUE) + + + ##### + # Dump requested debug information + ##### + + if int(DEBUGLEVEL) & DEBUGKEYS: + + debuginfo = [] + + # Command keys + for key in UI.KeyBindings.keys(): + debuginfo.append(key + " " * (10-len(key)) + UI.KeyBindings[key]) + debuginfo.sort() + PrintDebug(dKEYBINDS, debuginfo) + + debuginfo = [] + + # Function Keys (Directory Shortcuts) + for x in range(len(UI.FuncKeys)): + key = "F" + str(x+1) + debuginfo.append(key + " " * (10-len(key)) + UI.FuncKeys[x]) + + PrintDebug(dFUNCKEYS, debuginfo) + + if int(DEBUGLEVEL) & DEBUGSYMS: + + debuginfo = [] + for sym in UI.SymTable.keys(): + debuginfo.append(sym + " " * (16-len(sym)) + UI.SymTable[sym]) + + debuginfo.sort() + PrintDebug(dSYMTBL, debuginfo) + + if int(DEBUGLEVEL) & DEBUGCTBL: + + debuginfo = [] + for key in UI.CmdTable.keys(): + name = UI.CmdTable[key][0] + cmd = UI.CmdTable[key][1] + debuginfo.append(key + " " + name + " " * (16-len(name)) + cmd) + + PrintDebug(dCMDTBL, debuginfo) + + # Dump program variable during debug sessions + if int(DEBUGLEVEL) & DEBUGVARS: + + debuginfo = [] + for v in DebugVars: + debuginfo.append(v + " " * (12-len(v)) + (str(eval(v)) or dNULL)) + + PrintDebug(dINTVAR, debuginfo) + + # If we just wanted debug output, quit now + if int(DEBUGLEVEL) & DEBUGQUIT: + sys.exit() + +# End of 'ProcessOptions()' + + #----------------------------------------------------------# # Program Entry Point # #----------------------------------------------------------# @@ -2538,15 +2560,6 @@ print RCSID sys.exit(0) -# Read the debug level before anything else so that any -# subsequent operations will observe this. - -for opt, val in opts: - if opt == "-d": - DEBUGLEVEL = val - if int(DEBUGLEVEL): - print dHEADER % time.asctime() - # Read configuration file before any other arguments. This allows the # environment variable and then the command line to override any # settings in the configuration file. @@ -2555,13 +2568,23 @@ if opt == "-c": CONF = os.path.abspath(val) -ParseConfFile(None) +# Parse the configuration file, but suppress options +# processing - on startup this is done just before +# we enter the main message loop to make sure we +# pickup any options changes from the environment +# variable or command line. + +ParseConfFile(None, DoOptionsProcessing=FALSE) # Process the rest of the options, if any for opt, val in opts: if opt == "-b": BCOLOR = val + if opt == "-d": + DEBUGLEVEL = val + if int(DEBUGLEVEL): + print dHEADER % time.asctime() if opt == "-f": FCOLOR = val if opt == "-n": @@ -2611,11 +2634,10 @@ # Get starting directory into canonical form STARTDIR = os.path.abspath(STARTDIR) -# Setup event handlers - We have to do this here just -# in case we could not read any config file, which -# is when we normally bind the handlers. +# Process options to catch any changes detected in +# environment variable or command line -UI.BindAllHandlers() +ProcessOptions() # Need mutex to serialize on widget updates UI.DirListMutex = mutex.mutex()