diff options
Diffstat (limited to 'src/ee/kicad')
-rw-r--r-- | src/ee/kicad/__init__.py | 41 | ||||
-rw-r--r-- | src/ee/kicad/bom/__init__.py | 2 | ||||
-rw-r--r-- | src/ee/kicad/model.py | 4 |
3 files changed, 45 insertions, 2 deletions
diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py index c4c3af5..b270ba1 100644 --- a/src/ee/kicad/__init__.py +++ b/src/ee/kicad/__init__.py @@ -1,4 +1,5 @@ -import re +from typing import Any + from ee import EeException from ee.kicad.read_schematic import read_schematic from ee.kicad.to_bom import to_bom @@ -11,4 +12,42 @@ __all__ = [ "Schematic", "read_schematic", "to_bom", + "to_pandas", ] + + +def to_pandas(obj: Any, **kwarg): + import pandas + + def run_filter(filters, obj): + for f in filters: + if not f(obj): + return False + return True + + def to_pandas_schematic(sch: Schematic, **kwarg): + def make_dict(c: Component): + return { + "ref": c.ref, + "ref_type": c.ref_type, + "ref_num": c.ref_num, + "value": c.value + } + + components = sch.components + + filters = [] + + if not kwarg.get("include_pwr", False): + filters.append(lambda c: not c.is_pwr) + + data = [make_dict(c) for c in components if run_filter(filters, c)] + + return pandas.DataFrame(data=data, columns=["ref", "ref_type", "ref_num", "value"]).\ + set_index("ref").\ + sort_index() + + if isinstance(obj, Schematic): + return to_pandas_schematic(obj) + else: + raise EeException("Unsupported type: {}".format(type(obj))) diff --git a/src/ee/kicad/bom/__init__.py b/src/ee/kicad/bom/__init__.py index 2a32521..a36068b 100644 --- a/src/ee/kicad/bom/__init__.py +++ b/src/ee/kicad/bom/__init__.py @@ -67,7 +67,7 @@ class Bom(object): return self._components def all_field_names(self): - fields = set(['ref', 'value']) + fields = {'ref', 'value'} for c in self._components.values(): for f in c.fields: fields.add(f) diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index 7baa4a0..519a978 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -97,6 +97,10 @@ class Component(object): return int(r.split(self._ref)[2]) @property + def is_pwr(self) -> bool: + return self.ref_type == "#PWR" + + @property def value(self): return self._fields[1].value |