cubicweb/__init__.py
changeset 11457 d404fd8499dd
parent 11417 5e5e224239c3
child 11473 f765b1b16a2c
--- a/cubicweb/__init__.py	Wed Jul 06 17:46:39 2016 +0200
+++ b/cubicweb/__init__.py	Wed Aug 31 11:53:21 2016 +0200
@@ -20,6 +20,7 @@
 """
 __docformat__ = "restructuredtext en"
 
+import imp
 import logging
 import os
 import pickle
@@ -280,3 +281,41 @@
     not be processed, a memory allocation error occurred during processing,
     etc.
     """
+
+
+# Import hook for "legacy" cubes ##############################################
+
+class _CubesImporter(object):
+    """Module finder handling redirection of import of "cubes.<name>"
+    to "cubicweb_<name>".
+    """
+
+    @classmethod
+    def install(cls):
+        if not any(isinstance(x, cls) for x in sys.meta_path):
+            self = cls()
+            sys.meta_path.append(self)
+
+    def find_module(self, fullname, path=None):
+        if fullname.startswith('cubes.'):
+            modname = 'cubicweb_' + fullname.split('.', 1)[1]
+            try:
+                modinfo = imp.find_module(modname)
+            except ImportError:
+                return None
+            else:
+                return _CubesLoader(modinfo)
+
+
+class _CubesLoader(object):
+    """Module loader handling redirection of import of "cubes.<name>"
+    to "cubicweb_<name>".
+    """
+
+    def __init__(self, modinfo):
+        self.modinfo = modinfo
+
+    def load_module(self, fullname):
+        if fullname not in sys.modules:  # Otherwise, it's a reload.
+            sys.modules[fullname] = imp.load_module(fullname, *self.modinfo)
+        return sys.modules[fullname]