@@ -82,22 +82,22 @@ def load_xml(filename: str):
82
82
cleaned_xml = re .sub (ns_prefix_pattern , "" , xml_string ).strip ()
83
83
84
84
# Removes xmlns, xmlns:xsi and xsi:schemaLocation from the xml structure for conversion
85
- # it passes an element tree object to the element_to_dict function
85
+ # it passes an element tree object to the elementtree_element_to_dict function
86
86
removed_namespaces = process_xml_namespace (cleaned_xml )
87
87
88
88
# Converts the resulting xml stripped of xmlns, xmlns:xsi and xsi:schemaLocation into a dict
89
- data = element_to_dict (removed_namespaces )
89
+ data = elementtree_element_to_dict (removed_namespaces )
90
90
91
91
# Removes every key having 'id' and replaces it with it's value
92
- removed_id = handle_id (data )
92
+ removed_id = handle_xml_dict_id (data )
93
93
94
94
# Values are returned as strings after conversion, this corrects them to their actual values
95
- converted_to_actual_val = convert_values (removed_id )
95
+ converted_to_actual_val = convert_xml_dict_values (removed_id )
96
96
97
- return convert_values (converted_to_actual_val )
97
+ return convert_xml_dict_values (converted_to_actual_val )
98
98
99
99
100
- def element_to_dict (element ):
100
+ def elementtree_element_to_dict (element ):
101
101
"""
102
102
This convert an ElementTree element to a dictionary.
103
103
@@ -115,7 +115,7 @@ def element_to_dict(element):
115
115
children_by_tag = {}
116
116
for child_element in element :
117
117
child_key = child_element .tag + "s"
118
- child_value = element_to_dict (child_element )
118
+ child_value = elementtree_element_to_dict (child_element )
119
119
120
120
# Check if the child element has an 'id' attribute
121
121
if "id" in child_element .attrib :
@@ -148,21 +148,21 @@ def process_xml_namespace(xml_string):
148
148
return root
149
149
150
150
151
- def handle_id (dictionary ):
151
+ def handle_xml_dict_id (dictionary ):
152
152
if isinstance (dictionary , dict ):
153
153
if "id" in dictionary :
154
154
nested_dict = {dictionary ["id" ]: dictionary .copy ()}
155
155
del nested_dict [dictionary ["id" ]]["id" ]
156
- return {k : handle_id (v ) for k , v in nested_dict .items ()}
156
+ return {k : handle_xml_dict_id (v ) for k , v in nested_dict .items ()}
157
157
else :
158
- return {k : handle_id (v ) for k , v in dictionary .items ()}
158
+ return {k : handle_xml_dict_id (v ) for k , v in dictionary .items ()}
159
159
elif isinstance (dictionary , list ):
160
- return [handle_id (item ) for item in dictionary ]
160
+ return [handle_xml_dict_id (item ) for item in dictionary ]
161
161
else :
162
162
return dictionary
163
163
164
164
165
- def convert_values (value ):
165
+ def convert_xml_dict_values (value ):
166
166
"""
167
167
This recursively converts values to their actual types.
168
168
@@ -186,9 +186,9 @@ def convert_values(value):
186
186
elif value .lower () == "none" :
187
187
return None
188
188
elif isinstance (value , dict ):
189
- return {key : convert_values (val ) for key , val in value .items ()}
189
+ return {key : convert_xml_dict_values (val ) for key , val in value .items ()}
190
190
elif isinstance (value , list ):
191
- return [convert_values (item ) for item in value ]
191
+ return [convert_xml_dict_values (item ) for item in value ]
192
192
193
193
return value
194
194
@@ -246,47 +246,36 @@ def build_xml_element(data, parent=None):
246
246
Returns:
247
247
Parent
248
248
"""
249
-
250
249
if parent is None :
251
250
parent = ET .Element (data .__class__ .__name__ )
252
251
253
252
attrs = attr .fields (data .__class__ )
254
- id_attribute_value = None # Store id attribute value to be set after other attributes
255
253
for aattr in attrs :
256
- if aattr .name == 'id' :
257
- id_attribute_value = data .__getattribute__ (aattr .name )
258
- elif isinstance (aattr .default , attr .Factory ):
254
+ if isinstance (aattr .default , attr .Factory ):
259
255
children = data .__getattribute__ (aattr .name )
260
256
if not isinstance (children , (list , tuple )):
261
257
children = [children ]
262
258
263
259
for child in children :
264
260
child_element = build_xml_element (child )
265
261
parent .append (child_element )
266
-
267
- # Filters name space and schemaLoacation attributes, only allows non name space attributes to added as attributes
268
- elif not any (
269
- hasattr (data , attr_name )
270
- for attr_name in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]
271
- ):
262
+
263
+ # Filters name space and schemaLoacation attributes, only allows non name space attributes to be added as attributes
264
+ elif aattr .name not in ["xmlns" , "xmlns_url" , "xmlns_loc" , "xmln_loc_2" ]:
272
265
attribute_name = aattr .name
273
266
attribute_value = data .__getattribute__ (aattr .name )
274
267
parent .set (attribute_name , str (attribute_value ))
275
-
268
+
276
269
# This defines the various namespaces and schemaLocation of the generated xml
277
270
if hasattr (data , "xmlns" ):
278
271
parent .set ("xmlns" , data .xmlns )
279
272
if hasattr (data , "xmlns_url" ):
280
273
parent .set ("xmlns:xsi" , data .xmlns_url )
281
274
if hasattr (data , "xmlns_loc" ):
282
275
parent .set ("xsi:schemaLocation" , str (data .xmlns_loc + " " + data .xmln_loc_2 ))
283
-
284
- # Set the id attribute after processing all other attributes
285
- if id_attribute_value is not None :
286
- parent .set ("id" , str (id_attribute_value ))
287
-
288
276
return parent
289
277
278
+
290
279
def ascii_encode_dict (data ):
291
280
ascii_encode = (
292
281
lambda x : x .encode ("ascii" )
0 commit comments