invisibleconnectors.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # usage:
  2. # invisibleconnectors.py -d [svg folder]
  3. # looks for connector-like svg elements with no fill or stroke
  4. import getopt, sys, os, os.path, re, xml.dom.minidom, xml.dom
  5. def usage():
  6. print """
  7. usage:
  8. invisibleconnectors.py -d [svg folder]
  9. looks for connector-like svg elements with no fill or stroke
  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. changed = 0
  44. todo = [dom.documentElement]
  45. while len(todo) > 0:
  46. element = todo.pop(0)
  47. for node in element.childNodes:
  48. if node.nodeType == node.ELEMENT_NODE:
  49. todo.append(node)
  50. stroke = element.getAttribute("stroke")
  51. fill = element.getAttribute("fill")
  52. strokewidth = element.getAttribute("stroke-width")
  53. id = element.getAttribute("id")
  54. if not "connector" in id:
  55. continue
  56. if len(stroke) == 0:
  57. style = element.getAttribute("style")
  58. if len(style) != 0:
  59. style = style.replace(";", ":")
  60. styles = style.split(":")
  61. for index, name in enumerate(styles):
  62. if name == "stroke":
  63. stroke = styles[index + 1]
  64. elif name == "stroke-width":
  65. strokewidth = styles[index + 1]
  66. elif name == "fill":
  67. fill = styles[index + 1]
  68. if len(fill) > 0 and fill != "none":
  69. continue
  70. if len(strokewidth) > 0 and strokewidth != "0":
  71. continue
  72. print "invisible connector", svgFilename
  73. if __name__ == "__main__":
  74. main()