Skip to content

Commit 46f59db

Browse files
committed
clean up tags and names
1 parent 3b8906f commit 46f59db

File tree

3 files changed

+17
-58
lines changed

3 files changed

+17
-58
lines changed

src/warnet/constants.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343

4444
# Plugin architecture
4545
USER_DIR_TAG = "user_dir"
46-
PLUGINS_LABEL = "plugins"
46+
PLUGINS_TAG = "plugins"
47+
ENABLED_TAG = "enabled"
4748
PLUGIN_YAML = "plugin.yaml"
48-
PLUGINS_DIR = RESOURCES_DIR.joinpath(PLUGINS_LABEL)
49+
PLUGINS_DIR = RESOURCES_DIR.joinpath(PLUGINS_TAG)
4950
WARNET_USER_DIR_ENV_VAR = "WARNET_USER_DIR"
5051

5152
# Helm charts

src/warnet/plugins.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import tempfile
77
from pathlib import Path
88
from types import ModuleType
9-
from typing import Any, Callable, Optional
9+
from typing import Optional
1010

1111
import click
1212
import inquirer
@@ -15,9 +15,10 @@
1515

1616
from 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()
3131
imported_modules: dict[str, ModuleType] = {}
3232

3333

34-
@click.group(name=PLUGINS_LABEL)
34+
@click.group(name=PLUGINS_TAG)
3535
def 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:
191190
def 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

test/hooks_test.py

-41
This file was deleted.

0 commit comments

Comments
 (0)