local.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.local
  4. ~~~~~~~~~~~~
  5. This module contains critical utilities that
  6. needs to be loaded as soon as possible, and that
  7. shall not load any third party modules.
  8. :copyright: (c) 2009 - 2012 by Ask Solem.
  9. :license: BSD, see LICENSE for more details.
  10. """
  11. from __future__ import absolute_import
  12. def try_import(module):
  13. """Try to import and return module, or return
  14. None if the module does not exist."""
  15. from importlib import import_module
  16. try:
  17. return import_module(module)
  18. except ImportError:
  19. pass
  20. class Proxy(object):
  21. """Proxy to another object."""
  22. # Code stolen from werkzeug.local.Proxy.
  23. __slots__ = ('__local', '__dict__', '__name__')
  24. def __init__(self, local, name=None):
  25. object.__setattr__(self, '_Proxy__local', local)
  26. object.__setattr__(self, '__custom_name__', name)
  27. @property
  28. def __name__(self):
  29. try:
  30. return object.__getattr__(self, "__custom_name__")
  31. except AttributeError:
  32. return self._get_current_object().__name__
  33. @property
  34. def __doc__(self):
  35. return self._get_current_object().__doc__
  36. def _get_current_object(self):
  37. """Return the current object. This is useful if you want the real
  38. object behind the proxy at a time for performance reasons or because
  39. you want to pass the object into a different context.
  40. """
  41. if not hasattr(self.__local, '__release_local__'):
  42. return self.__local()
  43. try:
  44. return getattr(self.__local, self.__name__)
  45. except AttributeError:
  46. raise RuntimeError('no object bound to %s' % self.__name__)
  47. @property
  48. def __dict__(self):
  49. try:
  50. return self._get_current_object().__dict__
  51. except RuntimeError:
  52. raise AttributeError('__dict__')
  53. def __repr__(self):
  54. try:
  55. obj = self._get_current_object()
  56. except RuntimeError:
  57. return '<%s unbound>' % self.__class__.__name__
  58. return repr(obj)
  59. def __nonzero__(self):
  60. try:
  61. return bool(self._get_current_object())
  62. except RuntimeError:
  63. return False
  64. def __unicode__(self):
  65. try:
  66. return unicode(self._get_current_object())
  67. except RuntimeError:
  68. return repr(self)
  69. def __dir__(self):
  70. try:
  71. return dir(self._get_current_object())
  72. except RuntimeError:
  73. return []
  74. def __getattr__(self, name):
  75. if name == '__members__':
  76. return dir(self._get_current_object())
  77. return getattr(self._get_current_object(), name)
  78. def __setitem__(self, key, value):
  79. self._get_current_object()[key] = value
  80. def __delitem__(self, key):
  81. del self._get_current_object()[key]
  82. def __setslice__(self, i, j, seq):
  83. self._get_current_object()[i:j] = seq
  84. def __delslice__(self, i, j):
  85. del self._get_current_object()[i:j]
  86. __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
  87. __delattr__ = lambda x, n: delattr(x._get_current_object(), n)
  88. __str__ = lambda x: str(x._get_current_object())
  89. __lt__ = lambda x, o: x._get_current_object() < o
  90. __le__ = lambda x, o: x._get_current_object() <= o
  91. __eq__ = lambda x, o: x._get_current_object() == o
  92. __ne__ = lambda x, o: x._get_current_object() != o
  93. __gt__ = lambda x, o: x._get_current_object() > o
  94. __ge__ = lambda x, o: x._get_current_object() >= o
  95. __cmp__ = lambda x, o: cmp(x._get_current_object(), o)
  96. __hash__ = lambda x: hash(x._get_current_object())
  97. __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
  98. __len__ = lambda x: len(x._get_current_object())
  99. __getitem__ = lambda x, i: x._get_current_object()[i]
  100. __iter__ = lambda x: iter(x._get_current_object())
  101. __contains__ = lambda x, i: i in x._get_current_object()
  102. __getslice__ = lambda x, i, j: x._get_current_object()[i:j]
  103. __add__ = lambda x, o: x._get_current_object() + o
  104. __sub__ = lambda x, o: x._get_current_object() - o
  105. __mul__ = lambda x, o: x._get_current_object() * o
  106. __floordiv__ = lambda x, o: x._get_current_object() // o
  107. __mod__ = lambda x, o: x._get_current_object() % o
  108. __divmod__ = lambda x, o: x._get_current_object().__divmod__(o)
  109. __pow__ = lambda x, o: x._get_current_object() ** o
  110. __lshift__ = lambda x, o: x._get_current_object() << o
  111. __rshift__ = lambda x, o: x._get_current_object() >> o
  112. __and__ = lambda x, o: x._get_current_object() & o
  113. __xor__ = lambda x, o: x._get_current_object() ^ o
  114. __or__ = lambda x, o: x._get_current_object() | o
  115. __div__ = lambda x, o: x._get_current_object().__div__(o)
  116. __truediv__ = lambda x, o: x._get_current_object().__truediv__(o)
  117. __neg__ = lambda x: -(x._get_current_object())
  118. __pos__ = lambda x: +(x._get_current_object())
  119. __abs__ = lambda x: abs(x._get_current_object())
  120. __invert__ = lambda x: ~(x._get_current_object())
  121. __complex__ = lambda x: complex(x._get_current_object())
  122. __int__ = lambda x: int(x._get_current_object())
  123. __long__ = lambda x: long(x._get_current_object())
  124. __float__ = lambda x: float(x._get_current_object())
  125. __oct__ = lambda x: oct(x._get_current_object())
  126. __hex__ = lambda x: hex(x._get_current_object())
  127. __index__ = lambda x: x._get_current_object().__index__()
  128. __coerce__ = lambda x, o: x.__coerce__(x, o)
  129. __enter__ = lambda x: x.__enter__()
  130. __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)