local.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. Parts of this module is Copyright by Werkzeug Team.
  9. """
  10. from __future__ import absolute_import
  11. import importlib
  12. import sys
  13. from .five import string
  14. __all__ = ['Proxy', 'PromiseProxy', 'try_import', 'maybe_evaluate']
  15. __module__ = __name__ # used by Proxy class body
  16. PY3 = sys.version_info[0] == 3
  17. def _default_cls_attr(name, type_, cls_value):
  18. # Proxy uses properties to forward the standard
  19. # class attributes __module__, __name__ and __doc__ to the real
  20. # object, but these needs to be a string when accessed from
  21. # the Proxy class directly. This is a hack to make that work.
  22. # -- See Issue #1087.
  23. def __new__(cls, getter):
  24. instance = type_.__new__(cls, cls_value)
  25. instance.__getter = getter
  26. return instance
  27. def __get__(self, obj, cls=None):
  28. return self.__getter(obj) if obj is not None else self
  29. return type(name, (type_,), {
  30. '__new__': __new__, '__get__': __get__,
  31. })
  32. def try_import(module, default=None):
  33. """Try to import and return module, or return
  34. None if the module does not exist."""
  35. try:
  36. return importlib.import_module(module)
  37. except ImportError:
  38. return default
  39. class Proxy(object):
  40. """Proxy to another object."""
  41. # Code stolen from werkzeug.local.Proxy.
  42. __slots__ = ('__local', '__args', '__kwargs', '__dict__')
  43. def __init__(self, local,
  44. args=None, kwargs=None, name=None, __doc__=None):
  45. object.__setattr__(self, '_Proxy__local', local)
  46. object.__setattr__(self, '_Proxy__args', args or ())
  47. object.__setattr__(self, '_Proxy__kwargs', kwargs or {})
  48. if name is not None:
  49. object.__setattr__(self, '__custom_name__', name)
  50. if __doc__ is not None:
  51. object.__setattr__(self, '__doc__', __doc__)
  52. @_default_cls_attr('name', str, __name__)
  53. def __name__(self):
  54. try:
  55. return self.__custom_name__
  56. except AttributeError:
  57. return self._get_current_object().__name__
  58. @_default_cls_attr('module', str, __module__)
  59. def __module__(self):
  60. return self._get_current_object().__module__
  61. @_default_cls_attr('doc', str, __doc__)
  62. def __doc__(self):
  63. return self._get_current_object().__doc__
  64. def _get_class(self):
  65. return self._get_current_object().__class__
  66. @property
  67. def __class__(self):
  68. return self._get_class()
  69. def _get_current_object(self):
  70. """Return the current object. This is useful if you want the real
  71. object behind the proxy at a time for performance reasons or because
  72. you want to pass the object into a different context.
  73. """
  74. loc = object.__getattribute__(self, '_Proxy__local')
  75. if not hasattr(loc, '__release_local__'):
  76. return loc(*self.__args, **self.__kwargs)
  77. try:
  78. return getattr(loc, self.__name__)
  79. except AttributeError:
  80. raise RuntimeError('no object bound to {0.__name__}'.format(self))
  81. @property
  82. def __dict__(self):
  83. try:
  84. return self._get_current_object().__dict__
  85. except RuntimeError: # pragma: no cover
  86. raise AttributeError('__dict__')
  87. def __repr__(self):
  88. try:
  89. obj = self._get_current_object()
  90. except RuntimeError: # pragma: no cover
  91. return '<{0} unbound>'.format(self.__class__.__name__)
  92. return repr(obj)
  93. def __bool__(self):
  94. try:
  95. return bool(self._get_current_object())
  96. except RuntimeError: # pragma: no cover
  97. return False
  98. __nonzero__ = __bool__ # Py2
  99. def __unicode__(self):
  100. try:
  101. return string(self._get_current_object())
  102. except RuntimeError: # pragma: no cover
  103. return repr(self)
  104. def __dir__(self):
  105. try:
  106. return dir(self._get_current_object())
  107. except RuntimeError: # pragma: no cover
  108. return []
  109. def __getattr__(self, name):
  110. if name == '__members__':
  111. return dir(self._get_current_object())
  112. return getattr(self._get_current_object(), name)
  113. def __setitem__(self, key, value):
  114. self._get_current_object()[key] = value
  115. def __delitem__(self, key):
  116. del self._get_current_object()[key]
  117. def __setslice__(self, i, j, seq):
  118. self._get_current_object()[i:j] = seq
  119. def __delslice__(self, i, j):
  120. del self._get_current_object()[i:j]
  121. __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
  122. __delattr__ = lambda x, n: delattr(x._get_current_object(), n)
  123. __str__ = lambda x: str(x._get_current_object())
  124. __lt__ = lambda x, o: x._get_current_object() < o
  125. __le__ = lambda x, o: x._get_current_object() <= o
  126. __eq__ = lambda x, o: x._get_current_object() == o
  127. __ne__ = lambda x, o: x._get_current_object() != o
  128. __gt__ = lambda x, o: x._get_current_object() > o
  129. __ge__ = lambda x, o: x._get_current_object() >= o
  130. __hash__ = lambda x: hash(x._get_current_object())
  131. __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
  132. __len__ = lambda x: len(x._get_current_object())
  133. __getitem__ = lambda x, i: x._get_current_object()[i]
  134. __iter__ = lambda x: iter(x._get_current_object())
  135. __contains__ = lambda x, i: i in x._get_current_object()
  136. __getslice__ = lambda x, i, j: x._get_current_object()[i:j]
  137. __add__ = lambda x, o: x._get_current_object() + o
  138. __sub__ = lambda x, o: x._get_current_object() - o
  139. __mul__ = lambda x, o: x._get_current_object() * o
  140. __floordiv__ = lambda x, o: x._get_current_object() // o
  141. __mod__ = lambda x, o: x._get_current_object() % o
  142. __divmod__ = lambda x, o: x._get_current_object().__divmod__(o)
  143. __pow__ = lambda x, o: x._get_current_object() ** o
  144. __lshift__ = lambda x, o: x._get_current_object() << o
  145. __rshift__ = lambda x, o: x._get_current_object() >> o
  146. __and__ = lambda x, o: x._get_current_object() & o
  147. __xor__ = lambda x, o: x._get_current_object() ^ o
  148. __or__ = lambda x, o: x._get_current_object() | o
  149. __div__ = lambda x, o: x._get_current_object().__div__(o)
  150. __truediv__ = lambda x, o: x._get_current_object().__truediv__(o)
  151. __neg__ = lambda x: -(x._get_current_object())
  152. __pos__ = lambda x: +(x._get_current_object())
  153. __abs__ = lambda x: abs(x._get_current_object())
  154. __invert__ = lambda x: ~(x._get_current_object())
  155. __complex__ = lambda x: complex(x._get_current_object())
  156. __int__ = lambda x: int(x._get_current_object())
  157. __float__ = lambda x: float(x._get_current_object())
  158. __oct__ = lambda x: oct(x._get_current_object())
  159. __hex__ = lambda x: hex(x._get_current_object())
  160. __index__ = lambda x: x._get_current_object().__index__()
  161. __coerce__ = lambda x, o: x._get_current_object().__coerce__(o)
  162. __enter__ = lambda x: x._get_current_object().__enter__()
  163. __exit__ = lambda x, *a, **kw: x._get_current_object().__exit__(*a, **kw)
  164. __reduce__ = lambda x: x._get_current_object().__reduce__()
  165. if not PY3:
  166. __cmp__ = lambda x, o: cmp(x._get_current_object(), o) # noqa
  167. __long__ = lambda x: long(x._get_current_object()) # noqa
  168. class PromiseProxy(Proxy):
  169. """This is a proxy to an object that has not yet been evaulated.
  170. :class:`Proxy` will evaluate the object each time, while the
  171. promise will only evaluate it once.
  172. """
  173. __slots__ = ('__pending__',)
  174. def _get_current_object(self):
  175. try:
  176. return object.__getattribute__(self, '__thing')
  177. except AttributeError:
  178. return self.__evaluate__()
  179. def __then__(self, fun, *args, **kwargs):
  180. if self.__evaluated__():
  181. return fun(*args, **kwargs)
  182. from collections import deque
  183. try:
  184. pending = object.__getattribute__(self, '__pending__')
  185. except AttributeError:
  186. pending = None
  187. if pending is None:
  188. pending = deque()
  189. object.__setattr__(self, '__pending__', pending)
  190. pending.append((fun, args, kwargs))
  191. def __evaluated__(self):
  192. try:
  193. object.__getattribute__(self, '__thing')
  194. except AttributeError:
  195. return False
  196. return True
  197. def __maybe_evaluate__(self):
  198. return self._get_current_object()
  199. def __evaluate__(self,
  200. _clean=('_Proxy__local',
  201. '_Proxy__args',
  202. '_Proxy__kwargs')):
  203. try:
  204. thing = Proxy._get_current_object(self)
  205. except:
  206. raise
  207. else:
  208. object.__setattr__(self, '__thing', thing)
  209. for attr in _clean:
  210. try:
  211. object.__delattr__(self, attr)
  212. except AttributeError: # pragma: no cover
  213. # May mask errors so ignore
  214. pass
  215. try:
  216. pending = object.__getattribute__(self, '__pending__')
  217. except AttributeError:
  218. pass
  219. else:
  220. try:
  221. while pending:
  222. fun, args, kwargs = pending.popleft()
  223. fun(*args, **kwargs)
  224. finally:
  225. try:
  226. object.__delattr__(self, '__pending__')
  227. except AttributeError:
  228. pass
  229. return thing
  230. def maybe_evaluate(obj):
  231. try:
  232. return obj.__maybe_evaluate__()
  233. except AttributeError:
  234. return obj