From 57b54f3da7d9c4f5c0700bdd70d39954583ca1b2 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 30 Jul 2025 13:52:25 +0100 Subject: op-mode: T7669: render placeholders in commands with curly brackets to distinguish them from fixed parts of the command and allow rendering commands using a template processor --- python/vyos/xml_ref/generate_op_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python') diff --git a/python/vyos/xml_ref/generate_op_cache.py b/python/vyos/xml_ref/generate_op_cache.py index 266c81cd0..dec693032 100755 --- a/python/vyos/xml_ref/generate_op_cache.py +++ b/python/vyos/xml_ref/generate_op_cache.py @@ -81,7 +81,7 @@ def translate_position(s: str, pos: list[str]) -> str: # preferred to .format(*list) to avoid collisions with braces for i, p in enumerate(pos): - t = t.replace(f'_place_holder_{i+1}_', p) + t = t.replace(f'_place_holder_{i+1}_', f'{{{{{p}}}}}') return t -- cgit v1.2.3 From b6c9340417aaa691ef15d0346f903b6612f3fa64 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 30 Jul 2025 13:54:32 +0100 Subject: op-mode: T7669: Add support for virtual tag nodes in the op mode cache --- python/vyos/xml_ref/generate_op_cache.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/vyos/xml_ref/generate_op_cache.py b/python/vyos/xml_ref/generate_op_cache.py index dec693032..fec030b3d 100755 --- a/python/vyos/xml_ref/generate_op_cache.py +++ b/python/vyos/xml_ref/generate_op_cache.py @@ -153,10 +153,15 @@ def insert_node( if path is None: path = [] - path.append(name) + if node_type != 'virtualTagNode': + path.append(name) + if node_type == 'tagNode': path.append(f'{name}-tag_value') + if node_type == 'virtualTagNode': + path.append(f'{parent.name}-tag_value') + help_prop: OptElement = None if prop is None else prop.find('help') help_text = None if help_prop is None else help_prop.text command_text = None if command is None else command.text -- cgit v1.2.3 From 7b2bc979e07f0d554056be38990dd7a2b564d420 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 30 Jul 2025 13:55:42 +0100 Subject: op-mode: T7669: Make the path ambiguity check and JSON export capability mandatory in the op mode cache generator --- python/vyos/xml_ref/generate_op_cache.py | 47 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'python') diff --git a/python/vyos/xml_ref/generate_op_cache.py b/python/vyos/xml_ref/generate_op_cache.py index fec030b3d..ad1e785d0 100755 --- a/python/vyos/xml_ref/generate_op_cache.py +++ b/python/vyos/xml_ref/generate_op_cache.py @@ -49,7 +49,6 @@ from defaults import directories # noqa: E402 op_ref_cache = abspath(join(_here, 'op_cache.py')) -op_ref_json = abspath(join(_here, 'op_cache.json')) OptElement: TypeAlias = Optional[Element] @@ -262,17 +261,18 @@ def main(): action='store_true', help='check consistency of node data across files', ) - parser.add_argument( - '--check-path-ambiguity', - action='store_true', - help='attempt to reduce to unique paths, reporting if error', - ) parser.add_argument( '--select', type=str, help='limit cache to a subset of XML files: "power_ctl | multicast-group | ..."', ) + parser.add_argument( + '--export-json', + type=str, + help='Export a JSON version of the cache to a file', + ) + args = vars(parser.parse_args()) if args['check_xml_consistency']: @@ -281,7 +281,7 @@ def main(): xml_dir = abspath(args['xml_dir']) - d = {} + op_mode_data = {} select = args['select'] if select: @@ -290,25 +290,26 @@ def main(): for fname in sorted(glob.glob(f'{xml_dir}/*.xml')): file = os.path.basename(fname) if not select or os.path.splitext(file)[0] in select: - parse_file(fname, d) - - d = sort_op_data(d) - - if args['check_path_ambiguity']: - # when the following passes without error, return value will be the - # full dictionary indexed by str, not tuple - res, out, err = collapse(d) - if not err: - with open(op_ref_json, 'w') as f: - json.dump(res, f, indent=2) - else: - print('Found the following duplicate paths:\n') - print(out) - sys.exit(1) + parse_file(fname, op_mode_data) + + op_mode_data = sort_op_data(op_mode_data) + + res, out, err = collapse(op_mode_data) + if err: + print('Failed to generate operational command definition cache due to duplicate paths.') + print('Found the following duplicate paths:\n') + print(out) + sys.exit(1) + else: + op_mode_data = res with open(op_ref_cache, 'w') as f: f.write('from vyos.xml_ref.op_definition import NodeData\n') - f.write(f'op_reference = {str(d)}') + f.write(f'op_reference = {str(op_mode_data)}') + + if args['export_json']: + with open(args['export_json'], 'w') as f: + json.dump(op_mode_data, f) if __name__ == '__main__': -- cgit v1.2.3 From 1483b66eb957f48af9f86b54fc1780b41c4553e1 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 30 Jul 2025 19:29:21 +0100 Subject: op-mode: T7669: fix formatting in the op mode cache generator --- python/vyos/xml_ref/generate_op_cache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/vyos/xml_ref/generate_op_cache.py b/python/vyos/xml_ref/generate_op_cache.py index ad1e785d0..e985e8d6b 100755 --- a/python/vyos/xml_ref/generate_op_cache.py +++ b/python/vyos/xml_ref/generate_op_cache.py @@ -296,7 +296,9 @@ def main(): res, out, err = collapse(op_mode_data) if err: - print('Failed to generate operational command definition cache due to duplicate paths.') + print( + 'Failed to generate operational command definition cache due to duplicate paths.' + ) print('Found the following duplicate paths:\n') print(out) sys.exit(1) -- cgit v1.2.3 From 3d686f3922c204ce3d0793f62bc9eb4a10c23805 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 30 Jul 2025 20:01:59 +0100 Subject: op-mode: T7669: fail op mode cache generation if there are untranslated variables since that means the command is malformed --- python/vyos/xml_ref/generate_op_cache.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'python') diff --git a/python/vyos/xml_ref/generate_op_cache.py b/python/vyos/xml_ref/generate_op_cache.py index e985e8d6b..7f0bbf115 100755 --- a/python/vyos/xml_ref/generate_op_cache.py +++ b/python/vyos/xml_ref/generate_op_cache.py @@ -88,6 +88,14 @@ def translate_position(s: str, pos: list[str]) -> str: def translate_command(s: str, pos: list[str]) -> str: s = translate_exec(s) s = translate_position(s, pos) + + # If there are any untranslated occurences of '_place_holder_", + # it means the command is incorrect, + # e.g., it references "$6" when it only has five words. + if re.search(r'_place_holder_', s): + print(f"Command translation failed: {s}") + sys.exit(1) + return s -- cgit v1.2.3