Commit f8594570 by Calen Pennington

Cache loaded plugins in memory

parent 520fac1a
...@@ -23,6 +23,9 @@ class Plugin(object): ...@@ -23,6 +23,9 @@ class Plugin(object):
entry_point: The name of the entry point to load plugins from entry_point: The name of the entry point to load plugins from
""" """
_plugin_cache = None
@classmethod @classmethod
def load_class(cls, identifier, default=None): def load_class(cls, identifier, default=None):
""" """
...@@ -33,6 +36,10 @@ class Plugin(object): ...@@ -33,6 +36,10 @@ class Plugin(object):
If default is not None, will return default if no entry_point matching identifier If default is not None, will return default if no entry_point matching identifier
is found. Otherwise, will raise a ModuleMissingError is found. Otherwise, will raise a ModuleMissingError
""" """
if cls._plugin_cache is None:
cls._plugin_cache = {}
if identifier not in cls._plugin_cache:
identifier = identifier.lower() identifier = identifier.lower()
classes = list(pkg_resources.iter_entry_points(cls.entry_point, name=identifier)) classes = list(pkg_resources.iter_entry_points(cls.entry_point, name=identifier))
if len(classes) > 1: if len(classes) > 1:
...@@ -46,7 +53,8 @@ class Plugin(object): ...@@ -46,7 +53,8 @@ class Plugin(object):
return default return default
raise ModuleMissingError(identifier) raise ModuleMissingError(identifier)
return classes[0].load() cls._plugin_cache[identifier] = classes[0].load()
return cls._plugin_cache[identifier]
@classmethod @classmethod
def load_classes(cls): def load_classes(cls):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment