diff --git a/tren.py b/tren.py index 484d40c..31340b5 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.138 2010/02/03 01:04:08 tundra Exp $" +RCSID = "$Id: tren.py,v 1.139 2010/02/03 23:43:20 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -163,6 +163,7 @@ eERROR = "ERROR" eFILEOPEN = "Cannot open file '%s': %s!" eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN +eNOTHINGTODO = "Nothing to do!" eTOOMANYINC = "Too many includes! (Max is %d) Possible circular reference?" % MAXINCLUDES @@ -177,9 +178,8 @@ uTable = [PROGVER, HOMEPAGE, - "usage: " + PROGNAME + " [-1abCcdEefghqtvwXx] [-I file] [-l string] [-r old=new]... file|dir file|dir ...", + "usage: " + PROGNAME + " [-abCcdEefGghqtvwXx] [-I file] [-l string] [-r old=new]... file|dir file|dir ...", " where,", - " -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.", @@ -187,6 +187,7 @@ " -e Only perform renaming within extension portion of or directory name.", " -E Continue renaming even after an error is encountered", " -f Force renaming even if target file or directory name already exists.", + " -G Rename only the first instance of the specified string (Default)", " -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", @@ -326,7 +327,7 @@ [ST_CTIME, {}, ORDERBYCTIME], [ST_MTIME, {}, ORDERBYMTIME], [ST_SIZE, {}, ORDERBYSIZE], - ] + ] # Populate the data structures with each targets' stat information @@ -418,6 +419,63 @@ # End of 'DumpObj()' + + ##### + # Go Do The Requested Renaming + ##### + + def Rename(self): + + + # Make sure we actually have work to do + + if not self.SortViews[ORDERBYCMDLINE] or not self.RenRequests: + + ErrorMsg(eNOTHINGTODO) + return + + # Iterate over all the target filenames in command line order, + # applying each renaming in requested order + + + for target in self.SortViews[ORDERBYCMDLINE]: + + newname = target + for renrequest in self.RenRequests: + + old, new = self.ResolveRenameStrings(renrequest[OLD], renrequest[NEW]) + + # Handle global vs. 1st occurence replacement + + if renrequest[GLOBAL]: + newname = newname.replace(old, new) + + else: + i = newname.find(old) + if i >= 0: + newname = newname[:i] + new + newname[i + len(old):] + + print ColumnPad([target, newname], padwidth = 50) + + # End of 'Rename()' + + + ##### + # Resolve Rename Strings + ##### + + """ This takes "old" and "new" renaming strings as input and resolves + all outstanding renaming token references so that they can + then be applied to the rename. + """ + + def ResolveRenameStrings(self, old, new): + + return [old, new] + + # End of 'ReolveRenameStrings()' + + # End of class 'RenameTargets' @@ -720,7 +778,7 @@ ProgramOptions[DEBUG] = True try: - opts, args = getopt.getopt(OPTIONS, '1abbCcdEefghl:qR:r:tvw:Xx]') + opts, args = getopt.getopt(OPTIONS, 'abbCcdEefGghl:qR:r:tvw:Xx]') except getopt.GetoptError as e: ErrorMsg(eBADARG % e.args[0]) sys.exit(1) @@ -737,9 +795,6 @@ for opt, val in opts: - if opt == "-1": - ProgramOptions[GLOBAL] = False - if opt == "-a": ProgramOptions[TARGET] = ALL @@ -764,6 +819,9 @@ if opt == "-f": ProgramOptions[FORCERENAM] = True + if opt == "-G": + ProgramOptions[GLOBAL] = False + if opt == "-g": ProgramOptions[GLOBAL] = True @@ -829,8 +887,11 @@ targs.DumpObj() -for target in targs.SortViews[ORDERBYCMDLINE]: - print target + +# Perform reqested renamings + +targs.Rename() + # Release the target container if we created one