Files
edx-platform/openedx/core/lib/plugins.py
Andrés González 26b3a40133 INCR-108 (#20058)
* INCR-108: Run python-modernize on openedx/core/lib

* Fixed urlencode import

* Fixed diff quality warnings and six.string_types error

* Fixed pickle import

* Fixed iteritems import

* Fixed ungrouped imports

* Fixed six.moves import issues
2019-04-02 10:26:00 -04:00

48 lines
1.6 KiB
Python

"""
Adds support for first class plugins that can be added to the edX platform.
"""
from __future__ import absolute_import
from collections import OrderedDict
from stevedore.extension import ExtensionManager
from openedx.core.lib.cache_utils import process_cached
class PluginError(Exception):
"""
Base Exception for when an error was found regarding plugins.
"""
pass
class PluginManager(object):
"""
Base class that manages plugins for the edX platform.
"""
@classmethod
@process_cached
def get_available_plugins(cls, namespace=None):
"""
Returns a dict of all the plugins that have been made available through the platform.
"""
# Note: we're creating the extension manager lazily to ensure that the Python path
# has been correctly set up. Trying to create this statically will fail, unfortunately.
plugins = OrderedDict()
extension_manager = ExtensionManager(namespace=namespace or cls.NAMESPACE) # pylint: disable=no-member
for plugin_name in extension_manager.names():
plugins[plugin_name] = extension_manager[plugin_name].plugin
return plugins
@classmethod
def get_plugin(cls, name, namespace=None):
"""
Returns the plugin with the given name.
"""
plugins = cls.get_available_plugins(namespace)
if name not in plugins:
raise PluginError(u"No such plugin {name} for entry point {namespace}".format(
name=name,
namespace=namespace or cls.NAMESPACE, # pylint: disable=no-member
))
return plugins[name]