Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit cab4ff5

Browse files
committed
refactor subcommand parser
1 parent 04b589f commit cab4ff5

5 files changed

Lines changed: 28 additions & 27 deletions

File tree

netnir/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
netnir will create the default config and folders.
44
"""
55

6-
__version__ = "0.0.15"
6+
__version__ = "0.0.16"

netnir/cli.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,15 @@ def __init__(self):
1919
A class object used to setup the netnir cli, consume the available commands from plugins,
2020
display the available commands, and execute the available commands based on user input.
2121
"""
22+
from netnir.helpers import plugins_import
23+
2224
self.plugins = NETNIR_CONFIG["plugins"]
23-
self.loaded_plugins = dict()
2425
self.parser = MyParser(prog="netnir")
2526
self.parser.add_argument(
2627
"--version", default=False, action="store_true", help="display version"
2728
)
28-
2929
subparsers = self.parser.add_subparsers(title="netnir commands", dest="command")
30-
31-
for task_key, task in self.plugins.items():
32-
plugin = task["class"].split(".")[:-1]
33-
app = task["class"].split(".")[-1]
34-
cmdparser = subparsers.add_parser(
35-
task_key, help=task["description"], description=task["description"],
36-
)
37-
38-
try:
39-
plugin = getattr(__import__(".".join(plugin), fromlist=[app]), app)
40-
self.loaded_plugins.update({task_key: plugin})
41-
except ModuleNotFoundError:
42-
raise
43-
44-
plugin.parser(cmdparser)
30+
self.loaded_plugins = plugins_import(tasks=self.plugins, subparsers=subparsers)
4531

4632
self.args = self.parser.parse_args()
4733

netnir/core/tasks/fetch/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from netnir.core.tasks.fetch.config import FetchConfig
21
from netnir.helpers.scaffold.subcommand import SubCommandParser
32

43

@@ -14,7 +13,7 @@ class Fetch(SubCommandParser):
1413
title = "fetch commands"
1514
tasks = {
1615
"config": {
17-
"class": FetchConfig,
16+
"class": "netnir.core.tasks.fetch.config.FetchConfig",
1817
"description": "fetch current config from a network device",
1918
},
2019
}

netnir/helpers/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,19 @@ def netnir_config(config_file: str = "netnir.yaml"):
150150
logging.warning(message)
151151

152152
return yaml.load(open(config_file), Loader=yaml.SafeLoader)
153+
154+
155+
def plugins_import(tasks: dict, subparsers: object):
156+
loaded_plugins = dict()
157+
158+
for task_key, task in tasks.items():
159+
plugin = task["class"].split(".")[:-1]
160+
app = task["class"].split(".")[-1]
161+
cmdparser = subparsers.add_parser(
162+
task_key, help=task["description"], description=task["description"],
163+
)
164+
plugin = getattr(__import__(".".join(plugin), fromlist=[app]), app)
165+
loaded_plugins.update({task_key: plugin})
166+
plugin.parser(cmdparser)
167+
168+
return loaded_plugins

netnir/helpers/scaffold/subcommand.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ def parser(cls, parser):
3434
"""
3535
sub command parser.
3636
"""
37-
subparsers = parser.add_subparsers(title=cls.title, dest="command")
37+
from netnir.helpers import plugins_import
3838

39-
for task_key, task in cls.tasks.items():
40-
cmdparser = subparsers.add_parser(
41-
task_key, help=task["description"], description=task["description"],
42-
)
43-
task["class"].parser(cmdparser)
39+
subparsers = parser.add_subparsers(title=cls.title, dest="command")
40+
plugins_import(tasks=cls.tasks, subparsers=subparsers)
4441

4542
def run(self):
4643
"""
4744
execute the subcommand parser.
4845
"""
4946
command = self.args.command
50-
action_class = self.tasks[command]["class"]
47+
action = self.tasks[command]["class"]
48+
plugin = action.split(".")[:-1]
49+
app = action.split(".")[-1]
50+
action_class = getattr(__import__(".".join(plugin), fromlist=[app]), app)
5151
action = action_class(self.args)
5252
action.run()

0 commit comments

Comments
 (0)