diff --git a/tren.py b/tren.py index a96c290..c301a96 100755 --- a/tren.py +++ b/tren.py @@ -8,7 +8,7 @@ PROGNAME = "tren.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tren.py,v 1.150 2010/02/05 21:35:47 tundra Exp $" +RCSID = "$Id: tren.py,v 1.151 2010/02/12 18:29:38 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -89,10 +89,14 @@ DEFLEN = 80 # Default output line length DEFSEP = "=" # Default rename command separator: old=new DEFSUFFIX = ".backup" # String used to rename existing targets -ESC = "\\" # Escape character +DEFESC = "\\" # Escape character EXT = "Ext" # Rename target is extension INCL = "I" # Include file command line option NAM = "Nam" # Rename target is name +NULLESC = "Escape string" # Cannot be null +NULLEXT = "Extension delimiter string" # Cannot be null +NULLRENSEP = "Old/New separator string" # Cannot be null +NULLSUFFIX = "Forced renaming suffix string" # Cannot be null OPTINTRO = "-" # Option introducer # Internal program state literals @@ -173,6 +177,7 @@ eNAMELONG = "Renaming '%s' to new name '%s' too long! (Maximum length is %s.)" eNAMESHORT = "Renaming '%s' to new name '%s' too short! (Minimum length is %s.)" eNOTHINGTODO = "Nothing to do!" +eNULLARG = "%s cannot be empty!" eTOOMANYINC = "Too many includes! (Max is %d) Possible circular reference?" % MAXINCLUDES @@ -180,8 +185,9 @@ # Informational Messages ##### -iRENEXISTING = "Forced rename of existing '%s' to '%s' !" -iSKIPPING = "Skipping rename of '%s' - Newname '%s' Exists!" +iFORCEDREN = "Forced Rename:" +iRENAME = "Rename:" +iSKIPPING = "Target Exists, Rename Skipped:" ##### @@ -190,7 +196,7 @@ uTable = [PROGVER, HOMEPAGE, - "usage: " + PROGNAME + " [-abCcdEefGghqtvwXx] [-I file] [-l string] [-r old=new]... [-S suffix] file|dir file|dir ...", + "usage: " + PROGNAME + " [-abCcdEefGghqtvwXx] [-I file] [-L string] [-r old=new]... [-S suffix] file|dir file|dir ...", " where,", " -a Rename within the entire file or directory name (Default)", " -C Do case-sensitive renaming (Default)", @@ -203,7 +209,8 @@ " -g Replace all instances (global rename) of the old string with the new", " -h Print help information", " -I file Include command line arguments from file", - " -l string File extension delimiter string (Default: %s)" % DEFEXT, + " -L string File extension delimiter string (Default: %s)" % DEFEXT, + " -P string Use 'string' as the escape sequence (Default: %s)" % DEFESC, " -q Quiet mode, do not show progress", " -R rensep Separator string for -r rename arguments (Default: %s)" % DEFSEP, " -r old=new Replace old with new in file or directory names", @@ -484,7 +491,7 @@ oldstrings.append(i) i = name.find(old, i + len(old)) - # If we found any maching strings, replace them + # If we found any matching strings, replace them if oldstrings: @@ -518,29 +525,39 @@ newname = pathname + basename if (target != newname) and not RenamingError: + renamejobs = [(target, newname)] + renaming = " %s %s %s %s" % (iRENAME, renamejobs[0][0], ARROW, renamejobs[0][1]) + # Figure out if the newname already exists newexists = False if os.path.exists(newname): newexists = True renameexisting = newname + ProgramOptions[EXISTSUFFIX] - - # Just show what we would do + renamejobs.append((newname, renameexisting)) + forcedren = " %s %s %s %s" % (iFORCEDREN, renamejobs[1][0], ARROW, renamejobs[1][1]) + skipping = " %s %s %s %s" % (iSKIPPING, renamejobs[0][0], ARROW, renamejobs[0][1]) + + # In test mode, just show what we would do + if ProgramOptions[TESTMODE]: if newexists: # Forced renaming of existing targets + if ProgramOptions[FORCERENAM]: - InfoMsg(iRENEXISTING % (newname, renameexisting), TESTMODE) + InfoMsg(forcedren, TESTMODE) # Skip renaming if target already exists - else: - InfoMsg(iSKIPPING % (target, newname), TESTMODE) - # Target does not exist, do the rename - else: - InfoMsg(ColumnPad([target + " %s " % ARROW, newname]), TESTMODE) + else: + InfoMsg(skipping, TESTMODE) + + # Target does not exist or forced renaming is in effect, do the rename + + if not newexists or ProgramOptions[FORCERENAM]: + InfoMsg(renaming, TESTMODE) # Actually do the renaming else: @@ -696,7 +713,7 @@ if arg[i:].startswith(oldnewsep): - if (i > 0 and (arg[i-1] != ESC)) or i == 0: + if (i > 0 and (arg[i-1] != DEFESC)) or i == 0: sepindex = i numseps += 1 @@ -711,8 +728,8 @@ else: old, new = arg[:sepindex], arg[sepindex + len(oldnewsep):] - old = old.replace(ESC + oldnewsep, oldnewsep) - new = new.replace(ESC + oldnewsep, oldnewsep) + old = old.replace(DEFESC + oldnewsep, oldnewsep) + new = new.replace(DEFESC + oldnewsep, oldnewsep) return [old, new] # End of 'GetOldNew()' @@ -904,7 +921,7 @@ # And parse the command line try: - opts, args = getopt.getopt(OPTIONS, 'abbCcdEefGghl:qR:r:S:tvw:Xx]') + opts, args = getopt.getopt(OPTIONS, 'abbCcdEefGghL:P:qR:r:S:tvw:Xx]') except getopt.GetoptError as e: ErrorMsg(eBADARG % e.args[0], EXIT=True) @@ -955,14 +972,26 @@ Usage() sys.exit(0) - if opt == "-l": - ProgramOptions[EXTDELIM] = val + if opt == "-L": + if val: + ProgramOptions[EXTDELIM] = val + else: + ErrorMsg(eNULLARG % NULLEXT, EXIT=True) + + if opt == "-P": + if val: + pass + else: + ErrorMsg(eNULLARG % NULLESC, EXIT=True) if opt == "-q": ProgramOptions[QUIET] = True if opt == '-R': - ProgramOptions[RENSEP] = val + if val: + ProgramOptions[RENSEP] = val + else: + ErrorMsg(eNULLARG % NULLRENSEP, EXIT=True) if opt == "-r": req = {} @@ -972,7 +1001,10 @@ targs.RenRequests.append(req) if opt == "-S": - ProgramOptions[EXISTSUFFIX] = val + if val: + ProgramOptions[EXISTSUFFIX] = val + else: + ErrorMsg(eNULLARG % NULLSUFFIX, EXIT=True) if opt == "-t": ProgramOptions[TESTMODE] = True