local.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. # since each thread has its own greenlet we can just use those as identifiers
  12. # for the context. If greenlets are not available we fall back to the
  13. # current thread ident.
  14. try:
  15. from greenlet import getcurrent as get_ident
  16. except ImportError: # pragma: no cover
  17. try:
  18. from thread import get_ident # noqa
  19. except ImportError: # pragma: no cover
  20. try:
  21. from dummy_thread import get_ident # noqa
  22. except ImportError: # pragma: no cover
  23. from _thread import get_ident # noqa
  24. def try_import(module, default=None):
  25. """Try to import and return module, or return
  26. None if the module does not exist."""
  27. from importlib import import_module
  28. try:
  29. return import_module(module)
  30. except ImportError:
  31. return default
  32. class Proxy(object):
  33. """Proxy to another object."""
  34. # Code stolen from werkzeug.local.Proxy.
  35. __slots__ = ('__local', '__args', '__kwargs', '__dict__', '__name__')
  36. def __init__(self, local, args=None, kwargs=None, name=None):
  37. object.__setattr__(self, '_Proxy__local', local)
  38. object.__setattr__(self, '_Proxy__args', args or ())
  39. object.__setattr__(self, '_Proxy__kwargs', kwargs or {})
  40. if name is not None:
  41. object.__setattr__(self, '__custom_name__', name)
  42. @property
  43. def __name__(self):
  44. try:
  45. return self.__custom_name__
  46. except AttributeError:
  47. return self._get_current_object().__name__
  48. @property
  49. def __module__(self):
  50. return self._get_current_object().__module__
  51. @property
  52. def __doc__(self):
  53. return self._get_current_object().__doc__
  54. def _get_class(self):
  55. return self._get_current_object().__class__
  56. @property
  57. def __class__(self):
  58. return self._get_class()
  59. def _get_current_object(self):
  60. """Return the current object. This is useful if you want the real
  61. object behind the proxy at a time for performance reasons or because
  62. you want to pass the object into a different context.
  63. """
  64. if not hasattr(self.__local, '__release_local__'):
  65. return self.__local(*self.__args, **self.__kwargs)
  66. try:
  67. return getattr(self.__local, self.__name__)
  68. except AttributeError:
  69. raise RuntimeError('no object bound to {0.__name__}'.format(self))
  70. @property
  71. def __dict__(self):
  72. try:
  73. return self._get_current_object().__dict__
  74. except RuntimeError: # pragma: no cover
  75. raise AttributeError('__dict__')
  76. def __repr__(self):
  77. try:
  78. obj = self._get_current_object()
  79. except RuntimeError: # pragma: no cover
  80. return '<{0} unbound>'.format(self.__class__.__name__)
  81. return repr(obj)
  82. def __nonzero__(self):
  83. try:
  84. return bool(self._get_current_object())
  85. except RuntimeError: # pragma: no cover
  86. return False
  87. def __unicode__(self):
  88. try:
  89. return unicode(self._get_current_object())
  90. except RuntimeError: # pragma: no cover
  91. return repr(self)
  92. def __dir__(self):
  93. try:
  94. return dir(self._get_current_object())
  95. except RuntimeError: # pragma: no cover
  96. return []
  97. def __getattr__(self, name):
  98. if name == '__members__':
  99. return dir(self._get_current_object())
  100. return getattr(self._get_current_object(), name)
  101. def __setitem__(self, key, value):
  102. self._get_current_object()[key] = value
  103. def __delitem__(self, key):
  104. del self._get_current_object()[key]
  105. def __setslice__(self, i, j, seq):
  106. self._get_current_object()[i:j] = seq
  107. def __delslice__(self, i, j):
  108. del self._get_current_object()[i:j]
  109. __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
  110. __delattr__ = lambda x, n: delattr(x._get_current_object(), n)
  111. __str__ = lambda x: str(x._get_current_object())
  112. __lt__ = lambda x, o: x._get_current_object() < o
  113. __le__ = lambda x, o: x._get_current_object() <= o
  114. __eq__ = lambda x, o: x._get_current_object() == o
  115. __ne__ = lambda x, o: x._get_current_object() != o
  116. __gt__ = lambda x, o: x._get_current_object() > o
  117. __ge__ = lambda x, o: x._get_current_object() >= o
  118. __cmp__ = lambda x, o: cmp(x._get_current_object(), o)
  119. __hash__ = lambda x: hash(x._get_current_object())
  120. __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
  121. __len__ = lambda x: len(x._get_current_object())
  122. __getitem__ = lambda x, i: x._get_current_object()[i]
  123. __iter__ = lambda x: iter(x._get_current_object())
  124. __contains__ = lambda x, i: i in x._get_current_object()
  125. __getslice__ = lambda x, i, j: x._get_current_object()[i:j]
  126. __add__ = lambda x, o: x._get_current_object() + o
  127. __sub__ = lambda x, o: x._get_current_object() - o
  128. __mul__ = lambda x, o: x._get_current_object() * o
  129. __floordiv__ = lambda x, o: x._get_current_object() // o
  130. __mod__ = lambda x, o: x._get_current_object() % o
  131. __divmod__ = lambda x, o: x._get_current_object().__divmod__(o)
  132. __pow__ = lambda x, o: x._get_current_object() ** o
  133. __lshift__ = lambda x, o: x._get_current_object() << o
  134. __rshift__ = lambda x, o: x._get_current_object() >> o
  135. __and__ = lambda x, o: x._get_current_object() & o
  136. __xor__ = lambda x, o: x._get_current_object() ^ o
  137. __or__ = lambda x, o: x._get_current_object() | o
  138. __div__ = lambda x, o: x._get_current_object().__div__(o)
  139. __truediv__ = lambda x, o: x._get_current_object().__truediv__(o)
  140. __neg__ = lambda x: -(x._get_current_object())
  141. __pos__ = lambda x: +(x._get_current_object())
  142. __abs__ = lambda x: abs(x._get_current_object())
  143. __invert__ = lambda x: ~(x._get_current_object())
  144. __complex__ = lambda x: complex(x._get_current_object())
  145. __int__ = lambda x: int(x._get_current_object())
  146. __long__ = lambda x: long(x._get_current_object())
  147. __float__ = lambda x: float(x._get_current_object())
  148. __oct__ = lambda x: oct(x._get_current_object())
  149. __hex__ = lambda x: hex(x._get_current_object())
  150. __index__ = lambda x: x._get_current_object().__index__()
  151. __coerce__ = lambda x, o: x.__coerce__(x, o)
  152. __enter__ = lambda x: x._get_current_object().__enter__()
  153. __exit__ = lambda x, *a, **kw: x._get_current_object().__exit__(*a, **kw)
  154. __reduce__ = lambda x: x._get_current_object().__reduce__()
  155. class PromiseProxy(Proxy):
  156. """This is a proxy to an object that has not yet been evaulated.
  157. :class:`Proxy` will evaluate the object each time, while the
  158. promise will only evaluate it once.
  159. """
  160. def _get_current_object(self):
  161. try:
  162. return object.__getattribute__(self, '__thing')
  163. except AttributeError:
  164. return self.__evaluate__()
  165. def __evaluated__(self):
  166. try:
  167. object.__getattribute__(self, '__thing')
  168. except AttributeError:
  169. return False
  170. return True
  171. def __maybe_evaluate__(self):
  172. return self._get_current_object()
  173. def __evaluate__(self):
  174. try:
  175. thing = Proxy._get_current_object(self)
  176. object.__setattr__(self, '__thing', thing)
  177. return thing
  178. finally:
  179. object.__delattr__(self, '_Proxy__local')
  180. object.__delattr__(self, '_Proxy__args')
  181. object.__delattr__(self, '_Proxy__kwargs')
  182. def maybe_evaluate(obj):
  183. try:
  184. return obj.__maybe_evaluate__()
  185. except AttributeError:
  186. return obj
  187. def release_local(local):
  188. """Releases the contents of the local for the current context.
  189. This makes it possible to use locals without a manager.
  190. Example::
  191. >>> loc = Local()
  192. >>> loc.foo = 42
  193. >>> release_local(loc)
  194. >>> hasattr(loc, 'foo')
  195. False
  196. With this function one can release :class:`Local` objects as well
  197. as :class:`StackLocal` objects. However it is not possible to
  198. release data held by proxies that way, one always has to retain
  199. a reference to the underlying local object in order to be able
  200. to release it.
  201. .. versionadded:: 0.6.1
  202. """
  203. local.__release_local__()
  204. class Local(object):
  205. __slots__ = ('__storage__', '__ident_func__')
  206. def __init__(self):
  207. object.__setattr__(self, '__storage__', {})
  208. object.__setattr__(self, '__ident_func__', get_ident)
  209. def __iter__(self):
  210. return iter(self.__storage__.items())
  211. def __call__(self, proxy):
  212. """Create a proxy for a name."""
  213. return Proxy(self, proxy)
  214. def __release_local__(self):
  215. self.__storage__.pop(self.__ident_func__(), None)
  216. def __getattr__(self, name):
  217. try:
  218. return self.__storage__[self.__ident_func__()][name]
  219. except KeyError:
  220. raise AttributeError(name)
  221. def __setattr__(self, name, value):
  222. ident = self.__ident_func__()
  223. storage = self.__storage__
  224. try:
  225. storage[ident][name] = value
  226. except KeyError:
  227. storage[ident] = {name: value}
  228. def __delattr__(self, name):
  229. try:
  230. del self.__storage__[self.__ident_func__()][name]
  231. except KeyError:
  232. raise AttributeError(name)
  233. class LocalStack(object):
  234. """This class works similar to a :class:`Local` but keeps a stack
  235. of objects instead. This is best explained with an example::
  236. >>> ls = LocalStack()
  237. >>> ls.push(42)
  238. >>> ls.top
  239. 42
  240. >>> ls.push(23)
  241. >>> ls.top
  242. 23
  243. >>> ls.pop()
  244. 23
  245. >>> ls.top
  246. 42
  247. They can be force released by using a :class:`LocalManager` or with
  248. the :func:`release_local` function but the correct way is to pop the
  249. item from the stack after using. When the stack is empty it will
  250. no longer be bound to the current context (and as such released).
  251. By calling the stack without arguments it returns a proxy that resolves to
  252. the topmost item on the stack.
  253. """
  254. def __init__(self):
  255. self._local = Local()
  256. def __release_local__(self):
  257. self._local.__release_local__()
  258. def _get__ident_func__(self):
  259. return self._local.__ident_func__
  260. def _set__ident_func__(self, value):
  261. object.__setattr__(self._local, '__ident_func__', value)
  262. __ident_func__ = property(_get__ident_func__, _set__ident_func__)
  263. del _get__ident_func__, _set__ident_func__
  264. def __call__(self):
  265. def _lookup():
  266. rv = self.top
  267. if rv is None:
  268. raise RuntimeError('object unbound')
  269. return rv
  270. return Proxy(_lookup)
  271. def push(self, obj):
  272. """Pushes a new item to the stack"""
  273. rv = getattr(self._local, 'stack', None)
  274. if rv is None:
  275. self._local.stack = rv = []
  276. rv.append(obj)
  277. return rv
  278. def pop(self):
  279. """Removes the topmost item from the stack, will return the
  280. old value or `None` if the stack was already empty.
  281. """
  282. stack = getattr(self._local, 'stack', None)
  283. if stack is None:
  284. return None
  285. elif len(stack) == 1:
  286. release_local(self._local)
  287. return stack[-1]
  288. else:
  289. return stack.pop()
  290. @property
  291. def stack(self):
  292. """get_current_worker_task uses this to find
  293. the original task that was executed by the worker."""
  294. stack = getattr(self._local, 'stack', None)
  295. if stack is not None:
  296. return stack
  297. return []
  298. @property
  299. def top(self):
  300. """The topmost item on the stack. If the stack is empty,
  301. `None` is returned.
  302. """
  303. try:
  304. return self._local.stack[-1]
  305. except (AttributeError, IndexError):
  306. return None
  307. class LocalManager(object):
  308. """Local objects cannot manage themselves. For that you need a local
  309. manager. You can pass a local manager multiple locals or add them later
  310. by appending them to `manager.locals`. Everytime the manager cleans up
  311. it, will clean up all the data left in the locals for this context.
  312. The `ident_func` parameter can be added to override the default ident
  313. function for the wrapped locals.
  314. .. versionchanged:: 0.6.1
  315. Instead of a manager the :func:`release_local` function can be used
  316. as well.
  317. .. versionchanged:: 0.7
  318. `ident_func` was added.
  319. """
  320. def __init__(self, locals=None, ident_func=None):
  321. if locals is None:
  322. self.locals = []
  323. elif isinstance(locals, Local):
  324. self.locals = [locals]
  325. else:
  326. self.locals = list(locals)
  327. if ident_func is not None:
  328. self.ident_func = ident_func
  329. for local in self.locals:
  330. object.__setattr__(local, '__ident_func__', ident_func)
  331. else:
  332. self.ident_func = get_ident
  333. def get_ident(self):
  334. """Return the context identifier the local objects use internally for
  335. this context. You cannot override this method to change the behavior
  336. but use it to link other context local objects (such as SQLAlchemy's
  337. scoped sessions) to the Werkzeug locals.
  338. .. versionchanged:: 0.7
  339. You can pass a different ident function to the local manager that
  340. will then be propagated to all the locals passed to the
  341. constructor.
  342. """
  343. return self.ident_func()
  344. def cleanup(self):
  345. """Manually clean up the data in the locals for this context. Call
  346. this at the end of the request or use `make_middleware()`.
  347. """
  348. for local in self.locals:
  349. release_local(local)
  350. def __repr__(self):
  351. return '<{0} storages: {1}>'.format(
  352. self.__class__.__name__, len(self.locals))