svgNoLayer.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # usage:
  2. # svgNoLayer.py -d <directory>
  3. #
  4. # <directory> is a folder, with subfolders, containing .svg files. In each svg file in the directory or its children
  5. # look for id='[layer]' where layer is the set of all layers in fritzing
  6. import getopt, sys, os, re, xml.dom.minidom, xml.dom
  7. def usage():
  8. print """
  9. usage:
  10. svgNoLayer.py -d [directory]
  11. <directory> is a folder, with subfolders, containing .svg files. In each svg file in the directory or its children
  12. look for id='[layer]' where layer is the set of all layers in fritzing
  13. """
  14. layers = ["icon","breadboardbreadboard", "breadboard", "breadboardWire", "breadboardLabel", "breadboardNote", "breadboardRuler", "schematic", "schematicWire","schematicTrace","schematicLabel", "schematicRuler", "board", "ratsnest", "silkscreen", "silkscreenLabel", "groundplane", "copper0", "copper0trace", "groundplane1", "copper1", "copper1trace", "silkscreen0", "silkscreen0Label", "soldermask", "outline", "keepout", "partimage", "pcbNote", "pcbRuler"]
  15. def main():
  16. try:
  17. opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory"])
  18. except getopt.GetoptError, err:
  19. # print help information and exit:
  20. print str(err) # will print something like "option -a not recognized"
  21. usage()
  22. sys.exit(2)
  23. outputDir = None
  24. for o, a in opts:
  25. #print o
  26. #print a
  27. if o in ("-d", "--directory"):
  28. outputDir = a
  29. elif o in ("-h", "--help"):
  30. usage()
  31. sys.exit(2)
  32. else:
  33. assert False, "unhandled option"
  34. if(not(outputDir)):
  35. usage()
  36. sys.exit(2)
  37. for root, dirs, files in os.walk(outputDir, topdown=False):
  38. for filename in files:
  39. if (filename.endswith(".svg")):
  40. infile = open(os.path.join(root, filename), "r")
  41. svg = infile.read()
  42. infile.close()
  43. match = None
  44. for layer in layers:
  45. match = re.search('id=[\'\"]' + layer, svg)
  46. if (match != None):
  47. break
  48. if match == None:
  49. print "{0} {1}".format(os.path.join(root, filename), "has no layer ids")
  50. else:
  51. msg = parseIDs(svg)
  52. if (msg != None):
  53. print "{0} {1}".format(os.path.join(root, filename), msg)
  54. def parseIDs(svg):
  55. try:
  56. dom = xml.dom.minidom.parseString(svg)
  57. except xml.parsers.expat.ExpatError, err:
  58. return "xml error " + str(err)
  59. root = dom.documentElement
  60. id = root.getAttribute("id")
  61. if id != None:
  62. for layer in layers:
  63. if (layer == id):
  64. return "svg contains layer id " + id
  65. for c in root.childNodes:
  66. if c.nodeType != c.ELEMENT_NODE:
  67. continue
  68. tag = c.tagName
  69. if tag == "metadata":
  70. continue
  71. if tag == "title":
  72. continue
  73. if tag == "desc":
  74. continue
  75. if tag == "defs":
  76. continue
  77. if tag == "sodipodi:namedview":
  78. continue
  79. gotOne = 0
  80. id = c.getAttribute("id")
  81. if (id != None):
  82. for layer in layers:
  83. if (layer == id):
  84. gotOne = 1
  85. break
  86. if gotOne == 0:
  87. return "child element '" + tag + "' with no layer id"
  88. return None
  89. if __name__ == "__main__":
  90. main()