Fixed show_components validation in out_pcbdraw.

The same error could be in other places.
I had to change the internal logic in Optionable.
The old code allowed invalid values to be used.
This commit is contained in:
Salvador E. Tropea 2020-09-04 16:55:40 -03:00
parent cf46a8ba86
commit dab7baa2d4
4 changed files with 33 additions and 5 deletions

View File

@ -22,8 +22,8 @@ def filter(v):
class Optionable(object):
""" A class to validate and hold configuration outputs/options.
Is configured from a dict and the collected values are stored in its attributes. """
_str_values_re = compile(r"\[string=.*\] \[([^\]]+)\]")
_num_range_re = compile(r"\[number=.*\] \[(-?\d+),(-?\d+)\]")
_str_values_re = compile(r"string=.*\] \[([^\]]+)\]")
_num_range_re = compile(r"number=.*\] \[(-?\d+),(-?\d+)\]")
def __init__(self):
self._unkown_is_error = False
@ -37,7 +37,7 @@ class Optionable(object):
if not isinstance(val, str):
raise KiPlotConfigurationError("Option `{}` must be a string".format(key))
# If the docstring specifies the allowed values in the form [v1,v2...] enforce it
m = Optionable._str_values_re.match(doc)
m = Optionable._str_values_re.search(doc)
if m:
vals = m.group(1).split(',')
if val not in vals:
@ -48,7 +48,7 @@ class Optionable(object):
if not isinstance(val, (int, float)):
raise KiPlotConfigurationError("Option `{}` must be a number".format(key))
# If the docstring specifies a range in the form [from-to] enforce it
m = Optionable._num_range_re.match(doc)
m = Optionable._num_range_re.search(doc)
if m:
min = float(m.group(1))
max = float(m.group(2))

View File

@ -124,7 +124,7 @@ class PcbDrawOptions(BaseOptions):
self.highlight = Optionable
""" [list(string)=[]] list of components to highlight """
self.show_components = Optionable
""" [string|list(string)=none] [none,all] list of components to draw, can be also a string for none or all.
""" [list(string)|string=none] [none,all] list of components to draw, can be also a string for none or all.
The default is none """
self.vcuts = False
""" render V-CUTS on the Cmts.User layer """

View File

@ -592,3 +592,10 @@ def test_error_wrong_fil_name():
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err("Filter names starting with (.*) are reserved")
ctx.clean_up()
def test_error_pcbdraw_comp_key():
ctx = context.TestContext('test_error_pcbdraw_comp_key', 'bom', 'error_pcbdraw_comp_key', '')
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err("Option .?show_components.? must be any of")
ctx.clean_up()

View File

@ -0,0 +1,21 @@
# Example KiBot config file
kibot:
version: 1
variants:
- name: 'default'
comment: 'Default variant'
type: ibom
file_id: '-C1'
variants_blacklist: T2,T3
outputs:
- name: 'pcbdraw_default'
comment: "PcbDraw w/variant"
type: pcbdraw
options:
variant: default
format: png
# This isn't a good idea, but works
show_components: 'C1'