diff --git a/twander.py b/twander.py index f25dabe..cac36b8 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.55 2002/11/15 09:47:43 tundra Exp $" +RCSID = "$Id: twander.py,v 1.56 2002/11/16 22:12:10 tundra Exp $" VERSION = RCSID.split()[2] @@ -136,7 +136,7 @@ ##### -# Error & Warning Messages +# Error, Information, & Warning Messages ##### eBADROOT = " %s Is Not A Directory" @@ -148,6 +148,9 @@ eOPEN = "Cannot Open File: %s" eTOOMANY = "You Can Only Specify One Starting Directory." +iINFO = "INFORMATION" +iVERINFO = "Program Version Information: %s" + wCMDKEY = "Configuration File Entry For: \'%s\' Has No Command Key Defined." wWARN = "WARNING" @@ -187,7 +190,18 @@ def ErrMsg(emsg): - showerror(PROGNAME + " " + VERSION + " " + eERROR, eERROR + ": " + emsg) + showerror(PROGNAME + " " + VERSION + " " + eERROR, emsg) + +# End of 'ErrMsg()' + + +##### +# Print An Information Message +##### + +def InfoMsg(emsg): + + showinfo(PROGNAME + " " + VERSION + " " + iINFO, emsg) # End of 'ErrMsg()' @@ -198,7 +212,7 @@ def WrnMsg(wmsg): if WARN: - showwarning(PROGNAME + " " + VERSION + " " + wWARN, wWARN + ": " + wmsg) + showwarning(PROGNAME + " " + VERSION + " " + wWARN, wmsg) # End of 'WrnMsg()' @@ -419,7 +433,7 @@ def UpdateTitle(self, mainwin): # Get current selection - selected = self.CurrentSelection() + selected = self.DirList.get(ACTIVE) # If there was a file selection append to title if selected \ @@ -429,7 +443,7 @@ selected = "" # Update the titlebar - mainwin.title(PROGNAME + " " + VERSION + " " + UI.rootdir + selected) + mainwin.title(PROGNAME + " " + VERSION + " " + UI.CurrentDir + selected) # End of method 'twanderUI.UpdateTitle()' @@ -441,6 +455,7 @@ ##### def DirListHandler(event): + SAVE = TRUE # Get current selection. If none, just return, otherwise process selected = UI.CurrentSelection() @@ -456,8 +471,14 @@ # Strip off delimiters to get real name selected = selected[1:-1] + # We don't push this selection on the stack if + # we are at root directory and user presses '..' + + if (selected == '..') and (UI.CurrentDir == os.path.abspath(PSEP)): + SAVE = FALSE + # Build full path name - selected = os.path.join(os.path.abspath(UI.rootdir), selected) + selected = os.path.join(os.path.abspath(UI.CurrentDir), selected) # Convert ending ".." into canonical path if selected.endswith(".."): @@ -474,7 +495,7 @@ selected += PSEP # Load UI with new directory - LoadDirList(selected) + LoadDirList(selected, save=SAVE) # And always force selection of first item there. # This guarantees a selection in the new @@ -495,7 +516,7 @@ ##### -# Get Directory Of Current UI.rootdir And Load Into UI +# Load UI With Selected Direcotory ##### def LoadDirList(newdir, save=TRUE): @@ -511,24 +532,24 @@ # If there is anything on the stack, see if last element # matches what we're about to put there. - if UI.lastdir: - if UI.lastdir[-1] == UI.rootdir: - save = FALSE - if save: - UI.lastdir.append(UI.rootdir) + if UI.Lastdir and UI.Lastdir[-1] == UI.CurrentDir: + save = FALSE + + if save and UI.CurrentDir: + UI.Lastdir.append(UI.CurrentDir) # And select new directory to visit - UI.rootdir = newdir + UI.CurrentDir = newdir # And make sure it ends with a path separator character - if UI.rootdir[-1] != PSEP: - UI.rootdir = UI.rootdir + PSEP + if UI.CurrentDir[-1] != PSEP: + UI.CurrentDir = UI.CurrentDir + PSEP # Clear out the old contents UI.DirList.delete(0,END) # Load new directory contents into UI - for x in BuildDirList(UI.rootdir): + for x in BuildDirList(UI.CurrentDir): UI.DirList.insert(END, x) # And update the title to reflect changes @@ -541,20 +562,20 @@ # Return Ordered List Of Directories & Files For Current Root ##### -def BuildDirList(rootdir): +def BuildDirList(currentdir): dList, fList = [], [] # Walk the directory separate subdirs and files try: - for file in os.listdir(rootdir): - if os.path.isdir(os.path.join(rootdir,file)): + for file in os.listdir(currentdir): + if os.path.isdir(os.path.join(currentdir,file)): dList.append(DIR_LDELIM + file + DIR_RDELIM) else: fList.append(file) except: # Can't read selected directory - show error - ErrMsg(eDIRRD % rootdir) + ErrMsg(eDIRRD % currentdir) # Move up one directory level KeyUpDir(None) @@ -570,8 +591,7 @@ # OSs like Win32 like to use '$' in file names which # sorts before "." - if rootdir != os.path.abspath(PSEP): - dList.insert(0, DIR_LDELIM + ".." + DIR_RDELIM) + dList.insert(0, DIR_LDELIM + ".." + DIR_RDELIM) fList.sort() return dList + fList @@ -584,7 +604,7 @@ ##### def RefreshDirList(*args): - + rm_list = [] # Save current selection and active entry number = 0 @@ -594,9 +614,34 @@ if index: number = UI.DirList.index(index) - LoadDirList(UI.rootdir, save=FALSE) + # Confirm that everything in the DirList widget + # is still present in the directory. If not, + # remove it from the widget. - + for i in range(UI.DirList.index(END)): + + # Get entry + x = UI.DirList.get(i) + + # First strip any directory delimiters + if x[0] == DIR_LDELIM and x[-1] == DIR_RDELIM: + x = x[1:-1] + + # Make list of items which no longer exist in this directory + # Ignore '..', since it always exists + + if x != '..': + if not os.path.exists(UI.CurrentDir + x): + rm_list.append(i) + + # Now actually delete these items from the listbox + + for i in rm_list: + UI.DirList.delete(i) + + # Update it visually + UI.DirList.pack() + # Restore current selection and active entry # Make sure they are still in range singe we # may have fewer items after the refresh. @@ -651,8 +696,8 @@ def KeyUpDir(event): # Move up one directory level unless we're already at the root - if UI.rootdir != os.path.abspath(PSEP): - LoadDirList(UI.rootdir + "..") + if UI.CurrentDir != os.path.abspath(PSEP): + LoadDirList(UI.CurrentDir + "..") # End of 'KeyUpDir()' @@ -675,8 +720,8 @@ # 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) + if UI.Lastdir: + LoadDirList(UI.Lastdir.pop(), save=FALSE) # No previous directory else: @@ -715,7 +760,7 @@ # Replace runtime-determined tokens cmd = cmd.replace(FILENAME, UI.CurrentSelection()) - cmd = cmd.replace(DIRNAME, UI.rootdir) + cmd = cmd.replace(DIRNAME, UI.CurrentDir) # Actually execute the command os.system(cmd) @@ -759,7 +804,7 @@ if opt == "-s": FSZ = val if opt == "-v": - print RCSID + InfoMsg(iVERINFO % RCSID) sys.exit(0) if opt == "-w": FWT = val @@ -796,20 +841,20 @@ ErrMsg(eBADROOT % STARTDIR) sys.exit(1) -UI.rootdir = STARTDIR +# Get starting directory into canonical form +STARTDIR = os.path.abspath(STARTDIR) -# Initialize directory stack -UI.lastdir = [] - -# Parse contents into dictionary - +# Parse the and store configuration ParseConfFile() -# Canonicalize UI.rootdir -UI.rootdir = os.path.abspath(UI.rootdir) +# Initialize directory stack +UI.Lastdir = [] + +# And current location +UI.CurrentDir = "" # Initialize the UI directory listing -LoadDirList(UI.rootdir) +LoadDirList(STARTDIR) # And start the periodic polling of the widget UI.poll()