diff --git a/kibot/optionable.py b/kibot/optionable.py index c16ab938..ded90099 100644 --- a/kibot/optionable.py +++ b/kibot/optionable.py @@ -246,7 +246,8 @@ class Optionable(object): """ Returns the text to add for the current variant. Also try with the globally defined variant. If no variant is defined an empty string is returned. """ - if hasattr(self, 'variant') and self.variant and hasattr(self.variant, 'file_id'): + if hasattr(self, 'variant') and self.variant: + self.variant = GS.solve_variant(self.variant) return self.variant.file_id return Optionable._find_global_variant() @@ -260,7 +261,8 @@ class Optionable(object): """ Returns the name for the current variant. Also try with the globally defined variant. If no variant is defined an empty string is returned. """ - if hasattr(self, 'variant') and self.variant and hasattr(self.variant, 'name'): + if hasattr(self, 'variant') and self.variant: + self.variant = GS.solve_variant(self.variant) return self.variant.name return Optionable._find_global_variant_name() @@ -274,8 +276,10 @@ class Optionable(object): """ Returns the name of the sub-PCB. Also try with the globally defined variant. If no variant is defined an empty string is returned. """ - if hasattr(self, 'variant') and self.variant and self.variant._sub_pcb: - return self.variant._sub_pcb.name + if hasattr(self, 'variant') and self.variant: + self.variant = GS.solve_variant(self.variant) + if self.variant._sub_pcb: + return self.variant._sub_pcb.name return Optionable._find_global_variant() def expand_filename_common(self, name, parent): diff --git a/kibot/out_bom.py b/kibot/out_bom.py index c23df0eb..16eff5dc 100644 --- a/kibot/out_bom.py +++ b/kibot/out_bom.py @@ -596,11 +596,8 @@ class BoMOptions(BaseOptions): def _normalize_variant(self): """ Replaces the name of the variant by an object handling it. """ - if self.variant: - if not RegOutput.is_variant(self.variant): - raise KiPlotConfigurationError("Unknown variant name `{}`".format(self.variant)) - self.variant = RegOutput.get_variant(self.variant) - else: + self.variant = RegOutput.check_variant(self.variant) + if self.variant is None: # If no variant is specified use the KiBoM variant class with basic functionality self.variant = KiBoM() self.variant.config_field = self.fit_field diff --git a/kibot/registrable.py b/kibot/registrable.py index 83356a96..c4fd5886 100644 --- a/kibot/registrable.py +++ b/kibot/registrable.py @@ -5,8 +5,12 @@ # Project: KiBot (formerly KiPlot) from collections import OrderedDict from copy import copy +from .gs import GS from .optionable import Optionable from .error import KiPlotConfigurationError +from . import log + +logger = log.get_logger() class Registrable(object): @@ -125,6 +129,8 @@ class RegOutput(Optionable, Registrable): @staticmethod def check_variant(variant): if variant: + if isinstance(variant, RegVariant): + return variant if not RegOutput.is_variant(variant): raise KiPlotConfigurationError("Unknown variant name `{}`".format(variant)) return RegOutput.get_variant(variant) @@ -167,3 +173,12 @@ class RegDependency(Registrable): old_reg.roles.extend(aclass.roles) else: cl._registered[name] = aclass + + +def solve_variant(variant): + if isinstance(variant, str): + return RegOutput.check_variant(variant) + return variant + + +GS.solve_variant = solve_variant