From afe80052b4d977ab761f2da86d71152aee625371 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Thu, 25 Mar 2021 12:57:26 -0300 Subject: [PATCH] Added a new filter `var_rename_kicost` Is very similar to `var_rename` with a few differences that makes it suitable to emulate the KiCost field rename mechanism. --- CHANGELOG.md | 6 +- README.md | 11 ++ kibot/fil_var_rename_kicost.py | 82 +++++++++ tests/board_samples/kicad_5/f_rename_2.sch | 172 ++++++++++++++++++ tests/test_plot/test_int_bom.py | 12 ++ .../int_bom_var_rename_2_csv.kibot.yaml | 56 ++++++ 6 files changed, 337 insertions(+), 2 deletions(-) create mode 100644 kibot/fil_var_rename_kicost.py create mode 100644 tests/board_samples/kicad_5/f_rename_2.sch create mode 100644 tests/yaml_samples/int_bom_var_rename_2_csv.kibot.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 9107f995..ac1e5e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `erc_warnings` pre-flight option to consider ERC warnings as errors. - Pattern expansion in the `dir` option for outputs (#58) -- New filter type `suparts`. Adds support for KiCost's subparts feature. -- New filter type `field_rename`. Used to rename schematic fields. +- New filter types: + - `suparts`: Adds support for KiCost's subparts feature. + - `field_rename`: Used to rename schematic fields. + - `var_rename_kicost`: Like `var_rename` but using KiCost mechanism. ### Changed - Errors and warnings from KiAuto now are printed as errors and warnings. diff --git a/README.md b/README.md index 21d05736..5fc5d1ce 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,17 @@ Currently the only type available is `generic`. - `name`: [string=''] Used to identify this particular filter definition. - `separator`: [string=':'] Separator used between the variant and the field name. - `variant_to_value`: [boolean=false] Rename fields matching the variant to the value of the component. +- var_rename_kicost: Var_Rename_KiCost + This filter implements the kicost.VARIANT:FIELD=VALUE renamer to get FIELD=VALUE when VARIANT is in use. + It applies the KiCost concept of variants (a regex to match the VARIANT). + * Valid keys: + - `comment`: [string=''] A comment for documentation purposes. + - `name`: [string=''] Used to identify this particular filter definition. + - `prefix`: [string='kicost.'] A mandatory prefix. Is not case sensitive. + - `separator`: [string=':'] Separator used between the variant and the field name. + - `variant`: [string=''] Variant regex to match the VARIANT part. + When empty the currently selected variant is used. + - `variant_to_value`: [boolean=false] Rename fields matching the variant to the value of the component. diff --git a/kibot/fil_var_rename_kicost.py b/kibot/fil_var_rename_kicost.py new file mode 100644 index 00000000..03cc3f5b --- /dev/null +++ b/kibot/fil_var_rename_kicost.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020-2021 Salvador E. Tropea +# Copyright (c) 2020-2021 Instituto Nacional de Tecnología Industrial +# License: GPL-3.0 +# Project: KiBot (formerly KiPlot) +""" +Implements the kicost.VARIANT:FIELD=VALUE renamer to get FIELD=VALUE when VARIANT is in use. +It applies the KiCost concept of variants (a regex to match the VARIANT) +Can be configured, by default is what KiCost does. +""" +import re +from .gs import GS +from .macros import macros, document, filter_class # noqa: F401 +from . import log + +logger = log.get_logger(__name__) + + +@filter_class +class Var_Rename_KiCost(BaseFilter): # noqa: F821 + """ Var_Rename_KiCost + This filter implements the kicost.VARIANT:FIELD=VALUE renamer to get FIELD=VALUE when VARIANT is in use. + It applies the KiCost concept of variants (a regex to match the VARIANT) """ + def __init__(self): + super().__init__() + self._is_transform = True + with document: + self.prefix = 'kicost.' + """ A mandatory prefix. Is not case sensitive """ + self.separator = ':' + """ Separator used between the variant and the field name """ + self.variant = '' + """ Variant regex to match the VARIANT part. + When empty the currently selected variant is used """ + self.variant_to_value = False + """ Rename fields matching the variant to the value of the component """ + + def config(self, parent): + super().config(parent) + if not self.separator: + self.separator = ':' + self.prefix = self.prefix.lower() + self._l_prefix = len(self.prefix) + + def filter(self, comp): + """ Look for fields containing PREFIX VARIANT:FIELD used to change fields according to the variant """ + variant = self.variant + if not variant: + if not GS.variant: + # No variant in use, nothing to do + return + if len(GS.variant) == 1: + variant = GS.variant[0] + else: + variant = '('+'|'.join(GS.variant)+')' + var = re.compile(variant, re.IGNORECASE) + for name, value in comp.get_user_fields(): + name = name.strip().lower() + # Remove the prefix + if self._l_prefix: + if name.startswith(self.prefix): + name = name[self._l_prefix:] + else: + # Doesn't match the prefix + continue + # Apply the separator + res = name.split(self.separator) + if len(res) == 2: + # Successfully separated + f_variant = res[0].lower() + f_field = res[1].lower() + if var.match(f_variant): + # Variant matched + if GS.debug_level > 2: + logger.debug('ref: {} {}: {} -> {}'. + format(comp.ref, f_field, comp.get_field_value(f_field), value)) + comp.set_field(f_field, value) + elif self.variant_to_value and var.match(name): + # The field matches the variant and the user wants to change the value + if GS.debug_level > 2: + logger.debug('ref: {} value: {} -> {}'.format(comp.ref, comp.value, value)) + comp.set_field('value', value) diff --git a/tests/board_samples/kicad_5/f_rename_2.sch b/tests/board_samples/kicad_5/f_rename_2.sch new file mode 100644 index 00000000..37e3c998 --- /dev/null +++ b/tests/board_samples/kicad_5/f_rename_2.sch @@ -0,0 +1,172 @@ +EESchema Schematic File Version 4 +EELAYER 30 0 +EELAYER END +$Descr A4 11693 8268 +encoding utf-8 +Sheet 1 1 +Title "" +Date "" +Rev "" +Comp "" +Comment1 "" +Comment2 "" +Comment3 "" +Comment4 "" +$EndDescr +$Comp +L Device:R_US R1 +U 1 1 5FDD0409 +P 4850 3100 +F 0 "R1" V 4950 3125 50 0000 C CNN +F 1 "RES-000007-00" H 4850 2650 50 0001 C CNN +F 2 "Resistors:R0402" H 4850 2350 50 0001 C CNN +F 3 "" H 4850 2750 50 0001 C CNN +F 4 "Stackpole Electronics Inc" H 4850 2550 50 0001 C CNN "Manufacturer" +F 5 "RMCF0402FT4K70" H 4850 2450 50 0001 C CNN "Manufacturer Part Number" +F 6 "4.7K" V 4700 3125 50 0000 C CNN "Resistance (Ohms)" +F 7 "±1%" H 5000 3000 50 0001 L CNN "Tolerance (%)" +F 8 "0402" V 4775 3125 50 0000 C CNN "Package Type" +F 9 "1/16W" H 4850 2800 50 0001 C CNN "Power (Watts)" +F 10 "RES-000045-00" H 4850 3100 50 0001 C CNN "kicost.DEV:Value" +F 11 "Yageo" H 4850 3100 50 0001 C CNN "kicost.DEV:Manufacturer" +F 12 "RC0402FR-071KL" H 4850 3100 50 0001 C CNN "kicost.DEV:Manufacturer Part Number" +F 13 "RES SMD 1K OHM 1% 1/16W 0402" H 4850 3100 50 0001 C CNN "kicost.DEV:Description" + 1 4850 3100 + 0 1 1 0 +$EndComp +$Comp +L Device:LED_ALT D1 +U 1 1 5FDD1589 +P 5400 3100 +F 0 "D1" H 5400 3410 50 0000 C CNN +F 1 "DIO-000062-00" H 5400 2600 50 0001 C CNN +F 2 "Diodes:LED0603-YELLOW" H 5400 2300 50 0001 C CNN +F 3 "" H 5400 2700 50 0001 C CNN +F 4 "Würth Elektronik" H 5400 2500 50 0001 C CNN "Manufacturer" +F 5 "150060YS75000" H 5400 2400 50 0001 C CNN "Manufacturer Part Number" +F 6 "150060YS75000" H 5400 2700 50 0001 C CNN "Part Number" +F 7 "2V" H 5400 3319 50 0000 C CNN "Forward Voltage (Volt)" +F 8 "Yellow" H 5400 3228 50 0000 C CNN "LED Color" + 1 5400 3100 + -1 0 0 1 +$EndComp +$Comp +L power:+3.3V #PWR0101 +U 1 1 5FDD2CE3 +P 4500 3000 +F 0 "#PWR0101" H 4500 2850 50 0001 C CNN +F 1 "+3.3V" H 4500 3173 50 0000 C CNN +F 2 "" H 4500 3000 60 0000 C CNN +F 3 "" H 4500 3000 60 0000 C CNN + 1 4500 3000 + 1 0 0 -1 +$EndComp +$Comp +L power:GND #PWR0102 +U 1 1 5FDD32E7 +P 5750 3200 +F 0 "#PWR0102" H 5750 3030 50 0001 C CNN +F 1 "GND" H 5750 3050 50 0000 C CNN +F 2 "" H 5750 3300 60 0000 C CNN +F 3 "" H 5730 3110 60 0000 C CNN + 1 5750 3200 + 1 0 0 -1 +$EndComp +Wire Wire Line + 4500 3000 4500 3100 +Wire Wire Line + 4500 3100 4700 3100 +Wire Wire Line + 5000 3100 5250 3100 +Wire Wire Line + 5550 3100 5750 3100 +Wire Wire Line + 5750 3100 5750 3200 +$Comp +L Device:R_US R2 +U 1 1 5FDD4C1B +P 6500 3100 +F 0 "R2" V 6600 3100 50 0000 C CNN +F 1 "RES-000007-00" H 6500 2650 50 0001 C CNN +F 2 "Resistors:R0402" H 6500 2350 50 0001 C CNN +F 3 "" H 6500 2750 50 0001 C CNN +F 4 "Stackpole Electronics Inc" H 6500 2550 50 0001 C CNN "Manufacturer" +F 5 "RMCF0402FT4K70" H 6500 2450 50 0001 C CNN "Manufacturer Part Number" +F 6 "4.7K" V 6325 3100 50 0000 C CNN "Resistance (Ohms)" +F 7 "±1%" H 6650 3000 50 0001 L CNN "Tolerance (%)" +F 8 "0402" V 6400 3100 50 0000 C CNN "Package Type" +F 9 "1/16W" H 6500 2800 50 0001 C CNN "Power (Watts)" +F 10 "dnp" H 6500 3100 50 0001 C CNN "kicost.PROD" + 1 6500 3100 + 0 1 1 0 +$EndComp +$Comp +L Device:LED_ALT D2 +U 1 1 5FDD4C26 +P 7050 3100 +F 0 "D2" H 7050 3410 50 0000 C CNN +F 1 "DIO-000062-00" H 7050 2600 50 0001 C CNN +F 2 "Diodes:LED0603-YELLOW" H 7050 2300 50 0001 C CNN +F 3 "" H 7050 2700 50 0001 C CNN +F 4 "Würth Elektronik" H 7050 2500 50 0001 C CNN "Manufacturer" +F 5 "150060YS75000" H 7050 2400 50 0001 C CNN "Manufacturer Part Number" +F 6 "150060YS75000" H 7050 2700 50 0001 C CNN "Part Number" +F 7 "2V" H 7050 3319 50 0000 C CNN "Forward Voltage (Volt)" +F 8 "Yellow" H 7050 3228 50 0000 C CNN "LED Color" +F 9 "dnp" H 7050 3100 50 0001 C CNN "kicost.PROD" + 1 7050 3100 + -1 0 0 1 +$EndComp +$Comp +L power:+3.3V #PWR0103 +U 1 1 5FDD4C2C +P 6150 3000 +F 0 "#PWR0103" H 6150 2850 50 0001 C CNN +F 1 "+3.3V" H 6150 3173 50 0000 C CNN +F 2 "" H 6150 3000 60 0000 C CNN +F 3 "" H 6150 3000 60 0000 C CNN + 1 6150 3000 + 1 0 0 -1 +$EndComp +$Comp +L power:GND #PWR0104 +U 1 1 5FDD4C32 +P 7400 3200 +F 0 "#PWR0104" H 7400 3030 50 0001 C CNN +F 1 "GND" H 7400 3050 50 0000 C CNN +F 2 "" H 7400 3300 60 0000 C CNN +F 3 "" H 7380 3110 60 0000 C CNN + 1 7400 3200 + 1 0 0 -1 +$EndComp +Wire Wire Line + 6150 3000 6150 3100 +Wire Wire Line + 6150 3100 6350 3100 +Wire Wire Line + 6650 3100 6900 3100 +Wire Wire Line + 7200 3100 7400 3100 +Wire Wire Line + 7400 3100 7400 3200 +Wire Notes Line + 4350 2700 5900 2700 +Wire Notes Line + 5900 2700 5900 3500 +Wire Notes Line + 5900 3500 4350 3500 +Wire Notes Line + 4350 3500 4350 2700 +Wire Notes Line + 6000 2700 7550 2700 +Wire Notes Line + 7550 2700 7550 3500 +Wire Notes Line + 7550 3500 6000 3500 +Wire Notes Line + 6000 3500 6000 2700 +Text Notes 4350 2700 0 50 ~ 0 +[Prod] R1=4.7K / [Dev] R1=1K +Text Notes 6000 2700 0 50 ~ 0 +[Prod] R2, D2=dnp +$EndSCHEMATC diff --git a/tests/test_plot/test_int_bom.py b/tests/test_plot/test_int_bom.py index 99ad6fc9..7f487de7 100644 --- a/tests/test_plot/test_int_bom.py +++ b/tests/test_plot/test_int_bom.py @@ -1262,6 +1262,18 @@ def test_int_bom_variant_rename_1(test_dir): ctx.clean_up() +def test_int_bom_variant_rename_2(test_dir): + prj = 'f_rename_2' + ctx = context.TestContextSCH(test_dir, 'test_int_bom_variant_rename_2', prj, 'int_bom_var_rename_2_csv', BOM_DIR) + ctx.run(extra_debug=True) + rows, header, info = ctx.load_csv(prj+'-bom_(PROD).csv') + ref_column = header.index(REF_COLUMN_NAME) + check_kibom_test_netlist(rows, ref_column, 2, ['R2', 'D2'], ['R1', 'D1']) + rows, header, info = ctx.load_csv(prj+'-bom_(DEV).csv') + check_kibom_test_netlist(rows, ref_column, 3, None, ['R1', 'R2', 'D1', 'D2']) + ctx.clean_up() + + def test_int_bom_variant_t2s(test_dir): prj = 'kibom-variant_2' ctx = context.TestContextSCH(test_dir, 'test_int_bom_variant_t2s', prj, 'int_bom_var_t2s_csv', BOM_DIR) diff --git a/tests/yaml_samples/int_bom_var_rename_2_csv.kibot.yaml b/tests/yaml_samples/int_bom_var_rename_2_csv.kibot.yaml new file mode 100644 index 00000000..f3016ab7 --- /dev/null +++ b/tests/yaml_samples/int_bom_var_rename_2_csv.kibot.yaml @@ -0,0 +1,56 @@ +# Example KiBot config file +kibot: + version: 1 + +filters: + - name: 'Variant rename' + comment: 'Variant rename KiCost style' + type: var_rename_kicost + # Empty will default to : + separator: '' + variant_to_value: true + +variants: + - name: 'production' + comment: 'Production variant' + type: kibom + file_id: '_(PROD)' + variant: PROD + pre_transform: 'Variant rename' + + - name: 'development' + comment: 'Development variant' + type: kibom + file_id: '_(DEV)' + variant: DEV + pre_transform: 'Variant rename' + +outputs: + - name: 'bom_internal_production' + comment: "Bill of Materials in CSV format for production" + type: bom + dir: BoM + options: + variant: production + columns: + - Row + - References + - Value + - Description + - Manufacturer + - Manufacturer Part Number + + - name: 'bom_internal_test' + comment: "Bill of Materials in CSV format for development" + type: bom + dir: BoM + options: + variant: development + columns: + - Row + - References + - Value + - Description + - Manufacturer + - Manufacturer Part Number +