diff --git a/twander.py b/twander.py index 5587c67..4867b13 100755 --- a/twander.py +++ b/twander.py @@ -4,20 +4,11 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.32 2002/11/09 00:31:08 tundra Exp $" +RCSID = "$Id: twander.py,v 1.33 2002/11/09 05:47:45 tundra Exp $" VERSION = RCSID.split()[2] #----------------------------------------------------------# -# Variables User Might Change # -#----------------------------------------------------------# - - - -#------------------- Nothing Below Here Should Need Changing ------------------# - - -#----------------------------------------------------------# # Imports # #----------------------------------------------------------# @@ -28,6 +19,45 @@ #----------------------------------------------------------# +# Variables User Might Change # +#----------------------------------------------------------# + +##### +# Defaults +##### + +# Configuration file + +CONF = os.path.join(os.getenv("HOME"), # Name of default config file + "." + + PROGNAME + ) + +# Initial Dimensions + +HEIGHT = 25 +WIDTH = 60 + +# Starting directory + +ROOTDIR = "." + os.sep + +# Colors +BCOLOR = "black" +FCOLOR = "green" + +# Fonts + +FNAME = "Courier" +FSZ = 12 +FWT = "bold" + + + +#------------------- Nothing Below Here Should Need Changing ------------------# + + +#----------------------------------------------------------# # Aliases & Redefinitions # #----------------------------------------------------------# @@ -60,11 +90,6 @@ # Configuration File Related Literals ##### -CONFFILE = os.path.join(os.getenv("HOME"), # Name of default config file - "." + - PROGNAME - ) - CMDKEY = r'&' # Command key delimiter COMMENT = r"#" # Comment character NAME = r'[NAME]' # Substitution field in config files @@ -107,7 +132,7 @@ "usage: " + PROGNAME + " [-d dir] [-hv] [startdir] where,\n", " startdir name of directory in which to begin (default: current dir)", " -b color background color", - " -c file name of configuration file (default: " + CONFFILE + ")", + " -c file name of configuration file (default: " + CONF + ")", " -f color foreground color", " -h print this help information", " -n name name of font to use", @@ -138,60 +163,6 @@ #----------------------------------------------------------# -# GUI Handlers & Supporting Functions # -#----------------------------------------------------------# - - - -##### -# 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: -# os.system(rcfile["ls"][1].replace("[NAME]", os.path.join(rootdir, selected)) + "&") - pass - -# End of 'DirList_Handler()' - - -#----------------------------------------------------------# # Supporting Function Definitions # #----------------------------------------------------------# @@ -199,19 +170,19 @@ # Return Ordered List Of Directories & Files For Current Root ##### -def BuildDirList(rootdir): +def BuildDirList(ROOTDIR): dList, fList = [], [] 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)): + 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) + ErrMsg(eDIRRD % ROOTDIR) dList.sort() fList.sort() @@ -231,15 +202,32 @@ ##### +# Get Directory Of Current ROOTDIR And Load Into UI +##### + +def LoadDirList(): + + # Update the window title + UIroot.title(PROGNAME + " " + VERSION + " " + ROOTDIR) + + # Load new directory contents into UI + for x in BuildDirList(ROOTDIR): + UI.DirList.insert(END, x) + +# End of 'LoadDirList(): + + + +##### # Parse & Process The Configuraton File ##### def ParseRC(): try: - cf = open(conf) + cf = open(CONF) except: - ErrMsg(eOPEN % conf) + ErrMsg(eOPEN % CONF) sys.exit(1) # Process and massage the configuration file @@ -321,6 +309,88 @@ #----------------------------------------------------------# +# GUI Classes And Handlers # +#----------------------------------------------------------# + + + +##### +# Enacapsulate the UI in a class +##### + + +class twanderUI: + + def __init__(self, root): + + self.hSB = Scrollbar(root, orient=HORIZONTAL) + self.vSB = Scrollbar(root, orient=VERTICAL) + self.DirList = Listbox(root, + foreground = FCOLOR, + background = BCOLOR, + font = (FNAME, FSZ, FWT), + selectmode=SINGLE, + exportselection=0, + xscrollcommand=self.hSB.set, + yscrollcommand=self.vSB.set, + height = HEIGHT, + width = WIDTH, + ) + + self.hSB.config(command=self.DirList.xview) + self.hSB.pack(side=BOTTOM, fill=X) + self.vSB.config(command=self.DirList.yview) + self.vSB.pack(side=RIGHT, fill=Y) + self.DirList.pack(side=LEFT, fill=BOTH, expand=1) + +# End of class definition, 'twanderUI' + + +##### +# 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 new root and load its contents into the UI + ROOTDIR = selected + LoadDirList() + + else: # It's a file - handled by the single-click handler + pass + +# End of 'DirList_Handler()' + + +#----------------------------------------------------------# # Program Entry Point # #----------------------------------------------------------# @@ -337,46 +407,27 @@ Usage() sys.exit(1) -# Defaults - -# Configuration file -conf = CONFFILE - -# Starting directory -rootdir = "." + PSEP - -# Colors -bcolor = "black" -fcolor = "green" - -# Fonts - -fname = "Courier" -fsz = 12 -fwt = "bold" - - # Parse command line for opt, val in opts: if opt == "-b": - bcolor = val + BCOLOR = val if opt == "-c": - conf = val + CONF = val if opt == "-f": - fcolor = val + FCOLOR = val if opt == "-h": Usage() sys.exit(0) if opt == "-n": - fname = val + FNAME = val if opt == "-s": - fsz = val + FSZ = val if opt == "-v": print RCSID sys.exit(0) if opt == "-w": - fwt = val + FWT = val # Can only have 0 or 1 arguments # Make sure any starting directory argument is legit @@ -386,53 +437,32 @@ sys.exit(1) if len(args) == 1: - rootdir = args[0] - if not os.path.isdir(rootdir): - ErrMsg(eBADROOT % rootdir) + ROOTDIR = args[0] + if not os.path.isdir(ROOTDIR): + ErrMsg(eBADROOT % ROOTDIR) sys.exit(1) # This program requires a config file -if not os.path.exists(conf): - ErrMsg(eNOCONF % conf) +if not os.path.exists(CONF): + ErrMsg(eNOCONF % CONF) sys.exit(1) rcfile = {} ParseRC() -# Canonicalize the rootdir -rootdir = os.path.abspath(rootdir) +# Canonicalize the ROOTDIR +ROOTDIR = os.path.abspath(ROOTDIR) -##### -# GUI Related Setup -##### - +# Create an instance of the UI UIroot = Tk() +UI = twanderUI(UIroot) -SB = Scrollbar(UIroot, orient=VERTICAL) -DirList = Listbox(UIroot, - foreground = fcolor, - background = bcolor, - font = (fname, fsz, fwt), - selectmode=SINGLE, - exportselection=0, - yscrollcommand=SB.set) - -SB.config(command=DirList.yview) -SB.pack(side=RIGHT, fill=Y) -DirList.pack(side=LEFT, fill=BOTH, expand=1) # Initialize the UI directory listing - -for x in BuildDirList(rootdir): - DirList.insert(END, x) - - -# Set the window title -UIroot.wm_title(PROGNAME + " " + VERSION + " " + rootdir) - +LoadDirList() # Run the program interface UIroot.mainloop()