-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the ability to load plugins in config
- Loading branch information
Showing
7 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from dotbot.plugins.clean import Clean | ||
from dotbot.plugins.create import Create | ||
from dotbot.plugins.link import Link | ||
from dotbot.plugins.plugins import Plugins | ||
from dotbot.plugins.shell import Shell | ||
|
||
__all__ = ["Clean", "Create", "Link", "Shell"] | ||
__all__ = ["Clean", "Create", "Link", "Plugins", "Shell"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import glob | ||
import os | ||
from typing import Any | ||
|
||
from dotbot.plugin import Plugin | ||
from dotbot.util import module | ||
|
||
|
||
class Plugins(Plugin): | ||
""" | ||
Load plugins from a list of paths. | ||
""" | ||
|
||
_directive = "plugins" | ||
_has_shown_override_message = False | ||
|
||
def can_handle(self, directive: str) -> bool: | ||
return directive == self._directive | ||
|
||
def handle(self, directive: str, data: Any) -> bool: | ||
if directive != self._directive: | ||
raise ValueError(f"plugins cannot handle directive {directive}") | ||
return self._process_plugins(data) | ||
|
||
def _process_plugins(self, data: Any) -> bool: | ||
success = True | ||
plugin_paths = [] | ||
for item in data: | ||
self._log.lowinfo(f"Loading plugin from {item}") | ||
|
||
plugin_path_globs = glob.glob(os.path.join(item, "*.py")) | ||
if not plugin_path_globs: | ||
success = False | ||
self._log.warning(f"Failed to load plugin from {item}") | ||
else: | ||
for plugin_path in plugin_path_globs: | ||
plugin_paths.append(plugin_path) | ||
|
||
for plugin_path in plugin_paths: | ||
abspath = os.path.abspath(plugin_path) | ||
module.load(abspath) | ||
|
||
if success: | ||
self._log.info("All commands have been executed") | ||
else: | ||
self._log.error("Some commands were not successfully executed") | ||
return success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Test that a plugin can be loaded by config file. | ||
This file is copied to a location with the name "config_file.py", | ||
and is then loaded from within the `test_cli.py` code. | ||
""" | ||
|
||
import os.path | ||
|
||
import dotbot | ||
|
||
|
||
class ConfigFile(dotbot.Plugin): | ||
def can_handle(self, directive): | ||
return directive == "plugin_config_file" | ||
|
||
def handle(self, directive, data): | ||
with open(os.path.abspath(os.path.expanduser("~/flag")), "w") as file: | ||
file.write("config file plugin loading works") | ||
return True |