diff options
author | Philippe Pepiot <philippe.pepiot@logilab.fr> | 2016-12-23 13:39:44 +0100 |
---|---|---|
committer | Philippe Pepiot <philippe.pepiot@logilab.fr> | 2016-12-23 13:39:44 +0100 |
commit | 6c9ed73a6a26affe441840b2e7e37c397a6d2221 (patch) | |
tree | 16a06c7d68c8f64edb54dcb69a7edb9b29a31075 /logilab/common | |
parent | b85f119eb1f3064954e0454b1cfea85b1eb1ef2a (diff) | |
download | logilab-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()
Diffstat (limited to 'logilab/common')
-rw-r--r-- | logilab/common/registry.py | 20 |
1 files changed, 20 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(): |