diff --git a/tren.py b/tren.py index 5d88d2a..8949044 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.187 2010/03/11 03:22:09 tundra Exp $" +RCSID = "$Id: tren.py,v 1.188 2010/03/12 03:04:12 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -131,7 +131,7 @@ TOKFILCMD = "CMDLINE" TOKFILCTIME = "CTIME" TOKFILDEV = "DEV" -TOKFILFNAME = "FNAME" +TOKFILFNAME = "FNAME" TOKFILGID = "GID" TOKFILGRP = "GNAME" TOKFILINODE = "INODE" @@ -1056,7 +1056,7 @@ if r[2][0] == TOKDESCEND: orderedlist.reverse() - r[2] = str(orderedlist.index(target)) + r[2] = ComputeSeqString(field, orderedlist.index(target), ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]) ### # Unrecognized Renaming Token @@ -1100,6 +1100,105 @@ # End of 'ColumnPad()' +def ComputeSeqString(fmt, incr, alphabet): + + fmtlen = len(fmt) + alphalen = len(alphabet) + newval = "" + carry = None + + + # Do position-wise "addition" via symbol substitution moving from + # right-to-left adjusting for the fact that not all symbols in the + # format string will be in the alphabet. + + + # First convert the increment into a string in the base of the + # alphabet. + + idigits = [] + while incr > alphalen-1: + + incr, digit = incr/alphalen, incr % alphalen + idigits.append(alphabet[digit]) + + idigits.append(alphabet[incr]) + idigits.reverse() + incr = "".join(idigits) + + # Now to right-to-left digit addition with the format + # field. + + # Do position-wise "addition" via symbol substitution moving from + # right-to-left. Take into account that the format pattern string + # may be a different length than the increment string and that not + # all characters in the format pattern are guaranteed to exist in + # the alphabet. + + i = -1 + while abs(i) <= len(incr): + + sum = 0 + + if carry: + sum += carry + + if fmt and (abs(i) <= fmtlen) and fmt[i] in alphabet: + sum += alphabet.index(fmt[i]) + + sum += alphabet.index(incr[i]) + + # Do arithmetic modulo alphabet length. + + carry, digit = sum/alphalen, sum % alphalen + + if not carry: + carry = None + + newval = alphabet[digit] + newval + + i -= 1 + + if carry: + newval = alphabet[carry] + newval + + + # Suppress leading alphabet[0] symbols so we don't potentially + # overwrite non-alphabet symbols in the format string. If the + # user wants these leading alphabet[0] symbols, they can specify + # them in their format string. + + # Never clear out a lone Oth character though + + if newval != alphabet[0]: + while newval and newval[0] == alphabet[0]: + newval = newval[1:] + + + # Constrain the results to the length of the original format + # string, rolling over and warning the user as necessary. The one + # exception to this is when a null format string is passed. This + # is understood to mean that sequences of any length are + # permitted. + + # Result length constrained by format string + + if fmtlen: + + if len(newval) > fmtlen: + print "Result longer than pattern, rolling over" + newval = newval[-fmtlen:] + + return fmt[:-len(newval)] + newval + + # Any length result permitted + + else: + return newval + +# End of 'ComputeSeqString()' + + ##### # Condition Line Length With Fancy Wrap And Formatting #####