Skip to content

Commit

Permalink
feat: handle special "Component" class
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sanjayankur31 committed Aug 1, 2024
1 parent 9b56b77 commit 3fcfb7b
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions neuroml/nml/generatedssupersuper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3fcfb7b

Please sign in to comment.