Skip to content

Commit 814a658

Browse files
modified the name space check pattern to catch any pattern that arrises
1 parent b7ce03b commit 814a658

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/modelspec/base_types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ def to_xml(self) -> str:
125125
)
126126
from modelspec.utils import build_xml_element
127127

128-
# root = ET.Element("modelspec")
129128
root = build_xml_element(self)
130129

131130
xml_string = ET.tostring(

src/modelspec/utils.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,25 @@ def load_xml(filename: str):
6767
Args:
6868
filename: The name of the XML file to load.
6969
"""
70+
import re
71+
7072
with open(filename, "rb") as infile:
7173
tree = ET.parse(infile) # Parse the XML file into an ElementTree object
7274
root = tree.getroot() # Get the root element
7375

74-
# Converts the loaded xml into a string and removes unwanted string values ':ns0' and 'ns0:'
76+
# This defines regular expressions to match the namespace patterns to be removed
77+
ns_prefix_pattern = r"(ns\d+:|:ns\d+)"
78+
79+
# Converts the loaded xml into a string and removes unwanted string values ':ns0' to :ns∞ and 'ns0:' to ns∞:
7580
# They prevent the xml from loading correctly
76-
xml_string = ET.tostring(root).decode().replace('ns0:', '').replace(':ns0', '').strip()
81+
xml_string = ET.tostring(root).decode()
82+
cleaned_xml = re.sub(ns_prefix_pattern, "", xml_string).strip()
7783

7884
# Removes xmlns, xmlns:xsi and xsi:schemaLocation from the xml structure for conversion
79-
removed_namespaces = process_xml_namespace(xml_string)
85+
# it passes an element tree object to the element_to_dict function
86+
removed_namespaces = process_xml_namespace(cleaned_xml)
8087

81-
# Converts the resulting xml stripped of xmlns, xmlns:xsi and xsi:schemaLocation into a dict
88+
# Converts the resulting xml stripped of xmlns, xmlns:xsi and xsi:schemaLocation into a dict
8289
data = element_to_dict(removed_namespaces)
8390

8491
# Removes every key having 'id' and replaces it with it's value
@@ -107,11 +114,11 @@ def element_to_dict(element):
107114

108115
children_by_tag = {}
109116
for child_element in element:
110-
child_key = child_element.tag + 's'
117+
child_key = child_element.tag + "s"
111118
child_value = element_to_dict(child_element)
112119

113120
# Check if the child element has an 'id' attribute
114-
if 'id' in child_element.attrib:
121+
if "id" in child_element.attrib:
115122
# If the child element has an 'id', add it to the result dictionary directly
116123
result[child_key] = child_value
117124
else:
@@ -129,12 +136,12 @@ def process_xml_namespace(xml_string):
129136
ignored_elements = [
130137
'xmlns="http://www.neuroml.org/schema/neuroml2"',
131138
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
132-
'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"'
139+
'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"',
133140
]
134141

135142
# Loops through the xml string and removes every instance of the elements in the list named ignored_elements
136143
for ignored_element in ignored_elements:
137-
xml_string = xml_string.replace(ignored_element, '').strip()
144+
xml_string = xml_string.replace(ignored_element, "").strip()
138145

139146
# Parse the XML string into an ElementTree
140147
root = ET.fromstring(xml_string)
@@ -254,23 +261,27 @@ def build_xml_element(data, parent=None):
254261
for child in children:
255262
child_element = build_xml_element(child)
256263
parent.append(child_element)
257-
elif not any(hasattr(data, attr_name) for attr_name in ["xmlns", "xmlns_url", "xmlns_loc", "xmln_loc_2"]):
264+
265+
# Filters name space and schemaLoacation attributes, only allows non name space attributes to added as attributes
266+
elif not any(
267+
hasattr(data, attr_name)
268+
for attr_name in ["xmlns", "xmlns_url", "xmlns_loc", "xmln_loc_2"]
269+
):
258270
attribute_name = aattr.name
259271
attribute_value = data.__getattribute__(aattr.name)
260272
parent.set(attribute_name, str(attribute_value))
261-
273+
262274
# This defines the various namespaces and schemaLocation of the generated xml
263275
if hasattr(data, "xmlns"):
264276
parent.set("xmlns", data.xmlns)
265277
if hasattr(data, "xmlns_url"):
266278
parent.set("xmlns:xsi", data.xmlns_url)
267279
if hasattr(data, "xmlns_loc"):
268-
parent.set("xsi:schemaLocation", str(data.xmlns_loc + '\n' + data.xmln_loc_2))
280+
parent.set("xsi:schemaLocation", str(data.xmlns_loc + "\n" + data.xmln_loc_2))
269281

270282
return parent
271283

272284

273-
274285
def ascii_encode_dict(data):
275286
ascii_encode = (
276287
lambda x: x.encode("ascii")

0 commit comments

Comments
 (0)