diff options
author | John Estabrook <jestabro@vyos.io> | 2021-08-11 11:06:39 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2021-08-12 10:19:39 -0500 |
commit | e75db1c0c23bb8615dea92316148871edd18749e (patch) | |
tree | 219821192a4e333d68bd988ffd937246ba68c1f1 /scripts | |
parent | e4e3840ad5ede376e52b18e0aff777c25176a2e8 (diff) | |
download | vyos-1x-e75db1c0c23bb8615dea92316148871edd18749e.tar.gz vyos-1x-e75db1c0c23bb8615dea92316148871edd18749e.zip |
xml: T3732: merge leafNode with defaultValue with leafNode(s) of same path
(cherry picked from commit e7c69ead85af485b2b881f24b929a4bcc779a4ce and
commit ec6dc94a5144ea4ac75ba6fac4d85b4f66337ae2)
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/override-default | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/scripts/override-default b/scripts/override-default index c8a0ff1da..0c49087c8 100755 --- a/scripts/override-default +++ b/scripts/override-default @@ -27,6 +27,7 @@ import sys import glob import logging +from copy import deepcopy from lxml import etree debug = False @@ -60,30 +61,55 @@ def override_element(l: list): for el in parents: el.getparent().remove(el) +def merge_remaining(l: list, elementtree): + """ + Merge (now) single leaf node containing 'defaultValue' with leaf nodes + of same path and no 'defaultValue'. + """ + for p in l: + p = p.split() + path_str = f'/interfaceDefinition/*' + path_list = [] + for i in range(len(p)): + path_list.append(f'[@name="{p[i]}"]') + path_str += '/children/*'.join(path_list) + rp = elementtree.xpath(path_str) + if len(rp) > 1: + for el in rp[1:]: + # in practice there will only be one child of the path, + # either defaultValue or Properties, since + # override_element() has already run + for child in el: + rp[0].append(deepcopy(child)) + el.getparent().remove(el) + def collect_and_override(dir_name): """ - Collect elements with defaultValue tag into dictionary indexed by tuple - of (name: str, ancestor path: str). + Collect elements with defaultValue tag into dictionary indexed by name + attributes of ancestor path. """ for fname in glob.glob(f'{dir_name}/*.xml'): tree = etree.parse(fname) root = tree.getroot() defv = {} - xpath_str = f'//defaultValue' + xpath_str = '//defaultValue' xp = tree.xpath(xpath_str) for element in xp: ap = element.xpath('ancestor::*[@name]') ap_name = [el.get("name") for el in ap] - ap_path_str = ' '.join(ap_name[:-1]) - defv.setdefault((ap_name[-1], ap_path_str), []).append(element) + ap_path_str = ' '.join(ap_name) + defv.setdefault(ap_path_str, []).append(element) for k, v in defv.items(): if len(v) > 1: - logger.info(f"overridding default in {k[0]}, path '{k[1]}'") + logger.info(f"overridding default in path '{k}'") override_element(v) + to_merge = list(defv) + merge_remaining(to_merge, tree) + revised_str = etree.tostring(root, encoding='unicode', pretty_print=True) with open(f'{fname}', 'w') as f: |