Advertisement
ijontichy

mass renamer

Aug 20th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import os
  5. import collections
  6.  
  7. from pathwalker.classes import pathwalker
  8.  
  9. PROGNAME = os.path.basename(sys.argv[0])
  10. USAGE = "usage: {0} <oldnames> <newnames> <dir>".format(PROGNAME)
  11.  
  12. checkStrs = ["\"{0}\""]
  13. checkStrs2 = ["actor {0}:", "model {0}", "actor {0} :"]
  14.  
  15. def isText(x):
  16.     return x.endswith(".txt")
  17.  
  18. def main(onFile, nnFile, wDir):
  19.  
  20.     on = open(onFile)
  21.     nn = open(nnFile)
  22.  
  23.     oldNames = []
  24.     newNames = []
  25.     oldNamesL = []
  26.     newNamesL = []
  27.  
  28.     fileDict = collections.defaultdict(str)
  29.  
  30.     try:
  31.         walker = pathwalker.PathWalker(wDir)
  32.     except pathwalker.NotDirError:
  33.         usageAndExit("directory doesn't exist")
  34.  
  35.     for old, new in zip(on, nn):
  36.  
  37.         if old:
  38.             oldNames.append(old[:-1].strip('"').lower())
  39.             oldNamesL.append(old[:-1].strip('"').lower())
  40.  
  41.         if new:
  42.             newNames.append(new[:-1].strip('"'))
  43.             newNamesL.append(new[:-1].strip('"').lower())
  44.  
  45.     if len(oldNames) != len(newNames):
  46.         raise ValueError("old name count and new name count MUST be the same")
  47.  
  48.     nameTrans  = dict(zip(oldNames,  newNames)  )
  49.     nameTransL = dict(zip(oldNamesL, newNamesL) )
  50.  
  51.     txtFiles = walker.walk(isText, abs=True)
  52.  
  53.     txtFiles = txtFiles.flattenFiles()
  54.  
  55.     for txt in txtFiles:
  56.  
  57.         cFile = open(txt)
  58.  
  59.         for line in cFile:
  60.  
  61.             testLine = line[:-1].lower()
  62.             line = line[:-1]
  63.  
  64.             for n in nameTransL:
  65.  
  66.                 fName = "\"{0}\"".format(nameTrans[n])
  67.  
  68.  
  69.                 for s in checkStrs:
  70.  
  71.                     s2 = s.format(n)
  72.                     s3 = s.format(nameTrans[n])
  73.                     l = len(s2)
  74.  
  75.                     if s2 in testLine:
  76.  
  77.                         print(testLine)
  78.  
  79.                         p = testLine.find(s2)
  80.                         line = line[:p] + s3 + line[p+l:]
  81.  
  82.                         print(line)
  83.                         break
  84.  
  85.                 for s in checkStrs2:
  86.  
  87.                     s2 = s.format(n)
  88.                     s3 = s.format(fName)
  89.                     l = len(s2)
  90.  
  91.                     if s2 in testLine:
  92.  
  93.                         print(testLine)
  94.  
  95.  
  96.                         p = testLine.find(s2)
  97.                         line = line[:p] + s3 + line[p+l:]
  98.  
  99.                         print(line)
  100.                         break
  101.  
  102.             fileDict[txt] = "\n".join([fileDict[txt], line])
  103.  
  104.     for txt in sorted(fileDict):
  105.         cFile = open(txt, "w")
  106.         cFile.write(fileDict[txt])
  107.         cFile.close()
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. def usageAndExit(reason):
  116.     print("{0}: {1}".format(PROGNAME, reason) )
  117.     print(USAGE)
  118.     sys.exit()
  119.  
  120.  
  121. if __name__ == "__main__":
  122.  
  123.     if len(sys.argv) < 2:
  124.         usageAndExit("no old name file")
  125.  
  126.     if len(sys.argv) < 3:
  127.         usageAndExit("no new name file")
  128.  
  129.     if len(sys.argv) < 4:
  130.         usageAndExit("no working directory")
  131.  
  132.     main(*sys.argv[1:4])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement