Skip to content

Commit

Permalink
Fix to the variants logic.
Browse files Browse the repository at this point in the history
The problem was reported by @romavis
Personally I didn't like his two PRs, both had flaws and the changes in
the code are larger than this patch.
Fixes SchrodingersGat#136
  • Loading branch information
set-soft committed Sep 11, 2020
1 parent dd3b28e commit 6a6271f
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 5 deletions.
14 changes: 9 additions & 5 deletions kibom/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,19 +330,23 @@ def isFitted(self):

# Variants logic
opts = check.split(",")
# Only fit for ...
exclusive = False
for opt in opts:
opt = opt.strip()
# Any option containing a DNF is not fitted
if opt in DNF:
return False
# Options that start with '-' are explicitly removed from certain configurations
if opt.startswith("-") and str(opt[1:]) in self.prefs.pcbConfig:
if opt.startswith("-") and opt[1:] in self.prefs.pcbConfig:
return False
# Options that start with '+' are fitted only for certain configurations
if opt.startswith("+") and opt[1:] not in self.prefs.pcbConfig:
return False

return True
if opt.startswith("+"):
exclusive = True
if opt[1:] in self.prefs.pcbConfig:
return True
# No match
return not exclusive

def isFixed(self):
""" Determine if a component is FIXED or not.
Expand Down
67 changes: 67 additions & 0 deletions tests/input_samples/kibom-variant_2.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
EESchema Schematic File Version 4
EELAYER 30 0
EELAYER END
$Descr A4 11693 8268
encoding utf-8
Sheet 1 1
Title "KiBom Test Schematic"
Date "2020-03-12"
Rev "A"
Comp "https://github.com/SchrodingersGat/KiBom"
Comment1 ""
Comment2 ""
Comment3 ""
Comment4 ""
$EndDescr
Text Notes 500 600 0 79 ~ 0
This schematic serves as a test-file for the KiBot export script.\n
Text Notes 5950 2600 0 118 ~ 0
The test tests the following \nvariants matrix:\n production test default\nC1 X\nC2 X X\nR1 X X X\nR2 X X\n
$Comp
L Device:C C1
U 1 1 5F43BEC2
P 1000 1700
F 0 "C1" H 1115 1746 50 0000 L CNN
F 1 "1nF" H 1115 1655 50 0000 L CNN
F 2 "" H 1038 1550 50 0001 C CNN
F 3 "~" H 1000 1700 50 0001 C CNN
F 4 "-production,+test" H 1000 1700 50 0001 C CNN "Config"
1 1000 1700
1 0 0 -1
$EndComp
$Comp
L Device:C C2
U 1 1 5F43CE1C
P 1450 1700
F 0 "C2" H 1565 1746 50 0000 L CNN
F 1 "1000 pF" H 1565 1655 50 0000 L CNN
F 2 "" H 1488 1550 50 0001 C CNN
F 3 "~" H 1450 1700 50 0001 C CNN
F 4 "+production,+test" H 1450 1700 50 0001 C CNN "Config"
1 1450 1700
1 0 0 -1
$EndComp
$Comp
L Device:R R1
U 1 1 5F43D144
P 2100 1700
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
1 2100 1700
1 0 0 -1
$EndComp
$Comp
L Device:R R2
U 1 1 5F43D4BB
P 2500 1700
F 0 "R2" H 2570 1746 50 0000 L CNN
F 1 "1000" H 2570 1655 50 0000 L CNN
F 2 "" V 2430 1700 50 0001 C CNN
F 3 "~" H 2500 1700 50 0001 C CNN
F 4 "-test" H 2500 1700 50 0001 C CNN "Config"
1 2500 1700
1 0 0 -1
$EndComp
$EndSCHEMATC
123 changes: 123 additions & 0 deletions tests/input_samples/kibom-variant_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<export version="D">
<design>
<source>/home/salvador/0Data/Eccosur/kibom.INTI-CMNB/tests/input_samples/kibom-variant_2.sch</source>
<date>vie 11 sep 2020 20:10:07</date>
<tool>Eeschema 5.1.6+dfsg1-1</tool>
<sheet number="1" name="/" tstamps="/">
<title_block>
<title>KiBom Test Schematic</title>
<company>https://github.com/SchrodingersGat/KiBom</company>
<rev>A</rev>
<date>2020-03-12</date>
<source>kibom-variant_2.sch</source>
<comment number="1" value=""/>
<comment number="2" value=""/>
<comment number="3" value=""/>
<comment number="4" value=""/>
</title_block>
</sheet>
</design>
<components>
<comp ref="C1">
<value>1nF</value>
<datasheet>~</datasheet>
<fields>
<field name="Config">-production,+test</field>
</fields>
<libsource lib="Device" part="C" description="Unpolarized capacitor"/>
<sheetpath names="/" tstamps="/"/>
<tstamp>5F43BEC2</tstamp>
</comp>
<comp ref="C2">
<value>1000 pF</value>
<datasheet>~</datasheet>
<fields>
<field name="Config">+production,+test</field>
</fields>
<libsource lib="Device" part="C" description="Unpolarized capacitor"/>
<sheetpath names="/" tstamps="/"/>
<tstamp>5F43CE1C</tstamp>
</comp>
<comp ref="R1">
<value>1k</value>
<datasheet>~</datasheet>
<libsource lib="Device" part="R" description="Resistor"/>
<sheetpath names="/" tstamps="/"/>
<tstamp>5F43D144</tstamp>
</comp>
<comp ref="R2">
<value>1000</value>
<datasheet>~</datasheet>
<fields>
<field name="Config">-test</field>
</fields>
<libsource lib="Device" part="R" description="Resistor"/>
<sheetpath names="/" tstamps="/"/>
<tstamp>5F43D4BB</tstamp>
</comp>
</components>
<libparts>
<libpart lib="Device" part="C">
<description>Unpolarized capacitor</description>
<docs>~</docs>
<footprints>
<fp>C_*</fp>
</footprints>
<fields>
<field name="Reference">C</field>
<field name="Value">C</field>
</fields>
<pins>
<pin num="1" name="~" type="passive"/>
<pin num="2" name="~" type="passive"/>
</pins>
</libpart>
<libpart lib="Device" part="R">
<description>Resistor</description>
<docs>~</docs>
<footprints>
<fp>R_*</fp>
</footprints>
<fields>
<field name="Reference">R</field>
<field name="Value">R</field>
</fields>
<pins>
<pin num="1" name="~" type="passive"/>
<pin num="2" name="~" type="passive"/>
</pins>
</libpart>
</libparts>
<libraries>
<library logical="Device">
<uri>/usr/share/kicad/library/Device.lib</uri>
</library>
</libraries>
<nets>
<net code="1" name="Net-(C1-Pad1)">
<node ref="C1" pin="1"/>
</net>
<net code="2" name="Net-(C1-Pad2)">
<node ref="C1" pin="2"/>
</net>
<net code="3" name="Net-(C2-Pad1)">
<node ref="C2" pin="1"/>
</net>
<net code="4" name="Net-(C2-Pad2)">
<node ref="C2" pin="2"/>
</net>
<net code="5" name="Net-(R1-Pad1)">
<node ref="R1" pin="1"/>
</net>
<net code="6" name="Net-(R1-Pad2)">
<node ref="R1" pin="2"/>
</net>
<net code="7" name="Net-(R2-Pad1)">
<node ref="R2" pin="1"/>
</net>
<net code="8" name="Net-(R2-Pad2)">
<node ref="R2" pin="2"/>
</net>
</nets>
</export>
51 changes: 51 additions & 0 deletions tests/test_bom/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,54 @@ def test_column_sensitive():
logging.debug(heads)
assert len(heads) == 4
ctx.clean_up()


def test_variants_issue_SG136_default():
prj = 'kibom-variant_2'
ext = 'csv'
ctx = context.TestContext('test_variants_issue_SG136_default', prj, ext)
extra = ['-r', 'default']
ctx.run(no_config_file=True, extra=extra)
out = prj + '_bom_A_(default).' + ext
rows, components = ctx.load_csv(out)
assert len(rows) == 1
assert len(components) == 2
assert 'R1' in components
assert 'R2' in components
assert 'C1' not in components
assert 'C2' not in components
ctx.clean_up()


def test_variants_issue_SG136_production():
prj = 'kibom-variant_2'
ext = 'csv'
ctx = context.TestContext('test_variants_issue_SG136_default', prj, ext)
extra = ['-r', 'production']
ctx.run(no_config_file=True, extra=extra)
out = prj + '_bom_A_(production).' + ext
rows, components = ctx.load_csv(out)
assert len(rows) == 2
assert len(components) == 3
assert 'R1' in components
assert 'R2' in components
assert 'C1' not in components
assert 'C2' in components
ctx.clean_up()


def test_variants_issue_SG136_test():
prj = 'kibom-variant_2'
ext = 'csv'
ctx = context.TestContext('test_variants_issue_SG136_test', prj, ext)
extra = ['-r', 'test']
ctx.run(no_config_file=True, extra=extra)
out = prj + '_bom_A_(test).' + ext
rows, components = ctx.load_csv(out)
assert len(rows) == 2
assert len(components) == 3
assert 'R1' in components
assert 'R2' not in components
assert 'C1' in components
assert 'C2' in components
ctx.clean_up()

0 comments on commit 6a6271f

Please sign in to comment.