From eaf71455f4adabf896d51e93fdfa902c0ef75f37 Mon Sep 17 00:00:00 2001 From: qdp-odoo Date: Tue, 29 Aug 2017 10:17:27 +0200 Subject: [ADD] tools: add some xml utilities -create_xml_node() -create_xml_nod_chain() --- odoo/tools/xml_utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'odoo/tools/xml_utils.py') diff --git a/odoo/tools/xml_utils.py b/odoo/tools/xml_utils.py index b7c03a87f53..8b238d41552 100644 --- a/odoo/tools/xml_utils.py +++ b/odoo/tools/xml_utils.py @@ -14,3 +14,29 @@ def check_with_xsd(tree_or_str, xsd_path): #import UserError only here to avoid circular import statements with tools.func being imported in exceptions.py from odoo.exceptions import UserError raise UserError('\n'.join(str(e) for e in xml_errors.error_log)) + +def create_xml_node_chain(first_parent_node, nodes_list, last_node_value=None): + """ Utility function for generating XML files nodes. Generates as a hierarchical + chain of nodes (each new node being the son of the previous one) based on the tags + contained in `nodes_list`, under the given node `first_parent_node`. + It will also set the value of the last of these nodes to `last_node_value` if it is + specified. This function returns the list of created nodes. + """ + res = [] + current_node = first_parent_node + for tag in nodes_list: + current_node = etree.SubElement(current_node, tag) + res.append(current_node) + + if last_node_value is not None: + current_node.text = last_node_value + return res + +def create_xml_node(parent_node, node_name, node_value=None): + """ Utility function for managing XML. It creates a new node with the specified + `node_name` as a child of given `parent_node` and assigns it `node_value` as value. + :param parent_node: valid etree Element + :param node_name: string + :param node_value: string + """ + return create_xml_node_chain(parent_node, [node_name], node_value)[0] -- cgit v1.2.3