aboutsummaryrefslogtreecommitdiff
path: root/src/ee/kicad
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/kicad')
-rw-r--r--src/ee/kicad/functions.py128
1 files changed, 91 insertions, 37 deletions
diff --git a/src/ee/kicad/functions.py b/src/ee/kicad/functions.py
index 39614b6..2d8106a 100644
--- a/src/ee/kicad/functions.py
+++ b/src/ee/kicad/functions.py
@@ -1,7 +1,10 @@
import re
+import yaml
+
import ee.kicad.model
import ee.kicad.sch_fact_types as kicad_ft
+from ee import EeException
from ee.kicad import sch_fact_types
from ee.part import Part
from ee.part import common_fact_types
@@ -36,70 +39,121 @@ def part_type_from_ref_type(ref_type):
return uris.TRANSISTOR
-def part_type_strategy(part: Part) -> Part:
- pt = None
+def part_type_strategy(**kwargs):
+ def on_part(part: Part) -> Part:
+ pt = None
+
+ fp_lib = part.facts.get_value(kicad_ft.footprint_library)
+ if fp_lib is not None:
+ pt = part_type_from_footprint(fp_lib)
+
+ ref = part.get_only_schematic_reference()
+ if ref:
+ ref_type, ref_num = ee.kicad.model.split_ref(ref.referenceProp)
- fp_lib = part.facts.get_value(kicad_ft.footprint_library)
- if fp_lib is not None:
- pt = part_type_from_footprint(fp_lib)
+ if ref_type:
+ pt = part_type_from_ref_type(ref_type)
+
+ if pt is not None:
+ part.facts.add(common_fact_types.ee_component_type, pt)
+
+ return part
+
+ return on_part
+
+
+def fix_value_strategy(**kwargs):
+ def on_part(part: Part) -> Part:
+ ref = part.get_only_schematic_reference()
+
+ if not ref:
+ return part
- ref = part.get_only_schematic_reference()
- if ref:
ref_type, ref_num = ee.kicad.model.split_ref(ref.referenceProp)
- if ref_type:
- pt = part_type_from_ref_type(ref_type)
+ if not ref_num:
+ return part
- if pt is not None:
- part.facts.add(common_fact_types.ee_component_type, pt)
+ v = part.facts.get_value(sch_fact_types.value)
+ if not v:
+ return part
- return part
+ symbol_name = part.facts.get_value(sch_fact_types.symbol_name)
+ if ref_type in ("D", "R", "L", "C") and v == symbol_name:
+ part.remove_fact(uris.make_fact_key("value"))
-def fix_value_strategy(part: Part) -> Part:
- ref = part.get_only_schematic_reference()
+ if ref_type == "Q" and v == symbol_name and re.match("^Q_[NP]MOS_[DSG]{3}$", symbol_name):
+ part.remove_fact(uris.make_fact_key("value"))
- if not ref:
return part
- ref_type, ref_num = ee.kicad.model.split_ref(ref.referenceProp)
+ return on_part
- if not ref_num:
- return part
- v = part.facts.get_value(sch_fact_types.value)
- if not v:
+def mpn_strategy(**kwargs):
+ def on_part(part: Part) -> Part:
+ for field in part.facts.all(sch_fact_types.field):
+
+ k, v = re.split(":", field.value, 1)
+ if k == "mpn":
+ part.add_mpn(v)
+
return part
- symbol_name = part.facts.get_value(sch_fact_types.symbol_name)
+ return on_part
+
- if ref_type in ("D", "R", "L", "C") and v == symbol_name:
- part.remove_fact(uris.make_fact_key("value"))
+def map_footprint(footprint_mappings=None, **kwargs):
+ if footprint_mappings is None:
+ return None
- if ref_type == "Q" and v == symbol_name and re.match("^Q_[NP]MOS_[DSG]{3}$", symbol_name):
- part.remove_fact(uris.make_fact_key("value"))
+ mappings = {}
+ with open(footprint_mappings, "r") as f:
+ doc = yaml.load(f, Loader=yaml.SafeLoader)
+ if not isinstance(doc, dict):
+ raise EeException("The footprint mappings document must be a dict.")
- return part
+ if "kicad-to-common" not in doc:
+ raise EeException("The footprint mappings document must contain the key 'kicad-to-common'.")
+ for k, v in doc["kicad-to-common"].items():
+ if not isinstance(v, str):
+ raise EeException("Bad value for key {}, must be a string".format(k))
-def mpn_strategy(part: Part) -> Part:
- for field in part.facts.all(sch_fact_types.field):
+ mappings[k] = v
- k, v = re.split(":", field.value, 1)
- if k == "mpn":
- part.add_mpn(v)
+ def on_part(part: Part) -> Part:
+ kicad_footprint = part.facts.get_value(kicad_ft.footprint)
- return part
+ if not kicad_footprint:
+ return part
+ footprint = mappings.get(kicad_footprint, None)
+
+ if footprint:
+ part.facts.add(common_fact_types.footprint, footprint)
+
+ return part
-def default(part: Part) -> Part:
- functions = [
+ return on_part
+
+
+def default(**kwargs):
+ function_factories = [
fix_value_strategy,
mpn_strategy,
part_type_strategy,
+ map_footprint,
]
- for f in functions:
- part = f(part)
+ functions = [factory(**kwargs) for factory in function_factories]
+ functions = [f for f in functions if f is not None]
+
+ def on_part(part: Part) -> Part:
+ for f in functions:
+ part = f(part)
+
+ return part
- return part
+ return on_part