🔨 code refactoring
This commit is contained in:
parent
5d08638179
commit
616b233616
|
@ -12,13 +12,15 @@ from lml._version import __version__ # flake8: noqa
|
|||
from lml._version import __author__ # flake8: noqa
|
||||
|
||||
try:
|
||||
from logging import NullHandler
|
||||
from logging import NullHandler
|
||||
except ImportError:
|
||||
class NullHandler(logging.Handler):
|
||||
"""
|
||||
|
||||
class NullHandler(logging.Handler):
|
||||
"""
|
||||
Null handler for logging
|
||||
"""
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
logging.getLogger(__name__).addHandler(NullHandler())
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
logging.getLogger(__name__).addHandler(NullHandler())
|
||||
|
|
|
@ -19,8 +19,13 @@ from lml.utils import do_import
|
|||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def scan_plugins(prefix, pyinstaller_path, black_list=None, white_list=None,
|
||||
plugin_name_patterns=None):
|
||||
def scan_plugins(
|
||||
prefix,
|
||||
pyinstaller_path,
|
||||
black_list=None,
|
||||
white_list=None,
|
||||
plugin_name_patterns=None,
|
||||
):
|
||||
"""
|
||||
Implicitly discover plugins via pkgutil and pyinstaller path
|
||||
|
||||
|
@ -54,14 +59,20 @@ def scan_plugins(prefix, pyinstaller_path, black_list=None, white_list=None,
|
|||
is listed in white_list.
|
||||
"""
|
||||
__plugin_name_patterns = "^%s.+$" % prefix
|
||||
scan_plugins_regex(plugin_name_patterns=__plugin_name_patterns,
|
||||
pyinstaller_path=pyinstaller_path,
|
||||
black_list=black_list,
|
||||
white_list=white_list)
|
||||
scan_plugins_regex(
|
||||
plugin_name_patterns=__plugin_name_patterns,
|
||||
pyinstaller_path=pyinstaller_path,
|
||||
black_list=black_list,
|
||||
white_list=white_list,
|
||||
)
|
||||
|
||||
|
||||
def scan_plugins_regex(plugin_name_patterns=None, pyinstaller_path=None,
|
||||
black_list=None, white_list=None):
|
||||
def scan_plugins_regex(
|
||||
plugin_name_patterns=None,
|
||||
pyinstaller_path=None,
|
||||
black_list=None,
|
||||
white_list=None,
|
||||
):
|
||||
|
||||
"""
|
||||
Implicitly discover plugins via pkgutil and pyinstaller path using
|
||||
|
@ -98,16 +109,19 @@ def scan_plugins_regex(plugin_name_patterns=None, pyinstaller_path=None,
|
|||
|
||||
# scan pkgutil.iter_modules
|
||||
module_names = (
|
||||
module_info[1] for module_info in pkgutil.iter_modules()
|
||||
if module_info[2] and re.match(plugin_name_patterns, module_info[1]))
|
||||
module_info[1]
|
||||
for module_info in pkgutil.iter_modules()
|
||||
if module_info[2] and re.match(plugin_name_patterns, module_info[1])
|
||||
)
|
||||
|
||||
# scan pyinstaller
|
||||
module_names_from_pyinstaller = scan_from_pyinstaller(
|
||||
plugin_name_patterns, pyinstaller_path)
|
||||
plugin_name_patterns, pyinstaller_path
|
||||
)
|
||||
|
||||
all_modules = chain(module_names,
|
||||
module_names_from_pyinstaller,
|
||||
white_list)
|
||||
all_modules = chain(
|
||||
module_names, module_names_from_pyinstaller, white_list
|
||||
)
|
||||
# loop through modules and find our plug ins
|
||||
for module_name in all_modules:
|
||||
|
||||
|
@ -134,12 +148,15 @@ def scan_from_pyinstaller(plugin_name_patterns, path):
|
|||
Discover plugins from pyinstaller
|
||||
"""
|
||||
table_of_content = set()
|
||||
for a_toc in (importer.toc for importer in map(pkgutil.get_importer, path)
|
||||
if hasattr(importer, 'toc')):
|
||||
for a_toc in (
|
||||
importer.toc
|
||||
for importer in map(pkgutil.get_importer, path)
|
||||
if hasattr(importer, "toc")
|
||||
):
|
||||
table_of_content |= a_toc
|
||||
|
||||
for module_name in table_of_content:
|
||||
if '.' in module_name:
|
||||
if "." in module_name:
|
||||
continue
|
||||
if re.match(plugin_name_patterns, module_name):
|
||||
yield module_name
|
||||
|
|
|
@ -93,9 +93,10 @@ class PluginInfo(object):
|
|||
echoing hey..
|
||||
|
||||
"""
|
||||
def __init__(self, plugin_type,
|
||||
abs_class_path=None,
|
||||
tags=None, **keywords):
|
||||
|
||||
def __init__(
|
||||
self, plugin_type, abs_class_path=None, tags=None, **keywords
|
||||
):
|
||||
self.plugin_type = plugin_type
|
||||
self.absolute_import_path = abs_class_path
|
||||
self.cls = None
|
||||
|
@ -103,9 +104,9 @@ class PluginInfo(object):
|
|||
self.__tags = tags
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'module_name':
|
||||
if name == "module_name":
|
||||
if self.absolute_import_path:
|
||||
module_name = self.absolute_import_path.split('.')[0]
|
||||
module_name = self.absolute_import_path.split(".")[0]
|
||||
else:
|
||||
module_name = self.cls.__module__
|
||||
return module_name
|
||||
|
@ -124,8 +125,10 @@ class PluginInfo(object):
|
|||
yield tag
|
||||
|
||||
def __repr__(self):
|
||||
rep = {"plugin_type": self.plugin_type,
|
||||
"path": self.absolute_import_path}
|
||||
rep = {
|
||||
"plugin_type": self.plugin_type,
|
||||
"path": self.absolute_import_path,
|
||||
}
|
||||
rep.update(self.properties)
|
||||
return json_dumps(rep)
|
||||
|
||||
|
@ -141,13 +144,14 @@ class PluginInfoChain(object):
|
|||
|
||||
It is used in the plugin packages to list all plugin classes
|
||||
"""
|
||||
|
||||
def __init__(self, path):
|
||||
self._logger = logging.getLogger(
|
||||
self.__class__.__module__ + '.' + self.__class__.__name__)
|
||||
self.__class__.__module__ + "." + self.__class__.__name__
|
||||
)
|
||||
self.module_name = path
|
||||
|
||||
def add_a_plugin(self, plugin_type, submodule=None,
|
||||
**keywords):
|
||||
def add_a_plugin(self, plugin_type, submodule=None, **keywords):
|
||||
"""
|
||||
Add a plain plugin
|
||||
|
||||
|
@ -161,9 +165,8 @@ class PluginInfoChain(object):
|
|||
the relative import path to your plugin class
|
||||
"""
|
||||
a_plugin_info = PluginInfo(
|
||||
plugin_type,
|
||||
self._get_abs_path(submodule),
|
||||
**keywords)
|
||||
plugin_type, self._get_abs_path(submodule), **keywords
|
||||
)
|
||||
|
||||
self.add_a_plugin_instance(a_plugin_info)
|
||||
return self
|
||||
|
@ -180,9 +183,11 @@ class PluginInfoChain(object):
|
|||
|
||||
The developer has to specify the absolute import path
|
||||
"""
|
||||
self._logger.debug("add %s as '%s' plugin",
|
||||
plugin_info_instance.absolute_import_path,
|
||||
plugin_info_instance.plugin_type)
|
||||
self._logger.debug(
|
||||
"add %s as '%s' plugin",
|
||||
plugin_info_instance.absolute_import_path,
|
||||
plugin_info_instance.plugin_type,
|
||||
)
|
||||
_load_me_later(plugin_info_instance)
|
||||
return self
|
||||
|
||||
|
@ -201,12 +206,14 @@ class PluginManager(object):
|
|||
the plugin type. All plugins of this plugin type will be
|
||||
registered to it.
|
||||
"""
|
||||
|
||||
def __init__(self, plugin_type):
|
||||
self.plugin_name = plugin_type
|
||||
self.registry = defaultdict(list)
|
||||
self.tag_groups = dict()
|
||||
self._logger = logging.getLogger(
|
||||
self.__class__.__module__ + '.' + self.__class__.__name__)
|
||||
self.__class__.__module__ + "." + self.__class__.__name__
|
||||
)
|
||||
_register_class(self)
|
||||
|
||||
def get_a_plugin(self, key, **keywords):
|
||||
|
@ -237,8 +244,7 @@ class PluginManager(object):
|
|||
the key to find the plugin
|
||||
"""
|
||||
self._logger.debug(self.registry.keys())
|
||||
raise Exception(
|
||||
"No %s is found for %s" % (self.plugin_name, key))
|
||||
raise Exception("No %s is found for %s" % (self.plugin_name, key))
|
||||
|
||||
def load_me_later(self, plugin_info):
|
||||
"""
|
||||
|
@ -250,8 +256,7 @@ class PluginManager(object):
|
|||
plugin_info:
|
||||
a instance of plugin info
|
||||
"""
|
||||
self._logger.debug('load %s later',
|
||||
plugin_info.absolute_import_path)
|
||||
self._logger.debug("load %s later", plugin_info.absolute_import_path)
|
||||
for key in plugin_info.tags():
|
||||
self.registry[key.lower()].append(plugin_info)
|
||||
|
||||
|
@ -282,9 +287,7 @@ class PluginManager(object):
|
|||
else:
|
||||
# only library condition coud raise an exception
|
||||
raise Exception("%s is not installed" % library)
|
||||
self._logger.debug("load %s now for '%s'",
|
||||
cls,
|
||||
key)
|
||||
self._logger.debug("load %s now for '%s'", cls, key)
|
||||
return cls
|
||||
else:
|
||||
self.raise_exception(key)
|
||||
|
@ -317,8 +320,7 @@ class PluginManager(object):
|
|||
plugin_info:
|
||||
a instance of plugin info
|
||||
"""
|
||||
self._logger.debug("register %s",
|
||||
_show_me_your_name(plugin_cls))
|
||||
self._logger.debug("register %s", _show_me_your_name(plugin_cls))
|
||||
primary_tag = None
|
||||
for index, key in enumerate(plugin_info.tags()):
|
||||
plugin_info.cls = plugin_cls
|
||||
|
@ -334,18 +336,21 @@ class PluginManager(object):
|
|||
|
||||
def _register_class(cls):
|
||||
"""Reigister a newly created plugin manager"""
|
||||
log.debug("declare '%s' plugin manager",
|
||||
cls.plugin_name)
|
||||
log.debug("declare '%s' plugin manager", cls.plugin_name)
|
||||
PLUG_IN_MANAGERS[cls.plugin_name] = cls
|
||||
if cls.plugin_name in CACHED_PLUGIN_INFO:
|
||||
# check if there is early registrations or not
|
||||
for plugin_info in CACHED_PLUGIN_INFO[cls.plugin_name]:
|
||||
if plugin_info.absolute_import_path:
|
||||
log.debug("load cached plugin info: %s",
|
||||
plugin_info.absolute_import_path)
|
||||
log.debug(
|
||||
"load cached plugin info: %s",
|
||||
plugin_info.absolute_import_path,
|
||||
)
|
||||
else:
|
||||
log.debug("load cached plugin info: %s",
|
||||
_show_me_your_name(plugin_info.cls))
|
||||
log.debug(
|
||||
"load cached plugin info: %s",
|
||||
_show_me_your_name(plugin_info.cls),
|
||||
)
|
||||
cls.load_me_later(plugin_info)
|
||||
|
||||
del CACHED_PLUGIN_INFO[cls.plugin_name]
|
||||
|
@ -358,8 +363,7 @@ def _register_a_plugin(plugin_info, plugin_cls):
|
|||
manager.register_a_plugin(plugin_cls, plugin_info)
|
||||
else:
|
||||
# let's cache it and wait the manager to be registered
|
||||
log.debug("caching %s",
|
||||
_show_me_your_name(plugin_cls.__name__))
|
||||
log.debug("caching %s", _show_me_your_name(plugin_cls.__name__))
|
||||
CACHED_PLUGIN_INFO[plugin_info.plugin_type].append(plugin_info)
|
||||
|
||||
|
||||
|
@ -370,17 +374,19 @@ def _load_me_later(plugin_info):
|
|||
manager.load_me_later(plugin_info)
|
||||
else:
|
||||
# let's cache it and wait the manager to be registered
|
||||
log.debug("caching %s for %s",
|
||||
plugin_info.absolute_import_path,
|
||||
plugin_info.plugin_type)
|
||||
log.debug(
|
||||
"caching %s for %s",
|
||||
plugin_info.absolute_import_path,
|
||||
plugin_info.plugin_type,
|
||||
)
|
||||
CACHED_PLUGIN_INFO[plugin_info.plugin_type].append(plugin_info)
|
||||
|
||||
|
||||
def _get_me_pypi_package_name(module):
|
||||
try:
|
||||
module_name = module.__module__
|
||||
root_module_name = module_name.split('.')[0]
|
||||
return root_module_name.replace('_', '-')
|
||||
root_module_name = module_name.split(".")[0]
|
||||
return root_module_name.replace("_", "-")
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
|
|
14
lml/utils.py
14
lml/utils.py
|
@ -20,14 +20,14 @@ class PythonObjectEncoder(JSONEncoder):
|
|||
"""
|
||||
Custom object encoder for json dump
|
||||
"""
|
||||
|
||||
def default(self, obj):
|
||||
a_list_of_types = (list, dict, str,
|
||||
int, float, bool, type(None))
|
||||
a_list_of_types = (list, dict, str, int, float, bool, type(None))
|
||||
if PY2:
|
||||
a_list_of_types += (unicode,)
|
||||
if isinstance(obj, a_list_of_types):
|
||||
return JSONEncoder.default(self, obj)
|
||||
return {'_python_object': str(obj)}
|
||||
return {"_python_object": str(obj)}
|
||||
|
||||
|
||||
def json_dumps(keywords):
|
||||
|
@ -41,8 +41,8 @@ def do_import(plugin_module_name):
|
|||
"""dynamically import a module"""
|
||||
try:
|
||||
plugin_module = __import__(plugin_module_name)
|
||||
if '.' in plugin_module_name:
|
||||
modules = plugin_module_name.split('.')
|
||||
if "." in plugin_module_name:
|
||||
modules = plugin_module_name.split(".")
|
||||
for module in modules[1:]:
|
||||
plugin_module = getattr(plugin_module, module)
|
||||
log.debug("found " + plugin_module_name)
|
||||
|
@ -55,9 +55,9 @@ def do_import(plugin_module_name):
|
|||
def do_import_class(plugin_class):
|
||||
"""dynamically import a class"""
|
||||
try:
|
||||
plugin_module_name = plugin_class.rsplit('.', 1)[0]
|
||||
plugin_module_name = plugin_class.rsplit(".", 1)[0]
|
||||
plugin_module = __import__(plugin_module_name)
|
||||
modules = plugin_class.split('.')
|
||||
modules = plugin_class.split(".")
|
||||
for module in modules[1:]:
|
||||
plugin_module = getattr(plugin_module, module)
|
||||
return plugin_module
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from lml.registry import PluginInfoChain
|
||||
|
||||
|
||||
__test_plugins__ = PluginInfoChain(__name__).add_a_plugin(
|
||||
'test_io2', 'reader')
|
||||
__test_plugins__ = PluginInfoChain(__name__).add_a_plugin("test_io2", "reader")
|
||||
|
|
|
@ -3,7 +3,7 @@ from lml.plugin import PluginManager
|
|||
|
||||
class TestPluginManager(PluginManager):
|
||||
def __init__(self):
|
||||
PluginManager.__init__(self, 'test_io2')
|
||||
PluginManager.__init__(self, "test_io2")
|
||||
|
||||
def load_me_later(self, plugin_info):
|
||||
PluginManager.load_me_later(self, plugin_info)
|
||||
|
|
|
@ -9,27 +9,28 @@ try:
|
|||
from setuptools import setup, find_packages
|
||||
except ImportError:
|
||||
from ez_setup import use_setuptools
|
||||
|
||||
use_setuptools()
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='pyexcel-test_plugin2',
|
||||
name="pyexcel-test_plugin2",
|
||||
author="C. W.",
|
||||
version='0.0.1',
|
||||
version="0.0.1",
|
||||
author_email="wangc_2011@hotmail.com",
|
||||
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
|
||||
packages=find_packages(exclude=["ez_setup", "examples", "tests"]),
|
||||
include_package_data=True,
|
||||
long_description=__doc__,
|
||||
zip_safe=False,
|
||||
classifiers=[
|
||||
'Development Status :: 3 - Alpha',
|
||||
'Topic :: Office/Business',
|
||||
'Topic :: Utilities',
|
||||
'Topic :: Software Development :: Libraries',
|
||||
'Programming Language :: Python',
|
||||
'License :: OSI Approved :: GNU General Public License v3',
|
||||
'Intended Audience :: Developers',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7'
|
||||
]
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Topic :: Office/Business",
|
||||
"Topic :: Utilities",
|
||||
"Topic :: Software Development :: Libraries",
|
||||
"Programming Language :: Python",
|
||||
"License :: OSI Approved :: GNU General Public License v3",
|
||||
"Intended Audience :: Developers",
|
||||
"Programming Language :: Python :: 2.6",
|
||||
"Programming Language :: Python :: 2.7",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from lml.plugin import PluginInfoChain
|
||||
|
||||
|
||||
__test_plugins__ = PluginInfoChain(__name__).add_a_plugin(
|
||||
'test_io', 'x')
|
||||
__test_plugins__ = PluginInfoChain(__name__).add_a_plugin("test_io", "x")
|
||||
|
|
|
@ -9,27 +9,28 @@ try:
|
|||
from setuptools import setup, find_packages
|
||||
except ImportError:
|
||||
from ez_setup import use_setuptools
|
||||
|
||||
use_setuptools()
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='pyexcel-test_plugin',
|
||||
name="pyexcel-test_plugin",
|
||||
author="C. W.",
|
||||
version='0.0.1',
|
||||
version="0.0.1",
|
||||
author_email="wangc_2011@hotmail.com",
|
||||
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
|
||||
packages=find_packages(exclude=["ez_setup", "examples", "tests"]),
|
||||
include_package_data=True,
|
||||
long_description=__doc__,
|
||||
zip_safe=False,
|
||||
classifiers=[
|
||||
'Development Status :: 3 - Alpha',
|
||||
'Topic :: Office/Business',
|
||||
'Topic :: Utilities',
|
||||
'Topic :: Software Development :: Libraries',
|
||||
'Programming Language :: Python',
|
||||
'License :: OSI Approved :: GNU General Public License v3',
|
||||
'Intended Audience :: Developers',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7'
|
||||
]
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Topic :: Office/Business",
|
||||
"Topic :: Utilities",
|
||||
"Topic :: Software Development :: Libraries",
|
||||
"Programming Language :: Python",
|
||||
"License :: OSI Approved :: GNU General Public License v3",
|
||||
"Intended Audience :: Developers",
|
||||
"Programming Language :: Python :: 2.6",
|
||||
"Programming Language :: Python :: 2.7",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -4,26 +4,26 @@ from nose.tools import eq_
|
|||
|
||||
|
||||
def test_plugin_info():
|
||||
info = PluginInfo('renderer',
|
||||
abs_class_path='good.plugin.path',
|
||||
custom='property')
|
||||
assert info.custom == 'property'
|
||||
info = PluginInfo(
|
||||
"renderer", abs_class_path="good.plugin.path", custom="property"
|
||||
)
|
||||
assert info.custom == "property"
|
||||
keys = list(info.tags())
|
||||
assert len(keys) == 1
|
||||
assert keys[0] == 'renderer'
|
||||
assert info.module_name == 'good'
|
||||
expected = {"path": "good.plugin.path",
|
||||
"plugin_type": "renderer",
|
||||
"custom": "property"}
|
||||
assert keys[0] == "renderer"
|
||||
assert info.module_name == "good"
|
||||
expected = {
|
||||
"path": "good.plugin.path",
|
||||
"plugin_type": "renderer",
|
||||
"custom": "property",
|
||||
}
|
||||
eq_(json.loads(info.__repr__()), expected)
|
||||
|
||||
|
||||
def test_module_name_scenario_2():
|
||||
|
||||
class TestClass2:
|
||||
pass
|
||||
|
||||
info = PluginInfo('renderer',
|
||||
custom='property')
|
||||
info = PluginInfo("renderer", custom="property")
|
||||
info.cls = TestClass2
|
||||
eq_(info.module_name, 'test_plugin_info')
|
||||
eq_(info.module_name, "test_plugin_info")
|
||||
|
|
|
@ -2,96 +2,105 @@ from mock import patch
|
|||
from nose.tools import eq_
|
||||
|
||||
|
||||
@patch('pkgutil.get_importer')
|
||||
@patch("pkgutil.get_importer")
|
||||
def test_load_from_pyinstaller(pkgutil_get_importer):
|
||||
sample_toc = set(['pyexcel_io', 'pyexcel_xls', 'blah', 'test.dot.module'])
|
||||
sample_toc = set(["pyexcel_io", "pyexcel_xls", "blah", "test.dot.module"])
|
||||
pkgutil_get_importer.return_value.toc = sample_toc
|
||||
from lml.loader import scan_from_pyinstaller
|
||||
module_names = scan_from_pyinstaller('pyexcel_', 'path')
|
||||
expected = ['pyexcel_io', 'pyexcel_xls']
|
||||
|
||||
module_names = scan_from_pyinstaller("pyexcel_", "path")
|
||||
expected = ["pyexcel_io", "pyexcel_xls"]
|
||||
eq_(sorted(list(module_names)), sorted(expected))
|
||||
|
||||
|
||||
@patch('pkgutil.get_importer')
|
||||
@patch("pkgutil.get_importer")
|
||||
def test_load_from_pyinstaller_with_regex(pkgutil_get_importer):
|
||||
sample_toc = set(['pyexcel_io', 'pyexcel_xls', 'blah'])
|
||||
sample_toc = set(["pyexcel_io", "pyexcel_xls", "blah"])
|
||||
pkgutil_get_importer.return_value.toc = sample_toc
|
||||
from lml.loader import scan_from_pyinstaller
|
||||
module_names = scan_from_pyinstaller('^.+cel_.+$', 'path')
|
||||
expected = ['pyexcel_io', 'pyexcel_xls']
|
||||
|
||||
module_names = scan_from_pyinstaller("^.+cel_.+$", "path")
|
||||
expected = ["pyexcel_io", "pyexcel_xls"]
|
||||
eq_(sorted(list(module_names)), sorted(expected))
|
||||
|
||||
|
||||
@patch('pkgutil.get_importer')
|
||||
@patch('pkgutil.iter_modules')
|
||||
def test_load_plugins(pkgutil_iter_modules,
|
||||
pkgutil_get_importer):
|
||||
test_module_name = 'pyexcel_test'
|
||||
sample_toc = set(['pyexcel_io'])
|
||||
@patch("pkgutil.get_importer")
|
||||
@patch("pkgutil.iter_modules")
|
||||
def test_load_plugins(pkgutil_iter_modules, pkgutil_get_importer):
|
||||
test_module_name = "pyexcel_test"
|
||||
sample_toc = set(["pyexcel_io"])
|
||||
pkgutil_get_importer.return_value.toc = sample_toc
|
||||
pkgutil_iter_modules.return_value = [('not used', test_module_name, True)]
|
||||
pkgutil_iter_modules.return_value = [("not used", test_module_name, True)]
|
||||
from lml.loader import scan_plugins
|
||||
scan_plugins('pyexcel_', '.', ['pyexcel_io'])
|
||||
|
||||
scan_plugins("pyexcel_", ".", ["pyexcel_io"])
|
||||
from lml.plugin import CACHED_PLUGIN_INFO
|
||||
info = CACHED_PLUGIN_INFO['test_io'][0]
|
||||
eq_(info.plugin_type, 'test_io')
|
||||
eq_(info.absolute_import_path, 'pyexcel_test.x')
|
||||
|
||||
info = CACHED_PLUGIN_INFO["test_io"][0]
|
||||
eq_(info.plugin_type, "test_io")
|
||||
eq_(info.absolute_import_path, "pyexcel_test.x")
|
||||
|
||||
|
||||
@patch('pkgutil.get_importer')
|
||||
@patch('pkgutil.iter_modules')
|
||||
def test_load_plugins_without_pyinstaller(pkgutil_iter_modules,
|
||||
pkgutil_get_importer):
|
||||
test_module_name = 'pyexcel_test'
|
||||
@patch("pkgutil.get_importer")
|
||||
@patch("pkgutil.iter_modules")
|
||||
def test_load_plugins_without_pyinstaller(
|
||||
pkgutil_iter_modules, pkgutil_get_importer
|
||||
):
|
||||
test_module_name = "pyexcel_test"
|
||||
sample_toc = set()
|
||||
pkgutil_get_importer.return_value.toc = sample_toc
|
||||
# mock iter modules
|
||||
pkgutil_iter_modules.return_value = [('not used', test_module_name, True)]
|
||||
pkgutil_iter_modules.return_value = [("not used", test_module_name, True)]
|
||||
from lml.loader import scan_plugins
|
||||
scan_plugins('pyexcel_', '.', ['pyexcel_io'])
|
||||
|
||||
scan_plugins("pyexcel_", ".", ["pyexcel_io"])
|
||||
from lml.plugin import CACHED_PLUGIN_INFO
|
||||
info = CACHED_PLUGIN_INFO['test_io'][0]
|
||||
eq_(info.plugin_type, 'test_io')
|
||||
eq_(info.absolute_import_path, 'pyexcel_test.x')
|
||||
|
||||
info = CACHED_PLUGIN_INFO["test_io"][0]
|
||||
eq_(info.plugin_type, "test_io")
|
||||
eq_(info.absolute_import_path, "pyexcel_test.x")
|
||||
|
||||
|
||||
@patch('pkgutil.get_importer')
|
||||
@patch('pkgutil.iter_modules')
|
||||
@patch('lml.plugin._load_me_later')
|
||||
def test_load_plugins_without_any_plugins(mocked_load_me_later,
|
||||
pkgutil_iter_modules,
|
||||
pkgutil_get_importer):
|
||||
@patch("pkgutil.get_importer")
|
||||
@patch("pkgutil.iter_modules")
|
||||
@patch("lml.plugin._load_me_later")
|
||||
def test_load_plugins_without_any_plugins(
|
||||
mocked_load_me_later, pkgutil_iter_modules, pkgutil_get_importer
|
||||
):
|
||||
sample_toc = set()
|
||||
pkgutil_get_importer.return_value.toc = sample_toc
|
||||
pkgutil_iter_modules.return_value = []
|
||||
from lml.loader import scan_plugins
|
||||
scan_plugins('pyexcel_', '.', ['pyexcel_io'])
|
||||
|
||||
scan_plugins("pyexcel_", ".", ["pyexcel_io"])
|
||||
assert mocked_load_me_later.called is False
|
||||
|
||||
|
||||
@patch('pkgutil.get_importer')
|
||||
@patch('pkgutil.iter_modules')
|
||||
@patch('lml.plugin._load_me_later')
|
||||
def test_load_plugins_without_black_list(mocked_load_me_later,
|
||||
pkgutil_iter_modules,
|
||||
pkgutil_get_importer):
|
||||
@patch("pkgutil.get_importer")
|
||||
@patch("pkgutil.iter_modules")
|
||||
@patch("lml.plugin._load_me_later")
|
||||
def test_load_plugins_without_black_list(
|
||||
mocked_load_me_later, pkgutil_iter_modules, pkgutil_get_importer
|
||||
):
|
||||
sample_toc = set()
|
||||
pkgutil_get_importer.return_value.toc = sample_toc
|
||||
pkgutil_iter_modules.return_value = []
|
||||
from lml.loader import scan_plugins
|
||||
scan_plugins('pyexcel_', '.')
|
||||
|
||||
scan_plugins("pyexcel_", ".")
|
||||
assert mocked_load_me_later.called is False
|
||||
|
||||
|
||||
@patch('pkgutil.get_importer')
|
||||
@patch('pkgutil.iter_modules')
|
||||
@patch('lml.plugin._load_me_later')
|
||||
def test_load_plugins_import_error(mocked_load_me_later,
|
||||
pkgutil_iter_modules,
|
||||
pkgutil_get_importer):
|
||||
sample_toc = set(['test_non_existent_module'])
|
||||
@patch("pkgutil.get_importer")
|
||||
@patch("pkgutil.iter_modules")
|
||||
@patch("lml.plugin._load_me_later")
|
||||
def test_load_plugins_import_error(
|
||||
mocked_load_me_later, pkgutil_iter_modules, pkgutil_get_importer
|
||||
):
|
||||
sample_toc = set(["test_non_existent_module"])
|
||||
pkgutil_get_importer.return_value.toc = sample_toc
|
||||
pkgutil_iter_modules.return_value = [('not used', 'pyexcel_xls', False)]
|
||||
pkgutil_iter_modules.return_value = [("not used", "pyexcel_xls", False)]
|
||||
from lml.loader import scan_plugins
|
||||
scan_plugins('test_', '.', ['pyexcel_io'])
|
||||
|
||||
scan_plugins("test_", ".", ["pyexcel_io"])
|
||||
assert mocked_load_me_later.called is False
|
||||
|
|
|
@ -6,24 +6,24 @@ from nose.tools import eq_, raises
|
|||
|
||||
|
||||
def test_plugin_manager():
|
||||
test_plugin = 'my plugin'
|
||||
test_plugin = "my plugin"
|
||||
manager = PluginManager(test_plugin)
|
||||
assert PLUG_IN_MANAGERS[test_plugin] == manager
|
||||
|
||||
|
||||
def test_load_me_later():
|
||||
test_plugin = 'my plugin'
|
||||
test_plugin = "my plugin"
|
||||
manager = PluginManager(test_plugin)
|
||||
plugin_info = make_me_a_plugin_info(test_plugin)
|
||||
manager.load_me_later(plugin_info)
|
||||
assert list(manager.registry.keys()) == [test_plugin]
|
||||
|
||||
|
||||
@patch('lml.plugin.do_import_class')
|
||||
@patch("lml.plugin.do_import_class")
|
||||
def test_load_me_now(mock_import):
|
||||
custom_class = PluginInfo
|
||||
mock_import.return_value = custom_class
|
||||
test_plugin = 'my plugin'
|
||||
test_plugin = "my plugin"
|
||||
manager = PluginManager(test_plugin)
|
||||
plugin_info = make_me_a_plugin_info(test_plugin)
|
||||
manager.load_me_later(plugin_info)
|
||||
|
@ -32,27 +32,27 @@ def test_load_me_now(mock_import):
|
|||
|
||||
|
||||
@raises(Exception)
|
||||
@patch('lml.plugin.do_import_class')
|
||||
@patch("lml.plugin.do_import_class")
|
||||
def test_load_me_now_exception(mock_import):
|
||||
custom_class = PluginInfo
|
||||
mock_import.return_value = custom_class
|
||||
test_plugin = 'my plugin'
|
||||
test_plugin = "my plugin"
|
||||
manager = PluginManager(test_plugin)
|
||||
plugin_info = make_me_a_plugin_info('my')
|
||||
plugin_info = make_me_a_plugin_info("my")
|
||||
manager.load_me_later(plugin_info)
|
||||
manager.load_me_now('my', 'my special library')
|
||||
manager.load_me_now("my", "my special library")
|
||||
|
||||
|
||||
@raises(Exception)
|
||||
def test_load_me_now_no_key_found():
|
||||
test_plugin = 'my plugin'
|
||||
test_plugin = "my plugin"
|
||||
manager = PluginManager(test_plugin)
|
||||
manager.load_me_now('my', custom_property='here')
|
||||
manager.load_me_now("my", custom_property="here")
|
||||
|
||||
|
||||
@patch('lml.plugin.do_import_class')
|
||||
@patch("lml.plugin.do_import_class")
|
||||
def test_dynamic_load_library(mock_import):
|
||||
test_plugin = 'test plugin'
|
||||
test_plugin = "test plugin"
|
||||
custom_obj = object()
|
||||
mock_import.return_value = custom_obj
|
||||
manager = PluginManager(test_plugin)
|
||||
|
@ -61,9 +61,9 @@ def test_dynamic_load_library(mock_import):
|
|||
eq_(custom_obj, plugin_info.cls)
|
||||
|
||||
|
||||
@patch('lml.plugin.do_import_class')
|
||||
@patch("lml.plugin.do_import_class")
|
||||
def test_dynamic_load_library_no_action(mock_import):
|
||||
test_plugin = 'test plugin'
|
||||
test_plugin = "test plugin"
|
||||
manager = PluginManager(test_plugin)
|
||||
plugin_info = make_me_a_plugin_info(test_plugin)
|
||||
plugin_info.cls = object()
|
||||
|
@ -76,37 +76,38 @@ class TestClass:
|
|||
|
||||
|
||||
def test_register_a_plugin():
|
||||
test_plugin = 'test plugin'
|
||||
test_plugin = "test plugin"
|
||||
|
||||
manager = PluginManager(test_plugin)
|
||||
plugin_info = make_me_a_plugin_info('my')
|
||||
plugin_info = make_me_a_plugin_info("my")
|
||||
manager.register_a_plugin(TestClass, plugin_info)
|
||||
eq_(plugin_info.cls, TestClass)
|
||||
eq_(manager.registry['my'][0], plugin_info)
|
||||
eq_(manager.registry["my"][0], plugin_info)
|
||||
|
||||
|
||||
def test_get_a_plugin():
|
||||
test_plugin = 'test plugin'
|
||||
test_plugin = "test plugin"
|
||||
|
||||
manager = PluginManager(test_plugin)
|
||||
plugin_info = make_me_a_plugin_info('my')
|
||||
plugin_info = make_me_a_plugin_info("my")
|
||||
plugin_info.cls = TestClass
|
||||
manager.register_a_plugin(TestClass, plugin_info)
|
||||
the_plugin = manager.get_a_plugin('my')
|
||||
the_plugin = manager.get_a_plugin("my")
|
||||
assert isinstance(the_plugin, TestClass)
|
||||
|
||||
|
||||
def test_register_class():
|
||||
test_plugin = 'test_plugin'
|
||||
plugin_info = make_me_a_plugin_info('my')
|
||||
test_plugin = "test_plugin"
|
||||
plugin_info = make_me_a_plugin_info("my")
|
||||
CACHED_PLUGIN_INFO[test_plugin].append(plugin_info)
|
||||
manager = PluginManager(test_plugin)
|
||||
assert list(manager.registry.keys()) == ['my']
|
||||
assert list(manager.registry.keys()) == ["my"]
|
||||
|
||||
|
||||
def test_load_me_later_function():
|
||||
from lml.plugin import _load_me_later
|
||||
test_plugin = 'my plugin'
|
||||
|
||||
test_plugin = "my plugin"
|
||||
manager = PluginManager(test_plugin)
|
||||
plugin_info = make_me_a_plugin_info(test_plugin)
|
||||
_load_me_later(plugin_info)
|
||||
|
@ -116,13 +117,14 @@ def test_load_me_later_function():
|
|||
@raises(ImportError)
|
||||
def test_do_import_cls_error():
|
||||
from lml.plugin import do_import_class
|
||||
|
||||
do_import_class("non.exist.class")
|
||||
|
||||
|
||||
def test_register_a_plugin_function_1():
|
||||
PluginManager("test plugin")
|
||||
|
||||
@PluginInfo('test plugin', tags=['akey'])
|
||||
@PluginInfo("test plugin", tags=["akey"])
|
||||
class MyPlugin(object):
|
||||
pass
|
||||
|
||||
|
@ -130,9 +132,9 @@ def test_register_a_plugin_function_1():
|
|||
|
||||
|
||||
def test_register_a_plugin_function_2():
|
||||
non_existent_plugin = 'I have no plugin manager'
|
||||
non_existent_plugin = "I have no plugin manager"
|
||||
|
||||
@PluginInfo(non_existent_plugin, tags=['akey'])
|
||||
@PluginInfo(non_existent_plugin, tags=["akey"])
|
||||
class MyPlugin(object):
|
||||
pass
|
||||
|
||||
|
@ -143,35 +145,34 @@ def test_register_a_plugin_function_2():
|
|||
def test_primary_key():
|
||||
manager = PluginManager("test plugin2")
|
||||
|
||||
@PluginInfo('test plugin2', tags=['primary key', 'key 1', 'key 2'])
|
||||
@PluginInfo("test plugin2", tags=["primary key", "key 1", "key 2"])
|
||||
class MyPlugin(object):
|
||||
pass
|
||||
|
||||
pk = manager.get_primary_key('key 1')
|
||||
eq_(pk, 'primary key')
|
||||
pk = manager.get_primary_key("key 1")
|
||||
eq_(pk, "primary key")
|
||||
|
||||
|
||||
def test_dict_as_plugin_payload():
|
||||
manager = PluginManager("test plugin3")
|
||||
|
||||
plugin = PluginInfo('test plugin3', tags=['primary key', 'key 1', 'key 2'])
|
||||
plugin = PluginInfo("test plugin3", tags=["primary key", "key 1", "key 2"])
|
||||
plugin(dict(B=1))
|
||||
|
||||
instance = manager.load_me_now('key 1')
|
||||
instance = manager.load_me_now("key 1")
|
||||
eq_(instance, dict(B=1))
|
||||
|
||||
|
||||
def test_show_me_your_name():
|
||||
|
||||
class Test(object):
|
||||
pass
|
||||
|
||||
name = _show_me_your_name(Test)
|
||||
eq_(name, 'Test')
|
||||
eq_(name, "Test")
|
||||
|
||||
name2 = _show_me_your_name(dict(A=1))
|
||||
assert 'dict' in name2
|
||||
assert "dict" in name2
|
||||
|
||||
|
||||
def make_me_a_plugin_info(plugin_name):
|
||||
return PluginInfo(plugin_name, 'abs_path', custom='property')
|
||||
return PluginInfo(plugin_name, "abs_path", custom="property")
|
||||
|
|
|
@ -5,7 +5,6 @@ from lml.utils import do_import
|
|||
|
||||
|
||||
def test_json_dumps():
|
||||
|
||||
class TestClass:
|
||||
pass
|
||||
|
||||
|
@ -16,12 +15,14 @@ def test_json_dumps():
|
|||
|
||||
def test_do_import():
|
||||
import pyexcel_test
|
||||
|
||||
pyexcel_test_package = do_import("pyexcel_test")
|
||||
eq_(pyexcel_test_package, pyexcel_test)
|
||||
|
||||
|
||||
def test_do_import_2():
|
||||
import lml.plugin as plugin
|
||||
|
||||
themodule = do_import("lml.plugin")
|
||||
eq_(plugin, themodule)
|
||||
|
||||
|
@ -33,5 +34,6 @@ def test_do_import_error():
|
|||
|
||||
def test_do_import_cls():
|
||||
from lml.utils import do_import_class
|
||||
|
||||
manager = do_import_class("lml.plugin.PluginManager")
|
||||
eq_(manager, PluginManager)
|
||||
|
|
Loading…
Reference in New Issue