From f8928bb64ad5d3115fc9328976a6f9486d5a92e0 Mon Sep 17 00:00:00 2001 From: Valmantas Paliksa Date: Sun, 14 Jan 2024 20:14:18 +0200 Subject: [PATCH] Add newlines and indents to the userconfig xml file --- xmlutils.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/xmlutils.py b/xmlutils.py index ccb004c..2005fa0 100644 --- a/xmlutils.py +++ b/xmlutils.py @@ -10,11 +10,14 @@ print_debugs = False print_method_calls = False - device = '' userconfig_path = '' defaults_path = '' +def write_userconfig_xml(tree : ET.ElementTree): + ET.indent(tree, " ") + tree.write(userconfig_path, "utf-8") + def update_vars(_device, _userconfig_path, _defaults_path): global device, userconfig_path, defaults_path @@ -497,7 +500,7 @@ def write_models_to_xml(the_sim, the_model, the_value, setting_name, unit='', th if child_elem.tag == 'unit': child_elem.text = str(unit) if the_model != '': - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Updated element with values: sim={the_sim}, device={the_device}, " f"value={the_value}, unit={unit}, model={the_model}, name={setting_name}") @@ -541,7 +544,7 @@ def write_models_to_xml(the_sim, the_model, the_value, setting_name, unit='', th # Write the modified XML back to the file tree = ET.ElementTree(root) - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Added element with values: sim={the_sim}, device={the_device}, " f"value={the_value}, unit={unit}, model={the_model}, name={setting_name}") @@ -566,7 +569,7 @@ def write_class_to_xml(the_sim, the_class, the_value, setting_name, unit=''): for child_elem in class_elem: if child_elem.tag == 'value': child_elem.text = str(the_value) - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Updated element with values: sim={the_sim}, device={the_device}, " f"value={the_value}, model={the_class}, name={setting_name}") @@ -583,7 +586,7 @@ def write_class_to_xml(the_sim, the_class, the_value, setting_name, unit=''): # Write the modified XML back to the file tree = ET.ElementTree(root) - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Added element with values: sim={the_sim}, device={the_device}, " f"value={the_value}{unit}, type={the_class}, name={setting_name}") @@ -607,7 +610,8 @@ def write_sim_to_xml(the_sim, the_value, setting_name, unit=''): for child_elem in sim_elem: if child_elem.tag == 'value': child_elem.text = str(the_value) - tree.write(userconfig_path) + + write_userconfig_xml(tree) logging.info(f"Updated element with values: sim={the_sim}, device={the_device}, " f"value={the_value}, name={setting_name}") @@ -623,7 +627,7 @@ def write_sim_to_xml(the_sim, the_value, setting_name, unit=''): # Write the modified XML back to the file tree = ET.ElementTree(root) - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info( f"Added element with values: sim={the_sim}, device={the_device}, value={the_value}{unit}, name={setting_name}") @@ -683,7 +687,7 @@ def erase_models_from_xml(the_sim, the_model, setting_name): for elem in elements_to_remove: root.remove(elem) # Write the modified XML back to the file - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Removed element with values: sim={the_sim}, device={the_device}, " f"model={the_model}, name={setting_name}") @@ -708,7 +712,7 @@ def erase_entire_model_from_xml(the_sim, the_model): for elem in elements_to_remove: root.remove(elem) # Write the modified XML back to the file - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Removed all elements with values: sim={the_sim} model={the_model}") @@ -737,7 +741,7 @@ def erase_class_from_xml( the_sim, the_class, the_value, setting_name): for elem in elements_to_remove: root.remove(elem) # Write the modified XML back to the file - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Removed element with values: sim={the_sim}, device={the_device}, " f"value={the_value}, type={the_class}, name={setting_name}") @@ -766,7 +770,7 @@ def erase_sim_from_xml(the_sim, the_value, setting_name): for elem in elements_to_remove: root.remove(elem) # Write the modified XML back to the file - tree.write(userconfig_path) + write_userconfig_xml(tree) logging.info(f"Removed element with values: sim={the_sim}, device={the_device}, value={the_value}, name={setting_name}")