1717from lems .model .structure import *
1818
1919
20+ def get_nons_attribute (attribute ):
21+ """Get attributes without namespace prefixes.
22+
23+ The parsed attributes will include their namespaces in {}:
24+
25+ eg: `{some_namespace_url}attribute=value`
26+
27+ Similar to tags, we replace these with their corresponding LEMS component
28+ types.
29+
30+ :param attribute: attribute to strip namespace from
31+ :type attribute: str
32+ :returns: tweaked attribute
33+ """
34+ bits = attribute .split ("}" )
35+ if len (bits ) == 1 :
36+ return attribute
37+ elif "/lems/" in bits [0 ]:
38+ return bits [1 ]
39+ elif "neuroml2" in bits [0 ]:
40+ return bits [1 ]
41+ elif "rdf" in bits [0 ]:
42+ return "rdf_" + bits [1 ]
43+ elif "model-qualifiers" in bits [0 ]:
44+ return "bqmodel_" + bits [1 ]
45+ elif "biology-qualifiers" in bits [0 ]:
46+ return "bqbiol_" + bits [1 ]
47+ else :
48+ return "%s:%s" % (bits [0 ], bits [1 ])
49+
2050def get_nons_tag_from_node (node ):
51+ """Get tags without namespace prefixes.
52+
53+ The parsed tags will include their namespaces in {}:
54+
55+ eg: `<{some_namespace_url}mytag ..>`
56+
57+ Similar to attributes, we replace these with their corresponding LEMS
58+ component types.
59+
60+ :param tag: tag to strip namespace from
61+ :type tag: xml.ElementTree.Element
62+ :returns: tweaked tag
63+ """
2164 tag = node .tag
2265 bits = tag .split ("}" )
2366 if len (bits ) == 1 :
@@ -37,6 +80,12 @@ def get_nons_tag_from_node(node):
3780
3881
3982class LEMSXMLNode :
83+ """LEMS XML Node container class.
84+
85+ This does not include any namespace declarations in tags and their
86+ attributes with their corresponding Component definitions (because LEMS
87+ does not know what XML namespaces are).
88+ """
4089 def __init__ (self , pyxmlnode ):
4190 self .tag = get_nons_tag_from_node (pyxmlnode )
4291 self .ltag = self .tag .lower ()
@@ -45,8 +94,9 @@ def __init__(self, pyxmlnode):
4594 self .lattrib = dict ()
4695
4796 for k in pyxmlnode .attrib :
48- self .attrib [k ] = pyxmlnode .attrib [k ]
49- self .lattrib [k .lower ()] = pyxmlnode .attrib [k ]
97+ k_nons = get_nons_attribute (k )
98+ self .attrib [k_nons ] = pyxmlnode .attrib [k ]
99+ self .lattrib [k_nons .lower ()] = pyxmlnode .attrib [k ]
50100
51101 self .children = list ()
52102 for pyxmlchild in pyxmlnode :
0 commit comments