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 23:05:13 +0100 |
commit | 5fedb5d3cc8d6ff9e9e378bbda45345a6b867cd4 (patch) | |
tree | 61ccb9a9801f31fa783a0f7521dc03713eaaf53f | |
parent | d4ded7fcb159e55134b7612dbdc928613c7321c6 (diff) | |
download | poky-5fedb5d3cc8d6ff9e9e378bbda45345a6b867cd4.tar.gz |
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]
(From OE-Core rev: e62fe5a41bdcdd72b9b257fecff7ccdc59c76d33)
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-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 @@ | |||
15 | # with this program; if not, write to the Free Software Foundation, Inc., 59 | 15 | # with this program; if not, write to the Free Software Foundation, Inc., 59 |
16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 | 17 | ||
18 | from wic import msger | 18 | __all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins'] |
19 | |||
20 | class _Plugin(object): | ||
21 | class __metaclass__(type): | ||
22 | def __init__(cls, name, bases, attrs): | ||
23 | if not hasattr(cls, 'plugins'): | ||
24 | cls.plugins = {} | ||
25 | |||
26 | elif 'wic_plugin_type' in attrs: | ||
27 | if attrs['wic_plugin_type'] not in cls.plugins: | ||
28 | cls.plugins[attrs['wic_plugin_type']] = {} | ||
29 | 19 | ||
30 | elif hasattr(cls, 'wic_plugin_type') and 'name' in attrs: | 20 | import sys |
31 | cls.plugins[cls.wic_plugin_type][attrs['name']] = cls | 21 | from collections import defaultdict |
32 | 22 | ||
33 | def show_plugins(cls): | 23 | from wic import msger |
34 | for cls in cls.plugins[cls.wic_plugin_type]: | ||
35 | print cls | ||
36 | 24 | ||
37 | def get_plugins(cls): | 25 | class PluginMeta(type): |
38 | return cls.plugins | 26 | plugins = defaultdict(dict) |
27 | def __new__(cls, name, bases, attrs): | ||
28 | class_type = type.__new__(cls, name, bases, attrs) | ||
29 | if 'name' in attrs: | ||
30 | cls.plugins[class_type.wic_plugin_type][attrs['name']] = class_type | ||
39 | 31 | ||
32 | return class_type | ||
40 | 33 | ||
41 | class ImagerPlugin(_Plugin): | 34 | class ImagerPlugin(PluginMeta("Plugin", (), {})): |
42 | wic_plugin_type = "imager" | 35 | wic_plugin_type = "imager" |
43 | 36 | ||
44 | 37 | class SourcePlugin(PluginMeta("Plugin", (), {})): | |
45 | class SourcePlugin(_Plugin): | ||
46 | wic_plugin_type = "source" | 38 | wic_plugin_type = "source" |
47 | """ | 39 | """ |
48 | The methods that can be implemented by --source plugins. | 40 | The methods that can be implemented by --source plugins. |
@@ -99,10 +91,4 @@ class SourcePlugin(_Plugin): | |||
99 | msger.debug("SourcePlugin: do_prepare_partition: part: %s" % part) | 91 | msger.debug("SourcePlugin: do_prepare_partition: part: %s" % part) |
100 | 92 | ||
101 | def get_plugins(typen): | 93 | def get_plugins(typen): |
102 | plugins = ImagerPlugin.get_plugins() | 94 | return PluginMeta.plugins.get(typen) |
103 | if typen in plugins: | ||
104 | return plugins[typen] | ||
105 | else: | ||
106 | return None | ||
107 | |||
108 | __all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins'] | ||