From 28478a808ecf81aa13cd859b2e5c338cd8491a1e Mon Sep 17 00:00:00 2001 From: Dmitry Volodin Date: Thu, 8 Aug 2019 12:49:15 +0300 Subject: [PATCH] noc confdb syntax `path` parameter --- commands/confdb.py | 29 +++++++++++++++++++++++------ docs/src/en/man/confdb.rst | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/commands/confdb.py b/commands/confdb.py index 77744f71c5..5bf54e6d5a 100755 --- a/commands/confdb.py +++ b/commands/confdb.py @@ -8,6 +8,7 @@ # Python modules from __future__ import print_function +import argparse # NOC modules from noc.core.management.base import BaseCommand @@ -20,13 +21,14 @@ class Command(BaseCommand): def add_arguments(self, parser): subparsers = parser.add_subparsers(dest="cmd") # syntax command - subparsers.add_parser("syntax") + syntax_parser = subparsers.add_parser("syntax") + syntax_parser.add_argument("path", nargs=argparse.REMAINDER) def handle(self, cmd, *args, **options): return getattr(self, "handle_%s" % cmd)(*args, **options) - def handle_syntax(self, *args, **kwargs): - def dump_node(node, level=0): + def handle_syntax(self, path=None, *args, **kwargs): + def dump_node(node, level=0, recursive=True): indent = " " * level if node.name: label = "<%s>" % node.name @@ -37,14 +39,29 @@ class Command(BaseCommand): if node.multi: label = "*%s" % label self.print("%s%s" % (indent, label)) - if node.children: + if recursive and node.children: for nc in node.children: dump_node(nc, level + 1) + def find_root(children, rest_path, level=0): + if not rest_path: + return children + if len(children) == 1 and not children[0].token: + dump_node(children[0], level, recursive=False) + return find_root(children[0].children, rest_path[1:], level=level + 1) + p = rest_path[0] + for cc in children: + if cc.token == p: + dump_node(cc, level, recursive=False) + return find_root(cc.children, rest_path[1:], level=level + 1) + from noc.core.confdb.syntax.base import SYNTAX - for c in SYNTAX: - dump_node(c) + root = find_root(SYNTAX, path) + if not root: + return + for c in root: + dump_node(c, level=len(path) if path else 0) if __name__ == "__main__": diff --git a/docs/src/en/man/confdb.rst b/docs/src/en/man/confdb.rst index b231d6d745..2adf008cd6 100644 --- a/docs/src/en/man/confdb.rst +++ b/docs/src/en/man/confdb.rst @@ -17,7 +17,7 @@ Name Synopsis -------- - noc confdb syntax + noc confdb syntax [path ...] Description ----------- -- GitLab