From 3fcfb7b3049d4223669cd6bf2f2a10167a6429f2 Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Thu, 1 Aug 2024 09:32:32 +0100 Subject: [PATCH] feat: handle special "Component" class This can have any number of attributes that are not mentioned in the schema, so we need to handle it differently. https://www.w3.org/TR/xmlschema11-1/#Wildcards --- neuroml/nml/generatedssupersuper.py | 39 +++++++++++------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/neuroml/nml/generatedssupersuper.py b/neuroml/nml/generatedssupersuper.py index 74375de..5291f31 100644 --- a/neuroml/nml/generatedssupersuper.py +++ b/neuroml/nml/generatedssupersuper.py @@ -9,9 +9,6 @@ import logging import sys -import lems.api -from lems.api import Component - import neuroml.build_time_validation from .generatedscollector import GdsCollector @@ -61,10 +58,6 @@ def add(self, obj=None, hint=None, force=False, validate=True, **kwargs): if type(obj) is type or isinstance(obj, str): obj = self.component_factory(obj, validate=validate, **kwargs) - if type(obj).__name__ == "Component": - self.anytypeobjs_.append(obj) - return obj - # getattr only returns the value of the provided member but one cannot # then use this to modify the member. Using `vars` also allows us to # modify the value @@ -146,30 +139,28 @@ def component_factory(cls, component_type, validate=True, **kwargs): """ module_object = sys.modules[cls.__module__] - lems_module_object = sys.modules["lems.api"] - - new_component = False if isinstance(component_type, str): - if component_type in ["__ANY__", "Component"]: - comp_type_class = getattr(lems_module_object, "Component") - new_component = True - else: - comp_type_class = getattr(module_object, component_type) + comp_type_class = getattr(module_object, component_type) else: - if component_type.__name__ == "Component": - comp_type_class = getattr(lems_module_object, "Component") - new_component = True - else: - comp_type_class = getattr(module_object, component_type.__name__) + comp_type_class = getattr(module_object, component_type.__name__) - comp = comp_type_class(**kwargs) + # if new component, manually add the supplied arguments after + # extracting id and name + if comp_type_class.__name__ == "Component": + new_comp_args = kwargs.copy() + try: + id_ = new_comp_args.pop("id") + type_ = new_comp_args.pop("type") + except KeyError: + print("Error: Component requires 'id' and 'type'") - # if new component, don't do anything else because it's not from the - # schema - if new_component: + comp = comp_type_class(id=id_, type=type_) + comp.anyAttributes_ = new_comp_args return comp + comp = comp_type_class(**kwargs) + # additional setups where required if comp_type_class.__name__ == "Cell": comp.setup_nml_cell()