281 """ |
280 """ |
282 |
281 |
283 |
282 |
284 # Import hook for "legacy" cubes ############################################## |
283 # Import hook for "legacy" cubes ############################################## |
285 |
284 |
286 class _CubesImporter(object): |
285 class _CubesLoader(object): |
287 """Module finder handling redirection of import of "cubes.<name>" |
286 |
288 to "cubicweb_<name>". |
287 def __init__(self, *modinfo): |
289 """ |
288 self.modinfo = modinfo |
290 |
|
291 @classmethod |
|
292 def install(cls): |
|
293 if not any(isinstance(x, cls) for x in sys.meta_path): |
|
294 self = cls() |
|
295 sys.meta_path.append(self) |
|
296 |
|
297 def find_module(self, fullname, path=None): |
|
298 if fullname == 'cubes': |
|
299 return self |
|
300 elif fullname.startswith('cubes.'): |
|
301 modname = 'cubicweb_' + fullname.split('.', 1)[1] |
|
302 try: |
|
303 modinfo = imp.find_module(modname) |
|
304 except ImportError: |
|
305 return None |
|
306 else: |
|
307 return pkgutil.ImpLoader(fullname, *modinfo) |
|
308 |
289 |
309 def load_module(self, fullname): |
290 def load_module(self, fullname): |
310 try: |
291 try: |
311 # If there is an existing module object named 'fullname' in |
292 # If there is an existing module object named 'fullname' in |
312 # sys.modules , the loader must use that existing module. |
293 # sys.modules , the loader must use that existing module. |
313 # Otherwise, the reload() builtin will not work correctly. |
294 # Otherwise, the reload() builtin will not work correctly. |
314 return sys.modules[fullname] |
295 return sys.modules[fullname] |
315 except KeyError: |
296 except KeyError: |
316 pass |
297 pass |
317 if fullname != 'cubes': |
298 if fullname == 'cubes': |
318 raise ImportError('No module named {0}'.format(fullname)) |
299 mod = sys.modules[fullname] = types.ModuleType( |
319 mod = sys.modules[fullname] = types.ModuleType(fullname, doc='CubicWeb cubes') |
300 fullname, doc='CubicWeb cubes') |
|
301 else: |
|
302 modname, file, pathname, description = self.modinfo |
|
303 try: |
|
304 mod = sys.modules[fullname] = imp.load_module( |
|
305 modname, file, pathname, description) |
|
306 finally: |
|
307 # https://docs.python.org/2/library/imp.html#imp.load_module |
|
308 # Important: the caller is responsible for closing the file |
|
309 # argument, if it was not None, even when an exception is |
|
310 # raised. This is best done using a try ... finally statement |
|
311 if file is not None: |
|
312 file.close() |
320 return mod |
313 return mod |
|
314 |
|
315 |
|
316 class _CubesImporter(object): |
|
317 """Module finder handling redirection of import of "cubes.<name>" |
|
318 to "cubicweb_<name>". |
|
319 """ |
|
320 |
|
321 @classmethod |
|
322 def install(cls): |
|
323 if not any(isinstance(x, cls) for x in sys.meta_path): |
|
324 self = cls() |
|
325 sys.meta_path.append(self) |
|
326 |
|
327 def find_module(self, fullname, path=None): |
|
328 if fullname == 'cubes': |
|
329 return _CubesLoader() |
|
330 elif fullname.startswith('cubes.') and fullname.count('.') == 1: |
|
331 modname = 'cubicweb_' + fullname.split('.', 1)[1] |
|
332 try: |
|
333 modinfo = imp.find_module(modname) |
|
334 except ImportError: |
|
335 return None |
|
336 else: |
|
337 return _CubesLoader(modname, *modinfo) |