KiBot/kibot/out_base.py

118 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (c) 2020 Salvador E. Tropea
# Copyright (c) 2020 Instituto Nacional de Tecnología Industrial
# License: GPL-3.0
# Project: KiBot (formerly KiPlot)
from .gs import GS
from .kiplot import load_sch
from .registrable import RegOutput
from .optionable import Optionable, BaseOptions
from .fil_base import BaseFilter, apply_fitted_filter
from .macros import macros, document # noqa: F401
from . import log
logger = log.get_logger(__name__)
class BaseOutput(RegOutput):
def __init__(self):
super().__init__()
with document:
self.name = ''
""" Used to identify this particular output definition """
self.type = ''
""" Type of output """
self.dir = '.'
""" Output directory for the generated files """
self.comment = ''
""" A comment for documentation purposes """ # pragma: no cover
self._sch_related = False
self._unkown_is_error = True
@staticmethod
def attr2longopt(attr):
return '--'+attr.replace('_', '-')
def is_sch(self):
""" True for outputs that works on the schematic """
return self._sch_related
def is_pcb(self):
""" True for outputs that works on the PCB """
return not self._sch_related
def config(self):
super().config()
if getattr(self, 'options', None) and isinstance(self.options, type):
# No options, get the defaults
self.options = self.options()
# Configure them using an empty tree
self.options.config()
def run(self, output_dir, board):
self.options.run(output_dir, board)
class BoMRegex(Optionable):
""" Implements the pair column/regex """
def __init__(self):
super().__init__()
self._unkown_is_error = True
with document:
self.column = ''
""" Name of the column to apply the regular expression """
self.regex = ''
""" Regular expression to match """
self.field = None
""" {column} """
self.regexp = None
""" {regex} """
class VariantOptions(BaseOptions):
""" BaseOptions plus generic support for variants. """
def __init__(self):
super().__init__()
with document:
self.variant = ''
""" Board variant to apply """
self.dnf_filter = Optionable
""" [string|list(string)=''] Name of the filter to mark components as not fitted.
A short-cut to use for simple cases where a variant is an overkill """
self._comps = None
def config(self):
super().config()
self.variant = RegOutput.check_variant(self.variant)
self.dnf_filter = BaseFilter.solve_filter(self.dnf_filter, 'dnf_filter')
def get_refs_hash(self):
if not self._comps:
return None
return {c.ref: c for c in self._comps}
def get_fitted_refs(self):
if not self._comps:
return []
return [c.ref for c in self._comps if c.fitted]
def get_not_fitted_refs(self):
if not self._comps:
return []
return [c.ref for c in self._comps if not c.fitted]
def run(self, output_dir, board):
""" Makes the list of components available """
if not self.dnf_filter and not self.variant:
return
load_sch()
# Get the components list from the schematic
comps = GS.sch.get_components()
# Apply the filter
apply_fitted_filter(comps, self.dnf_filter)
# Apply the variant
if self.variant:
# Apply the variant
self.variant.filter(comps)
self._comps = comps