diff options
| author | qdp-odoo <qdp@odoo.com> | 2017-08-29 10:17:27 +0200 |
|---|---|---|
| committer | qdp-odoo <qdp@odoo.com> | 2017-08-29 10:17:27 +0200 |
| commit | eaf71455f4adabf896d51e93fdfa902c0ef75f37 (patch) | |
| tree | 14da973e2816bba926c4fa36a550550b9ab0b9d5 | |
| parent | 11244b5df7a3d22f5c2bd70297453cd0afc0e9b6 (diff) | |
[ADD] tools: add some xml utilities
-create_xml_node()
-create_xml_nod_chain()
| -rw-r--r-- | odoo/tools/xml_utils.py | 26 |
1 files changed, 26 insertions, 0 deletions
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] |
