From 046123c4f7e0ada2aa6b66558735f6cd704b3663 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sat, 16 Sep 2017 20:02:52 +0200 Subject: T396: add support for value constraint tags to the build-command-templates script --- scripts/build-command-templates | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'scripts/build-command-templates') diff --git a/scripts/build-command-templates b/scripts/build-command-templates index 6cfaa2bc1..09c7dc8c4 100755 --- a/scripts/build-command-templates +++ b/scripts/build-command-templates @@ -28,6 +28,11 @@ import functools from lxml import etree as ET +# Defaults + +validator_dir = "/opt/vyatta/libexec/validators" +default_constraint_err_msg = "Invalid value" + ## Get arguments @@ -100,6 +105,37 @@ def get_properties(p): except: props["val_help"] = [] + # Get the constraint statements + try: + error_msg = default_constraint_err_msg + # Get the error message if it's there + try: + error_msg = p.find("constraintErrorMessage").text + except: + pass + + vce = p.findall("constraint") + vc = [] + for v in vce: + if v.find("regex") is not None: + vc.append("pattern $VAR(@) \"{0}\"; {1}".format(v.find("regex").text, error_msg)) + else: + validator = v.find("validator") + v_name = validator.get("name") + + # XXX: lxml returns None for empty arguments + v_argument = None + try: + v_argument = validator.get("argument") + except: + pass + if v_argument is None: + v_argument = "" + vc.append("exec {0}/{1} {2} $VAR(@); {3}".format(validator_dir, v_name, v_argument, error_msg)) + props["constraints"] = vc + except: + props["constraints"] = [] + # Get the completion help strings try: che = p.findall("completionHelp") @@ -163,6 +199,10 @@ def make_node_def(props): if "comp_help" in props: node_def += "allowed: {0}\n".format(props["comp_help"]) + if "constraints" in props: + for c in props["constraints"]: + node_def += "syntax:expression: {0}\n".format(c) + if "owner" in props: node_def += "end: sudo sh -c \"{0}\"\n".format(props["owner"]) -- cgit v1.2.3