Skip to content

Commit 0318227

Browse files
committed
refactor for reusable get_installed_plugins
1 parent 4e74899 commit 0318227

3 files changed

Lines changed: 26 additions & 27 deletions

File tree

‎paths_cli/cli.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from .plugin_management import (FilePluginLoader, NamespacePluginLoader,
1717
OPSCommandPlugin)
18-
from .utils import app_dir_plugins
18+
from .utils import get_installed_plugins
1919

2020
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
2121

@@ -27,15 +27,10 @@ class OpenPathSamplingCLI(click.MultiCommand):
2727
def __init__(self, *args, **kwargs):
2828
# the logic here is all about loading the plugins
2929
commands = str(pathlib.Path(__file__).parent.resolve() / 'commands')
30-
self.plugin_loaders = [
31-
FilePluginLoader(commands, OPSCommandPlugin),
32-
FilePluginLoader(app_dir_plugins(posix=False), OPSCommandPlugin),
33-
FilePluginLoader(app_dir_plugins(posix=True), OPSCommandPlugin),
34-
NamespacePluginLoader('paths_cli_plugins', OPSCommandPlugin)
35-
]
36-
37-
plugins = sum([loader() for loader in self.plugin_loaders], [])
38-
30+
plugins = get_installed_plugins(
31+
default_loader=FilePluginLoader(commands, OPSCommandPlugin),
32+
plugin_types=OPSCommandPlugin
33+
)
3934
self._get_command = {}
4035
self._sections = collections.defaultdict(list)
4136
self.plugins = []

‎paths_cli/commands/compile.py‎

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
from paths_cli.compiling.plugins import (
88
CategoryPlugin, InstanceCompilerPlugin
99
)
10-
from paths_cli.plugin_management import (
11-
NamespacePluginLoader, FilePluginLoader
12-
)
10+
from paths_cli.plugin_management import NamespacePluginLoader
1311
import importlib
14-
from paths_cli.utils import app_dir_plugins
12+
from paths_cli.utils import get_installed_plugins
1513

1614
# this is just to handle a nicer error
1715
def import_module(module_name, format_type=None, install=None):
@@ -40,7 +38,7 @@ def load_json(f):
4038
# https://github.com/toml-lang/toml/issues/553#issuecomment-444814690
4139
# Conflicts with rules preventing mixed types in arrays. This seems to have
4240
# relaxed in TOML 1.0, but the toml package doesn't support the 1.0
43-
# standard. We'll add toml in once the pacakge supports the standard.
41+
# standard. We'll add toml in once the package supports the standard.
4442

4543
EXTENSIONS = {
4644
'yaml': load_yaml,
@@ -57,17 +55,6 @@ def select_loader(filename):
5755
except KeyError:
5856
raise RuntimeError(f"Unknown file extension: {ext}")
5957

60-
def load_plugins():
61-
plugin_types = (InstanceCompilerPlugin, CategoryPlugin)
62-
plugin_loaders = [
63-
NamespacePluginLoader('paths_cli.compiling', plugin_types),
64-
FilePluginLoader(app_dir_plugins(posix=False), plugin_types),
65-
FilePluginLoader(app_dir_plugins(posix=True), plugin_types),
66-
NamespacePluginLoader('paths_cli_plugins', plugin_types)
67-
]
68-
plugins = sum([loader() for loader in plugin_loaders], [])
69-
return plugins
70-
7158
@click.command(
7259
'compile',
7360
)
@@ -78,7 +65,12 @@ def compile_(input_file, output_file):
7865
with open(input_file, mode='r') as f:
7966
dct = loader(f)
8067

81-
plugins = load_plugins()
68+
plugin_types = (InstanceCompilerPlugin, CategoryPlugin)
69+
plugins = get_installed_plugins(
70+
default_loader=NamespacePluginLoader('paths_cli.compiling',
71+
plugin_types),
72+
plugin_types=plugin_types
73+
)
8274
register_plugins(plugins)
8375

8476
objs = do_compile(dct)

‎paths_cli/utils.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib
22
import pathlib
33
import click
4+
from .plugin_management import FilePluginLoader, NamespacePluginLoader
45

56
def tag_final_result(result, storage, tag='final_conditions'):
67
"""Save results to a tag in storage.
@@ -31,3 +32,14 @@ def app_dir_plugins(posix): # covered as smoke tests (too OS dependent)
3132
return str(pathlib.Path(
3233
click.get_app_dir("OpenPathSampling", force_posix=posix)
3334
).resolve() / 'cli-plugins')
35+
36+
37+
def get_installed_plugins(default_loader, plugin_types):
38+
loaders = [default_loader] + [
39+
FilePluginLoader(app_dir_plugins(posix=False), plugin_types),
40+
FilePluginLoader(app_dir_plugins(posix=True), plugin_types),
41+
NamespacePluginLoader('paths_cli_plugins', plugin_types)
42+
43+
]
44+
plugins = set(sum([loader() for loader in loaders], []))
45+
return list(plugins)

0 commit comments

Comments
 (0)