Skip to content

Commit

Permalink
[Added] Mechanism to mark options as case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
set-soft committed Aug 7, 2024
1 parent 89e3c41 commit bb99190
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
5 changes: 5 additions & 0 deletions docs/source/KiPlotYAML.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ And here are some examples for strings:
Note that quotes are optional and can be used to disambiguate.

.. _no_case
Also note that most strings are case sensitive, but things like schematic field names aren't.
In this case using *value* or *Value* is the same.

Compound data types
-------------------

Expand Down
1 change: 1 addition & 0 deletions kibot/config_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ def process_help_data_type(obj, help, v):
if def_val:
new_data_type += f' (default: ``{def_val}``)'
real_help = real_help.replace('{comma_sep}', '[:ref:`comma separated <comma_sep>`]')
real_help = real_help.replace('{no_case} ', '[:ref:`case insensitive <no_case>`]')
string_added = False
number_added = False
for tp, validation in zip(valid, validations):
Expand Down
2 changes: 1 addition & 1 deletion kibot/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class FieldPower(Optionable):


class FieldCurrent(Optionable):
_default = ['current']
_default = ['current', 'i']


class Globals(FiltersOptions):
Expand Down
5 changes: 5 additions & 0 deletions kibot/optionable.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def _check_str(key, val, doc, valid):
if not isinstance(val, str):
raise KiPlotConfigurationError("Option `{}` must be a string".format(key))
new_val = Optionable._promote_str_to_list(val, doc, valid)
if '{no_case}' in doc:
new_val = new_val.lower() if isinstance(new_val, str) else [v.lower() for v in new_val]
# If the docstring specifies the allowed values in the form [v1,v2...] enforce it
m = Optionable._str_values_re.search(doc)
if m:
Expand Down Expand Up @@ -259,6 +261,7 @@ def _perform_config_mapping(self):
elif isinstance(v, list):
new_val = []
filtered_valid = [t[5:-1] for t in valid if t.startswith('list(')]
no_case = '{no_case}' in cur_doc
for element in v:
e_type = typeof(element, Optionable)
if e_type not in filtered_valid:
Expand All @@ -270,6 +273,8 @@ def _perform_config_mapping(self):
nv.config(self)
new_val.append(nv)
else:
if no_case and isinstance(element, str):
element = element.lower()
new_val.append(element)
v = new_val
# Seems to be ok, map it
Expand Down
39 changes: 13 additions & 26 deletions kibot/out_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self, field=None):
self._unknown_is_error = True
with document:
self.field = ''
""" *Name of the field """
""" *{no_case} Name of the field """
self.text = ''
""" Text to use instead of a field. This option is incompatible with the `field` option.
Any space to separate it should be added in the text.
Expand Down Expand Up @@ -109,7 +109,6 @@ def config(self, parent):
if self.field and self.text:
raise KiPlotConfigurationError("You can't specify a `field` and a `text` in a join list ({})".
format(str(self._tree)))
self.field = self.field.lower()
if self.text_before is None:
self.text_before = ''
if self.text_after is None:
Expand Down Expand Up @@ -214,13 +213,13 @@ def __init__(self):
self.col_colors = True
""" Use colors to show the field type """
self.datasheet_as_link = ''
""" *Column with links to the datasheet """
""" *{no_case} Column with links to the datasheet """
self.digikey_link = Optionable
""" [string|list(string)=''] Column/s containing Digi-Key part numbers, will be linked to web page """
""" [string|list(string)=''] {no_case} Column/s containing Digi-Key part numbers, will be linked to web page """
self.mouser_link = Optionable
""" [string|list(string)=''] Column/s containing Mouser part numbers, will be linked to web page """
""" [string|list(string)=''] {no_case} Column/s containing Mouser part numbers, will be linked to web page """
self.lcsc_link = Optionable
""" [boolean|string|list(string)=''] Column/s containing LCSC part numbers, will be linked to web page.
""" [boolean|string|list(string)=''] {no_case} Column/s containing LCSC part numbers, will be linked to web page.
Use **true** to copy the value indicated by the `field_lcsc_part` global option """
self.generate_dnf = True
""" *Generate a separated section for DNF (Do Not Fit) components """
Expand All @@ -245,12 +244,8 @@ def __init__(self):

def config(self, parent):
super().config(parent)
# *_link
self.digikey_link = [v.lower() for v in self.digikey_link]
self.mouser_link = [v.lower() for v in self.mouser_link]
if isinstance(self.lcsc_link, bool):
self.lcsc_link = [self.solve_field_name('_field_lcsc_part')] if self.lcsc_link else []
self.lcsc_link = [v.lower() for v in self.lcsc_link]
# Logo
if isinstance(self.logo, bool):
self.logo = '' if self.logo else None
Expand All @@ -259,8 +254,6 @@ def config(self, parent):
self.logo = os.path.abspath(os.path.expandvars(os.path.expanduser(self.logo)))
if not os.path.isfile(self.logo):
raise KiPlotConfigurationError('Missing logo file `{}`'.format(self.logo))
# Datasheet as link
self.datasheet_as_link = self.datasheet_as_link.lower()


