diff options
Diffstat (limited to 'odoo/tools/misc.py')
| -rw-r--r-- | odoo/tools/misc.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/odoo/tools/misc.py b/odoo/tools/misc.py index 6233f4572b1..18a92b5fd9d 100644 --- a/odoo/tools/misc.py +++ b/odoo/tools/misc.py @@ -19,6 +19,7 @@ import socket import sys import threading import time +import types import werkzeug.utils import zipfile from cStringIO import StringIO @@ -1217,3 +1218,23 @@ class Pickle(object): dump = cPickle.dump pickle = Pickle + +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() |
