Newer
Older
twander / twander.py
#!/usr/local/bin/python
# twander - Wander around the file system
# Copyright (c) 2002 TundraWare Inc.  All Rights Reserved.


PROGNAME = "twander"
RCSID = "$Id: twander.py,v 1.2 2002/10/29 00:09:22 tundra Exp $"
VERSION = RCSID.split()[2]


#----------------------------------------------------------#
#          Variables User Might Change                     #
#----------------------------------------------------------#



#------------------- Nothing Below Here Should Need Changing ------------------#


#----------------------------------------------------------#
#                     Imports                              #
#----------------------------------------------------------#

import anygui as gui
import getopt
import os
import sys


#----------------------------------------------------------#
#               Aliases & Redefinitions                    #
#----------------------------------------------------------#



#----------------------------------------------------------#
#              Constants & Literals                        #
#----------------------------------------------------------#



#####
# Constants
#####

FALSE      = 0 == 1           # Booleans
TRUE       = not FALSE

DIR_LDELIM = '['              # Directory left dsply. delimiter
DIR_RDELIM = ']'              # Directory left dsply. delimiter

PSEP       = os.sep           # Character separating path components

#####
# Literals
#####

lCONFFILE = os.path.join(os.getenv("HOME"), "." + PROGNAME + "rc")  # conf file


#----------------------------------------------------------#
#            Prompts, & Application Strings                #
#----------------------------------------------------------#


#####
# Error Messages
#####

eBADROOT = " %s Is Not A Directory"
eERROR   = "ERROR"
eNOCONF  = "Cannot Find Configuration File: %s"
eTOOMANY = "You Can Only Specify One Starting Directory."


#####
# Informational Messages
#####



#####
# Usage Prompts
#####

uTable = [PROGNAME + " " + VERSION + " - Copyright 2002, TundraWare Inc., All Rights Reserved\n",
          "usage:  " + PROGNAME + " [-d dir] [-hv] [starting directory] where,\n",
          "          startdir name of directory in which to begin (default: current dir)",
          "          -c file  name of configuration file (default: " + lCONFFILE + ")",
          "          -h       print this help information",
          "          -v       print detailed version information",
          ]



#####
# Prompts
#####



#----------------------------------------------------------#
#        Global Variables & Data Structures                #
#----------------------------------------------------------#


#####
# GUI Related Setup
#####

MainApp   = gui.Application()
MainWin   = gui.Window(height=300,
                       width=500
                       )
DirList   = gui.ListBox()
MainWin.add(DirList,
            hstretch=1,
            vstretch=1,
            top=1,
            bottom=1,
            left= 1,
            right=1
            )



MainApp.add(MainWin)



#---------------------------Code Begins Here----------------------------------#


#----------------------------------------------------------#
#           Object Base Class Definitions                  #
#----------------------------------------------------------#

    

#----------------------------------------------------------#
#           Supporting Function Definitions                #
#----------------------------------------------------------#

#####
# Return Ordered List Of Directories & Files For Current Root
#####

def build_dirlist(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.sort()
    fList.sort()
    return dList + fList

# End 'build_dirlist()'


#####
# Print An Error Message
#####

def errmsg(emsg):
    print PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg

# End of 'errmsg()'


#####
# Print Usage Information
#####

def usage():
    for x in uTable:
        print x
        
# End of 'usage()'


#----------------------------------------------------------#
#                  Program Entry Point                     #
#----------------------------------------------------------#

# Command line processing

try:
    opts, args = getopt.getopt(sys.argv[1:], '-c:hv')
except getopt.GetoptError:
    usage()
    sys.exit(1)

conf    = lCONFFILE
rootdir = "." + PSEP
for opt, val in opts:
    if opt == "-c":
        conf = val
    if opt == "-h":
        usage()
        sys.exit(0)
    if opt == "-v":
        print RCSID
        sys.exit(0)

# Can only have 0 or 1 arguments
# Make sure any starting directory argument is legit

if len(args) > 1:
    errmsg(eTOOMANY)
    sys.exit(1)

if len(args) == 1:
    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)
    sys.exit(1)

# Fill the control with directory contents
DirList.items = build_dirlist(rootdir)

MainWin.set(title= PROGNAME + " " + VERSION + "    -     Examining:    " + rootdir)
MainApp.run()