-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrdParser.py
86 lines (69 loc) · 2.82 KB
/
brdParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# .brd File Parser, Version 1.0
# Patrick Robert Willmann - Group 13
from pcbpal import *
import xml.etree.ElementTree as ET
class FileManager:
def __init__(self, filename):
self.fileName = filename
def loadFile(self):
self.fileObject = open(self.fileName, "r")
self.tempStorage = self.fileObject.read()
def closeFile(self):
self.fileObject.close()
class BrdParser:
""" .somethingTop nomenclature refers to the xml.etree element dictionary
.somethingTag refers to the output dictionary which will be passed to the final Board object
"""
def __init__(self, inputstring):
self.inputString = inputstring
self.root = ET.fromstring(self.inputString)
print("\nTop Level Tag is: " + self.root.tag, self.root.attrib)
self.gridTop = self.root.find('.//grid')
print(self.gridTop)
self.plainTop = self.root.find('.//plain')
print(self.plainTop)
self.designRulesTop = self.root.find('.//designrules')
print(self.designRulesTop)
self.signalsTop = self.root.find('.//signals')
print(self.signalsTop)
self.elementsTop = self.root.find('.//elements')
print(self.elementsTop)
self.librariesTop = self.root.find('.//libraries')
print(self.librariesTop)
self.gridTag = Tag("grid")
self.plainTag = Tag("plain")
self.designRulesTag = Tag("designrules")
self.signalsTag = Tag("signals")
self.elementsTag = Tag("elements")
self.librariesTag = Tag("libraries")
self.place(self.gridTop, self.gridTag)
self.place(self.plainTop, self.plainTag)
self.place(self.designRulesTop, self.designRulesTag)
self.place(self.signalsTop, self.signalsTag)
self.place(self.elementsTop, self.elementsTag)
self.place(self.librariesTop, self.librariesTag)
def place(self, elementIn: ET.Element, tag: Tag):
print("Placing... ", elementIn.tag)
tag.attr = elementIn.attrib
if tag.attr:
print(tag.attr)
else:
print("No Attributes")
for child in elementIn:
tag.children.append(Tag(child.tag, child.attrib))
print(tag.children[-1].name, tag.children[-1].attr)
if len(list(child)):
self.place(child, tag.children[-1])
print("Successfully placed: ", tag.name)
def createBoard(path) -> Board:
fileIn = FileManager(path)
fileIn.loadFile()
parser = BrdParser(fileIn.tempStorage)
fileIn.closeFile()
board = Board(parser.gridTag, parser.plainTag, parser.librariesTag, parser.designRulesTag, parser.elementsTag, parser.signalsTag)
return board
if __name__== "__main__":
filepath = input("Enter the file path:\n")
# path = "sample.brd"
board = createBoard(filepath)
print(board)