Skip to content

Commit 679561f

Browse files
Improve maintainability of arg_helpers.py (#745)
1 parent 6ff1828 commit 679561f

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

linodecli/arg_helpers.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#!/usr/local/bin/python3
22
"""
3-
Argument parser for the linode CLI
3+
Argument parser for the linode CLI.
4+
This module defines argument parsing, plugin registration, and plugin removal
5+
functionalities for the Linode CLI.
46
"""
57
import sys
8+
from argparse import ArgumentParser
9+
from configparser import ConfigParser
610
from importlib import import_module
11+
from typing import Dict, Tuple
712

813
from linodecli import plugins
914
from linodecli.helpers import (
@@ -14,9 +19,13 @@
1419
from linodecli.output.helpers import register_output_args_shared
1520

1621

17-
def register_args(parser):
22+
def register_args(parser: ArgumentParser) -> ArgumentParser:
1823
"""
19-
Register static command arguments
24+
Register static command arguments for the Linode CLI.
25+
26+
:param parser: Argument parser object to which arguments will be added.
27+
28+
:return: The updated ArgumentParser instance.
2029
"""
2130
parser.add_argument(
2231
"command",
@@ -58,6 +67,7 @@ def register_args(parser):
5867
help="Prints version information and exits.",
5968
)
6069

70+
# Register shared argument groups
6171
register_output_args_shared(parser)
6272
register_pagination_args_shared(parser)
6373
register_args_shared(parser)
@@ -67,38 +77,51 @@ def register_args(parser):
6777

6878

6979
# TODO: maybe move to plugins/__init__.py
70-
def register_plugin(module, config, ops):
80+
def register_plugin(
81+
module: str, config: ConfigParser, ops: Dict[str, str]
82+
) -> Tuple[str, int]:
7183
"""
72-
Handle registering a plugin
73-
Registering sets up the plugin for all CLI users
84+
Handle registering a plugin for the Linode CLI.
85+
86+
:param module: The name of the module to be registered as a plugin.
87+
:param config: Configuration parser object.
88+
:param ops: Dictionary of existing CLI operations.
89+
90+
:return: A tuple containing a message and an exit code.
7491
"""
75-
# attempt to import the module to prove it is installed and exists
92+
93+
# Attempt to import the module to prove it is installed and exists
7694
try:
7795
plugin = import_module(module)
7896
except ImportError:
7997
return f"Module {module} not installed", 10
8098

99+
# Ensure the module defines a PLUGIN_NAME attribute
81100
try:
82101
plugin_name = plugin.PLUGIN_NAME
83102
except AttributeError:
84103
msg = f"{module} is not a valid Linode CLI plugin - missing PLUGIN_NAME"
85104
return msg, 11
86105

106+
# Ensure the module has a 'call' function, which is required for execution
87107
try:
88108
call_func = plugin.call
89-
del call_func
109+
del call_func # Just checking if it exists, so we can discard it
90110
except AttributeError:
91111
msg = f"{module} is not a valid Linode CLI plugin - missing call"
92112
return msg, 11
93113

114+
# Check if the plugin name conflicts with existing CLI operations
94115
if plugin_name in ops:
95116
msg = "Plugin name conflicts with CLI operation - registration failed."
96117
return msg, 12
97118

119+
# Check if the plugin name conflicts with an internal CLI plugin
98120
if plugin_name in plugins.AVAILABLE_LOCAL:
99121
msg = "Plugin name conflicts with internal CLI plugin - registration failed."
100122
return msg, 13
101123

124+
# Check if the plugin is already registered and ask for re-registration if needed
102125
reregistering = False
103126
if plugin_name in plugins.available(config):
104127
print(
@@ -110,20 +133,26 @@ def register_plugin(module, config, ops):
110133
return "Registration aborted.", 0
111134
reregistering = True
112135

136+
# Retrieve the list of already registered plugins from the config
113137
already_registered = []
114138
if config.config.has_option("DEFAULT", "registered-plugins"):
115139
already_registered = config.config.get(
116140
"DEFAULT", "registered-plugins"
117141
).split(",")
118142

143+
# If re-registering, remove the existing entry before adding it again
119144
if reregistering:
120145
already_registered.remove(plugin_name)
121146
config.config.remove_option("DEFAULT", f"plugin-name-{plugin_name}")
122147

148+
# Add the new plugin to the registered list
123149
already_registered.append(plugin_name)
124150
config.config.set(
125151
"DEFAULT", "registered-plugins", ",".join(already_registered)
126152
)
153+
154+
# Store the module name associated with this plugin in the config
155+
# and save the updated config to persist changes
127156
config.config.set("DEFAULT", f"plugin-name-{plugin_name}", module)
128157
config.write_config()
129158

@@ -136,19 +165,27 @@ def register_plugin(module, config, ops):
136165

137166

138167
# TODO: also maybe move to plugins
139-
def remove_plugin(plugin_name, config):
168+
def remove_plugin(plugin_name: str, config: ConfigParser) -> Tuple[str, int]:
140169
"""
141-
Remove a plugin
170+
Remove a registered plugin from the Linode CLI.
171+
172+
:param plugin_name: The name of the plugin to remove.
173+
:param config: Configuration parser object that manages CLI settings.
174+
175+
:return: A tuple containing a message and an exit code.
142176
"""
177+
178+
# Check if the plugin is a built-in CLI plugin that cannot be removed
143179
if plugin_name in plugins.AVAILABLE_LOCAL:
144180
msg = f"{plugin_name} is bundled with the CLI and cannot be removed"
145181
return msg, 13
146182

183+
# Check if the plugin is actually registered before attempting removal
147184
if plugin_name not in plugins.available(config):
148185
msg = f"{plugin_name} is not a registered plugin"
149186
return msg, 14
150187

151-
# do the removal
188+
# Do the removal
152189
current_plugins = config.config.get("DEFAULT", "registered-plugins").split(
153190
","
154191
)
@@ -157,7 +194,7 @@ def remove_plugin(plugin_name, config):
157194
"DEFAULT", "registered-plugins", ",".join(current_plugins)
158195
)
159196

160-
# if the config if malformed, don't blow up
197+
# If the config is malformed, don't blow up
161198
if config.config.has_option("DEFAULT", f"plugin-name-{plugin_name}"):
162199
config.config.remove_option("DEFAULT", f"plugin-name-{plugin_name}")
163200

0 commit comments

Comments
 (0)