From fcb613a618e4dbb6e06dc1e4910337fbc52e3467 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 15 Sep 2017 18:30:42 +0200 Subject: o Make ee.kicad.Component implement @total_ordering. o Adding ref_type and ref_num that is a split of ref. o Ignore #PWR components when exporting BOM. --- src/ee/kicad/model.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'src/ee/kicad/model.py') diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index f44868b..7baa4a0 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -1,5 +1,8 @@ +import re from typing import List +from functools import total_ordering + class Position(object): def __init__(self, x, y): @@ -8,11 +11,11 @@ class Position(object): @property def x(self): - return _x + return self._x @property def y(self): - return _y + return self._y class ComponentField(object): @@ -41,6 +44,7 @@ class ComponentField(object): return self._position +@total_ordering class Component(object): def __init__(self, position, timestamp, library, name, unit, ref, fields): self._position = position @@ -51,6 +55,25 @@ class Component(object): self._ref = ref self._fields = fields # type List[ComponentField] + def __eq__(self, o: object) -> bool: + other = o # type Component + return isinstance(o, Component) and self.ref == other.ref + + def __lt__(self, o: object) -> bool: + if not isinstance(o, Component): + return True + + other = o # type Component + + if self.has_ref_num and other.has_ref_num: + return (self.ref_type, self.ref_num) < (other.ref_type, other.ref_num) + else: + # raise Exception("fail, ref={}".format(self.ref)) + return self.ref < other.ref + + def __hash__(self) -> int: + return self._ref.__hash__() + @property def unit(self): return self._unit @@ -59,6 +82,20 @@ class Component(object): def ref(self): return self._ref + @property + def has_ref_num(self): + return not self._ref.endswith("?") + + @property + def ref_type(self) -> str: + r = re.compile("([^0-9]+)(.+)") + return r.split(self._ref)[1] + + @property + def ref_num(self) -> int: + r = re.compile("([^0-9]+)(.+)") + return int(r.split(self._ref)[2]) + @property def value(self): return self._fields[1].value @@ -113,7 +150,7 @@ class Schematic(object): # Getters @property - def components(self): + def components(self) -> List[Component]: a = [] for s in self._sheets: for c in s.components: -- cgit v1.2.3