aboutsummaryrefslogtreecommitdiff
path: root/src/ee/part
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-05-20 15:27:38 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2019-05-20 15:27:38 +0200
commit9eba62ef1d6b4896de693976116f69a9692332d9 (patch)
tree98ae9fdf48818206a6db552938c53e35abf1c45d /src/ee/part
parentdef66a1bd81283d38b468b66ff6e4e34621a5ce2 (diff)
downloadee-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')
-rw-r--r--src/ee/part/__init__.py52
-rw-r--r--src/ee/part/common_fact_types.py7
-rw-r--r--src/ee/part/fact_keys.py1
-rw-r--r--src/ee/part/requirement.py58
4 files changed, 114 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):
diff --git a/src/ee/part/common_fact_types.py b/src/ee/part/common_fact_types.py
new file mode 100644
index 0000000..12c099e
--- /dev/null
+++ b/src/ee/part/common_fact_types.py
@@ -0,0 +1,7 @@
+import ee
+from ee.part import EeValueFactType, fact_keys
+
+resistance = EeValueFactType(fact_keys.resistance, "Resistance", ee.resistance_type)
+capacitance = EeValueFactType(fact_keys.capacitance, "Capacitance", ee.capacitance_type)
+
+ee_component_type = EeValueFactType(fact_keys.ee_component_type, "EE component type", ee.capacitance_type)
diff --git a/src/ee/part/fact_keys.py b/src/ee/part/fact_keys.py
index f1f02c5..65dc699 100644
--- a/src/ee/part/fact_keys.py
+++ b/src/ee/part/fact_keys.py
@@ -1,3 +1,4 @@
+ee_component_type = "http://purl.org/ee/fact-type/ee-component-type"
capacitance = "http://purl.org/ee/fact-type/capacitance"
max_voltage = "http://purl.org/ee/fact-type/voltage"
# https://en.wikipedia.org/wiki/Ceramic_capacitor#Class_2_ceramic_capacitors
diff --git a/src/ee/part/requirement.py b/src/ee/part/requirement.py
new file mode 100644
index 0000000..a9381df
--- /dev/null
+++ b/src/ee/part/requirement.py
@@ -0,0 +1,58 @@
+from typing import List
+
+from ee.part import Part, fact_keys, FactType, common_fact_types
+
+
+class Requirement(object):
+ def __init__(self, part: Part):
+ self.part = part
+
+
+class EqualRequirement(Requirement):
+ def __init__(self, part: Part, fact_type: FactType, value: str):
+ super().__init__(part)
+ self.fact_type = fact_type
+ self.value = value
+
+ def __str__(self):
+ return "{}.{} == {}".format(self.part.printable_reference, self.fact_type, self.value)
+
+
+class MinRequirement(Requirement):
+ def __init__(self, part: Part, fact_type: FactType, value: str):
+ super().__init__(part)
+ self.fact_type = fact_type
+ self.value = value
+
+ def __str__(self):
+ return "{}.{} == {}".format(self.part.printable_reference, self.fact_type, self.value)
+
+
+class MaxRequirement(Requirement):
+ def __init__(self, part: Part, fact_type: FactType, value: str):
+ super().__init__(part)
+ self.fact_type = fact_type
+ self.value = value
+
+ def __str__(self):
+ return "{}.{} == {}".format(self.part.printable_reference, self.fact_type, self.value)
+
+
+class PartAnalysis(object):
+ def __init__(self, part: Part, requirements: List[Requirement]):
+ self.part = part
+ self.requirements = requirements
+
+
+def analyze_requirements(part: Part) -> PartAnalysis:
+ rs = []
+
+ resistance = part.find_fact(common_fact_types.resistance.uri)
+ if resistance:
+ rs.append(EqualRequirement(part, common_fact_types.resistance, resistance.valueProp))
+
+ capacitance = part.find_fact(common_fact_types.capacitance.uri)
+ if capacitance:
+ rs.append(EqualRequirement(part, common_fact_types.capacitance, capacitance.valueProp))
+
+ return PartAnalysis(part, rs)