mirror of
https://git.yoctoproject.org/poky
synced 2026-05-09 17:39:31 +00:00
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>
This commit is contained in:
committed by
Richard Purdie
parent
d4ded7fcb1
commit
5fedb5d3cc
@@ -15,34 +15,26 @@
|
|||||||
# with this program; if not, write to the Free Software Foundation, Inc., 59
|
# with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||||
# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
|
__all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins']
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from wic import msger
|
from wic import msger
|
||||||
|
|
||||||
class _Plugin(object):
|
class PluginMeta(type):
|
||||||
class __metaclass__(type):
|
plugins = defaultdict(dict)
|
||||||
def __init__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
if not hasattr(cls, 'plugins'):
|
class_type = type.__new__(cls, name, bases, attrs)
|
||||||
cls.plugins = {}
|
if 'name' in attrs:
|
||||||
|
cls.plugins[class_type.wic_plugin_type][attrs['name']] = class_type
|
||||||
|
|
||||||
elif 'wic_plugin_type' in attrs:
|
return class_type
|
||||||
if attrs['wic_plugin_type'] not in cls.plugins:
|
|
||||||
cls.plugins[attrs['wic_plugin_type']] = {}
|
|
||||||
|
|
||||||
elif hasattr(cls, 'wic_plugin_type') and 'name' in attrs:
|
class ImagerPlugin(PluginMeta("Plugin", (), {})):
|
||||||
cls.plugins[cls.wic_plugin_type][attrs['name']] = cls
|
|
||||||
|
|
||||||
def show_plugins(cls):
|
|
||||||
for cls in cls.plugins[cls.wic_plugin_type]:
|
|
||||||
print cls
|
|
||||||
|
|
||||||
def get_plugins(cls):
|
|
||||||
return cls.plugins
|
|
||||||
|
|
||||||
|
|
||||||
class ImagerPlugin(_Plugin):
|
|
||||||
wic_plugin_type = "imager"
|
wic_plugin_type = "imager"
|
||||||
|
|
||||||
|
class SourcePlugin(PluginMeta("Plugin", (), {})):
|
||||||
class SourcePlugin(_Plugin):
|
|
||||||
wic_plugin_type = "source"
|
wic_plugin_type = "source"
|
||||||
"""
|
"""
|
||||||
The methods that can be implemented by --source plugins.
|
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)
|
msger.debug("SourcePlugin: do_prepare_partition: part: %s" % part)
|
||||||
|
|
||||||
def get_plugins(typen):
|
def get_plugins(typen):
|
||||||
plugins = ImagerPlugin.get_plugins()
|
return PluginMeta.plugins.get(typen)
|
||||||
if typen in plugins:
|
|
||||||
return plugins[typen]
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
__all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins']
|
|
||||||
|
|||||||
Reference in New Issue
Block a user