Added support for filters that adds/removes components.
- Intended for BoM transformations - Also enhanced the support for filter chains
This commit is contained in:
parent
c88dceb6dc
commit
91dc9c5488
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright (c) 2020 Salvador E. Tropea
|
# Copyright (c) 2020-2021 Salvador E. Tropea
|
||||||
# Copyright (c) 2020 Instituto Nacional de Tecnología Industrial
|
# Copyright (c) 2020-2021 Instituto Nacional de Tecnología Industrial
|
||||||
# License: GPL-3.0
|
# License: GPL-3.0
|
||||||
# Project: KiBot (formerly KiPlot)
|
# Project: KiBot (formerly KiPlot)
|
||||||
from .registrable import RegFilter, Registrable, RegOutput
|
from .registrable import RegFilter, Registrable, RegOutput
|
||||||
|
|
@ -30,6 +30,7 @@ class DummyFilter(Registrable):
|
||||||
self.name = 'Dummy'
|
self.name = 'Dummy'
|
||||||
self.type = 'dummy'
|
self.type = 'dummy'
|
||||||
self.comment = 'A filter that does nothing'
|
self.comment = 'A filter that does nothing'
|
||||||
|
self._is_transform = False
|
||||||
|
|
||||||
def filter(self, comp):
|
def filter(self, comp):
|
||||||
return True
|
return True
|
||||||
|
|
@ -38,18 +39,48 @@ class DummyFilter(Registrable):
|
||||||
class MultiFilter(Registrable):
|
class MultiFilter(Registrable):
|
||||||
""" A filter containing a list of filters.
|
""" A filter containing a list of filters.
|
||||||
They are applied in sequence. """
|
They are applied in sequence. """
|
||||||
def __init__(self, filters):
|
def __init__(self, filters, is_transform):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.name = ','.join([f.name for f in filters])
|
self.name = ','.join([f.name for f in filters])
|
||||||
self.type = ','.join([f.type for f in filters])
|
self.type = ','.join([f.type for f in filters])
|
||||||
self.comment = 'Multi-filter'
|
self.comment = 'Multi-filter'
|
||||||
self.filters = filters
|
self.filters = filters
|
||||||
|
self._is_transform = is_transform
|
||||||
|
|
||||||
def filter(self, comp):
|
def filter(self, comp):
|
||||||
|
comps = [comp]
|
||||||
|
# We support logic and transform filters mixed
|
||||||
|
# Apply all the filters
|
||||||
for f in self.filters:
|
for f in self.filters:
|
||||||
if not f.filter(comp):
|
if f._is_transform:
|
||||||
return False
|
# A transform filter, doesn't affect the logic, but can affect the list of components
|
||||||
return True
|
new_comps = []
|
||||||
|
for c in comps:
|
||||||
|
ret = f.filter(c)
|
||||||
|
if ret is None:
|
||||||
|
# None means the component remains in the list
|
||||||
|
new_comps.append(c)
|
||||||
|
else:
|
||||||
|
# Replace the original by the list (could be empty)
|
||||||
|
new_comps.extend(ret)
|
||||||
|
comps = new_comps
|
||||||
|
else:
|
||||||
|
if self._is_transform:
|
||||||
|
# Interpret the logic filter as a transformation
|
||||||
|
comps = list(filter(lambda c: f.filter(c), comps))
|
||||||
|
else:
|
||||||
|
# Logic filter used for logic
|
||||||
|
for c in comps:
|
||||||
|
if not f.filter(c):
|
||||||
|
return False
|
||||||
|
if not self._is_transform:
|
||||||
|
# A logic filter that passed all tests
|
||||||
|
return True
|
||||||
|
# A transform filter
|
||||||
|
if len(comps) == 1 and comps[0] == comp:
|
||||||
|
# No changes to the list
|
||||||
|
return None
|
||||||
|
return comps
|
||||||
|
|
||||||
|
|
||||||
class NotFilter(Registrable):
|
class NotFilter(Registrable):
|
||||||
|
|
@ -60,6 +91,7 @@ class NotFilter(Registrable):
|
||||||
self.type = '!'+filter.type
|
self.type = '!'+filter.type
|
||||||
self.comment = filter.comment
|
self.comment = filter.comment
|
||||||
self._filter = filter
|
self._filter = filter
|
||||||
|
self._is_transform = False
|
||||||
|
|
||||||
def filter(self, comp):
|
def filter(self, comp):
|
||||||
return not self._filter.filter(comp)
|
return not self._filter.filter(comp)
|
||||||
|
|
@ -68,8 +100,15 @@ class NotFilter(Registrable):
|
||||||
def apply_pre_transform(comps, filter):
|
def apply_pre_transform(comps, filter):
|
||||||
if filter:
|
if filter:
|
||||||
logger.debug('Applying transform filter `{}`'.format(filter.name))
|
logger.debug('Applying transform filter `{}`'.format(filter.name))
|
||||||
|
new_comps = []
|
||||||
for c in comps:
|
for c in comps:
|
||||||
filter.filter(c)
|
ret = filter.filter(c)
|
||||||
|
if ret is None:
|
||||||
|
new_comps.append(c)
|
||||||
|
else:
|
||||||
|
new_comps.extend(ret)
|
||||||
|
return new_comps
|
||||||
|
return comps
|
||||||
|
|
||||||
|
|
||||||
def apply_exclude_filter(comps, filter):
|
def apply_exclude_filter(comps, filter):
|
||||||
|
|
@ -110,6 +149,12 @@ class BaseFilter(RegFilter):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._unkown_is_error = True
|
self._unkown_is_error = True
|
||||||
self._internal = False
|
self._internal = False
|
||||||
|
# Two type of filters:
|
||||||
|
# Transform: can change the component. Returns
|
||||||
|
# - None, the component remains
|
||||||
|
# - A list of components: the component is replaced by this list
|
||||||
|
# Logic: can't change the component. Return True/False indicating if the component pass the test.
|
||||||
|
self._is_transform = False
|
||||||
with document:
|
with document:
|
||||||
self.name = ''
|
self.name = ''
|
||||||
""" Used to identify this particular filter definition """
|
""" Used to identify this particular filter definition """
|
||||||
|
|
@ -189,13 +234,11 @@ class BaseFilter(RegFilter):
|
||||||
return filter
|
return filter
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def solve_filter(names, target_name, default=None):
|
def solve_filter(names, target_name, default=None, is_transform=False):
|
||||||
""" Name can be:
|
""" Name can be:
|
||||||
- A class, meaning we have to use a default.
|
- A class, meaning we have to use a default.
|
||||||
- A string, the name of a filter.
|
- A string, the name of a filter.
|
||||||
- A list of strings, the name of 1 or more filters.
|
- A list of strings, the name of 1 or more filters. """
|
||||||
If any of the names matches def_key we call creator asking to create the filter.
|
|
||||||
If def_real is not None we pass this name to creator. """
|
|
||||||
if isinstance(names, type):
|
if isinstance(names, type):
|
||||||
# Nothing specified, use the default
|
# Nothing specified, use the default
|
||||||
if default is None:
|
if default is None:
|
||||||
|
|
@ -226,18 +269,24 @@ class BaseFilter(RegFilter):
|
||||||
invert = False
|
invert = False
|
||||||
# Is already defined?
|
# Is already defined?
|
||||||
if RegOutput.is_filter(name):
|
if RegOutput.is_filter(name):
|
||||||
filter = RegOutput.get_filter(name)
|
fil = RegOutput.get_filter(name)
|
||||||
else: # Nope, can be created?
|
else: # Nope, can be created?
|
||||||
filter = BaseFilter._create_internal_filter(name)
|
fil = BaseFilter._create_internal_filter(name)
|
||||||
if filter is None:
|
if fil is None:
|
||||||
raise KiPlotConfigurationError("Unknown filter `{}` used for `{}`".format(name, target_name))
|
raise KiPlotConfigurationError("Unknown filter `{}` used for `{}`".format(name, target_name))
|
||||||
if invert:
|
if invert:
|
||||||
filters.append(NotFilter(filter))
|
if fil._is_transform:
|
||||||
|
raise KiPlotConfigurationError("Transform filter `{}` can't be inverted, used for `{}`"
|
||||||
|
.format(name, target_name))
|
||||||
|
filters.append(NotFilter(fil))
|
||||||
else:
|
else:
|
||||||
filters.append(filter)
|
filters.append(fil)
|
||||||
# Finished collecting filters
|
# Finished collecting filters
|
||||||
if not filters:
|
if not filters:
|
||||||
return DummyFilter()
|
return DummyFilter()
|
||||||
|
# If we need a `Logic` filter ensure that at least one in the list is `Logic`
|
||||||
|
if not is_transform and not next(filter(lambda x: not x._is_transform, filters), False):
|
||||||
|
raise KiPlotConfigurationError("At least one logic filter is needed for `{}`".format(target_name))
|
||||||
if len(filters) == 1:
|
if len(filters) == 1:
|
||||||
return filters[0]
|
return filters[0]
|
||||||
return MultiFilter(filters)
|
return MultiFilter(filters, is_transform)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright (c) 2020 Salvador E. Tropea
|
# Copyright (c) 2020-2021 Salvador E. Tropea
|
||||||
# Copyright (c) 2020 Instituto Nacional de Tecnología Industrial
|
# Copyright (c) 2020-2021 Instituto Nacional de Tecnología Industrial
|
||||||
# License: GPL-3.0
|
# License: GPL-3.0
|
||||||
# Project: KiBot (formerly KiPlot)
|
# Project: KiBot (formerly KiPlot)
|
||||||
"""
|
"""
|
||||||
|
|
@ -54,6 +54,7 @@ class Rot_Footprint(BaseFilter): # noqa: F821
|
||||||
Some manufacturers use a different rotation than KiCad. """
|
Some manufacturers use a different rotation than KiCad. """
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self._is_transform = True
|
||||||
with document:
|
with document:
|
||||||
self.extend = True
|
self.extend = True
|
||||||
""" Extends the internal list of rotations with the one provided.
|
""" Extends the internal list of rotations with the one provided.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright (c) 2020 Salvador E. Tropea
|
# Copyright (c) 2020-2021 Salvador E. Tropea
|
||||||
# Copyright (c) 2020 Instituto Nacional de Tecnología Industrial
|
# Copyright (c) 2020-2021 Instituto Nacional de Tecnología Industrial
|
||||||
# License: GPL-3.0
|
# License: GPL-3.0
|
||||||
# Project: KiBot (formerly KiPlot)
|
# Project: KiBot (formerly KiPlot)
|
||||||
"""
|
"""
|
||||||
|
|
@ -19,6 +19,7 @@ class Var_Rename(BaseFilter): # noqa: F821
|
||||||
This filter implements the VARIANT:FIELD=VALUE renamer to get FIELD=VALUE when VARIANT is in use """
|
This filter implements the VARIANT:FIELD=VALUE renamer to get FIELD=VALUE when VARIANT is in use """
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self._is_transform = True
|
||||||
with document:
|
with document:
|
||||||
self.separator = ':'
|
self.separator = ':'
|
||||||
""" Separator used between the variant and the field name """
|
""" Separator used between the variant and the field name """
|
||||||
|
|
|
||||||
|
|
@ -296,5 +296,5 @@ class VariantOptions(BaseOptions):
|
||||||
# Apply the variant
|
# Apply the variant
|
||||||
if self.variant:
|
if self.variant:
|
||||||
# Apply the variant
|
# Apply the variant
|
||||||
self.variant.filter(comps)
|
comps = self.variant.filter(comps)
|
||||||
self._comps = comps
|
self._comps = comps
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright (c) 2020 Salvador E. Tropea
|
# Copyright (c) 2020-2021 Salvador E. Tropea
|
||||||
# Copyright (c) 2020 Instituto Nacional de Tecnología Industrial
|
# Copyright (c) 2020-2021 Instituto Nacional de Tecnología Industrial
|
||||||
# License: GPL-3.0
|
# License: GPL-3.0
|
||||||
# Project: KiBot (formerly KiPlot)
|
# Project: KiBot (formerly KiPlot)
|
||||||
from .registrable import RegVariant
|
from .registrable import RegVariant
|
||||||
|
|
@ -38,11 +38,12 @@ class BaseVariant(RegVariant):
|
||||||
|
|
||||||
def config(self, parent):
|
def config(self, parent):
|
||||||
super().config(parent)
|
super().config(parent)
|
||||||
self.pre_transform = BaseFilter.solve_filter(self.pre_transform, 'pre_transform')
|
self.pre_transform = BaseFilter.solve_filter(self.pre_transform, 'pre_transform', is_transform=True)
|
||||||
|
|
||||||
def filter(self, comps):
|
def filter(self, comps):
|
||||||
# Apply all the filters
|
# Apply all the filters
|
||||||
apply_pre_transform(comps, self.pre_transform)
|
comps = apply_pre_transform(comps, self.pre_transform)
|
||||||
apply_exclude_filter(comps, self.exclude_filter)
|
apply_exclude_filter(comps, self.exclude_filter)
|
||||||
apply_fitted_filter(comps, self.dnf_filter)
|
apply_fitted_filter(comps, self.dnf_filter)
|
||||||
apply_fixed_filter(comps, self.dnc_filter)
|
apply_fixed_filter(comps, self.dnc_filter)
|
||||||
|
return comps
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ class IBoM(BaseVariant): # noqa: F821
|
||||||
|
|
||||||
def filter(self, comps):
|
def filter(self, comps):
|
||||||
GS.variant = self.variants_whitelist
|
GS.variant = self.variants_whitelist
|
||||||
super().filter(comps)
|
comps = super().filter(comps)
|
||||||
logger.debug("Applying IBoM style variants `{}`".format(self.name))
|
logger.debug("Applying IBoM style variants `{}`".format(self.name))
|
||||||
# Make black/white lists case insensitive
|
# Make black/white lists case insensitive
|
||||||
self.variants_whitelist = [v.lower() for v in self.variants_whitelist]
|
self.variants_whitelist = [v.lower() for v in self.variants_whitelist]
|
||||||
|
|
@ -82,3 +82,4 @@ class IBoM(BaseVariant): # noqa: F821
|
||||||
c.fitted = not self.skip_component(c)
|
c.fitted = not self.skip_component(c)
|
||||||
if not c.fitted and GS.debug_level > 2:
|
if not c.fitted and GS.debug_level > 2:
|
||||||
logger.debug('ref: {} value: {} -> False'.format(c.ref, c.value))
|
logger.debug('ref: {} value: {} -> False'.format(c.ref, c.value))
|
||||||
|
return comps
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ class KiBoM(BaseVariant): # noqa: F821
|
||||||
|
|
||||||
def filter(self, comps):
|
def filter(self, comps):
|
||||||
GS.variant = self.variant
|
GS.variant = self.variant
|
||||||
super().filter(comps)
|
comps = super().filter(comps)
|
||||||
logger.debug("Applying KiBoM style variants `{}`".format(self.name))
|
logger.debug("Applying KiBoM style variants `{}`".format(self.name))
|
||||||
for c in comps:
|
for c in comps:
|
||||||
if not (c.fitted and c.included):
|
if not (c.fitted and c.included):
|
||||||
|
|
@ -107,3 +107,4 @@ class KiBoM(BaseVariant): # noqa: F821
|
||||||
if not c.fitted and GS.debug_level > 2:
|
if not c.fitted and GS.debug_level > 2:
|
||||||
logger.debug('ref: {} value: {} config: {} variant: {} -> False'.
|
logger.debug('ref: {} value: {} config: {} variant: {} -> False'.
|
||||||
format(c.ref, value, config, self.variant))
|
format(c.ref, value, config, self.variant))
|
||||||
|
return comps
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue