Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opmode: T7084: reorganize the op mode cache format for ease of search #4313

Open
wants to merge 2 commits into
base: current
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 49 additions & 46 deletions python/vyos/xml_ref/generate_op_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2024 VyOS maintainers and contributors
# Copyright (C) 2024-2025 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
Expand Down Expand Up @@ -33,9 +33,9 @@
sys.path.append(join(_here, '..'))
from defaults import directories

from op_definition import NodeData
from op_definition import PathData


xml_op_cache_json = 'xml_op_cache.json'
xml_op_tmp = join('/tmp', xml_op_cache_json)
op_ref_cache = abspath(join(_here, 'op_cache.py'))
Expand Down Expand Up @@ -74,7 +74,7 @@ def translate_op_script(s: str) -> str:
return s


def insert_node(n: Element, l: list[PathData], path = None) -> None:
def insert_node(n: Element, l: list[PathData], path=None) -> None:
# pylint: disable=too-many-locals,too-many-branches
prop: OptElement = n.find('properties')
children: OptElement = n.find('children')
Expand All @@ -95,65 +95,67 @@ def insert_node(n: Element, l: list[PathData], path = None) -> None:
if command_text is not None:
command_text = translate_command(command_text, path)

comp_help = None
comp_help = {}
if prop is not None:
che = prop.findall("completionHelp")
che = prop.findall('completionHelp')

for c in che:
lists = c.findall("list")
paths = c.findall("path")
scripts = c.findall("script")

comp_help = {}
list_l = []
for i in lists:
list_l.append(i.text)
path_l = []
for i in paths:
path_str = re.sub(r'\s+', '/', i.text)
path_l.append(path_str)
script_l = []
for i in scripts:
script_str = translate_op_script(i.text)
script_l.append(script_str)

comp_help['list'] = list_l
comp_help['fs_path'] = path_l
comp_help['script'] = script_l

for d in l:
if name in list(d):
break
else:
d = {}
l.append(d)

inner_l = d.setdefault(name, [])

inner_d: PathData = {'node_data': NodeData(node_type=node_type,
help_text=help_text,
comp_help=comp_help,
command=command_text,
path=path)}
inner_l.append(inner_d)
comp_list_els = c.findall('list')
comp_path_els = c.findall('path')
comp_script_els = c.findall('script')

comp_lists = []
for i in comp_list_els:
comp_lists.append(i.text)

comp_paths = []
for i in comp_path_els:
comp_paths.append(i.text)

comp_scripts = []
for i in comp_script_els:
comp_script_str = translate_op_script(i.text)
comp_scripts.append(comp_script_str)

if comp_lists:
comp_help['list'] = comp_lists
if comp_paths:
comp_help['path'] = comp_paths
if comp_scripts:
comp_help['script'] = comp_scripts

cur_node_dict = {}
cur_node_dict['name'] = name
cur_node_dict['type'] = node_type
cur_node_dict['comp_help'] = comp_help
cur_node_dict['help'] = help_text
cur_node_dict['command'] = command_text
cur_node_dict['path'] = path
cur_node_dict['children'] = []
l.append(cur_node_dict)

if children is not None:
inner_nodes = children.iterfind("*")
inner_nodes = children.iterfind('*')
for inner_n in inner_nodes:
inner_path = path[:]
insert_node(inner_n, inner_l, inner_path)
insert_node(inner_n, cur_node_dict['children'], inner_path)


def parse_file(file_path, l):
tree = ET.parse(file_path)
root = tree.getroot()
for n in root.iterfind("*"):
for n in root.iterfind('*'):
insert_node(n, l)


def main():
parser = ArgumentParser(description='generate dict from xml defintions')
parser.add_argument('--xml-dir', type=str, required=True,
help='transcluded xml op-mode-definition file')
parser.add_argument(
'--xml-dir',
type=str,
required=True,
help='transcluded xml op-mode-definition file',
)

args = vars(parser.parse_args())

Expand All @@ -170,5 +172,6 @@ def main():
with open(op_ref_cache, 'w') as f:
f.write(f'op_reference = {str(l)}')


if __name__ == '__main__':
main()
Loading