oldfamily.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # usage:
  2. # oldfamily.py -d <directory>
  3. #
  4. # <directory> is a folder containing .fzp files. In each fzp file in the directory containing a <family> text element:
  5. # like <family>x</family>
  6. # the text is renamed to "obsolete x" and saved back to the file
  7. import getopt, sys, os, re
  8. def usage():
  9. print """
  10. usage:
  11. oldfamily.py -d [directory]
  12. directory is a folder containing .fzp files.
  13. In each fzp file in the directory containing a <family> text element:
  14. like <family>x</family>
  15. the text is renamed to "obsolete x" and saved back to the file.
  16. """
  17. def main():
  18. try:
  19. opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory"])
  20. except getopt.GetoptError, err:
  21. # print help information and exit:
  22. print str(err) # will print something like "option -a not recognized"
  23. usage()
  24. sys.exit(2)
  25. outputDir = None
  26. for o, a in opts:
  27. #print o
  28. #print a
  29. if o in ("-d", "--directory"):
  30. outputDir = a
  31. elif o in ("-h", "--help"):
  32. usage()
  33. sys.exit(2)
  34. else:
  35. assert False, "unhandled option"
  36. if(not(outputDir)):
  37. usage()
  38. sys.exit(2)
  39. for filename in os.listdir(outputDir):
  40. if (filename.endswith(".fzp")):
  41. infile = open(os.path.join(outputDir, filename), "r")
  42. fzp = infile.read();
  43. infile.close();
  44. match = re.search('(<property.+name=\"family\".*>)(.+)(</property>)', fzp)
  45. if (match != None):
  46. if (not match.group(2).startswith("obsolete")):
  47. oldfzp = ""
  48. if match.group(2).startswith("old "):
  49. oldfzp = re.sub(r'(<property.+name=\"family\".*>)old (.+)(</property>)', r'\1obsolete \2\3', fzp);
  50. else:
  51. oldfzp = re.sub(r'(<property.+name=\"family\".*>)(.+)(</property>)', r'\1obsolete \2\3', fzp);
  52. print "{0}:{1}".format(filename, match.group(2))
  53. outfile = open(os.path.join(outputDir, filename), "w")
  54. outfile.write(oldfzp);
  55. outfile.close()
  56. if __name__ == "__main__":
  57. main()