插件Plugin 架构

import importlib
import pkgutil
from collections import OrderedDict

register_plugins = OrderedDict()


def get_all_plugins(print_info=False):
    """
    get all plugins
    Returns:
        plugins: list
    """
    is_local_os = True if plugins_need_to_skip() else False
    if not register_plugins:
        for _, modname, ispkg in pkgutil.walk_packages(path=__path__,
                                                       prefix=__name__ + '.'):
            if not ispkg:
                importlib.import_module(modname)
    plugin_list = []
    for k, v in register_plugins.iteritems():
        plugin = k.replace('names.plugins.', '')
        if not is_local_os and getattr(v, 'NEED_LOCAL_OS', None):
            if print_info:
                print 'Skip {0} as it need to run on local installed OS'.format(plugin)
        else:
            plugin_list.append(plugin)
    return plugin_list


def plugins_need_to_skip():
    pass


def register_plugin(cls):
    register_plugins.update({cls.__module__: cls})
    return cls


class DiskBasePlugin(object):
    pass


@register_plugin
class Plugin(DiskBasePlugin):
    """Define plugin here"""
    pass

你可能感兴趣的:(Python)