diff options
Diffstat (limited to 'docs/_ext/vyos.py')
-rw-r--r-- | docs/_ext/vyos.py | 116 |
1 files changed, 99 insertions, 17 deletions
diff --git a/docs/_ext/vyos.py b/docs/_ext/vyos.py index caa51a90..61b7519d 100644 --- a/docs/_ext/vyos.py +++ b/docs/_ext/vyos.py @@ -2,13 +2,14 @@ import re import json import os from datetime import datetime +from unittest import defaultTestLoader from docutils import io, nodes, utils, statemachine from docutils.parsers.rst.roles import set_classes from docutils.parsers.rst import Directive, directives, states from sphinx.util.docutils import SphinxDirective -from testcoverage import get_working_commands +from testcoverage import get_working_commands, get_vyos_commands from sphinx.util import logging @@ -29,6 +30,11 @@ def setup(app): 'html' ) app.add_config_value( + 'vyos_commands', + get_vyos_commands(), + 'html' + ) + app.add_config_value( 'vyos_coverage', { 'cfgcmd': [0,len(app.config.vyos_working_commands['cfgcmd'])], @@ -393,6 +399,21 @@ class OpcmdlistDirective(Directive): return [oplist] +def get_default_value(title_text, config, cfgmode): + title_text = strip_cmd(title_text) + for cmd in config.vyos_working_commands[cfgmode]: + cmd_joined = ' '.join(cmd['name']) + cmd_striped = strip_cmd(cmd_joined) + if "table-size" in cmd['name']: + pass + #print(cmd) + #print(cmd_striped) + #print(title_text) + #print() + if cmd_striped == title_text: + if cmd['defaultvalue']: + return cmd['defaultvalue'] + return None class CmdDirective(SphinxDirective): @@ -405,19 +426,31 @@ class CmdDirective(SphinxDirective): content_list = [] title_text = '' content_text = '' + defaultvalue = None has_body = False cfgmode = self.custom_class + "cmd" + try: + if '' in self.content: + index = self.content.index('') + title_list = self.content[0:index] + content_list = self.content[index + 1:] + + title_text = ' '.join(title_list) + content_text = content_text + '\n'.join(content_list) + has_body = True + else: + title_list = self.content + title_text = ' '.join(title_list) + except Exception as e: + print("error", e) - if '' in self.content: - index = self.content.index('') - title_list = self.content[0:index] - content_list = self.content[index + 1:] - title_text = ' '.join(title_list) - content_text = '\n'.join(content_list) - has_body = True - else: - title_text = ' '.join(self.content) + # render defaultvalue + if os.getenv('VYOS_DEFAULT') or ':defaultvalue:' in title_text: + value = get_default_value(title_list, self.config, cfgmode) + title_text = title_text.replace(":defaultvalue:", '') + if value: + defaultvalue = f"default: {value}\n" anchor_id = nodes.make_id(self.custom_class + "cmd-" + title_text) target = nodes.target(ids=[anchor_id]) @@ -439,6 +472,11 @@ class CmdDirective(SphinxDirective): heading_element['classes'] += [self.custom_class + 'cmd-heading'] panel_element.append(heading_element) + if defaultvalue: + defaultvalue_element = nodes.paragraph(text=defaultvalue) + defaultvalue_element['classes'] = ["defaultvalue"] + panel_element.append(defaultvalue_element) + append_list = { 'docname': self.env.docname, @@ -521,9 +559,11 @@ def strip_cmd(cmd, debug=False): if debug: print(cmd) cmd = re.sub('\s+','',cmd) + cmd = cmd.replace(':defaultvalue:','') if debug: print(cmd) print("") + return cmd def build_row(app, fromdocname, rowdata): @@ -550,17 +590,20 @@ def build_row(app, fromdocname, rowdata): -def process_coverage(app, fromdocname, doccmd, xmlcmd, cli_type): +def process_coverage(app, fromdocname, doccmd, xmlcmd, vyoscmd, cli_type): coverage_list = {} strip_true_list = [] for cmd in doccmd: coverage_item = { 'doccmd': None, 'xmlcmd': None, + 'vyoscmd': None, 'doccmd_item': None, 'xmlcmd_item': None, + 'vyoscmd_item': None, 'indocs': False, 'inxml': False, + 'invyos': False, 'xmlfilename': None } coverage_item['doccmd'] = cmd['cmd'] @@ -576,10 +619,13 @@ def process_coverage(app, fromdocname, doccmd, xmlcmd, cli_type): coverage_item = { 'doccmd': None, 'xmlcmd': None, + 'vyoscmd': None, 'doccmd_item': None, 'xmlcmd_item': None, + 'vyoscmd_item': None, 'indocs': False, 'inxml': False, + 'invyos': False, 'xmlfilename': None } coverage_item['xmlcmd'] = cmd['cmd'] @@ -592,7 +638,33 @@ def process_coverage(app, fromdocname, doccmd, xmlcmd, cli_type): coverage_list[strip]['xmlcmd_item'] = cmd coverage_list[strip]['inxml'] = True coverage_list[strip]['xmlfilename'] = cmd['filename'] - strip_true_list.append(strip) + + + for item in vyoscmd[cli_type]: + cmd = ' '.join(item['cmd']) + strip = strip_cmd(cmd) + if strip not in coverage_list.keys(): + coverage_item = { + 'doccmd': None, + 'xmlcmd': None, + 'vyoscmd': None, + 'doccmd_item': None, + 'xmlcmd_item': None, + 'vyoscmd_item': None, + 'indocs': False, + 'inxml': False, + 'invyos': False, + 'xmlfilename': None + } + coverage_item['vyoscmd'] = cmd + coverage_item['invyos'] = True + coverage_list[strip] = dict(coverage_item) + else: + coverage_list[strip]['vyoscmd'] = cmd + coverage_list[strip]['invyos'] = True + if coverage_list[strip]['indocs'] and coverage_list[strip]['inxml']: + strip_true_list.append(strip) + strip_true_list = list(set(strip_true_list)) @@ -605,11 +677,11 @@ def process_coverage(app, fromdocname, doccmd, xmlcmd, cli_type): table = nodes.table() - tgroup = nodes.tgroup(cols=3) + tgroup = nodes.tgroup(cols=4) table += tgroup - header = (f'Status {len(strip_true_list)}/{len(coverage_list)}', 'Documentaion', 'XML') - colwidths = (5, 50 , 50) + header = (f'Status {len(strip_true_list)}/{len(coverage_list)}', 'Documentation', 'XML', f'in VyOS {vyoscmd["os"]}') + colwidths = (5, 33 , 33, 33) table = nodes.table() tgroup = nodes.tgroup(cols=len(header)) table += tgroup @@ -623,6 +695,7 @@ def process_coverage(app, fromdocname, doccmd, xmlcmd, cli_type): for entry in sorted(coverage_list): doc_cmd_text = [] doc_xml_text = [] + doc_vyos_text = [] if coverage_list[entry]['indocs']: doc_cmd_text.append(coverage_list[entry]['doccmd_item']) else: @@ -633,8 +706,14 @@ def process_coverage(app, fromdocname, doccmd, xmlcmd, cli_type): doc_xml_text.append(coverage_list[entry]['xmlcmd']) else: doc_xml_text.append('Nothing found in XML Definitions') + + if coverage_list[entry]['invyos']: + doc_vyos_text.append(coverage_list[entry]['vyoscmd']) + else: + doc_vyos_text.append('Nothing found in VyOS') + - if not coverage_list[entry]['indocs'] or not coverage_list[entry]['inxml']: + if not coverage_list[entry]['indocs'] or not coverage_list[entry]['inxml'] or not coverage_list[entry]['invyos']: status = False else: status = True @@ -643,7 +722,8 @@ def process_coverage(app, fromdocname, doccmd, xmlcmd, cli_type): ( status, doc_cmd_text, - doc_xml_text + doc_xml_text, + doc_vyos_text ) ) @@ -678,6 +758,7 @@ def process_cmd_nodes(app, doctree, fromdocname): fromdocname, env.vyos_cfgcmd, app.config.vyos_working_commands['cfgcmd'], + app.config.vyos_commands, 'cfgcmd' ) ) @@ -695,6 +776,7 @@ def process_cmd_nodes(app, doctree, fromdocname): fromdocname, env.vyos_opcmd, app.config.vyos_working_commands['opcmd'], + app.config.vyos_commands, 'opcmd' ) ) |