🤝 merge with master
This commit is contained in:
commit
56fbecb2aa
|
@ -1,7 +1,7 @@
|
|||
Change log
|
||||
================================================================================
|
||||
|
||||
-
|
||||
0.0.8 - 4/1/2019
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
Updated
|
||||
|
@ -10,6 +10,24 @@ Updated
|
|||
#. `#9 <https://github.com/chfw/lml/issues/9>`_: include tests, docs for
|
||||
OpenSuse package validation
|
||||
|
||||
0.0.7 - 17/11/2018
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
Fixed
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
#. `#8 <https://github.com/chfw/lml/issues/8>`_: get_primary_key will fail when
|
||||
a module is loaded later
|
||||
#. deprecated old style plugin scanner: scan_plugins
|
||||
|
||||
0.0.6 - 07/11/2018
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
Fixed
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
#. Revert the version 0.0.5 changes. Raise Import error and log the exception
|
||||
|
||||
0.0.5 - 06/11/2018
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -5,6 +5,21 @@ releases:
|
|||
- action: Updated
|
||||
details:
|
||||
- "`#9`: include tests, docs for OpenSuse package validation"
|
||||
date: 4/1/2019
|
||||
version: 0.0.8
|
||||
- changes:
|
||||
- action: Fixed
|
||||
details:
|
||||
- "`#8`: get_primary_key will fail when a module is loaded later"
|
||||
- "deprecated old style plugin scanner: scan_plugins"
|
||||
date: 17/11/2018
|
||||
version: 0.0.7
|
||||
- changes:
|
||||
- action: Fixed
|
||||
details:
|
||||
- "Revert the version 0.0.5 changes. Raise Import error and log the exception"
|
||||
date: 07/11/2018
|
||||
version: 0.0.6
|
||||
- changes:
|
||||
- action: Fixed
|
||||
details:
|
||||
|
|
|
@ -24,7 +24,7 @@ DESCRIPTION = (
|
|||
# -- Project information -----------------------------------------------------
|
||||
|
||||
project = u'lml'
|
||||
copyright = u'2017-2018 Onni Software Ltd.'
|
||||
copyright = u'2017-2019 Onni Software Ltd.'
|
||||
author = u'C.W.'
|
||||
|
||||
# The short X.Y version
|
||||
|
|
2
lml.yml
2
lml.yml
|
@ -7,7 +7,7 @@ company: "Onni Software Ltd."
|
|||
version: "0.0.8"
|
||||
current_version: "0.0.8"
|
||||
release: "0.0.8"
|
||||
copyright_year: 2017-2018
|
||||
copyright_year: 2017-2019
|
||||
license: New BSD
|
||||
dependencies: []
|
||||
description: "Load me later. A lazy plugin management system."
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
import re
|
||||
import logging
|
||||
import pkgutil
|
||||
import warnings
|
||||
from itertools import chain
|
||||
|
||||
from lml.utils import do_import
|
||||
|
@ -59,6 +60,9 @@ def scan_plugins(
|
|||
is listed in white_list.
|
||||
"""
|
||||
__plugin_name_patterns = "^%s.+$" % prefix
|
||||
warnings.warn(
|
||||
"Deprecated! since version 0.0.3. Please use scan_plugins_regex!"
|
||||
)
|
||||
scan_plugins_regex(
|
||||
plugin_name_patterns=__plugin_name_patterns,
|
||||
pyinstaller_path=pyinstaller_path,
|
||||
|
|
|
@ -255,8 +255,7 @@ class PluginManager(object):
|
|||
a instance of plugin info
|
||||
"""
|
||||
self._logger.debug("load %s later", plugin_info.absolute_import_path)
|
||||
for key in plugin_info.tags():
|
||||
self.registry[key.lower()].append(plugin_info)
|
||||
self._update_registry_and_expand_tag_groups(plugin_info)
|
||||
|
||||
def load_me_now(self, key, library=None, **keywords):
|
||||
"""
|
||||
|
@ -319,18 +318,21 @@ class PluginManager(object):
|
|||
a instance of plugin info
|
||||
"""
|
||||
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
|
||||
self.registry[key.lower()].append(plugin_info)
|
||||
if index == 0:
|
||||
primary_tag = key.lower()
|
||||
self.tag_groups[key.lower()] = primary_tag
|
||||
plugin_info.cls = plugin_cls
|
||||
self._update_registry_and_expand_tag_groups(plugin_info)
|
||||
|
||||
def get_primary_key(self, key):
|
||||
__key = key.lower()
|
||||
return self.tag_groups.get(__key, None)
|
||||
|
||||
def _update_registry_and_expand_tag_groups(self, plugin_info):
|
||||
primary_tag = None
|
||||
for index, key in enumerate(plugin_info.tags()):
|
||||
self.registry[key.lower()].append(plugin_info)
|
||||
if index == 0:
|
||||
primary_tag = key.lower()
|
||||
self.tag_groups[key.lower()] = primary_tag
|
||||
|
||||
|
||||
def _register_class(cls):
|
||||
"""Reigister a newly created plugin manager"""
|
||||
|
|
18
lml/utils.py
18
lml/utils.py
|
@ -12,7 +12,6 @@ import logging
|
|||
from json import JSONEncoder, dumps
|
||||
|
||||
PY2 = sys.version_info[0] == 2
|
||||
PY36 = sys.version_info[0] == 3 and sys.version_info[1] >= 6
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
@ -39,16 +38,13 @@ def json_dumps(keywords):
|
|||
|
||||
def do_import(plugin_module_name):
|
||||
"""dynamically import a module"""
|
||||
if PY36:
|
||||
try:
|
||||
return _do_import(plugin_module_name)
|
||||
except (ImportError, ModuleNotFoundError): # noqa: F821
|
||||
log.exception("failed to import %s", plugin_module_name)
|
||||
else:
|
||||
try:
|
||||
return _do_import(plugin_module_name)
|
||||
except ImportError:
|
||||
log.exception("failed to import %s", plugin_module_name)
|
||||
try:
|
||||
return _do_import(plugin_module_name)
|
||||
except ImportError:
|
||||
log.exception(
|
||||
"%s is abscent or cannot be imported", plugin_module_name
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
def _do_import(plugin_module_name):
|
||||
|
|
|
@ -33,6 +33,8 @@ def test_load_me_now(mock_import):
|
|||
manager.load_me_later(plugin_info)
|
||||
actual = manager.load_me_now(test_plugin)
|
||||
eq_(actual, custom_class)
|
||||
eq_(manager.tag_groups, {"my plugin": "my plugin"})
|
||||
eq_(plugin_info, manager.registry["my plugin"][0])
|
||||
|
||||
|
||||
@raises(Exception)
|
||||
|
@ -87,6 +89,7 @@ def test_register_a_plugin():
|
|||
manager.register_a_plugin(TestClass, plugin_info)
|
||||
eq_(plugin_info.cls, TestClass)
|
||||
eq_(manager.registry["my"][0], plugin_info)
|
||||
eq_(manager.tag_groups, {"my": "my"})
|
||||
|
||||
|
||||
def test_get_a_plugin():
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from mock import patch
|
||||
from lml.utils import do_import, json_dumps
|
||||
from lml.plugin import PluginManager
|
||||
from nose.tools import eq_
|
||||
from nose.tools import eq_, raises
|
||||
|
||||
|
||||
def test_json_dumps():
|
||||
|
@ -27,10 +27,11 @@ def test_do_import_2():
|
|||
eq_(plugin, themodule)
|
||||
|
||||
|
||||
@raises(ImportError)
|
||||
@patch("lml.utils.log.exception")
|
||||
def test_do_import_error(mock_exception):
|
||||
do_import("non.exist")
|
||||
mock_exception.assert_called_with("failed to import %s", "non.exist")
|
||||
mock_exception.assert_called_with("No module named 'non'")
|
||||
|
||||
|
||||
def test_do_import_cls():
|
||||
|
|
Loading…
Reference in New Issue