schem2csv.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # usage:
  2. # schem2csv.py -d <directory>
  3. #
  4. # <directory> is a folder containing schematic .svg files.
  5. # save a csv file to <csv file path>
  6. import getopt, sys, os, re, csv, xml.dom.minidom, xml.dom
  7. def usage():
  8. print """
  9. usage:
  10. schem2csv.py -d <directory> -c <csv file path>
  11. <directory> is a folder containing .svg files.
  12. save a csv file to <csv file path>
  13. """
  14. def main():
  15. try:
  16. opts, args = getopt.getopt(sys.argv[1:], "hd:c:", ["help", "directory", "csv"])
  17. except getopt.GetoptError, err:
  18. # print help information and exit:
  19. print str(err) # will print something like "option -a not recognized"
  20. usage()
  21. sys.exit(2)
  22. outputDir = None
  23. csvPath = 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 ("-c", "--csv"):
  30. csvPath = a
  31. elif o in ("-h", "--help"):
  32. usage()
  33. sys.exit(2)
  34. else:
  35. assert False, "unhandled option"
  36. if not outputDir:
  37. usage()
  38. sys.exit(2)
  39. if not csvPath:
  40. usage()
  41. sys.exit(2)
  42. writer = None
  43. file = None
  44. try:
  45. file = open(csvPath, 'wb')
  46. writer = csv.writer(file, delimiter=',')
  47. writer.writerow(["current svg","location","disp","use svg"] )
  48. writer.writerow(["","","rectangular\nillustrated\nsparkfun","path of sparkfun svg to use"] )
  49. writer.writerow(["","","",""] )
  50. except:
  51. print "unable to save to", csvPath
  52. sys.exit(2)
  53. names = []
  54. for filename in os.listdir(outputDir):
  55. if (filename.endswith(".svg")):
  56. svgFilename = os.path.join(outputDir, filename)
  57. try:
  58. dom = xml.dom.minidom.parse(svgFilename)
  59. except xml.parsers.expat.ExpatError, err:
  60. print str(err), svgFilename
  61. continue
  62. svg = dom.documentElement
  63. viewBox = svg.getAttribute("viewBox").split(" ")
  64. area = sys.float_info.max
  65. if len(viewBox) != 4:
  66. print "no viewBox in", svgFilename
  67. else:
  68. try:
  69. viewBoxWidth = float(viewBox[2]) - float(viewBox[0])
  70. viewBoxHeight = float(viewBox[3]) - float(viewBox[1])
  71. area = viewBoxWidth * viewBoxHeight * 0.75
  72. if (area <= 0):
  73. print "bad viewBox (1) in", svgFilename
  74. except:
  75. print "bad viewBox (2) in", svgFilename
  76. disp = "illustrated"
  77. rects = svg.getElementsByTagName("rect")
  78. for rect in rects:
  79. try:
  80. width = float(rect.getAttribute("width"))
  81. height = float(rect.getAttribute("height"))
  82. if width * height > area:
  83. disp = "rectangular"
  84. break
  85. except:
  86. print "bad rect in", svgFilename
  87. location = "core"
  88. if "contrib" in svgFilename:
  89. location = "contrib"
  90. writer.writerow([filename, location, disp, ""])
  91. if file:
  92. file.close()
  93. def getText(nodelist):
  94. rc = []
  95. for node in nodelist:
  96. if node.nodeType == node.TEXT_NODE:
  97. rc.append(node.data)
  98. return ''.join(rc)
  99. if __name__ == "__main__":
  100. main()