6
6
import tempfile
7
7
from pathlib import Path
8
8
from types import ModuleType
9
- from typing import Any , Callable , Optional
9
+ from typing import Optional
10
10
11
11
import click
12
12
import inquirer
15
15
16
16
from warnet .constants import (
17
17
CONTAINER_TAG ,
18
+ ENABLED_TAG ,
18
19
MISSION_TAG ,
19
20
PLUGIN_YAML ,
20
- PLUGINS_LABEL ,
21
+ PLUGINS_TAG ,
21
22
USER_DIR_TAG ,
22
23
WARNET_USER_DIR_ENV_VAR ,
23
24
)
@@ -27,11 +28,10 @@ class PluginError(Exception):
27
28
pass
28
29
29
30
30
- hook_registry : set [Callable [..., Any ]] = set ()
31
31
imported_modules : dict [str , ModuleType ] = {}
32
32
33
33
34
- @click .group (name = PLUGINS_LABEL )
34
+ @click .group (name = PLUGINS_TAG )
35
35
def plugins ():
36
36
"""Control plugins"""
37
37
pass
@@ -67,24 +67,23 @@ def toggle(ctx, plugin: str):
67
67
f"{ str (name .stem ):<25} ◦ enabled: { active } " for name , active in plugin_list
68
68
]
69
69
70
- plugins_tag = "plugins"
71
70
try :
72
71
q = [
73
72
inquirer .List (
74
- name = plugins_tag ,
73
+ name = PLUGINS_TAG ,
75
74
message = "Toggle a plugin, or ctrl-c to cancel" ,
76
75
choices = formatted_list ,
77
76
)
78
77
]
79
78
selected = inquirer .prompt (q , theme = GreenPassion ())
80
- plugin = selected [plugins_tag ].split ("◦" )[0 ].strip ()
79
+ plugin = selected [PLUGINS_TAG ].split ("◦" )[0 ].strip ()
81
80
except TypeError :
82
81
# user cancels and `selected[plugins_tag] fails with TypeError
83
82
sys .exit (0 )
84
83
85
84
plugin_settings = read_yaml (plugin_dir / Path (plugin ) / PLUGIN_YAML )
86
85
updated_settings = copy .deepcopy (plugin_settings )
87
- updated_settings ["enabled" ] = not plugin_settings ["enabled" ]
86
+ updated_settings [ENABLED_TAG ] = not plugin_settings [ENABLED_TAG ]
88
87
write_yaml (updated_settings , plugin_dir / Path (plugin ) / Path (PLUGIN_YAML ))
89
88
90
89
@@ -107,7 +106,7 @@ def load_user_modules(path: Optional[Path] = None) -> bool:
107
106
for plugin_path in enabled_plugins :
108
107
for file in plugin_path .glob ("*.py" ):
109
108
if file .stem not in ("__init__" ):
110
- module_name = f"{ PLUGINS_LABEL } .{ file .stem } "
109
+ module_name = f"{ PLUGINS_TAG } .{ file .stem } "
111
110
spec = importlib .util .spec_from_file_location (module_name , file )
112
111
module = importlib .util .module_from_spec (spec )
113
112
imported_modules [module_name ] = module
@@ -124,7 +123,7 @@ def register_command(command):
124
123
"""Register a command to the CLI."""
125
124
from warnet .main import cli
126
125
127
- register = cli .commands .get (PLUGINS_LABEL )
126
+ register = cli .commands .get (PLUGINS_TAG )
128
127
register .add_command (command )
129
128
130
129
@@ -141,13 +140,13 @@ def get_plugins_directory_or(path: Optional[Path] = None) -> Optional[Path]:
141
140
"""
142
141
if path :
143
142
if path .is_dir ():
144
- return path / PLUGINS_LABEL
143
+ return path / PLUGINS_TAG
145
144
else :
146
145
click .secho (f"Not a directory: { path } " , fg = "red" )
147
146
148
147
user_dir = os .getenv (WARNET_USER_DIR_ENV_VAR )
149
148
150
- plugin_dir = Path (user_dir ) / PLUGINS_LABEL if user_dir else Path .cwd () / PLUGINS_LABEL
149
+ plugin_dir = Path (user_dir ) / PLUGINS_TAG if user_dir else Path .cwd () / PLUGINS_TAG
151
150
152
151
if plugin_dir and plugin_dir .is_dir ():
153
152
return plugin_dir
@@ -191,8 +190,8 @@ def write_yaml(yaml_dict: dict, path: Path) -> None:
191
190
def check_if_plugin_enabled (path : Path ) -> bool :
192
191
enabled = None
193
192
try :
194
- plugin_dict = read_yaml (path / Path ("plugin.yaml" ))
195
- enabled = plugin_dict .get ("enabled" )
193
+ plugin_dict = read_yaml (path / Path (PLUGIN_YAML ))
194
+ enabled = plugin_dict .get (ENABLED_TAG )
196
195
except PluginError as e :
197
196
click .secho (e )
198
197
@@ -207,7 +206,7 @@ def get_plugins_with_status(plugin_dir: Optional[Path] = None) -> list[tuple[Pat
207
206
for name in os .listdir (plugin_dir )
208
207
if os .path .isdir (os .path .join (plugin_dir , name ))
209
208
]
210
- plugins = [plugin_dir for plugin_dir in candidates if any (plugin_dir .glob ("plugin.yaml" ))]
209
+ plugins = [plugin_dir for plugin_dir in candidates if any (plugin_dir .glob (PLUGIN_YAML ))]
211
210
return [(plugin , check_if_plugin_enabled (plugin )) for plugin in plugins ]
212
211
213
212
0 commit comments