@@ -71,14 +71,20 @@ def load_xml(filename: str):
71
71
tree = ET .parse (infile ) # Parse the XML file into an ElementTree object
72
72
root = tree .getroot () # Get the root element
73
73
74
- # Convert the ElementTree object to a dictionary
75
- xml_string = (
76
- ET .tostring (root ).decode ().replace ("ns0:" , "" ).replace (":ns0" , "" ).strip ()
77
- )
74
+ # Converts the loaded xml into a string and removes unwanted string values ':ns0' and 'ns0:'
75
+ # They prevent the xml from loading correctly
76
+ xml_string = ET .tostring (root ).decode ().replace ('ns0:' , '' ).replace (':ns0' , '' ).strip ()
78
77
78
+ # Removes xmlns, xmlns:xsi and xsi:schemaLocation from the xml structure for conversion
79
79
removed_namespaces = process_xml_namespace (xml_string )
80
+
81
+ # Converts the resulting xml stripped of xmlns, xmlns:xsi and xsi:schemaLocation into a dict
80
82
data = element_to_dict (removed_namespaces )
83
+
84
+ # Removes every key having 'id' and replaces it with it's value
81
85
removed_id = handle_id (data )
86
+
87
+ # Values are returned as strings after conversion, this corrects them to their actual values
82
88
converted_to_actual_val = convert_values (removed_id )
83
89
84
90
return convert_values (converted_to_actual_val )
@@ -101,11 +107,11 @@ def element_to_dict(element):
101
107
102
108
children_by_tag = {}
103
109
for child_element in element :
104
- child_key = child_element .tag + "s"
110
+ child_key = child_element .tag + 's'
105
111
child_value = element_to_dict (child_element )
106
112
107
113
# Check if the child element has an 'id' attribute
108
- if "id" in child_element .attrib :
114
+ if 'id' in child_element .attrib :
109
115
# If the child element has an 'id', add it to the result dictionary directly
110
116
result [child_key ] = child_value
111
117
else :
@@ -123,10 +129,12 @@ def process_xml_namespace(xml_string):
123
129
ignored_elements = [
124
130
'xmlns="http://www.neuroml.org/schema/neuroml2"' ,
125
131
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' ,
126
- 'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"' ,
132
+ 'xsi:schemaLocation="http://www.neuroml.org/schema/neuroml2 https://raw.github.com/NeuroML/NeuroML2/development/Schemas/NeuroML2/NeuroML_v2.3.xsd"'
127
133
]
134
+
135
+ # Loops through the xml string and removes every instance of the elements in the list named ignored_elements
128
136
for ignored_element in ignored_elements :
129
- xml_string = xml_string .replace (ignored_element , "" ).strip ()
137
+ xml_string = xml_string .replace (ignored_element , '' ).strip ()
130
138
131
139
# Parse the XML string into an ElementTree
132
140
root = ET .fromstring (xml_string )
@@ -231,6 +239,8 @@ def build_xml_element(data, parent=None):
231
239
Returns:
232
240
Parent
233
241
"""
242
+
243
+ # If a parent name isn't given, it extracts the name from the class and use that instead
234
244
if parent is None :
235
245
parent = ET .Element (data .__class__ .__name__ )
236
246
@@ -244,24 +254,23 @@ def build_xml_element(data, parent=None):
244
254
for child in children :
245
255
child_element = build_xml_element (child )
246
256
parent .append (child_element )
247
- elif not any (
248
- hasattr (data , attr_name )
249
- for attr_name in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]
250
- ):
257
+ elif not any (hasattr (data , attr_name ) for attr_name in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]):
251
258
attribute_name = aattr .name
252
259
attribute_value = data .__getattribute__ (aattr .name )
253
260
parent .set (attribute_name , str (attribute_value ))
254
-
261
+
262
+ # This defines the various namespaces and schemaLocation of the generated xml
255
263
if hasattr (data , "xmlns" ):
256
264
parent .set ("xmlns" , data .xmlns )
257
265
if hasattr (data , "xmlns_url" ):
258
266
parent .set ("xmlns:xsi" , data .xmlns_url )
259
267
if hasattr (data , "xmlns_loc" ):
260
- parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + " \n " + data .xmln_loc_2 ))
268
+ parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + ' \n ' + data .xmln_loc_2 ))
261
269
262
270
return parent
263
271
264
272
273
+
265
274
def ascii_encode_dict (data ):
266
275
ascii_encode = (
267
276
lambda x : x .encode ("ascii" )
0 commit comments