[cwconfig] Use imp.load_source instead of exec() to load site_cubicweb.py from apphome
authorJulien Cristau <julien.cristau@logilab.fr>
Wed, 16 Mar 2016 15:55:56 +0100
changeset 11211 34c903b883ab
parent 11210 c582542d3cc1
child 11212 3309ddb97059
[cwconfig] Use imp.load_source instead of exec() to load site_cubicweb.py from apphome Lets us return a module object and go back to the previous code in CubicWebConfiguration._load_site_cubicweb.
cubicweb/cwconfig.py
--- a/cubicweb/cwconfig.py	Tue Mar 15 11:24:51 2016 +0100
+++ b/cubicweb/cwconfig.py	Wed Mar 16 15:55:56 2016 +0100
@@ -776,24 +776,20 @@
                 continue
         if self.apphome is not None:
             # Would occur, e.g., upon `cubicweb-ctl i18ncube <cube>`.
-            self._load_app_site_cubicweb()
+            self._load_site_cubicweb(None)
 
     def _load_site_cubicweb(self, cube):
-        """Load site_cubicweb.py from `cube`."""
-        modname = 'cubes.%s.site_cubicweb' % cube
-        __import__(modname)
-        return sys.modules[modname].__dict__
-
-    def _load_app_site_cubicweb(self):
-        """Load site_cubicweb.py in `apphome`."""
-        assert self.apphome is not None
-        site_globals = {}
-        apphome_site = join(self.apphome, 'site_cubicweb.py')
-        if exists(apphome_site):
-            with open(apphome_site, 'rb') as f:
-                code = compile(f.read(), apphome_site, 'exec')
-                exec(code, site_globals)
-        return site_globals
+        """Load site_cubicweb.py from `cube` (or apphome if cube is None)."""
+        if cube is not None:
+            modname = 'cubes.%s.site_cubicweb' % cube
+            __import__(modname)
+            return sys.modules[modname]
+        else:
+            import imp
+            apphome_site = join(self.apphome, 'site_cubicweb.py')
+            if exists(apphome_site):
+                with open(apphome_site, 'rb') as f:
+                    return imp.load_source('site_cubicweb', apphome_site, f)
 
     def cwproperty_definitions(self):
         cfg = self.persistent_options_configuration()
@@ -1159,8 +1155,8 @@
     def _load_site_cubicweb(self, cube):
         # overridden to register cube specific options
         mod = super(CubicWebConfiguration, self)._load_site_cubicweb(cube)
-        if 'options' in mod:
-            self.register_options(mod['options'])
+        if getattr(mod, 'options', None):
+            self.register_options(mod.options)
             self.load_defaults()
 
     def init_log(self, logthreshold=None, force=False):