summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilippe Pepiot <philippe.pepiot@logilab.fr>2016-12-23 13:39:44 +0100
committerPhilippe Pepiot <philippe.pepiot@logilab.fr>2016-12-23 13:39:44 +0100
commit6c9ed73a6a26affe441840b2e7e37c397a6d2221 (patch)
tree16a06c7d68c8f64edb54dcb69a7edb9b29a31075
parentb85f119eb1f3064954e0454b1cfea85b1eb1ef2a (diff)
downloadlogilab-common-6c9ed73a6a26affe441840b2e7e37c397a6d2221.tar.gz
[registry] add register_modnames()
Alternative to register_objects() to inspect modules names instead of directories. Avoiding using bogus logilab.common.modutils.modpath_from_file()
-rw-r--r--logilab/common/registry.py20
-rw-r--r--test/unittest_registry.py9
2 files changed, 29 insertions, 0 deletions
diff --git a/logilab/common/registry.py b/logilab/common/registry.py
index 35b8eb7..9a90008 100644
--- a/logilab/common/registry.py
+++ b/logilab/common/registry.py
@@ -81,6 +81,7 @@ from __future__ import print_function
__docformat__ = "restructuredtext en"
import sys
+import pkgutil
import types
import weakref
import traceback as tb
@@ -492,6 +493,11 @@ class RegistryStore(dict):
.. automethod:: register_objects
+ Alternatively loading could be triggered by calling the
+ :meth:`register_modnames` method, given a list of modules names to inspect.
+
+ .. automethod:: register_modnames
+
For each module, by default, all compatible objects are registered
automatically. However if some objects come as replacement of
other objects, or have to be included only if some condition is
@@ -684,6 +690,20 @@ class RegistryStore(dict):
self.load_file(filepath, modname)
self.initialization_completed()
+ def register_modnames(self, modnames):
+ """register all objects found in <modnames>"""
+ self.reset()
+ self._loadedmods = {}
+ self._toloadmods = {}
+ toload = []
+ for modname in modnames:
+ filepath = pkgutil.find_loader(modname).get_filename()
+ self._toloadmods[modname] = filepath
+ toload.append((filepath, modname))
+ for filepath, modname in toload:
+ self.load_file(filepath, modname)
+ self.initialization_completed()
+
def initialization_completed(self):
"""call initialization_completed() on all known registries"""
for reg in self.values():
diff --git a/test/unittest_registry.py b/test/unittest_registry.py
index d07c73e..c322e05 100644
--- a/test/unittest_registry.py
+++ b/test/unittest_registry.py
@@ -183,6 +183,15 @@ class RegistryStoreTC(TestCase):
self.assertEqual(set(('appobject1', 'appobject2', 'appobject3')),
set(store['zereg']))
+ def test_autoload_modnames(self):
+ store = RegistryStore()
+ store.setdefault('zereg')
+ with prepended_syspath(self.datadir):
+ store.register_modnames(['regobjects', 'regobjects2'])
+ self.assertEqual(['zereg'], list(store.keys()))
+ self.assertEqual(set(('appobject1', 'appobject2', 'appobject3')),
+ set(store['zereg']))
+
class RegistrableInstanceTC(TestCase):