Skip to content

Commit

Permalink
Merge pull request #2496 from CyclingNinja/add_listplugins
Browse files Browse the repository at this point in the history
Add list plugins method and tests
  • Loading branch information
astrofrog authored Jan 10, 2025
2 parents ada1532 + f06618f commit d59f708
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion glue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .config import load_configuration
env = load_configuration()

from .main import load_plugins # noqa
from .main import load_plugins, list_loaded_plugins, list_available_plugins # noqa


def test(no_optional_skip=False):
Expand Down
18 changes: 17 additions & 1 deletion glue/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def load_plugins(splash=None, require_qt_plugins=False, plugins_to_load=None):
n_plugins = len(plugins_to_require)

for i_plugin, item in enumerate(list(iter_plugin_entry_points())):

if item.module in plugins_to_load:
if item.module not in _installed_plugins:
_installed_plugins.add(item.name)
Expand Down Expand Up @@ -101,3 +100,20 @@ def load_plugins(splash=None, require_qt_plugins=False, plugins_to_load=None):
# that were previously read.
from glue._settings_helpers import load_settings
load_settings()


def list_loaded_plugins():
"""
Function to list all plugins that are currently loaded
"""
return sorted(_loaded_plugins)


def list_available_plugins():
"""
Function to list all available plugins
"""
from glue._plugin_helpers import iter_plugin_entry_points

plugins_load_list = [i.module for i in list(iter_plugin_entry_points())]
return plugins_load_list
50 changes: 49 additions & 1 deletion glue/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
from unittest.mock import patch

from glue.main import load_plugins
from glue.main import load_plugins, list_loaded_plugins, list_available_plugins
from glue._plugin_helpers import REQUIRED_PLUGINS


def test_load_plugins(capsys):
"""
Test customisable list of plugins load
"""
from glue.logger import logger

with patch.object(logger, 'info') as info:
load_plugins()

# plugins = [call[0][0] for call in info.call_args_list if ('succeeded' or 'loaded') in call[0][0]]
plugins = []
for acall in info.call_args_list:
if ('loaded' or 'succeeded') in acall[0][0]:
plugins.append(acall[0][0].split(' ')[1])

assert 'coordinate_helpers' in plugins


def test_no_duplicate_loading(capsys):
Expand All @@ -15,6 +34,35 @@ def test_no_duplicate_loading(capsys):
with patch.object(logger, 'info') as info:
load_plugins()

plugins = []
for acall in info.call_args_list:
plugins.append(acall[0][0])
if 'Loading plugin' in acall[0][0]:
assert 'failed' in acall[0][0]

loaded_plugins = list_loaded_plugins()
assert 'glue.plugins.wcs_autolinking' in loaded_plugins
assert 'glue.core.data_exporters' in loaded_plugins
assert 'glue.plugins.coordinate_helpers' in loaded_plugins


def test_list_loaded_plugins():
"""
Unit test for retrieving the list of currently loaded plugins
"""
load_plugins(require_qt_plugins=False)
plugins = list_loaded_plugins()
assert isinstance(plugins, list)
for test_plugin in REQUIRED_PLUGINS:
assert test_plugin in plugins


def test_list_available_plugins():
"""
Unit test for retrieving the list of currently available plugins
"""
available_plugins = list_available_plugins()
assert isinstance(available_plugins, list)
assert 'glue.plugins.wcs_autolinking' in available_plugins
assert 'glue.core.data_exporters' in available_plugins
assert 'glue.plugins.coordinate_helpers' in available_plugins

0 comments on commit d59f708

Please sign in to comment.