diff --git a/twander.py b/twander.py index 46afc94..0512ca5 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.57 2002/11/16 22:31:51 tundra Exp $" +RCSID = "$Id: twander.py,v 1.58 2002/11/17 07:57:26 tundra Exp $" VERSION = RCSID.split()[2] @@ -13,7 +13,8 @@ #----------------------------------------------------------# from Tkinter import * -from tkMessageBox import * +from tkMessageBox import showerror, showinfo, showwarning +from tkSimpleDialog import askstring import getopt import os import sys @@ -30,16 +31,29 @@ # Key Assignments ##### -CHANGEDIR = '' -DIRHOME = '' -DIRPREV = '' -DIRSTART = '' -DIRUP = '' -KEYPRESS = '' -QUITPROG = '' -READCONF = '' -SELKEY = '' -SELMOUSE = '' +# General Program Commands + +KEYPRESS = '' # Any keypress (for commands) +QUITPROG = '' # Quit the program +READCONF = '' # Re-read the configuration file + + +# Directory Navigation + +CHANGEDIR = '' # Enter a new path +DIRHOME = '' # Goto $HOME +DIRPREV = '' # Goto previous directory +DIRSTART = '' # Goto starting directory +DIRUP = '' # Go up one directory level + +# Selection Keys + +SELNEXT = '' # Select next item +SELPREV = '' # Select previous item +SELFIRST = '' # Select top item +SELLAST = '' # Select bottom item +SELKEY = '' # Select item w/keyboard +SELMOUSE = '' # Select item w/mouse ##### @@ -55,7 +69,7 @@ HOME = os.getenv("HOME") or STARTDIR # Configuration file -CONF = os.path.join(HOME + "." + PROGNAME) +CONF = os.path.join(HOME, "." + PROGNAME) ##### @@ -139,6 +153,8 @@ # Error, Information, & Warning Messages ##### +# Errors + eBADROOT = " %s Is Not A Directory" eDIRRD = "Cannot Open Directory : %s --- Check Permissions." eDUPKEY = "Duplicate Key In Configuration File Found In Entry: \'%s\'" @@ -148,9 +164,16 @@ eOPEN = "Cannot Open File: %s" eTOOMANY = "You Can Only Specify One Starting Directory." +# Informational + iINFO = "INFORMATION" iVERINFO = "Program Version Information: %s" +# Prompts + +pCHPATH = "Change Path" +pENPATH = "Enter New Path Desired:" + wCMDKEY = "Configuration File Entry For: \'%s\' Has No Command Key Defined." wWARN = "WARNING" @@ -319,7 +342,7 @@ #----------------------------------------------------------# -# GUI Classes, Handlers, & Support Functions # +# GUI Definition # #----------------------------------------------------------# @@ -358,13 +381,10 @@ self.DirList.pack(side=LEFT, fill=BOTH, expand=1) ##### - # Bind the relevant widget event handlers + # Bind the relevant event handlers ##### - # Bind the relevant root window handlers - - # Bind handler for "Change Directory" - root.bind(CHANGEDIR, ChangeDir) + # General Program Commands # Bind handler for individual keystrokes root.bind(KEYPRESS, KeystrokeHandler) @@ -372,6 +392,15 @@ # Bind handler for "Quit Program" root.bind(QUITPROG, KeyQuitProg) + # Bind handler of "Read Config File" + root.bind(READCONF, ParseConfFile) + + + # Directory Navigation + + # Bind handler for "Change Directory" + root.bind(CHANGEDIR, ChangeDir) + # Bind handler for "Home Dir" root.bind(DIRHOME, KeyHomeDir) @@ -384,8 +413,20 @@ # Bind handler for "Up Dir" root.bind(DIRUP, KeyUpDir) - # Bind handler of "Read Config File" - root.bind(READCONF, ParseConfFile) + + # Selection Keys + + # Bind handler for "Next Item" + root.bind(SELNEXT, KeySelNext) + + # Bind handler for "Previous Item" + root.bind(SELPREV, KeySelPrev) + + # Bind handler for "First Item" + root.bind(SELFIRST, KeySelFirst) + + # Bind handler for "Last Item" + root.bind(SELLAST, KeySelLast) # Bind handler for "Item Select" root.bind(SELKEY, DirListHandler) @@ -450,6 +491,164 @@ # End of class definition, 'twanderUI' +#----------------------------------------------------------# +# Handler Functions # +#----------------------------------------------------------# + + +#--------------- General Program Commands -----------------# + + +##### +# Event Handler: Individual Keystrokes +##### + +def KeystrokeHandler(event): + + # If the key pressed is a command key, + # get its associated string and + # execute the command. + + cmd = UI.rcfile.get(event.char.lower(), ["",""])[1] + + # cmd == null means no matching command key - do nothing + # Otherwise, replace config tokens with actual file/dir names + if cmd: + + # Replace runtime-determined tokens + cmd = cmd.replace(FILENAME, UI.CurrentSelection()) + cmd = cmd.replace(DIRNAME, UI.CurrentDir) + + # Actually execute the command + os.system(cmd) + +# end of 'KeystrokeHandler()' + + +##### +# Event Handler: Program Quit +##### + +def KeyQuitProg(event): + sys.exit() + +# End of 'KeyQuitProg()' + + +#------------------- Directory Navigation -----------------# + + +##### +# Event Handler: Change Directory/Path +#### + +def ChangeDir(event): + + newpath = askstring(pCHPATH, pENPATH) + if newpath: + LoadDirList(newpath) + UI.DirList.focus() + +# End of 'ChangeDir()' + + +##### +# Event Handler: Goto $HOME +##### + +def KeyHomeDir(event): + + LoadDirList(HOME) + +# End of 'KeyHomeDir()' + + +##### +# Event Handler: Move To Previous Directory +##### + +def KeyPrevDir(event): + + # Move to last directory visited, if any - inhibit this from + # being placed on the directory traversal stack + if UI.LastDir: + LoadDirList(UI.LastDir.pop(), save=FALSE) + + # No previous directory + else: + pass + +# End of 'KeyPrevDir()' + + +##### +# Event Handler: Go Back to Initial Directory +##### + +def KeyStartDir(event): + global STARTDIR + + LoadDirList(STARTDIR) + +# End of 'KeyStartDir()' + + +##### +# Event Handler: Move up one directory +##### + +def KeyUpDir(event): + + # Move up one directory level unless we're already at the root + if UI.CurrentDir != os.path.abspath(PSEP): + LoadDirList(UI.CurrentDir + "..") + +# End of 'KeyUpDir()' + + +#---------------------- Selection Keys ---------------------# + + +##### +# Event Handler: Select Next Item +##### + +def KeySelNext(event): + print "KeySelNext()" + +# End of 'KeySelNext()' + + +##### +# Event Handler: Select Previous Item +##### + +def KeySelPrev(event): + print "KeySelPrev()" + +# End of 'KeySelPrev()' + + +##### +# Event Handler: Select First Item +##### + +def KeySelFirst(event): + print "KeySelFirst()" + +# End of 'KeySelFirst()' + + +##### +# Event Handler: Select Last Item +##### + +def KeySelLast(event): + print "KeySelLast()" + +# End of 'KeySelLast()' + + ##### # Event Hander: Process Current Selection ##### @@ -516,7 +715,7 @@ ##### -# Load UI With Selected Direcotory +# Load UI With Selected Directory ##### def LoadDirList(newdir, save=TRUE): @@ -525,18 +724,19 @@ newdir = os.path.abspath(newdir) # Push last directory visited onto the visited stack + # Do not do this if we've been told not to OR if # what we're about to save is the same as the top - # of the stack + # of the stack OR if the current directory is "" # If there is anything on the stack, see if last element # matches what we're about to put there. - if UI.Lastdir and UI.Lastdir[-1] == UI.CurrentDir: + if UI.LastDir and UI.LastDir[-1] == UI.CurrentDir: save = FALSE if save and UI.CurrentDir: - UI.Lastdir.append(UI.CurrentDir) + UI.LastDir.append(UI.CurrentDir) # And select new directory to visit UI.CurrentDir = newdir @@ -563,6 +763,7 @@ ##### def BuildDirList(currentdir): + global UI dList, fList = [], [] @@ -577,8 +778,26 @@ # Can't read selected directory - show error ErrMsg(eDIRRD % currentdir) - # Move up one directory level - KeyUpDir(None) + # By setting this to null, we assure that it never gets pushed + UI.CurrentDir = "" + + # If there was a previous dir, go there + if UI.LastDir: + + # Get the previous directory + curdir = UI.LastDir.pop() + + # Make it look like we are coming from the one before that + if UI.LastDir: + UI.CurrentDir = UI.LastDir.pop() + + # Now go there + LoadDirList(curdir) + + # If no previous directory, we failed on pgm startup - abort + else: + sys.exit(1) + # Satisfy the original call return [] @@ -667,107 +886,6 @@ # End of 'RefreshDirList() -##### -# Event Handler: Change Directory -#### - -def ChangeDir(event): - - pass - -# End of 'ChangeDir()' - - -##### -# Event Handler: Goto $HOME -##### - -def KeyHomeDir(event): - - LoadDirList(HOME) - -# End of 'KeyHomeDir()' - - -##### -# Event Handler: Move up one directory -##### - -def KeyUpDir(event): - - # Move up one directory level unless we're already at the root - if UI.CurrentDir != os.path.abspath(PSEP): - LoadDirList(UI.CurrentDir + "..") - -# End of 'KeyUpDir()' - - -##### -# Event Handler: Program Quit -##### - -def KeyQuitProg(event): - sys.exit() - -# End of 'KeyQuitProg()' - - -##### -# Event Handler: Move To Previous Directory -##### - -def KeyPrevDir(event): - - # Move to last directory visited, if any - inhibit this from - # being placed on the directory traversal stack - if UI.Lastdir: - LoadDirList(UI.Lastdir.pop(), save=FALSE) - - # No previous directory - else: - pass - -# End of 'KeyPrevDir()' - - -##### -# Event Handler: Go Back to Initial Directory -##### - -def KeyStartDir(event): - global STARTDIR - - LoadDirList(STARTDIR) - -# End of 'KeyStartDir()' - - -##### -# Event Handler: Individual Keystrokes -##### - -def KeystrokeHandler(event): - - # If the key pressed is a command key, - # get its associated string and - # execute the command. - - cmd = UI.rcfile.get(event.char.lower(), ["",""])[1] - - # cmd == null means no matching command key - do nothing - # Otherwise, replace config tokens with actual file/dir names - if cmd: - - # Replace runtime-determined tokens - cmd = cmd.replace(FILENAME, UI.CurrentSelection()) - cmd = cmd.replace(DIRNAME, UI.CurrentDir) - - # Actually execute the command - os.system(cmd) - -# end of 'KeystrokeHandler()' - - #----------------------------------------------------------# # Program Entry Point # #----------------------------------------------------------# @@ -855,7 +973,7 @@ ParseConfFile() # Initialize directory stack -UI.Lastdir = [] +UI.LastDir = [] # And current location UI.CurrentDir = ""