diff --git a/tren.py b/tren.py index 4c3c66d..e79c5c7 100755 --- a/tren.py +++ b/tren.py @@ -5,8 +5,8 @@ # Program Information -PROGNAME = "tren" -RCSID = "$Id: tren.py,v 1.101 2010/01/14 22:30:06 tundra Exp $" +PROGNAME = "tren.py" +RCSID = "$Id: tren.py,v 1.102 2010/01/22 00:41:23 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -52,11 +52,14 @@ ##### - ##### # Literals ##### +ALL = "All" +EXT = "Ext" +EXTDELIM = "." +NAM = "Nam" #----------------------------------------------------------# @@ -65,9 +68,16 @@ ##### +# Debug Messages +##### + +dDEBUG = "DEBUG" + +##### # Error Messages ##### +eBADARG = "Invalid or malformed command line argument!" eERROR = "ERROR" @@ -81,19 +91,45 @@ ##### uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, - "usage: " + PROGNAME + " [-fhv]", + "usage: " + PROGNAME + " [-1abCcdEefghqtvXx] [-F file] [-l string] [-r old=new]... file|dir file|dir ...", " where,", - " -f file configuration file to use", - " -h print this help information", - " -v print detailed version information", - ] - + " -1 Rename only the first instance of the specified string (Default)", + " -a Rename within the entire file or directory name (Default)", + " -C Do case-sensitive renaming (Default)", + " -c Collapse case when doing string substitution.", + " -d Dump debugging information", + " -e Only perform renaming within extension portion of or directory name.", + " -E Continue renaming even after an error is encountered", + " -F file Read command line arguments from file", + " -f Force renaming even if target file or directory name already exists.", + " -g Replace all instances (global rename) of the old string with the new.", + " -h Print help information.", + " -l string File extension delimiter string. (Default: .)", + " -q Quiet mode, do not show progress.", + " -r Replace old with new in file or directory names.", + " -t Test mode, don't rename, just show what the program *would* do", + " -v Print detailed program version information and exit.", + " -X Treat the renaming strings literally (Default)", + " -x Treat the old replacement string as a Python regular expression", + ] #----------------------------------------------------------# # Global Variables & Data Structures # #----------------------------------------------------------# -CFGFILE = os.path.join(os.getenv("HOME"), "." + PROGNAME) # conf file +# Program toggle and option defaults + +DEBUG = False # Debugging off +CASE = True # Search is case-sensitive +ERRORCONTINUE = False # Do not continue after error +EXTDELIM = EXTDELIM # Extension Delimiter +FORCERENAM = False # Do not rename if target already exists +GLOBAL = False # Only rename first instance of old string +QUIET = False # Display progress +REGEX = False # Do not treat old string as a regex +TARGET = ALL # Can be "All", "Name", or "Ext" +TESTMODE = False + #--------------------------- Code Begins Here ---------------------------------# @@ -111,22 +147,97 @@ ##### +# Turn A List Into Columns With Space Padding +##### + +def ColumnPad(list, padchar=" ", padwidth=20): + + retval = "" + for l in list: + l = str(l) + retval += l + ((padwidth - len(l)) * padchar) + + return retval.strip() + +# End of 'ColumnPad()' + + +##### +# Print A Debug Message +##### + +def DebugMsg(msg): + PrintStderr(PROGNAME + " " + VERSION + " " + dDEBUG + ": " + msg) + +# End of 'DebugMsg()' + + +##### +# Dump The State Of The Program +##### + +def DumpState(): + + # Dump the command line + DebugMsg(ColumnPad(["Command Line", sys.argv])) + + # Names of all the state variables we want dumped + state = [ + "DEBUG", + "CASE", + "ERRORCONTINUE", + "EXTDELIM", + "FORCERENAM", + "GLOBAL", + "QUIET", + "REGEX", + "TARGET", + "TESTMODE", + ] + + for k in state: + DebugMsg(ColumnPad([k, eval(k)])) + +# End of 'DumpState()' + + +##### # Print An Error Message ##### def ErrorMsg(emsg): - print PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg + PrintStderr(PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg) # End of 'ErrorMsg()' ##### +# Print To stderr +##### + +def PrintStderr(msg, trailing="\n"): + sys.stderr.write(msg + trailing) + +# End of 'PrintStderr()' + + +##### +# Print To stdout +##### + +def PrintStdout(msg, trailing="\n"): + sys.stdout.write(msg + trailing) + +# End of 'PrintStdout' + + +##### # Print Usage Information ##### def Usage(): for line in uTable: - print line + PrintStdout(line) # End of 'Usage()' @@ -144,18 +255,51 @@ OPTIONS = envopt.split() + OPTIONS try: - opts, args = getopt.getopt(OPTIONS, '-f:hv') + opts, args = getopt.getopt(OPTIONS, '1abCcbEeF:fghl:qr:tvXx]') except getopt.GetoptError: + ErrorMsg(eBADARG) Usage() sys.exit(1) for opt, val in opts: + + if opt == "-1": + print opt + if opt == "-a": + print opt + if opt == "-b": + print opt + if opt == "-C": + print opt + if opt == "-c": + print opt + if opt == "-d": + DumpState() + if opt == "-E": + print opt + if opt == "-e": + print opt + if opt == "-F": + print opt if opt == "-f": - print val + print opt + if opt == "-g": + print opt if opt == "-h": Usage() sys.exit(0) + if opt == "-l": + print opt + if opt == "-q": + print opt + if opt == "-r": + print opt + if opt == "-t": + print opt if opt == "-v": print RCSID sys.exit(0) - + if opt == "-X": + print opt + if opt == "-x": + print opt