diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-05-20 15:27:38 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-05-20 15:27:38 +0200 |
commit | 9eba62ef1d6b4896de693976116f69a9692332d9 (patch) | |
tree | 98ae9fdf48818206a6db552938c53e35abf1c45d /src/ee/part/__init__.py | |
parent | def66a1bd81283d38b468b66ff6e4e34621a5ce2 (diff) | |
download | ee-python-9eba62ef1d6b4896de693976116f69a9692332d9.tar.gz ee-python-9eba62ef1d6b4896de693976116f69a9692332d9.tar.bz2 ee-python-9eba62ef1d6b4896de693976116f69a9692332d9.tar.xz ee-python-9eba62ef1d6b4896de693976116f69a9692332d9.zip |
ee:
o Adding FactType as a smaller wrapper around the fact uri.
o Adding ee.part.Facts, used as Part.facts
o Renaming 'type' uri to 'ee-component-type'.
kicad-make-bom: Removing strategy functionality, replaced with
part-apply-function. Moving default strategy contents into
ee.kicad.functions.
Diffstat (limited to 'src/ee/part/__init__.py')
-rw-r--r-- | src/ee/part/__init__.py | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/src/ee/part/__init__.py b/src/ee/part/__init__.py index ff6c7f1..a2face8 100644 --- a/src/ee/part/__init__.py +++ b/src/ee/part/__init__.py @@ -1,11 +1,13 @@ from pathlib import Path from typing import List, Optional, Iterator, Union -from ee import EeException +from ee import EeException, EeValType from ee.money import Money from ee.xml import types __all__ = [ + "FactType", + "EeValueFactType", "Part", "PartDb", "load_db", @@ -13,6 +15,18 @@ __all__ = [ ] +class FactType(object): + def __init__(self, uri: str, label: str): + self.uri = uri + self.label = label + + +class EeValueFactType(FactType): + def __init__(self, uri: str, label, ee_type: EeValType): + super().__init__(uri, label) + self.ee_type = ee_type + + class Reference(object): pass @@ -324,12 +338,42 @@ class Part(object): self.xml.factsProp.fact = [f for f in self.xml.factsProp.fact if f.keyProp != key] +class Fact(object): + def __init__(self, xml: types.Fact): + self.xml = xml + + @property + def key(self) -> str: + return self.xml.keyProp + + @property + def value(self) -> str: + return self.xml.valueProp + + class Facts(object): - def __init__(self, part): + def __init__(self, part: Part): self.part = part - def add(self, key: str, value: str, label=None): - self.part.get_facts().append(types.Fact(key=key, label=label, value=value)) + @staticmethod + def _get_key(key: Union[str, FactType]): + return key if isinstance(key, str) else key.uri + + def add(self, key: Union[str, FactType], value: str, label=None): + k = self._get_key(key) + self.part.get_facts().append(types.Fact(key=k, label=label, value=value)) + + def get(self, key: Union[str, FactType]) -> Fact: + k = self._get_key(key) + return next((Fact(f) for f in self.part.get_facts() if f.keyProp == k), None) + + def all(self, key: Union[str, FactType]) -> List[Fact]: + k = self._get_key(key) + return [Fact(f) for f in self.part.get_facts() if f.keyProp == k] + + def get_value(self, key: Union[str, FactType]) -> Optional[str]: + k = self._get_key(key) + return next((f.valueProp for f in self.part.get_facts() if f.keyProp == k), None) class Entry(object): |