unzeroradius.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import getopt, sys, os, os.path, re, xml.dom.minidom, xml.dom
  2. def usage():
  3. print """
  4. usage:
  5. unzeroradius.py -d [svg folder]
  6. if a file has <circle r='0' replace with r='.00000000001'
  7. """
  8. def main():
  9. try:
  10. opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory"])
  11. except getopt.GetoptError, err:
  12. # print help information and exit:
  13. print str(err) # will print something like "option -a not recognized"
  14. usage()
  15. sys.exit(2)
  16. dir = None
  17. for o, a in opts:
  18. #print o
  19. #print a
  20. if o in ("-d", "--directory"):
  21. dir = a
  22. elif o in ("-h", "--help"):
  23. usage()
  24. sys.exit(2)
  25. else:
  26. assert False, "unhandled option"
  27. if(not(dir)):
  28. usage()
  29. sys.exit(2)
  30. for root, dirs, files in os.walk(dir, topdown=False):
  31. for filename in files:
  32. if not filename.endswith(".svg"):
  33. continue
  34. svgFilename = os.path.join(root, filename)
  35. try:
  36. dom = xml.dom.minidom.parse(svgFilename)
  37. except xml.parsers.expat.ExpatError, err:
  38. print str(err), svgFilename
  39. continue
  40. svg = dom.documentElement
  41. circleNodes = svg.getElementsByTagName("circle")
  42. count = 0
  43. for circle in circleNodes:
  44. r = circle.getAttribute("r")
  45. if r == "0":
  46. circle.setAttribute("r", "0.00000000001")
  47. count += 1
  48. if count == 0:
  49. #print ".",
  50. continue
  51. print "got zero", svgFilename
  52. outfile = open(svgFilename, 'wb')
  53. s = dom.toxml("UTF-8")
  54. outfile.write(s)
  55. outfile.flush()
  56. outfile.close()
  57. if __name__ == "__main__":
  58. main()