Skip to content

Commit aefa0b4

Browse files
Merge pull request #91 from LEMS/fix-namespaced-attributes
Fix namespaced attributes
2 parents 35ba66f + 0612b1d commit aefa0b4

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

lems/model/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ def export_to_dom(self):
369369
xmldom = minidom.parseString(xmlstr)
370370
except ExpatError as er:
371371
print("Parsing error:", errors.messages[er.code])
372-
print("at: " + xmlstr[er.offset : er.offset + 20])
372+
print("at: " + xmlstr[er.offset - 50: er.offset + 50])
373+
print("at: " + ("-" * 50) + "^")
373374
raise
374375
return xmldom
375376

lems/parser/LEMS.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,50 @@
1717
from 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+
2050
def 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

3982
class 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

Comments
 (0)