Restructured the literals so that both the File Attribute and Sequence renaming tokens use same strings where they are common.
This is also going to be used to index into self.SortViews when doing sequence lookups.
Added code to catch the (illegal) situation where a user tries to rename the root of the file tree.
Made the rest of the 'stat' variables available as File Attribute renaming tokens.
1 parent 1a3bcf7 commit 4334a9f3174992e830491418756420074622ba98
@tundra tundra authored on 9 Mar 2010
Showing 1 changed file
View
119
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.179 2010/03/09 23:01:57 tundra Exp $"
RCSID = "$Id: tren.py,v 1.180 2010/03/10 00:50:14 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
#####
 
TOKDELIM = "/" # Delimiter for all renaming tokens
 
# File Attribute Renaming Tokens
 
TOKFILGID = "GID" # File GID replacement token
TOKFILGRP = "GNAME" # File Group Name replacement token
TOKFILLEN = "LEN" # File Length replacement token
TOKFILNAM = "FNAME" # File Name replacement token
TOKFILUID = "UID" # File UID replacement token
TOKFILUNAM = "UNAME" # File User Name replacement token
# Shared File Attribute And Sequence Renaming Tokens
 
TOKFILALPHA = "ALPHA"
TOKFILATIME = "ATIME"
TOKFILCMD = "CMDLINE"
TOKFILCTIME = "CTIME"
TOKFILDEV = "DEV"
TOKFILGID = "GID"
TOKFILGRP = "GNAME"
TOKFILINODE = "INODE"
TOKFILMODE = "MODE"
TOKFILMTIME = "MTIME"
TOKFILNAM = "FNAME"
TOKFILNLINK = "NLINK"
TOKFILSIZE = "SIZE"
TOKFILUID = "UID"
TOKFILUNAM = "UNAME"
 
# File Time Renaming Tokens
 
TOKADAY = "ADAY" # mm replacement token
 
BASE = "BASENAME"
PATHNAME = "PATHNAME"
STATS = "STATS"
ORDERBYCMDLINE = "ORDERBYCOMMANDLINE"
ORDERBYALPHA = "ORDERBYALPHA"
ORDERBYMODE = "ORDERBYMODE"
ORDERBYINODE = "ORDERBYINODE"
ORDERBYDEV = "ORDERBYDEV"
ORDERBYNLINK = "ORDERBYNLINK"
ORDERBYUID = "ORDERBYUID"
ORDERBYGID = "ORDERBYGID"
ORDERBYATIME = "ORDERBYATIME"
ORDERBYCTIME = "ORDERBYCTIME"
ORDERBYMTIME = "ORDERBYMTIME"
ORDERBYSIZE = "ORDERBYSIZE"
 
# These literals serve two purposes:
#
# 1) They are used as the type indicator in a Sequence Renaming Token
# 2) They are keys to the SortViews dictionary that stores the prestorted views
 
ORDERBYALPHA = TOKFILALPHA
ORDERBYATIME = TOKFILATIME
ORDERBYCMDLINE = TOKFILCMD
ORDERBYCTIME = TOKFILCTIME
ORDERBYDEV = TOKFILDEV
ORDERBYGID = TOKFILGID
ORDERBYINODE = TOKFILINODE
ORDERBYMODE = TOKFILMODE
ORDERBYMTIME = TOKFILMTIME
ORDERBYNLINK = TOKFILNLINK
ORDERBYSIZE = TOKFILSIZE
ORDERBYUID = TOKFILUID
 
# Rename string keys
 
NEW = "NEW"
eFILEOPEN = "Cannot open file '%s': %s!"
eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN
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.)"
eNOROOTRENAME = "Cannot rename root of file tree!"
eNULLARG = "%s cannot be empty!"
eRENAMEFAIL = "Attempt to rename '%s' to '%s' failed : %s!"
eTOKDELIM = "Renaming token '%s' missing delimiter!"
eTOKUNKNOWN = "Renaming Token '%s' is unknown type!"
except (IOError, OSError) as e:
ErrorMsg(eFILEOPEN % (fullname, e.args[1]))
 
# Store fullname, basename, and stat info for this file
 
self.RenNames[fullname] = {BASE : basename, PATHNAME : fullname.split(basename)[0], STATS : stats}
if basename:
self.RenNames[fullname] = {BASE : basename, PATHNAME : fullname.split(basename)[0], STATS : stats}
 
# Catch the case where they're trying to rename the root of the directory tree
 
else:
ErrorMsg(eNOROOTRENAME)
 
# Incrementally build lists of keys that will later be
# used to create sequence renaming tokens
 
###
# File Attribute Renaming Tokens
###
 
if r[2] == TOKFILGID:
if r[2] == TOKFILDEV:
r[2] = str(self.RenNames[target][STATS][ST_DEV])
elif r[2] == TOKFILGID:
r[2] = str(self.RenNames[target][STATS][ST_GID])
 
elif r[2] == TOKFILGRP:
r[2] = grp.getgrgid(self.RenNames[target][STATS][ST_GID])[0]
 
elif r[2] == TOKFILLEN:
r[2] = str(self.RenNames[target][STATS][ST_SIZE])
elif r[2] == TOKFILINODE:
r[2] = str(self.RenNames[target][STATS][ST_INO])
 
elif r[2] == TOKFILMODE:
r[2] = str(self.RenNames[target][STATS][ST_MODE])
 
elif r[2] == TOKFILNAM:
r[2] = os.path.basename(target)
 
elif r[2] == TOKFILNLINK:
r[2] = str(self.RenNames[target][STATS][ST_NLINK])
 
elif r[2] == TOKFILSIZE:
r[2] = str(self.RenNames[target][STATS][ST_SIZE])
 
elif r[2] == TOKFILUID:
r[2] = str(self.RenNames[target][STATS][ST_UID])
 
# Sequence Renaming Tokens
###
elif r[2] != "" and (r[2][0] == TOKASCEND or r[2][0] == TOKDESCEND):
pass
 
for token in self.SortViews:
print token
###
# Unrecognized Renaming Token
###