diff options
Diffstat (limited to 'scripts')
-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'] | ||