[MERGE] merged the new floats rounding code.
[odoo/odoo.git] / openerp / modules / module.py
index 2bf8631..7f93427 100644 (file)
@@ -31,7 +31,6 @@ import openerp.osv as osv
 import openerp.tools as tools
 import openerp.tools.osutil as osutil
 from openerp.tools.safe_eval import safe_eval as eval
-import openerp.pooler as pooler
 from openerp.tools.translate import _
 
 import openerp.netsvc as netsvc
@@ -58,6 +57,11 @@ loaded = []
 logger = netsvc.Logger()
 
 def initialize_sys_path():
+    """ Add all addons paths in sys.path.
+
+    This ensures something like ``import crm`` works even if the addons are
+    not in the PYTHONPATH.
+    """
     global ad_paths
 
     if ad_paths:
@@ -214,6 +218,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):
     """
@@ -227,32 +236,32 @@ 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['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)
-            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': '',
+            }
+            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
@@ -289,23 +298,22 @@ 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")
 
-def load_module(module_name):
-    """ Load a Python module found on the addons paths."""
-    fm = imp.find_module(module_name, ad_paths)
-    try:
-        imp.load_module(module_name, *fm)
-    finally:
-        if fm[0]:
-            fm[0].close()
-
+#sys.meta_path.append(MyImportHook())
 
 def register_module_classes(m):
     """ Register module named m, if not already registered.
 
-    This will load the module and register all of its models. (Actually, the
-    explicit constructor call of each of the models inside the module will
-    register them.)
+    This loads the module and register all of its models, thanks to either
+    the MetaModel metaclass, or the explicit instantiation of the model.
 
     """
 
@@ -325,7 +333,7 @@ def register_module_classes(m):
     try:
         zip_mod_path = mod_path + '.zip'
         if not os.path.isfile(zip_mod_path):
-            load_module(m)
+            __import__(m)
         else:
             zimp = zipimport.zipimporter(zip_mod_path)
             zimp.load_module(m)