From b9c8e02072ea6da04ddc9fe9795f699697f25d07 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 21 Feb 2019 08:18:52 +0100 Subject: o Reformat. to_bom: export to_tom_xml too. model: o More type annotations. o Parse ref_type and ref_num when constructed. --- src/ee/kicad/__init__.py | 10 +++++----- src/ee/kicad/model.py | 33 +++++++++++++++++++++++---------- src/ee/kicad/to_bom.py | 4 ++-- 3 files changed, 30 insertions(+), 17 deletions(-) (limited to 'src/ee/kicad') diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py index 1492fc4..d28dcdc 100644 --- a/src/ee/kicad/__init__.py +++ b/src/ee/kicad/__init__.py @@ -1,10 +1,9 @@ from typing import Any -from ee import EeException from ee.kicad.read_schematic import read_schematic, read_schematics from ee.kicad.to_bom import to_bom, to_bom_xml -from .._utils import run_filters from .model import * +from .._utils import run_filters __all__ = [ "Component", @@ -18,19 +17,20 @@ __all__ = [ "to_pandas", ] + def parse_ref(ref): m = parse_ref.r.match(ref) if not m: return g = m.groups() -# print("groups={}".format(g)) - return (g[0], None if g[1] == "?" else int(g[1])) + return g[0], None if g[1] == "?" else int(g[1]) + parse_ref.r = re.compile("([^0-9]+)([0-9]+|\?)$") + def to_pandas(obj: Any, **kwarg): import pandas - import numpy as np def to_pandas_schematics(ss: Schematics): dfs = [to_pandas_schematic(schematic) for schematic in ss.schematics] diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index f6bd8c1..edc900c 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -1,8 +1,10 @@ import re -from typing import List +from typing import List, Set from functools import total_ordering +from ee import EeException + class Position(object): def __init__(self, x, y): @@ -61,8 +63,17 @@ class Component(object): self._ref = ref self._fields = fields # type List[ComponentField] + r = re.compile("([^0-9]+)(.+)") + try: + parts = r.split(self._ref) + self._ref_type = parts[1] + self._ref_num = int(parts[2]) + except ValueError: + self._ref_type = None + self._ref_num = None + def __eq__(self, o: object) -> bool: - other = o # type Component + other = o # type: Component return isinstance(o, Component) and self.ref == other.ref def __lt__(self, o: object) -> bool: @@ -94,17 +105,19 @@ class Component(object): @property def has_ref_num(self): - return not self._ref.endswith("?") + return self._ref_num is not None @property def ref_type(self) -> str: - r = re.compile("([^0-9]+)(.+)") - return r.split(self._ref)[1] + if self._ref_type is not None: + return self._ref_type + raise EeException("This component does not have a ref type") @property def ref_num(self) -> int: - r = re.compile("([^0-9]+)(.+)") - return int(r.split(self._ref)[2]) + if self._ref_num is not None: + return self._ref_num + raise EeException("This component does not have a ref num") @property def is_pwr(self) -> bool: @@ -115,11 +128,11 @@ class Component(object): return self.ref_type == "#FLG" @property - def value(self): + def value(self) -> str: return self._fields[1].value @property - def footprint(self): + def footprint(self) -> str: return self._fields[2].value @property @@ -177,7 +190,7 @@ class Schematic(object): self._sheets.append(sheet) @property - def components(self): + def components(self) -> Set[Component]: return frozenset(self._components) def add_component(self, component): diff --git a/src/ee/kicad/to_bom.py b/src/ee/kicad/to_bom.py index 62eb075..5c3d988 100644 --- a/src/ee/kicad/to_bom.py +++ b/src/ee/kicad/to_bom.py @@ -2,7 +2,7 @@ from typing import Iterable from xml.etree.ElementTree import Element from ee.kicad.model import * -__all__ = ["to_bom"] +__all__ = ["to_bom", "to_bom_xml"] def simple_element(parent, e, text): @@ -36,7 +36,7 @@ def comp(c: Component) -> Element: def to_bom(schematic: Schematic) -> Iterable[Component]: - return [c for c in sorted(schematic.components) if c.ref_type != "#PWR" and c.ref_type != "#FLG"] + return [c for c in sorted(schematic.components) if c.has_ref_num and c.ref_type != "#PWR" and c.ref_type != "#FLG"] def to_bom_xml(schematic: Schematic) -> Element: -- cgit v1.2.3