-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FLOC-4245] Factor out plugin loading. #2668
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2da18ca
Factor out plugin loading.
548277f
Factor out backend description code from `node.script`.
37e8145
Testable.
f332d76
Update tests.
0f12295
Copyright headers.
45b9fc5
Docstring and errors.
c5f0fef
Simplify naming.
061b259
Plugin tests.
c2f1477
Docstring.
4ea6977
Get rid of unneeded subclassing of object.
b1ac004
Docstring.
2a7d300
Docstring.
0ff2c91
Adjust acceptance tests to use plugin loader for finding backend.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Copyright ClusterHQ Inc. See LICENSE file for details. | ||
|
||
""" | ||
Tools for loading third-party plugins. | ||
""" | ||
|
||
from characteristic import attributes | ||
from pyrsistent import PClass, field, PVector, pvector | ||
from twisted.python.reflect import namedAny | ||
|
||
|
||
@attributes(["plugin_name"]) | ||
class PluginNotFound(Exception): | ||
""" | ||
A plugin with the given name was not found. | ||
|
||
:attr str plugin_name: Name of the plugin looked for. | ||
""" | ||
def __str__(self): | ||
return ( | ||
"'{!s}' is neither a built-in plugin nor a 3rd party " | ||
"module.".format(self.plugin_name) | ||
) | ||
|
||
|
||
class InvalidPlugin(Exception, object): | ||
""" | ||
A module with the given plugin name was found, but doesn't | ||
provide a valid flocker plugin. | ||
""" | ||
|
||
|
||
@attributes(["plugin_name", "module_attribute"]) | ||
class MissingPluginAttribute(InvalidPlugin): | ||
""" | ||
The named module doesn't have the attribute expected of plugins. | ||
""" | ||
def __str__(self): | ||
return ( | ||
"The 3rd party plugin '{plugin_name!s}' does not " | ||
"correspond to the expected interface. " | ||
"`{plugin_name!s}.{module_attribute!s}` is not defined." | ||
.format( | ||
plugin_name=self.plugin_name, | ||
module_attribute=self.module_attribute, | ||
) | ||
) | ||
|
||
|
||
@attributes(["plugin_name", "plugin_type", "actual_type", "module_attribute"]) | ||
class InvalidPluginType(InvalidPlugin): | ||
""" | ||
A plugin with the given name was not found. | ||
""" | ||
def __str__(self): | ||
return ( | ||
"The 3rd party plugin '{plugin_name!s}' does not " | ||
"correspond to the expected interface. " | ||
"`{plugin_name!s}.{module_attribute!s}` is of " | ||
"type `{actual_type.__name__}`, not `{plugin_type.__name__}`." | ||
.format( | ||
plugin_name=self.plugin_name, | ||
actual_type=self.actual_type, | ||
plugin_type=self.plugin_type, | ||
module_attribute=self.module_attribute, | ||
) | ||
) | ||
|
||
|
||
class PluginLoader(PClass): | ||
""" | ||
:ivar PVector builtin_plugins: The plugins shipped with flocker. | ||
:ivar str module_attribute: The module attribute that third-party plugins | ||
should declare. | ||
:ivar type plugin_type: The type describing a plugin. | ||
""" | ||
builtin_plugins = field(PVector, mandatory=True, factory=pvector) | ||
module_attribute = field(str, mandatory=True) | ||
plugin_type = field(type, mandatory=True) | ||
|
||
def __invariant__(self): | ||
for builtin in self.builtin_plugins: | ||
if not isinstance(builtin, self.plugin_type): | ||
return ( | ||
False, | ||
"Builtin plugins must be of `{plugin_type.__name__}`, not " | ||
"`{actual_type.__name__}`.".format( | ||
plugin_type=self.plugin_type, | ||
actual_type=type(builtin), | ||
) | ||
) | ||
|
||
return (True, "") | ||
|
||
def get(self, plugin_name): | ||
""" | ||
Find the backend in ``backends`` that matches the one named by | ||
``backend_name``. If not found then attempt is made to load it as | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some grammar stuff here: .. Unless I'm misreading... |
||
plugin. | ||
|
||
:param plugin_name: The name of the backend. | ||
:param backends: Collection of `BackendDescription`` instances. | ||
|
||
:raise PluginNotFound: If ``plugin_name`` doesn't match any | ||
known plugin. | ||
:raise InvalidPlugin: If ``plugin_name`` names a module that | ||
doesn't satisfy the plugin interface. | ||
:return: The matching ``plugin_type`` instance. | ||
""" | ||
for builtin in self.builtin_plugins: | ||
if builtin.name == plugin_name: | ||
return builtin | ||
|
||
try: | ||
plugin_module = namedAny(plugin_name) | ||
except (AttributeError, ValueError): | ||
raise PluginNotFound(plugin_name=plugin_name) | ||
|
||
try: | ||
plugin = getattr(plugin_module, self.module_attribute) | ||
except AttributeError: | ||
raise MissingPluginAttribute( | ||
plugin_name=plugin_name, | ||
module_attribute=self.module_attribute, | ||
) | ||
|
||
if not isinstance(plugin, self.plugin_type): | ||
raise InvalidPluginType( | ||
plugin_name=plugin_name, | ||
plugin_type=self.plugin_type, | ||
actual_type=type(plugin), | ||
module_attribute=self.module_attribute, | ||
) | ||
|
||
return plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Copyright ClusterHQ Inc. See LICENSE file for details. | ||
|
||
""" | ||
Tests for ``flocker.common.plugin`` | ||
""" | ||
|
||
from pyrsistent import PClass, field | ||
|
||
from flocker.testtools import TestCase | ||
|
||
from ..plugin import ( | ||
PluginLoader, | ||
PluginNotFound, | ||
MissingPluginAttribute, | ||
InvalidPluginType, | ||
) | ||
|
||
|
||
class DummyDescription(PClass): | ||
""" | ||
Dummy plugin type." | ||
""" | ||
name = field(unicode, mandatory=True) | ||
|
||
|
||
# The following test examples use classes instead of modules for | ||
# namespacing. Real plugins should use modules. | ||
class DummyPlugin(object): | ||
""" | ||
A plugin." | ||
""" | ||
FLOCKER_PLUGIN = DummyDescription( | ||
name=u"dummyplugin", | ||
) | ||
|
||
|
||
class DummyPluginMissingAttribute(object): | ||
""" | ||
A purported plugin that is missing the expected attribute. | ||
""" | ||
|
||
|
||
class DummyPluginWrongType(object): | ||
""" | ||
A purported plugin that has the wrong type of description. | ||
""" | ||
FLOCKER_PLUGIN = object() | ||
|
||
|
||
DUMMY_LOADER = PluginLoader( | ||
builtin_plugins=[], | ||
module_attribute="FLOCKER_PLUGIN", | ||
plugin_type=DummyDescription, | ||
) | ||
|
||
|
||
class PluginLoaderTests(TestCase): | ||
""" | ||
Tests for ``PluginLoader``. | ||
""" | ||
|
||
def test_builtin_backend(self): | ||
""" | ||
If the plugin name is that of a pre-configured plugin, the | ||
corresponding builtin plugin is returned. | ||
""" | ||
loader = DUMMY_LOADER.set( | ||
"builtin_plugins", [ | ||
DummyDescription(name=u"other-builtin"), | ||
DummyDescription(name=u"builtin"), | ||
] | ||
) | ||
plugin = loader.get("builtin") | ||
self.assertEqual(plugin, DummyDescription(name=u"builtin")) | ||
|
||
def test_3rd_party_plugin(self): | ||
""" | ||
If the plugin name is not that of a pre-configured plugin, the | ||
plugin name is treated as a Python import path, and the | ||
specified attribute of that is used as the plugin. | ||
""" | ||
plugin = DUMMY_LOADER.get( | ||
"flocker.common.test.test_plugin.DummyPlugin" | ||
) | ||
self.assertEqual(plugin, DummyDescription(name=u"dummyplugin")) | ||
|
||
def test_wrong_package_3rd_party_backend(self): | ||
""" | ||
If the plugin name refers to an unimportable package, | ||
``PluginNotFound`` is raised. | ||
""" | ||
self.assertRaises( | ||
PluginNotFound, | ||
DUMMY_LOADER.get, | ||
"notarealmoduleireallyhope", | ||
) | ||
|
||
def test_missing_attribute_3rd_party_backend(self): | ||
""" | ||
If the plugin name refers to an object that doesn't have the | ||
specified attribute, ``MissingPluginAttribute`` is raised. | ||
""" | ||
self.assertRaises( | ||
MissingPluginAttribute, | ||
DUMMY_LOADER.get, | ||
"flocker.common.test.test_plugin.DummyPluginMissingAttribute" | ||
) | ||
|
||
def test_wrong_attribute_type_3rd_party_backend(self): | ||
""" | ||
If the plugin name refers to an object whose specified | ||
attribute isn't of the right type, ``InvalidPluginType`` is | ||
raised. | ||
""" | ||
self.assertRaises( | ||
InvalidPluginType, | ||
DUMMY_LOADER.get, | ||
"flocker.common.test.test_plugin.DummyPluginWrongType" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ What's the purpose of also subclassing object here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added that when trying to debug hynek/characteristic#37. It isn't needed.