From ee62dd01720c8481599a717d067014164af7e096 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 13 May 2019 09:45:09 +0200 Subject: ee.kicad.model: Adding Symbol, exposing in addition existing parsing of value. v4 and v5 compatible. --- src/ee/kicad/model.py | 26 ++++++++++++++++++++++---- src/ee/kicad/read_schematic.py | 6 +++--- test/test_read_schematic.py | 4 ++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index edc900c..116d2ac 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -52,13 +52,27 @@ class ComponentField(object): return self._index >= len(ComponentField.names) +class Symbol(object): + def __init__(self, symbol: str): + assert symbol is not None + self._symbol = symbol + i = symbol.find(":") + if i == -1: + # KiCAD 4 + self.library = None + self.name = symbol + else: + self.library = symbol[0:i] + self.name = symbol[i+1:] + + @total_ordering class Component(object): - def __init__(self, position, timestamp, library, name, unit, ref, fields): + def __init__(self, position, timestamp, library, symbol, unit, ref, fields): self._position = position self._timestamp = timestamp self._library = library - self._name = name + self._symbol = symbol self._unit = unit self._ref = ref self._fields = fields # type List[ComponentField] @@ -95,6 +109,10 @@ class Component(object): def timestamp(self): return self._timestamp + @property + def symbol(self) -> Symbol: + return self._symbol + @property def unit(self): return self._unit @@ -204,7 +222,7 @@ class Schematic(object): raise KeyError("No such component: {}".format(ref)) - def find_component(self, ref, unit=1): + def find_component(self, ref, unit=1) -> Component: for c in self.components: if c.ref == ref and unit == unit: return c @@ -226,7 +244,7 @@ class Schematics(object): cs.append(c) return cs - def get_component(self, ref, unit=1): + def get_component(self, ref, unit=1) -> Component: for s in self._schematics: c = s.find_component(ref, unit) if c: diff --git a/src/ee/kicad/read_schematic.py b/src/ee/kicad/read_schematic.py index 401fc32..b5b4a1b 100644 --- a/src/ee/kicad/read_schematic.py +++ b/src/ee/kicad/read_schematic.py @@ -46,7 +46,7 @@ def read_schematic(path: str) -> Schematic: timestamp = None position = None library = None - name = None + symbol = None unit = None ref = None fields = [] @@ -67,7 +67,7 @@ def read_schematic(path: str) -> Schematic: raise EeException("Bad component line: {}:{}".format(path_basename, line)) if parts[0] == "L" and len(parts) == 3: - name = parts[1] + symbol = Symbol(parts[1]) ref = parts[2] elif parts[0] == "U" and len(parts) == 4: unit = int(parts[1]) @@ -97,7 +97,7 @@ def read_schematic(path: str) -> Schematic: else: raise EeException("Bad component field: {}: '{}'".format(path_basename, line)) - schematic.add_component(Component(position, timestamp, library, name, unit, ref, fields)) + schematic.add_component(Component(position, timestamp, library, symbol, unit, ref, fields)) def load(f): header = f.readline() diff --git a/test/test_read_schematic.py b/test/test_read_schematic.py index 2b269a6..9486c6d 100644 --- a/test/test_read_schematic.py +++ b/test/test_read_schematic.py @@ -45,6 +45,10 @@ def test_demo_1(): dump_bom(sch) r101 = sch.get_component("R101") assert r101 + # KiCAD 4 + assert r101.symbol.name == "R" + # If this doesn't fail, you've upgraded the .sch files to KiCAD5+. Don't do that, make a new one. + assert r101.symbol.library is None def test_sch(): -- cgit v1.2.3