| |
---|
| | |
---|
| | PROGNAME = "tren.py" |
---|
| | BASENAME = PROGNAME.split(".py")[0] |
---|
| | PROGENV = BASENAME.upper() |
---|
| | RCSID = "$Id: tren.py,v 1.151 2010/02/12 18:29:38 tundra Exp $" |
---|
| | RCSID = "$Id: tren.py,v 1.152 2010/02/17 16:31:35 tundra Exp $" |
---|
| | VERSION = RCSID.split()[2] |
---|
| | |
---|
| | # Copyright Information |
---|
| | |
---|
| |
---|
| | DEFEXT = "." # Default name/extension separator |
---|
| | DEFLEN = 80 # Default output line length |
---|
| | DEFSEP = "=" # Default rename command separator: old=new |
---|
| | DEFSUFFIX = ".backup" # String used to rename existing targets |
---|
| | DEFESC = "\\" # 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 |
---|
| |
---|
| | # Internal program state literals |
---|
| | |
---|
| | DEBUG = "DEBUG" |
---|
| | CASESENSITIVE = "CASESENSITIVE" |
---|
| | ESCAPE = "ESCAPE" |
---|
| | ERRORCONTINUE = "ERRORCONTINUE" |
---|
| | EXISTSUFFIX = "EXISTSUFFIX" |
---|
| | EXTDELIM = "EXTDELIM" |
---|
| | FORCERENAM = "FORCERENAM" |
---|
| |
---|
| | ##### |
---|
| | # Error Messages |
---|
| | ##### |
---|
| | |
---|
| | eARGLENGTH = "%s must contain exactly %s character(s)!" |
---|
| | eBADARG = "Invalid command line: %s!" |
---|
| | eBADINCL = "option -%s requires argument" % INCL |
---|
| | eBADNEWOLD = "Bad -r argument '%s'! Requires exactly one new, old string separator (Default: " + DEFSEP + ")" |
---|
| | eBADLEN = "Bad line length '%s'!" |
---|
| |
---|
| | " -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, |
---|
| | " -P string Use 'string' as the escape sequence (Default: %s)" % DEFESC, |
---|
| | " -P char Use 'char' 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 char Separator character for -r rename arguments (Default: %s)" % DEFSEP, |
---|
| | " -r old=new Replace old with new in file or directory names", |
---|
| | " -S suffix Suffix to use when renaming existing filenames (Default: %s)" % DEFSUFFIX, |
---|
| | " -t Test mode, don't rename, just show what the program *would* do", |
---|
| | " -v Print detailed program version information and continue", |
---|
| |
---|
| | ProgramOptions = { |
---|
| | |
---|
| | DEBUG : False, # Debugging off |
---|
| | CASESENSITIVE : True, # Search is case-sensitive |
---|
| | ESCAPE : DEFESC, # Escape string |
---|
| | ERRORCONTINUE : False, # Do not continue after error |
---|
| | EXISTSUFFIX : DEFSUFFIX, # What to tack on when renaming existing targets |
---|
| | EXTDELIM : DEFEXT, # Name/Extension delimiter |
---|
| | FORCERENAM : False, # Do not rename if target already exists |
---|
| |
---|
| | |
---|
| | ErrorMsg(eNOTHINGTODO) |
---|
| | return |
---|
| | |
---|
| | # Iterate over all the target filenames in command line order, |
---|
| | # applying each renaming in requested order |
---|
| | # Create a list of all renaming to be done. |
---|
| | # This includes the renaming of any existing targets. |
---|
| | |
---|
| | |
---|
| | for target in self.SortViews[ORDERBYCMDLINE]: |
---|
| | |
---|
| |
---|
| | # Scan string ignoring escaped separators |
---|
| | |
---|
| | if arg[i:].startswith(oldnewsep): |
---|
| | |
---|
| | if (i > 0 and (arg[i-1] != DEFESC)) or i == 0: |
---|
| | if (i > 0 and (arg[i-1] != ProgramOptions[ESCAPE])) or i == 0: |
---|
| | sepindex = i |
---|
| | numseps += 1 |
---|
| | |
---|
| | i += len(oldnewsep) |
---|
| |
---|
| | ErrorMsg(eBADNEWOLD % arg, EXIT=True) |
---|
| | |
---|
| | else: |
---|
| | old, new = arg[:sepindex], arg[sepindex + len(oldnewsep):] |
---|
| | old = old.replace(DEFESC + oldnewsep, oldnewsep) |
---|
| | new = new.replace(DEFESC + oldnewsep, oldnewsep) |
---|
| | old = old.replace(ProgramOptions[ESCAPE] + oldnewsep, oldnewsep) |
---|
| | new = new.replace(ProgramOptions[ESCAPE] + oldnewsep, oldnewsep) |
---|
| | return [old, new] |
---|
| | |
---|
| | # End of 'GetOldNew()' |
---|
| | |
---|
| |
---|
| | else: |
---|
| | ErrorMsg(eNULLARG % NULLEXT, EXIT=True) |
---|
| | |
---|
| | if opt == "-P": |
---|
| | if val: |
---|
| | pass |
---|
| | if len(val) == 1: |
---|
| | ProgramOptions[ESCAPE] = val |
---|
| | else: |
---|
| | ErrorMsg(eNULLARG % NULLESC, EXIT=True) |
---|
| | ErrorMsg(eARGLENGTH % (NULLESC, "1"), EXIT=True) |
---|
| | |
---|
| | if opt == "-q": |
---|
| | ProgramOptions[QUIET] = True |
---|
| | |
---|
| | if opt == '-R': |
---|
| | if val: |
---|
| | if len(val) == 1: |
---|
| | ProgramOptions[RENSEP] = val |
---|
| | else: |
---|
| | ErrorMsg(eNULLARG % NULLRENSEP, EXIT=True) |
---|
| | ErrorMsg(eARGLENGTH % (NULLRENSEP, "1"), EXIT=True) |
---|
| | |
---|
| | if opt == "-r": |
---|
| | req = {} |
---|
| | req[OLD], req[NEW] = GetOldNew(val) |
---|
| |
---|
| | |