Skip to content

Commit b60f4d8

Browse files
committed
add plugin blacklist
1 parent fbefe73 commit b60f4d8

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/hhd/__main__.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
save_profile_yaml,
3131
save_state_yaml,
3232
validate_config,
33+
load_blacklist_yaml,
34+
save_blacklist_yaml,
3335
)
3436
from .utils import expanduser, fix_perms, get_context, switch_priviledge
3537

@@ -142,6 +144,14 @@ def main():
142144
except Exception:
143145
pass
144146

147+
# Remove old dir
148+
try:
149+
os.rename(
150+
join(hhd_dir, "plugins"), join(hhd_dir, "plugins_old_USE_STATEYML")
151+
)
152+
except Exception:
153+
pass
154+
145155
set_log_plugin("main")
146156
setup_logger(join(CONFIG_DIR, "log"), ctx=ctx)
147157

@@ -152,12 +162,27 @@ def main():
152162
else:
153163
logger.error(f"Command '{args.command[0]}' is unknown. Ignoring...")
154164

165+
# Use blacklist
166+
blacklist_fn = join(hhd_dir, "plugins.yml")
167+
blacklist = load_blacklist_yaml(blacklist_fn)
168+
169+
logger.info(f"Running autodetection...")
170+
171+
detector_names = []
155172
for autodetect in pkg_resources.iter_entry_points("hhd.plugins"):
156-
detectors[autodetect.name] = autodetect.resolve()
173+
name = autodetect.name
174+
detector_names.append(name)
175+
if name in blacklist:
176+
logger.info(f"Skipping blacklisted provider '{name}'.")
177+
else:
178+
detectors[autodetect.name] = autodetect.resolve()
179+
180+
# Save new blacklist file
181+
save_blacklist_yaml(blacklist_fn, detector_names, blacklist)
182+
fix_perms(blacklist_fn, ctx)
157183

158184
logger.info(f"Found plugin providers: {', '.join(list(detectors))}")
159185

160-
logger.info(f"Running autodetection...")
161186
for name, autodetect in detectors.items():
162187
plugins[name] = autodetect([])
163188

src/hhd/plugins/settings.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,40 @@ def save_state_yaml(fn: str, set: HHDSettings, conf: Config):
487487
return True
488488

489489

490+
def save_blacklist_yaml(fn: str, avail: Sequence[str], blacklist: Sequence[str]):
491+
import yaml
492+
493+
with open(fn, "w") as f:
494+
f.write(
495+
(
496+
""
497+
+ "# \n"
498+
+ "# Plugin blacklist\n"
499+
+ "# The plugin providers under blacklist will not run.\n"
500+
+ "# \n"
501+
+ "# Warning: this file is read only on startup.\n"
502+
+ "# `sudo systemctl restart hhd@$(whoami)`\n"
503+
+ "# \n"
504+
+ "# Available providers:\n"
505+
+ f"# [{', '.join(avail)}]\n\n"
506+
)
507+
)
508+
yaml.safe_dump({"blacklist": blacklist}, f, width=85, sort_keys=False)
509+
510+
return True
511+
512+
513+
def load_blacklist_yaml(fn: str):
514+
import yaml
515+
516+
try:
517+
with open(fn, "r") as f:
518+
return yaml.safe_load(f)["blacklist"]
519+
except Exception as e:
520+
logger.warning(f"Plugin blacklist not found, using default (empty).")
521+
return ["myplugin1"]
522+
523+
490524
def save_profile_yaml(fn: str, set: HHDSettings, conf: Config | None = None):
491525
import yaml
492526

0 commit comments

Comments
 (0)