diff --git a/InteractiveHtmlBom/schematic_data/__init__.py b/InteractiveHtmlBom/schematic_data/__init__.py index 0e4e242d..5601da78 100644 --- a/InteractiveHtmlBom/schematic_data/__init__.py +++ b/InteractiveHtmlBom/schematic_data/__init__.py @@ -1,9 +1,11 @@ import os from xmlparser import XmlParser +from netlistparser import NetlistParser PARSERS = { - '.xml': XmlParser + '.xml': XmlParser, + '.net': NetlistParser } diff --git a/InteractiveHtmlBom/schematic_data/netlistparser.py b/InteractiveHtmlBom/schematic_data/netlistparser.py new file mode 100644 index 00000000..b36805a2 --- /dev/null +++ b/InteractiveHtmlBom/schematic_data/netlistparser.py @@ -0,0 +1,36 @@ +from parser_base import ParserBase +from sexpressions import parse_sexpression + + +class NetlistParser(ParserBase): + def get_extra_field_data(self): + with open(self.file_name, 'r') as f: + sexpression = parse_sexpression(f.read()) + components = None + for s in sexpression: + if s[0] == 'components': + components = s[1:] + if components is None: + return None + field_set = set() + comp_dict = {} + for c in components: + ref = None + fields = None + for f in c[1:]: + if f[0] == 'ref': + ref = f[1] + if f[0] == 'fields': + fields = f[1:] + if ref is None: + return None + if fields is None: + continue + for f in fields: + if len(f) > 1: + field_set.add(f[1][1]) + if len(f) > 2: + ref_fields = comp_dict.setdefault(ref, {}) + ref_fields[f[1][1]] = f[2] + + return list(field_set), comp_dict diff --git a/InteractiveHtmlBom/schematic_data/sexpressions.py b/InteractiveHtmlBom/schematic_data/sexpressions.py new file mode 100644 index 00000000..54bef57d --- /dev/null +++ b/InteractiveHtmlBom/schematic_data/sexpressions.py @@ -0,0 +1,32 @@ +import re + +term_regex = r'''(?mx) + \s*(?: + (?P\()| + (?P\))| + (?P"[^"]*")| + (?P[^(^)\s]+) + )''' +pattern = re.compile(term_regex) + + +def parse_sexpression(sexpression): + stack = [] + out = [] + for terms in pattern.finditer(sexpression): + term, value = [(t, v) for t, v in terms.groupdict().items() if v][0] + if term == 'open': + stack.append(out) + out = [] + elif term == 'close': + assert stack, "Trouble with nesting of brackets" + tmp, out = out, stack.pop(-1) + out.append(tmp) + elif term == 'sq': + out.append(value[1:-1]) + elif term == 's': + out.append(value) + else: + raise NotImplementedError("Error: %s, %s" % (term, value)) + assert not stack, "Trouble with nesting of brackets" + return out[0]