from odoo.tools import wrap_module
mods = {'parser', 'relativedelta', 'rrule', 'tz'}
attribs = {atr for m in mods for atr in getattr(dateutil, m).__all__}
dateutil = wrap_module(dateutil, mods | attribs)
The above code is used in odoo13, how to modify the code into odoo14.
- odoo14 has no wrap module function, only the wrap module class
- odoo14->safe_eval.py
Class wrap_module:
def __init__(self, module, attributes):
"""Helper for wrapping a package/module to expose selected attributes
:param module: the actual package/module to wrap, as returned by ``import ``
:param iterable attributes: attributes to expose / whitelist. If a dict,
the keys are the attributes and the values
are used as an ``attributes`` in case the
corresponding item is a submodule
"""
# builtin modules don't have a __file__ at all
modfile = getattr(module, '__file__', '(built-in)')
self._repr = f""
for attrib in attributes:
target = getattr(module, attrib)
if isinstance(target, types.ModuleType):
target = wrap_module(target, attributes[attrib])
setattr(self, attrib, target)
def __repr__(self):
return self._repr
- odoo13->misc.py
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 ``
: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.
"""
wrapper = _cache.get(module)
if wrapper:
return wrapper
attr_list = attr_list and set(attr_list)
class WrappedModule(object):
def __getattr__(self, attrib):
# respect whitelist if there is one
if attr_list is not None and attrib not in attr_list:
raise AttributeError(attrib)
target = getattr(module, attrib)
if isinstance(target, types.ModuleType):
wrapper = _cache.get(target, _missing)
if wrapper is None:
raise AttributeError(attrib)
if wrapper is _missing:
target = wrap_module(target, attr_list)
else:
target = wrapper
setattr(self, attrib, target)
return target
# module and attr_list are in the closure
wrapper = WrappedModule()
_cache.setdefault(module, wrapper)
return wrapper