plainbox.impl.secure.plugins
– interface for accessing extension points¶
This module contains plugin interface for plainbox. Plugins are based on pkg_resources entry points feature. Any python package can advertise the existence of entry points associated with a given namespace. Any other package can query a given namespace and enumerate a sequence of entry points.
Each entry point has a name and importable identifier. The identifier can
be imported using the load() method. A loaded entry point is exposed as an
instance of PlugIn
. A high-level collection of plugins (that may
eventually also query alternate backends) is offered by
PlugInCollection
.
Using PlugInCollection.load()
one can load all plugins from
a particular namespace and work with them using provided utility methods
such as PlugInCollection.get_by_name()
or
PlugInCollection.get_all_names()
The set of loaded plugins can be overridden by mock/patching
PlugInCollection._get_entry_points()
. This is especially useful for
testing in isolation from whatever entry points may exist in the system.
-
class
plainbox.impl.secure.plugins.
FsPlugInCollection
(dir_list, ext, recursive=False, load=False, wrapper=<class 'plainbox.impl.secure.plugins.PlugIn'>, *wrapper_args, **wrapper_kwargs)[source]¶ Bases:
plainbox.impl.secure.plugins.PlugInCollectionBase
Collection of plug-ins based on filesystem entries
Instantiate with
dir_list
andext
, callload()
and then access any of the loaded plug-ins using the API offered. All loaded plugin information files are wrapped by a plug-in container. By default that isPlugIn
but it may be adjusted if required.The name of each plugin is the base name of the plugin file, the object of each plugin is the text read from the plugin file.
-
discovery_time
¶ Time, in fractional seconds, that was required to discover all objects.
This time is separate from the load and wrap time of all each individual plug-in. Typically this is either a fixed cost or a predictable cost related to traversing the file system.
-
fake_plugins
(plugins, problem_list=None)¶ Context manager for using fake list of plugins
Parameters: - plugins – list of PlugIn-alike objects
- problem_list – list of problems (exceptions)
The provided list of plugins overrides any previously loaded plugins and prevent loading any other, real, plugins. After the context manager exits the previous state is restored.
-
get_all_items
()¶ Get a list of all the pairs of plugin name and plugin
Returns: a list of tuples (plugin.plugin_name, plugin)
-
get_all_names
()¶ Get a list of all the plug-in names
Returns: a list of plugin names
-
get_all_plugin_objects
()¶ Get an list of plug-in objects
-
get_all_plugins
()¶ Get a list of all the plug-ins
Returns: a list of plugin objects
-
get_by_name
(name)¶ Get the specified plug-in (by name)
Parameters: name – name of the plugin to locate Returns: PlugIn
like object associated with the nameRaises: KeyError – if the specified name cannot be found
-
get_total_time
() → float¶ Get the sum of load and wrap time of each plugin object
Returns: The total number of fractional seconds of wall-clock time spent by loading this collection. This value doesn’t include some small overhead of this class but is representative of the load times of pluggable code.
-
load
()[source]¶ Load all plug-ins.
This method loads all plug-ins from the search directories (as defined by the path attribute). Missing directories are silently ignored.
-
problem_list
¶ List of problems encountered while loading plugins
-
wrap_and_add_plugin
(plugin_name, plugin_obj, plugin_load_time)¶ Internal method of PlugInCollectionBase.
Parameters: - plugin_name – plugin name, some arbitrary string
- plugin_obj – plugin object, some arbitrary object.
- plugin_load_time – number of seconds it took to load this plugin
This method prepares a wrapper (PlugIn subclass instance) for the specified plugin name/object by attempting to instantiate the wrapper class. If a PlugInError exception is raised then it is added to the problem_list and the corresponding plugin is not added to the collection of plugins.
-
-
class
plainbox.impl.secure.plugins.
IPlugIn
[source]¶ Bases:
object
Piece of code loaded at runtime, one of many for a given extension point
-
plugin_load_time
¶ time, in fractional seconds, that was needed to load the plugin
-
plugin_name
¶ name of the plugin, may not be unique
-
plugin_object
¶ external object
-
plugin_wrap_time
¶ time, in fractional seconds, that was needed to wrap the plugin
Note
The difference between
plugin_wrap_time
andplugin_load_time
depends on context. In practical terms the sum of the two is interesting for analysis but in some cases having access to both may be important.
-
-
class
plainbox.impl.secure.plugins.
IPlugInCollection
[source]¶ Bases:
object
A collection of IPlugIn objects.
-
discovery_time
¶ Time, in fractional seconds, that was used to discover all objects.
This time is separate from the load and wrap time of all each individual plug-in. Typically this is either a fixed cost or a predictable cost related to traversing the file system.
-
fake_plugins
(plugins, problem_list=None)[source]¶ Context manager for using fake list of plugins
Parameters: - plugins – list of PlugIn-alike objects
- problem_list – list of problems (exceptions)
The provided list of plugins and exceptions overrides any previously loaded plugins and prevent loading any other, real, plugins. After the context manager exits the previous state is restored.
-
get_all_plugin_objects
()[source]¶ Get an list of plug-in objects
This is a shortcut that gives fastest access to a list of
IPlugIn.plugin_object
of each loaded plugin.
-
get_total_time
() → float[source]¶ Get the cost to prepare everything required by this collection
Returns: The total number of fractional seconds of wall-clock time spent on discovering, loading and wrapping each object now contained in this collection.
-
load
()[source]¶ Load all plug-ins.
This method loads all plug-ins from the specified name-space. It may perform a lot of IO so it’s somewhat slow / expensive on a cold disk cache.
-
problem_list
¶ List of problems encountered while loading plugins
-
-
class
plainbox.impl.secure.plugins.
LazyFileContent
(name)[source]¶ Bases:
object
Support class for FsPlugInCollection’s subclasses that behaves like a string of text loaded from a file. The actual text is loaded on demand, the first time it is needed.
The actual methods implemented here are just enough to work for loading a provider. Since __getattr__() is implemented the class should be pretty versatile but your millage may vary.
-
class
plainbox.impl.secure.plugins.
LazyFsPlugInCollection
(dir_list, ext, recursive=False, load=False, wrapper=<class 'plainbox.impl.secure.plugins.PlugIn'>, *wrapper_args, **wrapper_kwargs)[source]¶ Bases:
plainbox.impl.secure.plugins.FsPlugInCollection
Collection of plug-ins based on filesystem entries
Instantiate with
dir_list
andext
, callload()
and then access any of the loaded plug-ins using the API offered. All loaded plugin information files are wrapped by a plug-in container. By default that isPlugIn
but it may be adjusted if required.The name of each plugin is the base name of the plugin file, the object of each plugin is a handle that can be used to optionally load the content of the file.
-
discovery_time
¶ Time, in fractional seconds, that was required to discover all objects.
This time is separate from the load and wrap time of all each individual plug-in. Typically this is either a fixed cost or a predictable cost related to traversing the file system.
-
fake_plugins
(plugins, problem_list=None)¶ Context manager for using fake list of plugins
Parameters: - plugins – list of PlugIn-alike objects
- problem_list – list of problems (exceptions)
The provided list of plugins overrides any previously loaded plugins and prevent loading any other, real, plugins. After the context manager exits the previous state is restored.
-
get_all_items
()¶ Get a list of all the pairs of plugin name and plugin
Returns: a list of tuples (plugin.plugin_name, plugin)
-
get_all_names
()¶ Get a list of all the plug-in names
Returns: a list of plugin names
-
get_all_plugin_objects
()¶ Get an list of plug-in objects
-
get_all_plugins
()¶ Get a list of all the plug-ins
Returns: a list of plugin objects
-
get_by_name
(name)¶ Get the specified plug-in (by name)
Parameters: name – name of the plugin to locate Returns: PlugIn
like object associated with the nameRaises: KeyError – if the specified name cannot be found
-
get_total_time
() → float¶ Get the sum of load and wrap time of each plugin object
Returns: The total number of fractional seconds of wall-clock time spent by loading this collection. This value doesn’t include some small overhead of this class but is representative of the load times of pluggable code.
-
load
()¶ Load all plug-ins.
This method loads all plug-ins from the search directories (as defined by the path attribute). Missing directories are silently ignored.
-
problem_list
¶ List of problems encountered while loading plugins
-
wrap_and_add_plugin
(plugin_name, plugin_obj, plugin_load_time)¶ Internal method of PlugInCollectionBase.
Parameters: - plugin_name – plugin name, some arbitrary string
- plugin_obj – plugin object, some arbitrary object.
- plugin_load_time – number of seconds it took to load this plugin
This method prepares a wrapper (PlugIn subclass instance) for the specified plugin name/object by attempting to instantiate the wrapper class. If a PlugInError exception is raised then it is added to the problem_list and the corresponding plugin is not added to the collection of plugins.
-
-
class
plainbox.impl.secure.plugins.
LazyPlugInCollection
(mapping, load=False, wrapper=<class 'plainbox.impl.secure.plugins.PlugIn'>, *wrapper_args, **wrapper_kwargs)[source]¶ Bases:
plainbox.impl.secure.plugins.PlugInCollectionBase
Collection of plug-ins based on a mapping of imported objects
All loaded plugin information files are wrapped by a plug-in container. By default that is
PlugIn
but it may be adjusted if required.-
discovery_time
¶ Time, in fractional seconds, that was required to discover all objects.
This time is separate from the load and wrap time of all each individual plug-in. Typically this is either a fixed cost or a predictable cost related to traversing the file system.
Note
This overridden version can be called at any time, unlike the base class implementation. Before all discovery is done, it simply returns zero.
-
get_all_items
()¶ Get a list of all the pairs of plugin name and plugin
Returns: a list of tuples (plugin.plugin_name, plugin)
-
get_all_plugin_objects
()¶ Get an list of plug-in objects
-
get_all_plugins
()¶ Get a list of all the plug-ins
Returns: a list of plugin objects
-
get_by_name
(name)[source]¶ Get the specified plug-in (by name)
Parameters: name – name of the plugin to locate Returns: PlugIn
like object associated with the nameRaises: KeyError – if the specified name cannot be found
-
get_total_time
() → float¶ Get the sum of load and wrap time of each plugin object
Returns: The total number of fractional seconds of wall-clock time spent by loading this collection. This value doesn’t include some small overhead of this class but is representative of the load times of pluggable code.
-
problem_list
¶ List of problems encountered while loading plugins
-
wrap_and_add_plugin
(plugin_name, plugin_obj, plugin_load_time)¶ Internal method of PlugInCollectionBase.
Parameters: - plugin_name – plugin name, some arbitrary string
- plugin_obj – plugin object, some arbitrary object.
- plugin_load_time – number of seconds it took to load this plugin
This method prepares a wrapper (PlugIn subclass instance) for the specified plugin name/object by attempting to instantiate the wrapper class. If a PlugInError exception is raised then it is added to the problem_list and the corresponding plugin is not added to the collection of plugins.
-
-
class
plainbox.impl.secure.plugins.
PkgResourcesPlugInCollection
(namespace, load=False, wrapper=<class 'plainbox.impl.secure.plugins.PlugIn'>, *wrapper_args, **wrapper_kwargs)[source]¶ Bases:
plainbox.impl.secure.plugins.PlugInCollectionBase
Collection of plug-ins based on pkg_resources
Instantiate with
namespace
, callload()
and then access any of the loaded plug-ins using the API offered. All loaded objects are wrapped by a plug-in container. By default that isPlugIn
but it may be adjusted if required.-
discovery_time
¶ Time, in fractional seconds, that was required to discover all objects.
This time is separate from the load and wrap time of all each individual plug-in. Typically this is either a fixed cost or a predictable cost related to traversing the file system.
-
fake_plugins
(plugins, problem_list=None)¶ Context manager for using fake list of plugins
Parameters: - plugins – list of PlugIn-alike objects
- problem_list – list of problems (exceptions)
The provided list of plugins overrides any previously loaded plugins and prevent loading any other, real, plugins. After the context manager exits the previous state is restored.
-
get_all_items
()¶ Get a list of all the pairs of plugin name and plugin
Returns: a list of tuples (plugin.plugin_name, plugin)
-
get_all_names
()¶ Get a list of all the plug-in names
Returns: a list of plugin names
-
get_all_plugin_objects
()¶ Get an list of plug-in objects
-
get_all_plugins
()¶ Get a list of all the plug-ins
Returns: a list of plugin objects
-
get_by_name
(name)¶ Get the specified plug-in (by name)
Parameters: name – name of the plugin to locate Returns: PlugIn
like object associated with the nameRaises: KeyError – if the specified name cannot be found
-
get_total_time
() → float¶ Get the sum of load and wrap time of each plugin object
Returns: The total number of fractional seconds of wall-clock time spent by loading this collection. This value doesn’t include some small overhead of this class but is representative of the load times of pluggable code.
-
load
()[source]¶ Load all plug-ins.
This method loads all plug-ins from the specified name-space. It may perform a lot of IO so it’s somewhat slow / expensive on a cold disk cache.
Note
this method queries pkg-resources only once.
-
problem_list
¶ List of problems encountered while loading plugins
-
wrap_and_add_plugin
(plugin_name, plugin_obj, plugin_load_time)¶ Internal method of PlugInCollectionBase.
Parameters: - plugin_name – plugin name, some arbitrary string
- plugin_obj – plugin object, some arbitrary object.
- plugin_load_time – number of seconds it took to load this plugin
This method prepares a wrapper (PlugIn subclass instance) for the specified plugin name/object by attempting to instantiate the wrapper class. If a PlugInError exception is raised then it is added to the problem_list and the corresponding plugin is not added to the collection of plugins.
-
-
class
plainbox.impl.secure.plugins.
PlugIn
(name: str, obj: object, load_time: float=0, wrap_time: float=0)[source]¶ Bases:
plainbox.impl.secure.plugins.IPlugIn
Simple plug-in that does not offer any guarantees beyond knowing it’s name and some arbitrary external object.
-
plugin_load_time
¶ time, in fractional seconds, that was needed to load the plugin
-
plugin_name
¶ plugin name, arbitrary string
-
plugin_object
¶ plugin object, arbitrary object
-
plugin_wrap_time
¶ time, in fractional seconds, that was needed to wrap the plugin
-
-
class
plainbox.impl.secure.plugins.
PlugInCollectionBase
(load=False, wrapper=<class 'plainbox.impl.secure.plugins.PlugIn'>, *wrapper_args, **wrapper_kwargs)[source]¶ Bases:
plainbox.impl.secure.plugins.IPlugInCollection
Base class that shares some of the implementation with the other PlugInCollection implemenetations.
-
discovery_time
¶ Time, in fractional seconds, that was required to discover all objects.
This time is separate from the load and wrap time of all each individual plug-in. Typically this is either a fixed cost or a predictable cost related to traversing the file system.
-
fake_plugins
(plugins, problem_list=None)[source]¶ Context manager for using fake list of plugins
Parameters: - plugins – list of PlugIn-alike objects
- problem_list – list of problems (exceptions)
The provided list of plugins overrides any previously loaded plugins and prevent loading any other, real, plugins. After the context manager exits the previous state is restored.
-
get_all_items
()[source]¶ Get a list of all the pairs of plugin name and plugin
Returns: a list of tuples (plugin.plugin_name, plugin)
-
get_by_name
(name)[source]¶ Get the specified plug-in (by name)
Parameters: name – name of the plugin to locate Returns: PlugIn
like object associated with the nameRaises: KeyError – if the specified name cannot be found
-
get_total_time
() → float[source]¶ Get the sum of load and wrap time of each plugin object
Returns: The total number of fractional seconds of wall-clock time spent by loading this collection. This value doesn’t include some small overhead of this class but is representative of the load times of pluggable code.
-
load
()¶ Load all plug-ins.
This method loads all plug-ins from the specified name-space. It may perform a lot of IO so it’s somewhat slow / expensive on a cold disk cache.
-
problem_list
¶ List of problems encountered while loading plugins
-
wrap_and_add_plugin
(plugin_name, plugin_obj, plugin_load_time)[source]¶ Internal method of PlugInCollectionBase.
Parameters: - plugin_name – plugin name, some arbitrary string
- plugin_obj – plugin object, some arbitrary object.
- plugin_load_time – number of seconds it took to load this plugin
This method prepares a wrapper (PlugIn subclass instance) for the specified plugin name/object by attempting to instantiate the wrapper class. If a PlugInError exception is raised then it is added to the problem_list and the corresponding plugin is not added to the collection of plugins.
-
-
exception
plainbox.impl.secure.plugins.
PlugInError
[source]¶ Bases:
Exception
Exception that may be raised by PlugIn.__init__() to signal it cannot be fully loaded and should not be added to any collection.
-
args
¶
-
with_traceback
()¶ Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
-
-
plainbox.impl.secure.plugins.
now
() → float[source]¶ Get the current “time”.
Returns: A fractional number of seconds since some undefined base event. This methods returns the current “time” that is useful for measuring plug-in loading time. The return value is meaningless but delta between two values is a fractional number of seconds between the two corresponding events.