coppersvgparent.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # usage:
  2. # coppersvgparent.py -d [svg folder]
  3. # looks for files where copper0/copper1 are not parent/child or child/parent
  4. import getopt, sys, os, os.path, re, xml.dom.minidom, xml.dom
  5. def usage():
  6. print """
  7. usage:
  8. coppersvgparent.py -d [svg folder]
  9. looks for files where copper0/copper1 are not parent/child or child/parent
  10. """
  11. def main():
  12. try:
  13. opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory"])
  14. except getopt.GetoptError, err:
  15. # print help information and exit:
  16. print str(err) # will print something like "option -a not recognized"
  17. usage()
  18. sys.exit(2)
  19. dir = None
  20. for o, a in opts:
  21. #print o
  22. #print a
  23. if o in ("-d", "--directory"):
  24. dir = a
  25. elif o in ("-h", "--help"):
  26. usage()
  27. sys.exit(2)
  28. else:
  29. assert False, "unhandled option"
  30. if(not(dir)):
  31. usage()
  32. sys.exit(2)
  33. for root, dirs, files in os.walk(dir, topdown=False):
  34. for filename in files:
  35. if not filename.endswith(".svg"):
  36. continue
  37. svgFilename = os.path.join(root, filename)
  38. try:
  39. dom = xml.dom.minidom.parse(svgFilename)
  40. except xml.parsers.expat.ExpatError, err:
  41. print str(err), svgFilename
  42. continue
  43. svg = dom.documentElement
  44. gNodes = svg.getElementsByTagName("g")
  45. copper1 = None
  46. copper0 = None
  47. for g in gNodes:
  48. if g.getAttribute("id") == "copper1":
  49. copper1 = g
  50. if g.getAttribute("id") == "copper0":
  51. copper0 = g
  52. if not copper1:
  53. continue
  54. if not copper0:
  55. continue
  56. if copper1.parentNode == copper0:
  57. continue
  58. if copper0.parentNode == copper1:
  59. continue
  60. print "not parents", svgFilename
  61. if __name__ == "__main__":
  62. main()