aboutsummaryrefslogtreecommitdiff
path: root/src/ee
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-02-21 08:18:52 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-02-21 08:21:06 +0100
commitb9c8e02072ea6da04ddc9fe9795f699697f25d07 (patch)
treee4c71df9d67a7e535cb7ab81aeea3cb27b974a33 /src/ee
parent3937c49feaca7883f025b4f4e86958dc1ada09fe (diff)
downloadee-python-b9c8e02072ea6da04ddc9fe9795f699697f25d07.tar.gz
ee-python-b9c8e02072ea6da04ddc9fe9795f699697f25d07.tar.bz2
ee-python-b9c8e02072ea6da04ddc9fe9795f699697f25d07.tar.xz
ee-python-b9c8e02072ea6da04ddc9fe9795f699697f25d07.zip
o Reformat.
to_bom: export to_tom_xml too. model: o More type annotations. o Parse ref_type and ref_num when constructed.
Diffstat (limited to 'src/ee')
-rw-r--r--src/ee/kicad/__init__.py10
-rw-r--r--src/ee/kicad/model.py33
-rw-r--r--src/ee/kicad/to_bom.py4
3 files changed, 30 insertions, 17 deletions
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: