local.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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: # pragma: no cover
  78. # not sure what this is about
  79. return getattr(loc, self.__name__)
  80. except AttributeError: # pragma: no cover
  81. raise RuntimeError('no object bound to {0.__name__}'.format(self))
  82. @property
  83. def __dict__(self):
  84. try:
  85. return self._get_current_object().__dict__
  86. except RuntimeError: # pragma: no cover
  87. raise AttributeError('__dict__')
  88. def __repr__(self):
  89. try:
  90. obj = self._get_current_object()
  91. except RuntimeError: # pragma: no cover
  92. return '<{0} unbound>'.format(self.__class__.__name__)
  93. return repr(obj)
  94. def __bool__(self):
  95. try:
  96. return bool(self._get_current_object())
  97. except RuntimeError: # pragma: no cover
  98. return False
  99. __nonzero__ = __bool__ # Py2
  100. def __unicode__(self):
  101. try:
  102. return string(self._get_current_object())
  103. except RuntimeError: # pragma: no cover
  104. return repr(self)
  105. def __dir__(self):
  106. try:
  107. return dir(self._get_current_object())
  108. except RuntimeError: # pragma: no cover
  109. return []
  110. def __getattr__(self, name):
  111. if name == '__members__':
  112. return dir(self._get_current_object())
  113. return getattr(self._get_current_object(), name)
  114. def __setitem__(self, key, value):
  115. self._get_current_object()[key] = value
  116. def __delitem__(self, key):
  117. del self._get_current_object()[key]
  118. def __setslice__(self, i, j, seq):
  119. self._get_current_object()[i:j] = seq
  120. def __delslice__(self, i, j):
  121. del self._get_current_object()[i:j]
  122. def __setattr__(self, name, value):
  123. setattr(self._get_current_object(), name, value)
  124. def __delattr__(self, name):
  125. delattr(self._get_current_object(), name)
  126. def __str__(self):
  127. return str(self._get_current_object())
  128. def __lt__(self, other):
  129. return self._get_current_object() < other
  130. def __le__(self, other):
  131. return self._get_current_object() <= other
  132. def __eq__(self, other):
  133. return self._get_current_object() == other
  134. def __ne__(self, other):
  135. return self._get_current_object() != other
  136. def __gt__(self, other):
  137. return self._get_current_object() > other
  138. def __ge__(self, other):
  139. return self._get_current_object() >= other
  140. def __hash__(self):
  141. return hash(self._get_current_object())
  142. def __call__(self, *a, **kw):
  143. return self._get_current_object()(*a, **kw)
  144. def __len__(self):
  145. return len(self._get_current_object())
  146. def __getitem__(self, i):
  147. return self._get_current_object()[i]
  148. def __iter__(self):
  149. return iter(self._get_current_object())
  150. def __contains__(self, i):
  151. return i in self._get_current_object()
  152. def __getslice__(self, i, j):
  153. return self._get_current_object()[i:j]
  154. def __add__(self, other):
  155. return self._get_current_object() + other
  156. def __sub__(self, other):
  157. return self._get_current_object() - other
  158. def __mul__(self, other):
  159. return self._get_current_object() * other
  160. def __floordiv__(self, other):
  161. return self._get_current_object() // other
  162. def __mod__(self, other):
  163. return self._get_current_object() % other
  164. def __divmod__(self, other):
  165. return self._get_current_object().__divmod__(other)
  166. def __pow__(self, other):
  167. return self._get_current_object() ** other
  168. def __lshift__(self, other):
  169. return self._get_current_object() << other
  170. def __rshift__(self, other):
  171. return self._get_current_object() >> other
  172. def __and__(self, other):
  173. return self._get_current_object() & other
  174. def __xor__(self, other):
  175. return self._get_current_object() ^ other
  176. def __or__(self, other):
  177. return self._get_current_object() | other
  178. def __div__(self, other):
  179. return self._get_current_object().__div__(other)
  180. def __truediv__(self, other):
  181. return self._get_current_object().__truediv__(other)
  182. def __neg__(self):
  183. return -(self._get_current_object())
  184. def __pos__(self):
  185. return +(self._get_current_object())
  186. def __abs__(self):
  187. return abs(self._get_current_object())
  188. def __invert__(self):
  189. return ~(self._get_current_object())
  190. def __complex__(self):
  191. return complex(self._get_current_object())
  192. def __int__(self):
  193. return int(self._get_current_object())
  194. def __float__(self):
  195. return float(self._get_current_object())
  196. def __oct__(self):
  197. return oct(self._get_current_object())
  198. def __hex__(self):
  199. return hex(self._get_current_object())
  200. def __index__(self):
  201. return self._get_current_object().__index__()
  202. def __coerce__(self, other):
  203. return self._get_current_object().__coerce__(other)
  204. def __enter__(self):
  205. return self._get_current_object().__enter__()
  206. def __exit__(self, *a, **kw):
  207. return self._get_current_object().__exit__(*a, **kw)
  208. def __reduce__(self):
  209. return self._get_current_object().__reduce__()
  210. if not PY3: # pragma: no cover
  211. def __cmp__(self, other):
  212. return cmp(self._get_current_object(), other) # noqa
  213. def __long__(self):
  214. return long(self._get_current_object()) # noqa
  215. class PromiseProxy(Proxy):
  216. """This is a proxy to an object that has not yet been evaulated.
  217. :class:`Proxy` will evaluate the object each time, while the
  218. promise will only evaluate it once.
  219. """
  220. __slots__ = ('__pending__',)
  221. def _get_current_object(self):
  222. try:
  223. return object.__getattribute__(self, '__thing')
  224. except AttributeError:
  225. return self.__evaluate__()
  226. def __then__(self, fun, *args, **kwargs):
  227. if self.__evaluated__():
  228. return fun(*args, **kwargs)
  229. from collections import deque
  230. try:
  231. pending = object.__getattribute__(self, '__pending__')
  232. except AttributeError:
  233. pending = None
  234. if pending is None:
  235. pending = deque()
  236. object.__setattr__(self, '__pending__', pending)
  237. pending.append((fun, args, kwargs))
  238. def __evaluated__(self):
  239. try:
  240. object.__getattribute__(self, '__thing')
  241. except AttributeError:
  242. return False
  243. return True
  244. def __maybe_evaluate__(self):
  245. return self._get_current_object()
  246. def __evaluate__(self,
  247. _clean=('_Proxy__local',
  248. '_Proxy__args',
  249. '_Proxy__kwargs')):
  250. try:
  251. thing = Proxy._get_current_object(self)
  252. except:
  253. raise
  254. else:
  255. object.__setattr__(self, '__thing', thing)
  256. for attr in _clean:
  257. try:
  258. object.__delattr__(self, attr)
  259. except AttributeError: # pragma: no cover
  260. # May mask errors so ignore
  261. pass
  262. try:
  263. pending = object.__getattribute__(self, '__pending__')
  264. except AttributeError:
  265. pass
  266. else:
  267. try:
  268. while pending:
  269. fun, args, kwargs = pending.popleft()
  270. fun(*args, **kwargs)
  271. finally:
  272. try:
  273. object.__delattr__(self, '__pending__')
  274. except AttributeError: # pragma: no cover
  275. pass
  276. return thing
  277. def maybe_evaluate(obj):
  278. try:
  279. return obj.__maybe_evaluate__()
  280. except AttributeError:
  281. return obj