cubicweb/__init__.py
changeset 12543 71aa20cb43f2
parent 12508 a8c1ea390400
child 12567 26744ad37953
equal deleted inserted replaced
12542:85194bd49119 12543:71aa20cb43f2
    18 """CubicWeb is a generic framework to quickly build applications which describes
    18 """CubicWeb is a generic framework to quickly build applications which describes
    19 relations between entitites.
    19 relations between entitites.
    20 """
    20 """
    21 
    21 
    22 
    22 
    23 import imp
       
    24 import logging
    23 import logging
    25 import os
    24 import os
    26 import pickle
    25 import pickle
    27 import sys
    26 import sys
    28 import types
       
    29 import warnings
    27 import warnings
    30 import zlib
    28 import zlib
    31 
    29 
    32 from six import PY2, binary_type, text_type
    30 from six import PY2, binary_type, text_type
    33 
    31 
   266     and not necessarily under the control of the programmer, e.g. an unexpected
   264     and not necessarily under the control of the programmer, e.g. an unexpected
   267     disconnect occurs, the data source name is not found, a transaction could
   265     disconnect occurs, the data source name is not found, a transaction could
   268     not be processed, a memory allocation error occurred during processing,
   266     not be processed, a memory allocation error occurred during processing,
   269     etc.
   267     etc.
   270     """
   268     """
   271 
       
   272 
       
   273 # Import hook for "legacy" cubes ##############################################
       
   274 
       
   275 class _CubesLoader(object):
       
   276 
       
   277     def __init__(self, *modinfo):
       
   278         self.modinfo = modinfo
       
   279 
       
   280     def load_module(self, fullname):
       
   281         try:
       
   282             # If there is an existing module object named 'fullname' in
       
   283             # sys.modules , the loader must use that existing module.
       
   284             # Otherwise, the reload() builtin will not work correctly.
       
   285             return sys.modules[fullname]
       
   286         except KeyError:
       
   287             pass
       
   288         if fullname == 'cubes':
       
   289             mod = sys.modules[fullname] = types.ModuleType(
       
   290                 fullname, doc='CubicWeb cubes')
       
   291         else:
       
   292             modname, file, pathname, description = self.modinfo
       
   293             try:
       
   294                 mod = sys.modules[fullname] = imp.load_module(
       
   295                     modname, file, pathname, description)
       
   296             finally:
       
   297                 # https://docs.python.org/2/library/imp.html#imp.load_module
       
   298                 # Important: the caller is responsible for closing the file
       
   299                 # argument, if it was not None, even when an exception is
       
   300                 # raised. This is best done using a try ... finally statement
       
   301                 if file is not None:
       
   302                     file.close()
       
   303         return mod
       
   304 
       
   305 
       
   306 class _CubesImporter(object):
       
   307     """Module finder handling redirection of import of "cubes.<name>"
       
   308     to "cubicweb_<name>".
       
   309     """
       
   310 
       
   311     @classmethod
       
   312     def install(cls):
       
   313         if not any(isinstance(x, cls) for x in sys.meta_path):
       
   314             self = cls()
       
   315             sys.meta_path.append(self)
       
   316 
       
   317     def find_module(self, fullname, path=None):
       
   318         if fullname == 'cubes':
       
   319             return _CubesLoader()
       
   320         elif fullname.startswith('cubes.') and fullname.count('.') == 1:
       
   321             modname = 'cubicweb_' + fullname.split('.', 1)[1]
       
   322             try:
       
   323                 modinfo = imp.find_module(modname)
       
   324             except ImportError:
       
   325                 return None
       
   326             else:
       
   327                 return _CubesLoader(modname, *modinfo)