diff --git a/twander.py b/twander.py index 1520cd9..1ecb298 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.22 2002/10/30 00:00:23 tundra Exp $" +RCSID = "$Id: twander.py,v 1.23 2002/10/30 23:28:35 tundra Exp $" VERSION = RCSID.split()[2] @@ -81,9 +81,11 @@ ##### eBADROOT = " %s Is Not A Directory" +eDIRRD = " Problem Reading Directory : %s" eDUPKEY = "Duplicate Key In Configuration File Found In Entry: \'%s\'" eERROR = "ERROR" eNOCONF = "Cannot Find Configuration File: %s" +eNOENV = "Configuration File References Undefined Environment Variable: %s" eOPEN = "Cannot Open File: %s" eTOOMANY = "You Can Only Specify One Starting Directory." @@ -128,8 +130,9 @@ MainApp = gui.Application() MainWin = gui.Window(height=300, - width=500 + width=500, ) + DirList = gui.ListBox() MainWin.add(DirList, hstretch=1, @@ -166,13 +169,16 @@ def BuildDirList(rootdir): dList, fList = [], [] - - dList.append("..") - for file in os.listdir(rootdir): - if os.path.isdir(os.path.join(rootdir,file)): - dList.append(DIR_LDELIM + file + DIR_RDELIM) - else: - fList.append(file) + dList.append(DIR_LDELIM + ".." + DIR_RDELIM) # Always show one directory up + + try: + for file in os.listdir(rootdir): + if os.path.isdir(os.path.join(rootdir,file)): + dList.append(DIR_LDELIM + file + DIR_RDELIM) + else: + fList.append(file) + except: + ErrMsg(eDIRRD % rootdir) dList.sort() fList.sort() @@ -182,6 +188,52 @@ ##### +# Event Handler For Directory Listing ListBox +##### + +def DirList_Handler(**kwargs): + global rootdir + selected = DirList.items[DirList._get_selection()] + + # If selection is a directory, move there and list contents. + # We examine this by checking the string for the directory + # delimiter characters previously inserted in BuildDirList() + + if selected[0] == DIR_LDELIM and selected[-1] == DIR_RDELIM: + # Strip off delimiters to get real name + selected = selected[1:-1] + + # Build full path name + selected = os.path.join(os.path.abspath(rootdir), selected) + + # Convert ending ".." into canonical path + if selected.endswith(".."): + selected = PSEP.join(selected.split(PSEP)[:-2]) + + # Need to end the directory string with a path + # separator character so that subsequent navigation + # will work when we hit the root directory of the file + # system. In the case of Unix, this means that + # if we ended up at the root directory, we'll just + # get "/". In the case of Win32, we will get + # "DRIVE:/". + + selected += PSEP + + # Save the new root location and get its contents + rootdir = selected + DirList.items = BuildDirList(rootdir) + + # Update main window to reflect new location + MainWin.set(title= PROGNAME + " " + VERSION + " " + rootdir) + + else: + print("Not a DIR!") + +# End of 'DirList_Handler() + + +##### # Print An Error Message ##### @@ -224,7 +276,12 @@ # Process environment variables if fields[x][0] == ENVIRO: - fields[x] = os.getenv(fields[x][1:]) + envval = os.getenv(fields[x][1:]) + if not envval: # Environment variable not defined + ErrMsg(eNOENV % fields[x]) + sys.exit(1) + else: + fields[x] = envval # Get command key value and store in dictionary @@ -239,7 +296,7 @@ else: key = fields[0][keypos+1] # Use selected key as index - if key in rcfile: + if key in rcfile: # This is a Python 2.2 or later idiom ErrMsg(eDUPKEY % fields[0]) # Duplicate key found cf.close() sys.exit(1) @@ -328,9 +385,19 @@ rcfile = {} ParseRC() -# Fill the control with directory contents +# Canonicalize the rootdir +rootdir = os.path.abspath(rootdir) + +# Initialize ListBox with info for starting directory DirList.items = BuildDirList(rootdir) -MainWin.set(title= PROGNAME + " " + VERSION + " - Examining: " + rootdir) +# Initialize the title for the main window +MainWin.set(title= PROGNAME + " " + VERSION + " " + rootdir) + + +# Link ListBox handler +gui.link(DirList, DirList_Handler) + + MainApp.run()