class BoMHTML(BoMLinkable):
Expand Down Expand Up @@ -484,7 +477,7 @@ def __init__(self):
self.ignore_dnf = True
""" *Exclude DNF (Do Not Fit) components """
self.fit_field = 'Config'
""" Field name used for internal filters (not for variants) """
""" {no_case} Field name used for internal filters (not for variants) """
self.use_alt = False
""" Print grouped references in the alternate compressed style eg: R1-R7,R18 """
self.columns = BoMColumns
Expand Down Expand Up @@ -533,7 +526,7 @@ def __init__(self):
self.merge_both_blank = True
""" When creating groups two components with empty/missing field will be interpreted as with the same value """
self.group_fields = GroupFields
""" *[list(string)] List of fields used for sorting individual components into groups.
""" *[list(string)] {no_case} List of fields used for sorting individual components into groups.
Components which match (comparing *all* fields) will be grouped together.
Field names are case-insensitive.
For empty fields the behavior is defined by the `group_fields_fallbacks`, `merge_blank_fields` and
Expand All @@ -544,7 +537,7 @@ def __init__(self):
If empty: ['Part', 'Part Lib', 'Value', 'Footprint', 'Footprint Lib',
. 'Voltage', 'Tolerance', 'Current', 'Power'] is used """
self.group_fields_fallbacks = Optionable
""" [list(string)=[]] List of fields to be used when the fields in `group_fields` are empty.
""" [list(string)=[]] {no_case} List of fields to be used when the fields in `group_fields` are empty.
The first field in this list is the fallback for the first in `group_fields`, and so on """
self.component_aliases = ComponentAliases
""" [list(list(string))] A series of values which are considered to be equivalent for the part name.
Expand All @@ -563,7 +556,7 @@ def __init__(self):
extra information split it in separated fields, add the fields to `group_fields` and disable
`merge_blank_fields` """
self.no_conflict = Optionable
""" [list(string)=?] List of fields where we tolerate conflicts.
""" [list(string)=?] {no_case} List of fields where we tolerate conflicts.
Use it to avoid undesired warnings.
By default the field indicated in `fit_field`, the field used for variants and
the field `part` are excluded """
Expand Down Expand Up @@ -669,6 +662,7 @@ def process_columns_config(self, cols, valid_columns, extra_columns):
valid_columns_l = {c.lower(): c for c in valid_columns + extra_columns}
logger.debug("Valid columns: {} ({})".format(valid_columns, len(valid_columns)))
# Create the different lists
logger.error(cols)
for col in cols:
if isinstance(col, str):
# Just a string, add to the list of used
Expand Down Expand Up @@ -714,10 +708,6 @@ def config(self, parent):
if self._format == 'xlsx' and self.xlsx.title:
self.xlsx.title = self.expand_filename_both(self.xlsx.title, make_safe=False)
self.xlsx.extra_info = [self.expand_filename_both(t, make_safe=False) for t in self.xlsx.extra_info]
# Make the grouping fields lowercase
self.group_fields = [f.lower() for f in self.group_fields]
# Make the grouping fields lowercase
self.group_fields_fallbacks = [f.lower() for f in self.group_fields_fallbacks]
# Fill with empty if needed
if len(self.group_fields_fallbacks) < len(self.group_fields):
self.group_fields_fallbacks.extend(['']*(len(self.group_fields)-len(self.group_fields_fallbacks)))
Expand All @@ -726,19 +716,16 @@ def config(self, parent):
self.exclude_filter = BaseFilter.solve_filter(self.exclude_filter, 'exclude_filter')
self.dnf_filter = BaseFilter.solve_filter(KiBoM.fix_dnx_filter(self.dnf_filter, self.fit_field), 'dnf_filter')
self.dnc_filter = BaseFilter.solve_filter(KiBoM.fix_dnx_filter(self.dnc_filter, self.fit_field), 'dnc_filter')
# Field names are handled in lowercase
self.fit_field = self.fit_field.lower()
# Fields excluded from conflict warnings
no_conflict = set()
if isinstance(self.no_conflict, type):
no_conflict = set()
no_conflict.add(self.fit_field)
no_conflict.add('part')
var_field = self.variant.get_variant_field() if self.variant else None
if var_field is not None:
no_conflict.add(var_field)
no_conflict.add(var_field.lower())
else:
for field in self.no_conflict:
no_conflict.add(field.lower())
no_conflict = set(self.no_conflict)
self._no_conflict = no_conflict
# Column values
if len(self.footprint_populate_values) != 2:
Expand Down
4 changes: 2 additions & 2 deletions tests/yaml_samples/int_bom_subparts_1.kibot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ outputs:
options:
<<: *bom_options
html:
digikey_link: 'digikey#'
digikey_link: ['Digikey#']
highlight_empty: false


Expand All @@ -59,5 +59,5 @@ outputs:
options:
<<: *bom_options
xlsx:
digikey_link: 'digikey#'
digikey_link: 'Digikey#'
highlight_empty: false

0 comments on commit bb99190

Please sign in to comment.