From f24a29717ebf506e310cca40638db6ccdf17d4b6 Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Mon, 9 Jan 2012 11:16:47 +0100 Subject: [PATCH] [FIX] orm: correctly set the module name on the model, even when imported with `import openerp.modules.`. bzr revid: vmt@openerp.com-20120109101647-4hvy3n6eifzeozzq --- openerp-server | 18 +++++++++++------- openerp/addons/__init__.py | 6 +++--- openerp/modules/module.py | 5 +++-- openerp/osv/orm.py | 11 ++++++++++- openerp/tools/config.py | 3 --- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/openerp-server b/openerp-server index 8d710141500..641bc7e97e2 100755 --- a/openerp-server +++ b/openerp-server @@ -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.` instead of - # `import ` is ugly but not harmful. + # Using `import ` instead of + # `import openerp.modules.` 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() diff --git a/openerp/addons/__init__.py b/openerp/addons/__init__.py index 1f863eca509..02a5c9aa0a7 100644 --- a/openerp/addons/__init__.py +++ b/openerp/addons/__init__.py @@ -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. diff --git a/openerp/modules/module.py b/openerp/modules/module.py index 43b11b0d1b0..936c798767a 100644 --- a/openerp/modules/module.py +++ b/openerp/modules/module.py @@ -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 diff --git a/openerp/osv/orm.py b/openerp/osv/orm.py index 8236f096abf..23cc040af47 100644 --- a/openerp/osv/orm.py +++ b/openerp/osv/orm.py @@ -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 diff --git a/openerp/tools/config.py b/openerp/tools/config.py index 00815ed2055..8c980447f52 100644 --- a/openerp/tools/config.py +++ b/openerp/tools/config.py @@ -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): """