summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchfw <wangc_2011@hotmail.com>2018-06-12 22:19:42 (GMT)
committerchfw <wangc_2011@hotmail.com>2018-06-12 22:19:42 (GMT)
commit619a64cd6963f5fdc5caa04adae3ce08d035f18f (patch)
tree7a51f0d87148780197f45faf69d5fdb43e8c32be
parentb7245d0976536adde93204f03f0297b09d1e5608 (diff)
downloadpython-lml-619a64cd6963f5fdc5caa04adae3ce08d035f18f.zip
python-lml-619a64cd6963f5fdc5caa04adae3ce08d035f18f.tar.gz
python-lml-619a64cd6963f5fdc5caa04adae3ce08d035f18f.tar.bz2
:sparkles: law changes: dict is allowed to be plugin payload. functions, classes were allowed before but not dict. this change helps.
-rw-r--r--lml/plugin.py27
-rw-r--r--tests/test_plugin_manager.py24
2 files changed, 43 insertions, 8 deletions
diff --git a/lml/plugin.py b/lml/plugin.py
index 35e26ed..132db4a 100644
--- a/lml/plugin.py
+++ b/lml/plugin.py
@@ -274,7 +274,7 @@ class PluginManager(object):
if __key in self.registry:
for plugin_info in self.registry[__key]:
cls = self.dynamic_load_library(plugin_info)
- module_name = _get_me_pypi_package_name(cls.__module__)
+ module_name = _get_me_pypi_package_name(cls)
if library and module_name != library:
continue
else:
@@ -317,7 +317,8 @@ class PluginManager(object):
plugin_info:
a instance of plugin info
"""
- self._logger.debug("register " + plugin_cls.__name__)
+ 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
@@ -344,7 +345,7 @@ def _register_class(cls):
plugin_info.absolute_import_path)
else:
log.debug("load cached plugin info: %s",
- plugin_info.cls.__name__)
+ _show_me_your_name(plugin_info.cls))
cls.load_me_later(plugin_info)
del CACHED_PLUGIN_INFO[cls.plugin_name]
@@ -357,7 +358,8 @@ 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", plugin_cls.__name__)
+ log.debug("caching %s",
+ _show_me_your_name(plugin_cls.__name__))
CACHED_PLUGIN_INFO[plugin_info.plugin_type].append(plugin_info)
@@ -374,6 +376,17 @@ def _load_me_later(plugin_info):
CACHED_PLUGIN_INFO[plugin_info.plugin_type].append(plugin_info)
-def _get_me_pypi_package_name(module_name):
- root_module_name = module_name.split('.')[0]
- return root_module_name.replace('_', '-')
+def _get_me_pypi_package_name(module):
+ try:
+ module_name = module.__module__
+ root_module_name = module_name.split('.')[0]
+ return root_module_name.replace('_', '-')
+ except AttributeError:
+ return None
+
+
+def _show_me_your_name(cls_func_or_data_type):
+ try:
+ return cls_func_or_data_type.__name__
+ except AttributeError:
+ return str(type(cls_func_or_data_type))
diff --git a/tests/test_plugin_manager.py b/tests/test_plugin_manager.py
index 33782a7..2ef9c76 100644
--- a/tests/test_plugin_manager.py
+++ b/tests/test_plugin_manager.py
@@ -1,6 +1,6 @@
from mock import patch
from lml.plugin import PluginManager, PLUG_IN_MANAGERS
-from lml.plugin import PluginInfo
+from lml.plugin import PluginInfo, _show_me_your_name
from lml.plugin import CACHED_PLUGIN_INFO
from nose.tools import eq_, raises
@@ -151,5 +151,27 @@ def test_primary_key():
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(dict(B=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')
+
+ name2 = _show_me_your_name(dict(A=1))
+ eq_(name2, "<type 'dict'>")
+
+
def make_me_a_plugin_info(plugin_name):
return PluginInfo(plugin_name, 'abs_path', custom='property')