Skip to content

Commit

Permalink
KiBoM variants: support to field overwrite according to variant
Browse files Browse the repository at this point in the history
  • Loading branch information
set-soft committed Dec 18, 2020
1 parent bb3087c commit 986f0c7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- File name patterns: %F is the name of the source file without extension, but
with the path.
- A hint for pip installations without using `--no-compile`.
- KiBoM variants: support to field overwrite according to variant (experimental).
### Fixed
- Now we support missing field names in schematic library entries.

Expand Down
9 changes: 9 additions & 0 deletions kibot/kicad/v5_sch.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,15 @@ def get_field_value(self, field):
return self.dfields[field].value
return ''

def set_field(self, field, value):
""" Change the value for an existing field """
if field in self.dfields:
target = self.dfields[field]
target.value = value
# Adjust special fields
if target.number < 4:
self._solve_fields(LineReader(None, '**Internal**'))

# def get_field_names(self):
# return [f.name for f in self.fields]

Expand Down
22 changes: 21 additions & 1 deletion kibot/var_kibom.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def __init__(self):
""" Name of the field used to clasify components """
self.variant = Optionable
""" [string|list(string)=''] Board variant(s) """
self.field_changer = False
""" Enable the VARIANT.FIELD to FIELD rename mechanism """
self.field_variant_separator = '.'
""" Separator used for the VARIANT.FIELD rename mechanism """

def set_def_filters(self, exclude_filter, dnf_filter, dnc_filter):
""" Filters delegated to the variant """
Expand Down Expand Up @@ -93,6 +97,20 @@ def _variant_comp_is_fitted(self, value, config):
# No match
return not exclusive

def _rename_var_fields(self, comp):
""" Look for fields containing VARIANT:FIELD used to change fields according to the variant """
for variant in self.variant:
for name, value in comp.get_user_fields():
res = name.strip().split(self.field_variant_separator)
if len(res) == 2:
f_variant = res[0].lower()
f_field = res[1].lower()
if f_variant == variant:
if GS.debug_level > 2:
logger.debug('ref: {} value: {} -> {}'.format(comp.ref, comp.get_field_value(f_field), value))
comp.set_field(f_field, value)


def filter(self, comps):
super().filter(comps)
logger.debug("Applying KiBoM style variants `{}`".format(self.name))
Expand All @@ -103,6 +121,8 @@ def filter(self, comps):
value = c.value.lower()
config = c.get_field_value(self.config_field).lower()
c.fitted = self._variant_comp_is_fitted(value, config)
if not c.fitted and GS.debug_level > 2:
if c.fitted and self.field_changer:
self._rename_var_fields(c)
elif not c.fitted and GS.debug_level > 2:
logger.debug('ref: {} value: {} config: {} variant: {} -> False'.
format(c.ref, value, config, self.variant))
1 change: 1 addition & 0 deletions tests/board_samples/kicad_5/kibom-variant_2.sch
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ F 0 "R1" H 2170 1746 50 0000 L CNN
F 1 "1k" H 2170 1655 50 0000 L CNN
F 2 "" V 2030 1700 50 0001 C CNN
F 3 "~" H 2100 1700 50 0001 C CNN
F 4 "3k3" H 2100 1700 50 0001 C CNN "test:Value"
1 2100 1700
1 0 0 -1
$EndComp
Expand Down
19 changes: 17 additions & 2 deletions tests/test_plot/test_int_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
QTY_COLUMN_NAME = 'Quantity Per PCB'
COMP_COLUMN_NAME = 'Row'
COMP_COLUMN_NAME_R = 'Renglón'
VALUE_COLUMN_NAME = 'Value'
DATASHEET_COLUMN_NAME = 'Datasheet'
KIBOM_TEST_HEAD = [COMP_COLUMN_NAME, 'Description', 'Part', REF_COLUMN_NAME, 'Value', 'Footprint', QTY_COLUMN_NAME,
DATASHEET_COLUMN_NAME, 'Config']
Expand Down Expand Up @@ -1197,17 +1198,31 @@ def test_int_bom_variant_t1():
ctx.clean_up()


def test_int_bom_variant_t2():
def check_value(rows, r_col, ref, v_col, val):
for r in rows:
refs = r[r_col].split(' ')
if ref in refs:
assert r[v_col] == val
logging.debug(ref+'='+val+' OK')
return
assert False, "Failed to find "+ref


def test_int_bom_variant_t2b():
prj = 'kibom-variant_2'
ctx = context.TestContextSCH('test_int_bom_variant_t2', prj, 'int_bom_var_t2_csv', BOM_DIR)
ctx = context.TestContextSCH('test_int_bom_variant_t2b', prj, 'int_bom_var_t2_csv', BOM_DIR)
ctx.run()
rows, header, info = ctx.load_csv(prj+'-bom.csv')
ref_column = header.index(REF_COLUMN_NAME)
val_column = header.index(VALUE_COLUMN_NAME)
check_kibom_test_netlist(rows, ref_column, 1, ['C1', 'C2'], ['R1', 'R2'])
check_value(rows, ref_column, 'R1', val_column, '1k')
rows, header, info = ctx.load_csv(prj+'-bom_(production).csv')
check_kibom_test_netlist(rows, ref_column, 2, ['C1'], ['R1', 'R2', 'C2'])
check_value(rows, ref_column, 'R1', val_column, '1k')
rows, header, info = ctx.load_csv(prj+'-bom_(test).csv')
check_kibom_test_netlist(rows, ref_column, 2, ['R2'], ['R1', 'C1', 'C2'])
check_value(rows, ref_column, 'R1', val_column, '3k3')
ctx.clean_up()


Expand Down
2 changes: 2 additions & 0 deletions tests/yaml_samples/int_bom_var_t2_csv.kibot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ variants:
type: kibom
file_id: '_(test)'
variant: test
field_changer: true
field_variant_separator: ':'

outputs:
- name: 'bom_internal'
Expand Down

0 comments on commit 986f0c7

Please sign in to comment.