From 411adf73605f427ebbc5db363f481b2e19574cee Mon Sep 17 00:00:00 2001 From: CyclingNinja Date: Thu, 28 Mar 2024 17:18:53 +0000 Subject: [PATCH 1/2] Adds custom list of plugins to be passed on load --- glue/_plugin_helpers.py | 13 +++++++++++ glue/main.py | 51 ++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/glue/_plugin_helpers.py b/glue/_plugin_helpers.py index 52820b6c7..26c92a099 100644 --- a/glue/_plugin_helpers.py +++ b/glue/_plugin_helpers.py @@ -16,6 +16,19 @@ from importlib_metadata import entry_points +REQUIRED_PLUGINS = ['glue.plugins.coordinate_helpers', + 'glue.core.data_exporters', + 'glue.io.formats.fits'] + + +REQUIRED_PLUGINS_QT = ['glue_qt.plugins.tools.pv_slicer', + 'glue_qt.viewers.image', + 'glue_qt.viewers.scatter', + 'glue_qt.viewers.histogram', + 'glue_qt.viewers.profile', + 'glue_qt.viewers.table'] + + def iter_plugin_entry_points(): return iter(entry_points(group='glue.plugins')) diff --git a/glue/main.py b/glue/main.py index dc566e1cf..3a03a240b 100755 --- a/glue/main.py +++ b/glue/main.py @@ -3,25 +3,29 @@ from importlib import import_module from glue.logger import logger +from glue._plugin_helpers import REQUIRED_PLUGINS, REQUIRED_PLUGINS_QT _loaded_plugins = set() _installed_plugins = set() -REQUIRED_PLUGINS = ['glue.plugins.coordinate_helpers', - 'glue.core.data_exporters', - 'glue.io.formats.fits'] +def load_plugins(splash=None, require_qt_plugins=False, plugins_to_use=None): + """ -REQUIRED_PLUGINS_QT = ['glue.plugins.tools.pv_slicer.qt', - 'glue.viewers.image.qt', - 'glue.viewers.scatter.qt', - 'glue.viewers.histogram.qt', - 'glue.viewers.profile.qt', - 'glue.viewers.table.qt'] + Parameters + ---------- + splash : default: None + instance of splash http rendering service + require_qt_plugins : boolean default: False + whether to use qt plugins defined in constant REQUIRED_PLUGINS_QT + plugins_to_use : list + desired valid plugin strings + Returns + ------- -def load_plugins(splash=None, require_qt_plugins=False): + """ # Search for plugins installed via entry_points. Basically, any package can # define plugins for glue, and needs to define an entry point using the @@ -45,17 +49,26 @@ def load_plugins(splash=None, require_qt_plugins=False): config = PluginConfig.load() n_plugins = len(list(iter_plugin_entry_points())) + if plugins_to_use is None: + if require_qt_plugins: + plugins_to_use = [*REQUIRED_PLUGINS, *REQUIRED_PLUGINS_QT] + else: + plugins_to_use = REQUIRED_PLUGINS + else: + n_plugins = len(plugins_to_use) - for iplugin, item in enumerate(iter_plugin_entry_points()): - if item.module not in _installed_plugins: - _installed_plugins.add(item.name) + for i_plugins, item in enumerate(list(iter_plugin_entry_points())): + if item.value.replace(':setup', '') in plugins_to_use: + if item.module not in _installed_plugins: + _installed_plugins.add(item.name) - if item.module in _loaded_plugins: - logger.info("Plugin {0} already loaded".format(item.name)) - continue + if item.module in _loaded_plugins: + logger.info("Plugin {0} already loaded".format(item.name)) + continue - if not config.plugins[item.name]: - continue + # loads all plugins, want to make this more customisable + if not config.plugins[item.name]: + continue # We don't use item.load() because that then checks requirements of all # the imported packages, which can lead to errors like this one that @@ -68,7 +81,7 @@ def load_plugins(splash=None, require_qt_plugins=False): # old version of a package in the environment, but this can confuse # users as importing astropy directly would work (as setuptools then # doesn't do a stringent test of dependency versions). Often this kind - # of error can occur if there is a conda version of a package and and + # of error can occur if there is a conda version of a package and an # older pip version. try: From 0a975cfee5c9f9c9012388c578612006e4f3b6d2 Mon Sep 17 00:00:00 2001 From: CyclingNinja Date: Thu, 4 Apr 2024 21:32:01 +0100 Subject: [PATCH 2/2] Refactor to plugins_to_require / load --- glue/main.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/glue/main.py b/glue/main.py index b47d15a2c..37b1013c3 100755 --- a/glue/main.py +++ b/glue/main.py @@ -10,7 +10,7 @@ _installed_plugins = set() -def load_plugins(splash=None, require_qt_plugins=False, plugins_to_use=None): +def load_plugins(splash=None, require_qt_plugins=False, plugins_to_load=None): """ Parameters @@ -19,7 +19,7 @@ def load_plugins(splash=None, require_qt_plugins=False, plugins_to_use=None): instance of splash http rendering service require_qt_plugins : boolean default: False whether to use qt plugins defined in constant REQUIRED_PLUGINS_QT - plugins_to_use : list + plugins_to_load : list desired valid plugin strings Returns @@ -48,17 +48,18 @@ def load_plugins(splash=None, require_qt_plugins=False, plugins_to_use=None): from glue._plugin_helpers import iter_plugin_entry_points, PluginConfig config = PluginConfig.load() - n_plugins = len(list(iter_plugin_entry_points())) - if plugins_to_use is None: + if plugins_to_load is None: + plugins_to_load = [i.value for i in list(iter_plugin_entry_points())] if require_qt_plugins: - plugins_to_use = [*REQUIRED_PLUGINS, *REQUIRED_PLUGINS_QT] + plugins_to_require = [*REQUIRED_PLUGINS, *REQUIRED_PLUGINS_QT] else: - plugins_to_use = REQUIRED_PLUGINS + plugins_to_require = REQUIRED_PLUGINS else: - n_plugins = len(plugins_to_use) + plugins_to_require = plugins_to_load + n_plugins = len(plugins_to_require) for i_plugin, item in enumerate(list(iter_plugin_entry_points())): - if item.value.replace(':setup', '') in plugins_to_use: + if item.value.replace(':setup', '') in plugins_to_load: if item.module not in _installed_plugins: _installed_plugins.add(item.name) @@ -91,9 +92,7 @@ def load_plugins(splash=None, require_qt_plugins=False, plugins_to_use=None): except Exception as exc: # Here we check that some of the 'core' plugins load well and # raise an actual exception if not. - if item.module in REQUIRED_PLUGINS: - raise - elif item.module in REQUIRED_PLUGINS_QT and require_qt_plugins: + if item.module in plugins_to_require: raise else: logger.info("Loading plugin {0} failed "