Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Bugfix: Picker: Attach params on ID or MFR pick (#109)
Browse files Browse the repository at this point in the history
When picking a part by LCSC ID or MFR attach the parsed parameters to the module.
  • Loading branch information
iopapamanoglou authored Oct 29, 2024
1 parent d28810e commit 80c5fde
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/faebryk/libs/picker/jlcpcb/jlcpcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,17 @@ def attach(
f"Failed to parse parameters for component {self.partno}: {params_str}"
)

for name, value in zip([m.param_name for m in mapping], params):
getattr(module, name).override(value)
# Override module parameters with picked component parameters
# sort by type to avoid merge conflicts
module_params: list[tuple[Parameter, Parameter]] = [
(getattr(module, name), value)
for name, value in zip([m.param_name for m in mapping], params)
]
types_sort = [F.ANY, F.TBD, F.Constant, F.Range, F.Set, F.Operation]
for p, value in sorted(
module_params, key=lambda x: types_sort.index(type(x[0].get_most_narrow()))
):
p.override(value)

module.add(
F.has_descriptive_properties_defined(
Expand Down
19 changes: 17 additions & 2 deletions src/faebryk/libs/picker/jlcpcb/picker_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.core.parameter import Parameter
from faebryk.libs.e_series import E_SERIES_VALUES
from faebryk.libs.picker.jlcpcb.jlcpcb import (
Component,
Expand Down Expand Up @@ -216,6 +217,10 @@ def f(x: str) -> F.Constant[T]:
}


def try_get_param_mapping(module: Module) -> list[MappingParameterDB]:
return _MAPPINGS_BY_TYPE.get(type(module), [])


# Generic pickers ----------------------------------------------------------------------


Expand Down Expand Up @@ -261,7 +266,14 @@ def find_and_attach_by_lcsc_id(module: Module):
f"Part with LCSC part number {lcsc_pn} has insufficient stock", module
)

part.attach(module, [])
try:
part.attach(module, try_get_param_mapping(module), allow_TBD=True)
except Parameter.MergeException as e:
# TODO might be better to raise an error that makes the picker give up
# but this works for now, just not extremely efficient
raise PickError(
f"Could not attach part with LCSC part number {lcsc_pn}: {e}", module
) from e


def find_component_by_mfr(mfr: str, mfr_pn: str) -> Component:
Expand Down Expand Up @@ -323,11 +335,14 @@ def find_and_attach_by_mfr(module: Module):

for part in parts:
try:
part.attach(module, [])
part.attach(module, try_get_param_mapping(module), allow_TBD=True)
return
except ValueError as e:
logger.warning(f"Failed to attach component: {e}")
continue
except Parameter.MergeException:
# TODO not very efficient
pass

raise PickError(
f"Could not attach any part with manufacturer part number {mfr_pn}", module
Expand Down

0 comments on commit 80c5fde

Please sign in to comment.