[FIX] orm: correctly set the module name on the model, even when imported with

`import openerp.modules.<module_name>`.

bzr revid: vmt@openerp.com-20120109101647-4hvy3n6eifzeozzq
This commit is contained in:
Vo Minh Thu 2012-01-09 11:16:47 +01:00
parent e50025945a
commit f24a29717e
5 changed files with 27 additions and 16 deletions

View File

@ -222,19 +222,20 @@ if __name__ == "__main__":
def find_module(self, module_name, package_path):
module_parts = module_name.split('.')
if len(module_parts) == 3 and module_name.startswith('openerp.addons.'):
if len(module_parts) == 3 and module_name.startswith('openerp.modules.'):
return self # We act as a loader too.
# TODO list of loadable modules can be cached instead of always
# calling get_module_path().
if len(module_parts) == 1 and \
openerp.modules.module.get_module_path(module_parts[0]):
openerp.modules.module.get_module_path(module_parts[0],
display_warning=False):
return self # We act as a loader too.
def load_module(self, module_name):
module_parts = module_name.split('.')
if len(module_parts) == 3 and module_name.startswith('openerp.addons.'):
if len(module_parts) == 3 and module_name.startswith('openerp.modules.'):
module_part = module_parts[2]
if module_name in sys.modules:
return sys.modules[module_name]
@ -247,16 +248,19 @@ if __name__ == "__main__":
try:
# Check if the bare module name clashes with another module.
f, path, descr = imp.find_module(module_part)
print "Warning: ambiguous import:", module_name, f, path, descr
logger = logging.getLogger('init')
logger.warning("""
Ambiguous import: the OpenERP module `%s` is shadowing another module
(available at %s).""" % (module_name, path))
except ImportError, e:
# Using `import openerp.addons.<module_name>` instead of
# `import <module_name>` is ugly but not harmful.
# Using `import <module_name>` instead of
# `import openerp.modules.<module_name>` is ugly but not harmful.
pass
f, path, descr = imp.find_module(module_part, openerp.modules.module.ad_paths)
mod = imp.load_module(module_name, f, path, descr)
sys.modules[module_part] = mod
sys.modules['openerp.addons.' + module_part] = mod
sys.modules['openerp.modules.' + module_part] = mod
return mod
openerp.modules.module.initialize_sys_path()

View File

@ -25,9 +25,9 @@
This module serves to contain all OpenERP addons, across all configured addons
paths. For the code to manage those addons, see openerp.modules.
Addons are made available here (i.e. under openerp.addons) after
openerp.tools.config.parse_config() is called (so that the addons paths
are known).
Addons are made available under `openerp.modules` after
openerp.tools.config.parse_config() is called (so that the addons paths are
known).
This module also conveniently reexports some symbols from openerp.modules.
Importing them from here is deprecated.

View File

@ -70,7 +70,7 @@ def initialize_sys_path():
ad_paths.append(_ad) # for get_module_path
def get_module_path(module, downloaded=False):
def get_module_path(module, downloaded=False, display_warning=True):
"""Return the path of the given module.
Search the addons paths and return the first path where the given
@ -85,7 +85,8 @@ def get_module_path(module, downloaded=False):
if downloaded:
return opj(_ad, module)
logger.notifyChannel('init', netsvc.LOG_WARNING, 'module %s: module not found' % (module,))
if display_warning:
logger.notifyChannel('init', netsvc.LOG_WARNING, 'module %s: module not found' % (module,))
return False

View File

@ -608,7 +608,16 @@ class MetaModel(type):
super(MetaModel, self).__init__(name, bases, attrs)
return
module_name = self.__module__.split('.')[0]
# The (OpenERP) module name can be in the `openerp.modules` namespace
# or not. For instance module `sale` can be imported as
# `openerp.modules.sale` (the good way) or `sale` (for backward
# compatibility).
module_parts = self.__module__.split('.')
if len(module_parts) > 2 and module_parts[0] == 'openerp' and \
module_parts[1] == 'modules':
module_name = self.__module__.split('.')[2]
else:
module_name = self.__module__.split('.')[0]
if not hasattr(self, '_module'):
self._module = module_name

View File

@ -461,9 +461,6 @@ class configmanager(object):
if complete:
openerp.modules.module.initialize_sys_path()
openerp.modules.loading.open_openerp_namespace()
# openerp.addons.__path__.extend(openerp.conf.addons_paths) # This
# is not compatible with initialize_sys_path(): import crm and
# import openerp.addons.crm load twice the module.
def _generate_pgpassfile(self):
"""