diff options
author | Ed Bartosh <ed.bartosh@linux.intel.com> | 2016-05-04 16:06:21 +0300 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2016-05-14 07:26:42 +0100 |
commit | e62fe5a41bdcdd72b9b257fecff7ccdc59c76d33 (patch) | |
tree | 33cde5590311efb20085fc4b912e1cf806374165 /scripts/lib/wic | |
parent | eb87d591ef67f1953b2689430ef6c5a6a27a5b6e (diff) | |
download | openembedded-core-e62fe5a41bdcdd72b9b257fecff7ccdc59c76d33.tar.gz openembedded-core-e62fe5a41bdcdd72b9b257fecff7ccdc59c76d33.tar.bz2 openembedded-core-e62fe5a41bdcdd72b9b257fecff7ccdc59c76d33.zip |
wic: refactor pluginbase
Wic plugin machinery implemented using metaclasses.
Reimplemented plugin machinery using this advice from
https://wiki.python.org/moin/PortingToPy3k/BilingualQuickRef
Syntax for creating instances with different metaclasses is very
different between Python 2 and 3. Use the ability to call type instances
as a way to portably create such instances.
Now it should work under both Python 2 and Python 3.
[YOCTO #9412]
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic')
-rw-r--r-- | scripts/lib/wic/pluginbase.py | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/scripts/lib/wic/pluginbase.py b/scripts/lib/wic/pluginbase.py index ee8fe95c6f..e737dee7bc 100644 --- a/scripts/lib/wic/pluginbase.py +++ b/scripts/lib/wic/pluginbase.py @@ -15,34 +15,26 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # Temple Place - Suite 330, Boston, MA 02111-1307, USA. -from wic import msger - -class _Plugin(object): - class __metaclass__(type): - def __init__(cls, name, bases, attrs): - if not hasattr(cls, 'plugins'): - cls.plugins = {} - - elif 'wic_plugin_type' in attrs: - if attrs['wic_plugin_type'] not in cls.plugins: - cls.plugins[attrs['wic_plugin_type']] = {} +__all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins'] - elif hasattr(cls, 'wic_plugin_type') and 'name' in attrs: - cls.plugins[cls.wic_plugin_type][attrs['name']] = cls +import sys +from collections import defaultdict - def show_plugins(cls): - for cls in cls.plugins[cls.wic_plugin_type]: - print cls +from wic import msger - def get_plugins(cls): - return cls.plugins +class PluginMeta(type): + plugins = defaultdict(dict) + def __new__(cls, name, bases, attrs): + class_type = type.__new__(cls, name, bases, attrs) + if 'name' in attrs: + cls.plugins[class_type.wic_plugin_type][attrs['name']] = class_type + return class_type -class ImagerPlugin(_Plugin): +class ImagerPlugin(PluginMeta("Plugin", (), {})): wic_plugin_type = "imager" - -class SourcePlugin(_Plugin): +class SourcePlugin(PluginMeta("Plugin", (), {})): wic_plugin_type = "source" """ The methods that can be implemented by --source plugins. @@ -99,10 +91,4 @@ class SourcePlugin(_Plugin): msger.debug("SourcePlugin: do_prepare_partition: part: %s" % part) def get_plugins(typen): - plugins = ImagerPlugin.get_plugins() - if typen in plugins: - return plugins[typen] - else: - return None - -__all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins'] + return PluginMeta.plugins.get(typen) |