66import tempfile
77from pathlib import Path
88from types import ModuleType
9- from typing import Any , Callable , Optional
9+ from typing import Optional
1010
1111import click
1212import inquirer
1515
1616from warnet .constants import (
1717 CONTAINER_TAG ,
18+ ENABLED_TAG ,
1819 MISSION_TAG ,
1920 PLUGIN_YAML ,
20- PLUGINS_LABEL ,
21+ PLUGINS_TAG ,
2122 USER_DIR_TAG ,
2223 WARNET_USER_DIR_ENV_VAR ,
2324)
@@ -27,11 +28,10 @@ class PluginError(Exception):
2728 pass
2829
2930
30- hook_registry : set [Callable [..., Any ]] = set ()
3131imported_modules : dict [str , ModuleType ] = {}
3232
3333
34- @click .group (name = PLUGINS_LABEL )
34+ @click .group (name = PLUGINS_TAG )
3535def plugins ():
3636 """Control plugins"""
3737 pass
@@ -67,24 +67,23 @@ def toggle(ctx, plugin: str):
6767 f"{ str (name .stem ):<25} ◦ enabled: { active } " for name , active in plugin_list
6868 ]
6969
70- plugins_tag = "plugins"
7170 try :
7271 q = [
7372 inquirer .List (
74- name = plugins_tag ,
73+ name = PLUGINS_TAG ,
7574 message = "Toggle a plugin, or ctrl-c to cancel" ,
7675 choices = formatted_list ,
7776 )
7877 ]
7978 selected = inquirer .prompt (q , theme = GreenPassion ())
80- plugin = selected [plugins_tag ].split ("◦" )[0 ].strip ()
79+ plugin = selected [PLUGINS_TAG ].split ("◦" )[0 ].strip ()
8180 except TypeError :
8281 # user cancels and `selected[plugins_tag] fails with TypeError
8382 sys .exit (0 )
8483
8584 plugin_settings = read_yaml (plugin_dir / Path (plugin ) / PLUGIN_YAML )
8685 updated_settings = copy .deepcopy (plugin_settings )
87- updated_settings ["enabled" ] = not plugin_settings ["enabled" ]
86+ updated_settings [ENABLED_TAG ] = not plugin_settings [ENABLED_TAG ]
8887 write_yaml (updated_settings , plugin_dir / Path (plugin ) / Path (PLUGIN_YAML ))
8988
9089
@@ -107,7 +106,7 @@ def load_user_modules(path: Optional[Path] = None) -> bool:
107106 for plugin_path in enabled_plugins :
108107 for file in plugin_path .glob ("*.py" ):
109108 if file .stem not in ("__init__" ):
110- module_name = f"{ PLUGINS_LABEL } .{ file .stem } "
109+ module_name = f"{ PLUGINS_TAG } .{ file .stem } "
111110 spec = importlib .util .spec_from_file_location (module_name , file )
112111 module = importlib .util .module_from_spec (spec )
113112 imported_modules [module_name ] = module
@@ -124,7 +123,7 @@ def register_command(command):
124123 """Register a command to the CLI."""
125124 from warnet .main import cli
126125
127- register = cli .commands .get (PLUGINS_LABEL )
126+ register = cli .commands .get (PLUGINS_TAG )
128127 register .add_command (command )
129128
130129
@@ -141,13 +140,13 @@ def get_plugins_directory_or(path: Optional[Path] = None) -> Optional[Path]:
141140 """
142141 if path :
143142 if path .is_dir ():
144- return path / PLUGINS_LABEL
143+ return path / PLUGINS_TAG
145144 else :
146145 click .secho (f"Not a directory: { path } " , fg = "red" )
147146
148147 user_dir = os .getenv (WARNET_USER_DIR_ENV_VAR )
149148
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
151150
152151 if plugin_dir and plugin_dir .is_dir ():
153152 return plugin_dir
@@ -191,8 +190,8 @@ def write_yaml(yaml_dict: dict, path: Path) -> None:
191190def check_if_plugin_enabled (path : Path ) -> bool :
192191 enabled = None
193192 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 )
196195 except PluginError as e :
197196 click .secho (e )
198197
@@ -207,7 +206,7 @@ def get_plugins_with_status(plugin_dir: Optional[Path] = None) -> list[tuple[Pat
207206 for name in os .listdir (plugin_dir )
208207 if os .path .isdir (os .path .join (plugin_dir , name ))
209208 ]
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 ))]
211210 return [(plugin , check_if_plugin_enabled (plugin )) for plugin in plugins ]
212211
213212
0 commit comments