-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtango.py
65 lines (57 loc) · 2.37 KB
/
tango.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
#!/usr/bin/python
import re
from netlists import *
#
# Parses and handles a netlist in Tango format
#
class TangoNetlist(Netlist):
def __init__(self, filename=None, debug=False):
if not (filename is None):
self.readFromFile(filename)
self.parse(debug)
#
# Parse the imported text and generate a
# list of components and nets
#
def parse(self, debug=False, debugComponents=False):
# Use regular expression to parse components
r = re.compile("\[[\r\n]*([^\r\n]+)[\r\n]+([^\r\n]*)[\r\n]+([^\r\n]*)[\r\n]+[^\]]*\]")
l = re.findall(r, self.text)
self.components = []
for result in l:
designator = result[0]
footprint = result[1]
description = result[2]
self.components += [ Component(designator=designator, description=description, footprint=footprint) ]
if debug:
print("{:s}: {:s} ({:s})".format(designator, description, footprint))
if debug:
print("Found {:d} components.".format(len(self.components)))
if debugComponents:
print([c.getDesignator() for c in self.components])
# Use regular expression to parse nets
r = re.compile("\([\r\n]*([^\r\n]+)[\r\n]+([^\)]*)[\r\n]*\)")
l = re.findall(r, self.text)
self.nets = []
for result in l:
netlabel = result[0]
net = Net(label=netlabel)
pins = result[1].replace("\r", "\n").split("\n")
for pin in pins:
if len(pin.strip()) < 1:
# Skip empty lines
continue
if pin.find(",") < 0:
# Skip illegaly formated lines
continue
designator = pin.split(",")[0]
name = pin.split(",")[1]
component = self.getComponent(designator=designator)
if component is None:
print("Error: Net '{:s}' references unknown component '{:s}'. Skipping.".format(netlabel, designator))
continue
p = component.createPinFromName(name)
net.addPin(p)
self.nets += [net]
if debug:
print("{:d} pin(s) are connected to net '{:s}': {:s}".format(len(net.getPins()), net.getLabel(), str([str(p) for p in net.getPins()])))