diff options
| author | Christophe Simonis <chs@odoo.com> | 2018-08-07 19:08:07 +0200 |
|---|---|---|
| committer | Christophe Simonis <chs@odoo.com> | 2018-08-07 19:08:07 +0200 |
| commit | efe2dcd7ae6573bf6830918d58f5b89739d67301 (patch) | |
| tree | aec1110b92af4cd8b9153d8b08d63c6878c3efe2 /odoo/tools/misc.py | |
| parent | d7e2c767ce51ddf112babc5507105da4dc127279 (diff) | |
| parent | ced9156a97ea502050b4e827ef2237fa2cd9410f (diff) | |
[MERGE] forward port branch 11.0 up to ced9156a97e
Diffstat (limited to 'odoo/tools/misc.py')
| -rw-r--r-- | odoo/tools/misc.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/odoo/tools/misc.py b/odoo/tools/misc.py index 8b1d7107c0d..639d9c7626e 100644 --- a/odoo/tools/misc.py +++ b/odoo/tools/misc.py @@ -1221,3 +1221,23 @@ pickle.load = _pickle_load pickle.loads = lambda text, encoding='ASCII': _pickle_load(io.BytesIO(text), encoding=encoding) pickle.dump = pickle_.dump pickle.dumps = pickle_.dumps + +def wrap_module(module, attr_list): + """Helper for wrapping a package/module to expose selected attributes + + :param Module module: the actual package/module to wrap, as returned by ``import <module>`` + :param iterable attr_list: a global list of attributes to expose, usually the top-level + attributes and their own main attributes. No support for hiding attributes in case + of name collision at different levels. + """ + attr_list = set(attr_list) + class WrappedModule(object): + def __getattr__(self, attrib): + if attrib in attr_list: + target = getattr(module, attrib) + if isinstance(target, types.ModuleType): + return wrap_module(target, attr_list) + return target + raise AttributeError(attrib) + # module and attr_list are in the closure + return WrappedModule() |
