Added the concept of `filters`

Closely related to variants, but more abstract.
Will allow much more configurability.
This commit is contained in:
Salvador E. Tropea 2020-08-29 17:38:32 -03:00
parent 006072e842
commit ad7ed9183a
6 changed files with 76 additions and 13 deletions

View File

@ -75,6 +75,8 @@ logger = None
has_macro = [
'layer',
'drill_marks',
'fil_base',
'fil_generic',
'out_any_drill',
'out_any_layer',
'out_base',

View File

@ -17,7 +17,7 @@ from .error import (KiPlotConfigurationError, config_error)
from .kiplot import (load_board)
from .misc import (NO_YAML_MODULE, EXIT_BAD_ARGS, EXAMPLE_CFG, WONT_OVERWRITE)
from .gs import GS
from .registrable import RegOutput, RegVariant
from .registrable import RegOutput, RegVariant, RegFilter
from .pre_base import BasePreFlight
# Logger
@ -89,24 +89,25 @@ class CfgYamlReader(object):
config_error("`outputs` must be a list")
return outputs
def _parse_variant(self, o_tree):
def _parse_variant(self, o_tree, kind, reg_class):
kind_f = kind[0].upper()+kind[1:]
try:
name = o_tree['name']
if not name:
raise KeyError
except KeyError:
config_error("Variant needs a name in: "+str(o_tree))
config_error(kind_f+" needs a name in: "+str(o_tree))
try:
otype = o_tree['type']
except KeyError:
config_error("Variant `"+name+"` needs a type")
config_error(kind_f+" `"+name+"` needs a type")
# Is a valid type?
if not RegVariant.is_registered(otype):
config_error("Unknown variant type: `{}`".format(otype))
if not reg_class.is_registered(otype):
config_error("Unknown {} type: `{}`".format(kind, otype))
# Load it
name_type = "`"+name+"` ("+otype+")"
logger.debug("Parsing variant "+name_type)
o_var = RegVariant.get_class_for(otype)()
logger.debug("Parsing "+kind+" "+name_type)
o_var = reg_class.get_class_for(otype)()
o_var.set_tree(o_tree)
try:
o_var.config()
@ -118,12 +119,22 @@ class CfgYamlReader(object):
variants = {}
if isinstance(v, list):
for o in v:
o_var = self._parse_variant(o)
o_var = self._parse_variant(o, 'variant', RegVariant)
variants[o_var.name] = o_var
else:
config_error("`variants` must be a list")
return variants
def _parse_filters(self, v):
filters = {}
if isinstance(v, list):
for o in v:
o_fil = self._parse_variant(o, 'filter', RegFilter)
filters[o_fil.name] = o_fil
else:
config_error("`filters` must be a list")
return filters
def _parse_preflight(self, pf):
logger.debug("Parsing preflight options: {}".format(pf))
if not isinstance(pf, dict):
@ -177,6 +188,8 @@ class CfgYamlReader(object):
self._parse_global(v)
elif k == 'variants':
RegOutput.set_variants(self._parse_variants(v))
elif k == 'filters':
RegOutput.set_filters(self._parse_filters(v))
elif k == 'outputs':
outputs = self._parse_outputs(v)
else:

View File

@ -56,7 +56,8 @@ def _import(name, path):
def _load_actions(path):
logger.debug("Importing from "+path)
lst = glob(os.path.join(path, 'out_*.py')) + glob(os.path.join(path, 'pre_*.py')) + glob(os.path.join(path, 'var_*.py'))
lst = glob(os.path.join(path, 'out_*.py')) + glob(os.path.join(path, 'pre_*.py'))
lst += glob(os.path.join(path, 'var_*.py')) + glob(os.path.join(path, 'fil_*.py'))
for p in lst:
name = os.path.splitext(os.path.basename(p))[0]
logger.debug("- Importing "+name)

View File

@ -133,6 +133,17 @@ def variant_class(tree, **kw):
return _do_wrap_class_register(tree, 'var_base', 'BaseVariant')
def filter_class(tree, **kw):
"""A decorator to wrap a class with:
from .fil_base import BaseFilter
... Class definition
BaseFilter.register(CLASS_NAME_LOWER_STRING, CLASS_NAME)
Allowing to register the class as a variant. """
return _do_wrap_class_register(tree, 'fil_base', 'BaseFilter')
def pre_class(tree, **kw):
"""A decorator to wrap a class with:

View File

@ -27,12 +27,17 @@ class Registrable(object):
def get_registered(cl):
return cl._registered
def __str__(self):
return "'{}' ({}) [{}]".format(self.comment, self.name, self.type)
class RegOutput(Optionable, Registrable):
""" An optionable that is also registrable.
Used by BaseOutput.
Here because it doesn't need macros. """
_registered = {}
# List of defined filters
_def_filters = {}
# List of defined variants
_def_variants = {}
@ -43,6 +48,30 @@ class RegOutput(Optionable, Registrable):
def set_variants(variants):
RegOutput._def_variants = variants
@staticmethod
def is_variant(name):
return name in RegOutput._def_variants
@staticmethod
def get_variant(name):
return RegOutput._def_variants[name]
@staticmethod
def set_filters(filters):
RegOutput._def_filters = filters
@staticmethod
def is_filter(name):
return name in RegOutput._def_filters
@staticmethod
def get_filter(name):
return RegOutput._def_filters[name]
@staticmethod
def add_filter(obj):
RegOutput._def_filters[obj.name] = obj
class RegVariant(Optionable, Registrable):
""" An optionable that is also registrable.
@ -52,3 +81,13 @@ class RegVariant(Optionable, Registrable):
def __init__(self):
super().__init__()
class RegFilter(Optionable, Registrable):
""" An optionable that is also registrable.
Used by BaseFilter.
Here because it doesn't need macros. """
_registered = {}
def __init__(self):
super().__init__()

View File

@ -20,6 +20,3 @@ class BaseVariant(RegVariant):
""" A comment for documentation purposes """
self.file_id = ''
""" Text to use as the """
def __str__(self):
return "'{}' ({}) [{}]".format(self.comment, self.name, self.type)