code refactoring: better function names and better code organisation

This commit is contained in:
chfw 2017-04-05 19:59:40 +01:00
parent 6dfe84bae3
commit 85aa803a5e
3 changed files with 63 additions and 26 deletions

View File

@ -1,31 +1,50 @@
import logging
from collections import defaultdict
PLUG_IN_MANAGERS = {}
log = logging.getLogger(__name__)
def register_class(cls):
log.debug("register " + cls.name)
PLUG_IN_MANAGERS[cls.name] = cls
class PluginManager(object):
name = "plugin"
def plugin_first(self, meta, module_name):
pass
def __init__(self, plugin_type):
self.name = plugin_type
self.registry = defaultdict(list)
self._logger = logging.getLogger(
self.__class__.__module__ + '.' + self.__class__.__name__)
register_class(self)
def list_registry(self):
pass
def load_me_later(self, meta, module_name):
self._logger.debug('load me later: ' + module_name)
self._logger.debug(meta)
def load_me_later(self, registry_key):
pass
def load_me_now(self, key, **keywords):
self._logger.debug("load me now:" + key)
if keywords:
self._logger.debug(keywords)
def plugin_now(self, cls):
pass
def get_plugin(self):
pass
def dynamic_load_library(self, library_import_path):
self._logger.debug("import " + library_import_path[0])
return do_import(library_import_path[0])
def plugin_first(meta, module_name):
def load_me_later(meta, module_name):
manager = PLUG_IN_MANAGERS.get(meta['plugin_type'])
manager.plugin_first(meta, module_name)
if manager:
manager.load_me_later(meta, module_name)
else:
raise Exception("%s has not loader" % meta['plugin_type'])
def do_import(plugin_module_name):
plugin_module = __import__(plugin_module_name)
if '.' in plugin_module_name:
modules = plugin_module_name.split('.')
for module in modules[1:]:
plugin_module = getattr(plugin_module, module)
return plugin_module

View File

@ -1,27 +1,38 @@
import pkgutil
import logging
from itertools import chain
from lml.manager import plugin_first
from lml.manager import load_me_later, do_import
log = logging.getLogger(__name__)
def scan_plugins(prefix, marker, path, black_list=None, white_list=None):
if black_list is None:
black_list = []
if white_list is None:
white_list = []
def scan_plugins(prefix, marker, path, black_list):
# scan pkgutil.iter_modules
module_names = (module_info[1] for module_info in pkgutil.iter_modules()
if module_info[2] and module_info[1].startswith(prefix))
# scan pyinstaller
module_names_from_pyinstaller = scan_from_pyinstaller(prefix, path)
all_modules = chain(module_names,
module_names_from_pyinstaller,
white_list)
# loop through modules and find our plug ins
for module_name in chain(module_names, module_names_from_pyinstaller):
for module_name in all_modules:
if module_name in black_list:
continue
try:
plugin = __import__(module_name)
if hasattr(plugin, marker):
for plugin_meta in getattr(plugin, marker):
plugin_first(plugin_meta, module_name)
except ImportError:
load_plugins(module_name, marker)
except ImportError as e:
log.debug(module_name)
log.debug(e)
continue
@ -40,3 +51,10 @@ def scan_from_pyinstaller(prefix, path):
for module_name in table_of_content:
if module_name.startswith(prefix) and '.' not in module_name:
yield module_name
def load_plugins(plugin_module_name, marker):
plugin_module = do_import(plugin_module_name)
if hasattr(plugin_module, marker):
for plugin_meta in getattr(plugin_module, marker):
load_me_later(plugin_meta, plugin_module_name)

View File

@ -14,7 +14,7 @@ def test_load_from_pyinstaller(pkgutil_get_importer):
@patch('pkgutil.get_importer')
@patch('pkgutil.iter_modules')
@patch('lml.plugin.plugin_first')
@patch('lml.plugin.load_me_later')
def test_load_plugins(pre_register,
pkgutil_iter_modules,
pkgutil_get_importer):
@ -35,7 +35,7 @@ def test_load_plugins(pre_register,
@patch('pkgutil.get_importer')
@patch('pkgutil.iter_modules')
@patch('lml.plugin.plugin_first')
@patch('lml.plugin.load_me_later')
def test_load_plugins_without_pyinstaller(pre_register,
pkgutil_iter_modules,
pkgutil_get_importer):
@ -56,7 +56,7 @@ def test_load_plugins_without_pyinstaller(pre_register,
@patch('pkgutil.get_importer')
@patch('pkgutil.iter_modules')
@patch('lml.plugin.plugin_first')
@patch('lml.plugin.load_me_later')
def test_load_plugins_without_any_plugins(pre_register,
pkgutil_iter_modules,
pkgutil_get_importer):
@ -70,7 +70,7 @@ def test_load_plugins_without_any_plugins(pre_register,
@patch('pkgutil.get_importer')
@patch('pkgutil.iter_modules')
@patch('lml.plugin.plugin_first')
@patch('lml.plugin.load_me_later')
def test_load_plugins_import_error(pre_register,
pkgutil_iter_modules,
pkgutil_get_importer):