[IMP] module: use the `openerp.addons.` namespace to load addons.
[odoo/odoo.git] / openerp / modules / module.py
index d35ca74..a5c4327 100644 (file)
@@ -63,24 +63,14 @@ def initialize_sys_path():
     not in the PYTHONPATH.
     """
     global ad_paths
-
     if ad_paths:
         return
 
     ad_paths = map(lambda m: os.path.abspath(tools.ustr(m.strip())), tools.config['addons_path'].split(','))
+    ad_paths.append(_ad) # for get_module_path
 
-    sys.path.insert(1, _ad)
-
-    ad_cnt=1
-    for adp in ad_paths:
-        if adp != _ad:
-            sys.path.insert(ad_cnt, adp)
-            ad_cnt+=1
-
-    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
@@ -95,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
 
 
@@ -218,6 +209,11 @@ def get_module_resource(module, *args):
         return resource_path
     return False
 
+def get_module_icon(module):
+    iconpath = ['static', 'src', 'img', 'icon.png']
+    if get_module_resource(module, *iconpath):
+        return ('/' + module + '/') + '/'.join(iconpath)
+    return '/base/'  + '/'.join(iconpath)
 
 def load_information_from_description_file(module):
     """
@@ -231,37 +227,33 @@ def load_information_from_description_file(module):
     if terp_file:
         info = {}
         if os.path.isfile(terp_file) or zipfile.is_zipfile(mod_path+'.zip'):
-            terp_f = tools.file_open(terp_file)
-            try:
-                info = eval(terp_f.read())
-            except Exception:
-                logger.notifyChannel('modules', netsvc.LOG_ERROR,
-                    'module %s: exception while evaluating file %s' %
-                    (module, terp_file))
-                raise
-            finally:
-                terp_f.close()
-            # TODO the version should probably be mandatory
-            info.setdefault('version', '0')
-            info.setdefault('category', 'Uncategorized')
-            info.setdefault('depends', [])
-            info.setdefault('author', '')
-            info.setdefault('website', '')
-            info.setdefault('name', False)
-            info.setdefault('description', '')
-            info.setdefault('complexity', 'normal')
-            info.setdefault('core', False)
-            info.setdefault('icon', 'images/icon.png')
-            info['certificate'] = info.get('certificate') or None
-            info['web'] = info.get('web') or False
-            info['license'] = info.get('license') or 'AGPL-3'
-            info.setdefault('installable', True)
-            info.setdefault('active', False)
-            # If the following is provided, it is called after the module is --loaded.
-            info.setdefault('post_load', None)
-            for kind in ['data', 'demo', 'test',
-                'init_xml', 'update_xml', 'demo_xml']:
-                info.setdefault(kind, [])
+            # default values for descriptor
+            info = {
+                'active': False,
+                'application': False,
+                'author': '',
+                'category': 'Uncategorized',
+                'certificate': None,
+                'complexity': 'normal',
+                'depends': [],
+                'description': '',
+                'icon': get_module_icon(module),
+                'installable': True,
+                'license': 'AGPL-3',
+                'name': False,
+                'post_load': None,
+                'version': '0.0.0',
+                'web': False,
+                'website': '',
+                'sequence': 100,
+            }
+            info.update(itertools.izip(
+                'depends data demo test init_xml update_xml demo_xml'.split(),
+                iter(list, None)))
+
+            with tools.file_open(terp_file) as terp_f:
+                info.update(eval(terp_f.read()))
+
             return info
 
     #TODO: refactor the logger in this file to follow the logging guidelines
@@ -298,17 +290,6 @@ def init_module_models(cr, module_name, obj_list):
         t[1](cr, *t[2])
     cr.commit()
 
-# Import hook to write a addon m in both sys.modules['m'] and
-# sys.modules['openerp.addons.m']. Otherwise it could be loaded twice
-# if imported twice using different names.
-#class MyImportHook(object):
-#    def find_module(self, module_name, package_path):
-#       print ">>>", module_name, package_path
-#    def load_module(self, module_name):
-#       raise ImportError("Restricted")
-
-#sys.meta_path.append(MyImportHook())
-
 def register_module_classes(m):
     """ Register module named m, if not already registered.
 
@@ -333,7 +314,7 @@ def register_module_classes(m):
     try:
         zip_mod_path = mod_path + '.zip'
         if not os.path.isfile(zip_mod_path):
-            __import__(m)
+            __import__('openerp.modules.' + m)
         else:
             zimp = zipimport.zipimporter(zip_mod_path)
             zimp.load_module(m)