imports.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.utils.import
  4. ~~~~~~~~~~~~~~~~~~~
  5. Utilities related to importing modules and symbols by name.
  6. """
  7. from __future__ import absolute_import
  8. import imp as _imp
  9. import importlib
  10. import os
  11. import sys
  12. from contextlib import contextmanager
  13. from kombu.utils import symbol_by_name
  14. from .compat import reload
  15. class NotAPackage(Exception):
  16. pass
  17. def qualname(obj): # noqa
  18. if not hasattr(obj, '__name__') and hasattr(obj, '__class__'):
  19. obj = obj.__class__
  20. return '%s.%s' % (obj.__module__, obj.__name__)
  21. def instantiate(name, *args, **kwargs):
  22. """Instantiate class by name.
  23. See :func:`symbol_by_name`.
  24. """
  25. return symbol_by_name(name)(*args, **kwargs)
  26. @contextmanager
  27. def cwd_in_path():
  28. cwd = os.getcwd()
  29. if cwd in sys.path:
  30. yield
  31. else:
  32. sys.path.insert(0, cwd)
  33. try:
  34. yield cwd
  35. finally:
  36. try:
  37. sys.path.remove(cwd)
  38. except ValueError: # pragma: no cover
  39. pass
  40. def find_module(module, path=None, imp=None):
  41. """Version of :func:`imp.find_module` supporting dots."""
  42. if imp is None:
  43. imp = importlib.import_module
  44. with cwd_in_path():
  45. if '.' in module:
  46. last = None
  47. parts = module.split('.')
  48. for i, part in enumerate(parts[:-1]):
  49. mpart = imp('.'.join(parts[:i + 1]))
  50. try:
  51. path = mpart.__path__
  52. except AttributeError:
  53. raise NotAPackage(module)
  54. last = _imp.find_module(parts[i + 1], path)
  55. return last
  56. return _imp.find_module(module)
  57. def import_from_cwd(module, imp=None, package=None):
  58. """Import module, but make sure it finds modules
  59. located in the current directory.
  60. Modules located in the current directory has
  61. precedence over modules located in `sys.path`.
  62. """
  63. if imp is None:
  64. imp = importlib.import_module
  65. with cwd_in_path():
  66. return imp(module, package=package)
  67. def reload_from_cwd(module, reloader=None):
  68. if reloader is None:
  69. reloader = reload
  70. with cwd_in_path():
  71. return reloader(module)
  72. def module_file(module):
  73. name = module.__file__
  74. return name[:-1] if name.endswith('.pyc') else name