Skip to content

Commit

Permalink
Add basic plugin support (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
JrGoodle committed Oct 2, 2020
1 parent b6f2923 commit 45129bc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
24 changes: 24 additions & 0 deletions .idea/runConfigurations/clowder_debug.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions clowder/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from clowder.cli.herd import add_herd_parser
from clowder.cli.init import add_init_parser
from clowder.cli.link import add_link_parser
from clowder.cli.plugins import add_plugins_parser
from clowder.cli.prune import add_prune_parser
from clowder.cli.repo import add_repo_parser
from clowder.cli.reset import add_reset_parser
Expand Down
64 changes: 64 additions & 0 deletions clowder/cli/plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
"""Clowder command line plugins controller
.. codeauthor:: Joe Decapo <joe@polka.cat>
"""

import argparse
import os
import importlib.util

from clowder.environment import ENVIRONMENT
from clowder.util.decorators import clowder_repo_required, valid_clowder_yaml_required

from .util import add_parser_arguments


def add_plugins_parser(subparsers: argparse._SubParsersAction) -> None: # noqa
"""Add clowder plugins parser
:param argparse._SubParsersAction subparsers: Subparsers action to add parser to
"""

plugins_dir = ENVIRONMENT.clowder_repo_plugins_dir
if plugins_dir is None:
return
dir_exists = plugins_dir.exists()
is_dir = plugins_dir.is_dir()
if not dir_exists or not is_dir:
return

files = os.listdir(plugins_dir)
files = [ENVIRONMENT.clowder_repo_plugins_dir / f for f in files]
plugins = [f for f in files if f.is_dir()]
# TODO: Check for .py file
if not plugins:
return

for plugin in plugins:
# TODO: Add way to define help
parser = subparsers.add_parser(plugin.name, help=f"Run {plugin.name} command")
# parser.formatter_class = argparse.RawTextHelpFormatter
add_parser_arguments(parser, [])
parser.set_defaults(func=run)


@clowder_repo_required
@valid_clowder_yaml_required
def run(args) -> None:
"""Clowder plugins command private implementation"""

plugins_dir = ENVIRONMENT.clowder_repo_plugins_dir

plugin_name = args.subcommand
module = f"{plugin_name}.{plugin_name}"
module_file = plugins_dir / plugin_name / f"{plugin_name}.py"

# module = importlib.import_module(f"{plugin_name}.{plugin_name}")
# module.main() # noqa

spec = importlib.util.spec_from_file_location(module, module_file)
plugin = importlib.util.module_from_spec(spec)
spec.loader.exec_module(plugin) # noqa
plugin.main() # noqa
3 changes: 2 additions & 1 deletion clowder/clowder_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create_parsers() -> argparse.ArgumentParser:
(['-v', '--version'], dict(action='version', version=version_message))
]
cmd.add_parser_arguments(clowder_parser, arguments)
subparsers = clowder_parser.add_subparsers(help='sub-command help')
subparsers = clowder_parser.add_subparsers(dest='subcommand', help='sub-command help')

cmd.add_branch_parser(subparsers)
cmd.add_checkout_parser(subparsers)
Expand All @@ -68,6 +68,7 @@ def create_parsers() -> argparse.ArgumentParser:
cmd.add_herd_parser(subparsers)
cmd.add_init_parser(subparsers)
cmd.add_link_parser(subparsers)
cmd.add_plugins_parser(subparsers)
cmd.add_prune_parser(subparsers)
cmd.add_repo_parser(subparsers)
cmd.add_reset_parser(subparsers)
Expand Down
3 changes: 3 additions & 0 deletions clowder/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ClowderEnvironment(object):
:cvar Optional[Path] clowder_dir: Path to clowder directory if it exists
:cvar Optional[Path] clowder_repo_dir: Path to clowder repo directory if it exists
:cvar Optional[Path] clowder_repo_versaions_dir: Path to clowder repo versions directory
:cvar Optional[Path] clowder_repo_plugins_dir: Path to clowder repo plugins directory
:cvar Optional[Path] clowder_yaml: Path to clowder yaml file if it exists
:cvar Optional[ClowderError] clowder_yaml_missing_source_error: Possible error for broken clowder yaml symlink
:cvar Optional[ClowderError] ambiguous_clowder_yaml_error: Possible error due to ambiguous clowder yaml
Expand All @@ -35,6 +36,7 @@ class ClowderEnvironment(object):
clowder_repo_dir: Optional[Path] = None
clowder_git_repo_dir: Optional[Path] = None
clowder_repo_versions_dir: Optional[Path] = None
clowder_repo_plugins_dir: Optional[Path] = None
clowder_yaml: Optional[Path] = None

clowder_yaml_missing_source_error: Optional[ClowderError] = None
Expand Down Expand Up @@ -119,6 +121,7 @@ def _configure_directories(self) -> None:
self.clowder_repo_versions_dir: Optional[Path] = self.clowder_repo_dir / 'versions'
self.clowder_config_dir: Optional[Path] = self.clowder_repo_dir / "config"
self.clowder_config_yaml: Optional[Path] = self.clowder_config_dir / 'clowder.config.yml'
self.clowder_repo_plugins_dir: Optional[Path] = self.clowder_repo_dir / "plugins"

def _get_possible_yaml_path(self, name: str) -> Path:
"""Get possible yaml path based on other environment variables
Expand Down

0 comments on commit 45129bc

Please sign in to comment.