diff options
author | Christian Geier <geier@lostpackets.de> | 2023-10-28 17:26:54 +0200 |
---|---|---|
committer | Christian Geier <geier@lostpackets.de> | 2023-10-28 17:26:54 +0200 |
commit | 05f11b3328ce92688e326124447a7223901fc5f4 (patch) | |
tree | 3b0e9c5f44e5d7c0bd57957e600f2274a6e534ff | |
parent | 4e7b398cf0f632f6e3765d2d8d8f70d72fa6771c (diff) |
support for plugins implementing new khal commands
-rw-r--r-- | khal/api.py | 9 | ||||
-rw-r--r-- | khal/cli.py | 18 |
2 files changed, 24 insertions, 3 deletions
diff --git a/khal/api.py b/khal/api.py index 1db9595e..84587684 100644 --- a/khal/api.py +++ b/khal/api.py @@ -1,3 +1,10 @@ +from typing import Callable, Dict + from .ui.colors import register_color_theme -__all__ = ["register_color_theme"] +_plugin_commands: Dict[str, Callable] = {} + +def register_command(name: str, command: Callable): + _plugin_commands[name] = command + +__all__ = ["register_color_theme", "register_command"] diff --git a/khal/cli.py b/khal/cli.py index b040eac1..3a60dfae 100644 --- a/khal/cli.py +++ b/khal/cli.py @@ -36,6 +36,7 @@ from .exceptions import FatalError from .settings import InvalidSettingsError, NoConfigFile, get_config from .terminal import colored from .utils import human_formatter, json_formatter +from .api import _plugin_commands try: from setproctitle import setproctitle @@ -236,7 +237,19 @@ def stringify_conf(conf): return '\n'.join(out) -@click.group() +class _KhalGroup(click.Group): + def list_commands(self, ctx): + return super().list_commands(ctx) + list(_plugin_commands.keys()) + + def get_command(self, ctx, name): + if name in _plugin_commands: + print(f'found command {name} as a plugin') + return _plugin_commands[name] + return super().get_command(ctx, name) + + + +@click.group(cls=_KhalGroup) @click_log.simple_verbosity_option('khal') @global_options @click.pass_context @@ -723,8 +736,9 @@ def find_load_plugins(): sys.path.append(plugin_dir) for plugin in os.listdir(plugin_dir): if os.path.isfile(os.path.join(plugin_dir, plugin, '__init__.py')): - logger.debug(f'loading plugin {plugin}') + logger.debug(f'loading plugin {plugin} at {plugin_dir}') __import__(plugin) +find_load_plugins() main_khal, main_ikhal = cli, interactive_cli |