summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2021-01-21 09:17:59 -0600
committerGitHub <noreply@github.com>2021-01-21 09:17:59 -0600
commit173e4599394c0283953ef2a8299ff94eca802369 (patch)
treea5ff65ff03da4f0c925077aa2c4a5fd91b2b876b
parent511c127832667f5db218cf803ad88355f873d399 (diff)
parentdd89255dcc82e315d6106936a97b8c0be360519b (diff)
downloadvyos-1x-173e4599394c0283953ef2a8299ff94eca802369.tar.gz
vyos-1x-173e4599394c0283953ef2a8299ff94eca802369.zip
Merge pull request #692 from jestabro/override-default
XML: T2910: add support for override of tag 'defaultValue' values
-rw-r--r--Makefile2
-rwxr-xr-xscripts/override-default101
2 files changed, 103 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 2ba640d5b..339793a71 100644
--- a/Makefile
+++ b/Makefile
@@ -33,6 +33,8 @@ op_xml_obj = $(op_xml_src:.xml.in=.xml)
interface_definitions: $(config_xml_obj)
mkdir -p $(TMPL_DIR)
+ $(CURDIR)/scripts/override-default $(BUILD_DIR)/interface-definitions
+
find $(BUILD_DIR)/interface-definitions -type f -name "*.xml" | xargs -I {} $(CURDIR)/scripts/build-command-templates {} $(CURDIR)/schema/interface_definition.rng $(TMPL_DIR) || exit 1
# XXX: delete top level node.def's that now live in other packages
diff --git a/scripts/override-default b/scripts/override-default
new file mode 100755
index 000000000..d91b89426
--- /dev/null
+++ b/scripts/override-default
@@ -0,0 +1,101 @@
+#!/usr/bin/env python3
+#
+# override-default: preprocessor for XML interface definitions to interpret
+# redundant entries (relative to path) with tag 'defaultValue' as an override
+# directive. Must be called before build-command-templates, as the schema
+# disallows redundancy.
+#
+# Copyright (C) 2021 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#
+
+# Use lxml xpath capability to find multiple elements with tag defaultValue
+# relative to path; replace and remove to override the value.
+
+import sys
+import glob
+import logging
+from lxml import etree
+
+debug = False
+
+logger = logging.getLogger(__name__)
+logs_handler = logging.StreamHandler()
+logger.addHandler(logs_handler)
+
+if debug:
+ logger.setLevel(logging.DEBUG)
+else:
+ logger.setLevel(logging.INFO)
+
+def override_element(l: list):
+ """
+ Allow multiple override elements; use the final one (in document order).
+ """
+ if len(l) < 2:
+ logger.debug("passing list of single element to override_element")
+ return
+
+ # assemble list of leafNodes of overriding defaultValues, for later removal
+ parents = []
+ for el in l[1:]:
+ parents.append(el.getparent())
+
+ # replace element with final override
+ l[0].getparent().replace(l[0], l[-1])
+
+ # remove all but overridden element
+ for el in parents:
+ 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)); the second component must be immutable for
+ tuple to act as key, hence str().
+ """
+ for fname in glob.glob(f'{dir_name}/*.xml'):
+ tree = etree.parse(fname)
+ root = tree.getroot()
+ defv = {}
+
+ xpath_str = f'//defaultValue'
+ xp = tree.xpath(xpath_str)
+
+ for element in xp:
+ ap = element.xpath('ancestor::*[@name]')
+ defv.setdefault((ap[-1].get("name"), str(ap[:-1])), []).append(element)
+
+ for k, v in defv.items():
+ if len(v) > 1:
+ logger.debug(f'overridding default in {k[0]}')
+ override_element(v)
+
+ revised_str = etree.tostring(root, encoding='unicode', pretty_print=True)
+
+ with open(f'{fname}', 'w') as f:
+ f.write(revised_str)
+
+def main():
+ if len(sys.argv) < 2:
+ logger.critical('Must specify XML directory!')
+ sys.exit(1)
+
+ dir_name = sys.argv[1]
+
+ collect_and_override(dir_name)
+
+if __name__ == '__main__':
+ main()