findsvgunits.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # usage:
  2. # copper1svg.py -d [svg folder]
  3. # adds a <g id="copper1"> if there isn't one found already.
  4. import getopt, sys, os, os.path, re, xml.dom.minidom, xml.dom
  5. def usage():
  6. print """
  7. usage:
  8. findsvgunits.py -d [svg folder]
  9. looks for <svg> width and height attributes with no units or px units
  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. printFilename = svgFilename[len(dir):]
  39. illustratorString = "illustrator"
  40. noIllustratorString = " "
  41. try:
  42. dom = xml.dom.minidom.parse(svgFilename)
  43. except xml.parsers.expat.ExpatError, err:
  44. print str(err), svgFilename
  45. continue
  46. svg = dom.documentElement
  47. w = svg.getAttribute("width")
  48. h = svg.getAttribute("height")
  49. hok = None
  50. wok = None
  51. for unit in ["in", "cm", "mm"]:
  52. if w.endswith(unit):
  53. wok = 1
  54. if h.endswith(unit):
  55. hok = 1
  56. if (hok and wok):
  57. continue
  58. descr = noIllustratorString
  59. s = dom.toxml("UTF-8")
  60. if "Generator: Adobe Illustrator" in s:
  61. descr = illustratorString
  62. if not (w.endswith("px") or h.endswith("px")):
  63. print "no units {0} {1}".format(descr, printFilename)
  64. continue
  65. print "px units {0} {1}".format(descr, printFilename)
  66. if __name__ == "__main__":
  67. main()