local.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 - 2011 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, '__name__', name)
  27. def _get_current_object(self):
  28. """Return the current object. This is useful if you want the real
  29. object behind the proxy at a time for performance reasons or because
  30. you want to pass the object into a different context.
  31. """
  32. if not hasattr(self.__local, '__release_local__'):
  33. return self.__local()
  34. try:
  35. return getattr(self.__local, self.__name__)
  36. except AttributeError:
  37. raise RuntimeError('no object bound to %s' % self.__name__)
  38. @property
  39. def __dict__(self):
  40. try:
  41. return self._get_current_object().__dict__
  42. except RuntimeError:
  43. raise AttributeError('__dict__')
  44. def __repr__(self):
  45. try:
  46. obj = self._get_current_object()
  47. except RuntimeError:
  48. return '<%s unbound>' % self.__class__.__name__
  49. return repr(obj)
  50. def __nonzero__(self):
  51. try:
  52. return bool(self._get_current_object())
  53. except RuntimeError:
  54. return False
  55. def __unicode__(self):
  56. try:
  57. return unicode(self._get_current_object())
  58. except RuntimeError:
  59. return repr(self)
  60. def __dir__(self):
  61. try:
  62. return dir(self._get_current_object())
  63. except RuntimeError:
  64. return []
  65. def __getattr__(self, name):
  66. if name == '__members__':
  67. return dir(self._get_current_object())
  68. return getattr(self._get_current_object(), name)
  69. def __setitem__(self, key, value):
  70. self._get_current_object()[key] = value
  71. def __delitem__(self, key):
  72. del self._get_current_object()[key]
  73. def __setslice__(self, i, j, seq):
  74. self._get_current_object()[i:j] = seq
  75. def __delslice__(self, i, j):
  76. del self._get_current_object()[i:j]
  77. __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
  78. __delattr__ = lambda x, n: delattr(x._get_current_object(), n)
  79. __str__ = lambda x: str(x._get_current_object())
  80. __lt__ = lambda x, o: x._get_current_object() < o
  81. __le__ = lambda x, o: x._get_current_object() <= o
  82. __eq__ = lambda x, o: x._get_current_object() == o
  83. __ne__ = lambda x, o: x._get_current_object() != o
  84. __gt__ = lambda x, o: x._get_current_object() > o
  85. __ge__ = lambda x, o: x._get_current_object() >= o
  86. __cmp__ = lambda x, o: cmp(x._get_current_object(), o)
  87. __hash__ = lambda x: hash(x._get_current_object())
  88. __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
  89. __len__ = lambda x: len(x._get_current_object())
  90. __getitem__ = lambda x, i: x._get_current_object()[i]
  91. __iter__ = lambda x: iter(x._get_current_object())
  92. __contains__ = lambda x, i: i in x._get_current_object()
  93. __getslice__ = lambda x, i, j: x._get_current_object()[i:j]
  94. __add__ = lambda x, o: x._get_current_object() + o
  95. __sub__ = lambda x, o: x._get_current_object() - o
  96. __mul__ = lambda x, o: x._get_current_object() * o
  97. __floordiv__ = lambda x, o: x._get_current_object() // o
  98. __mod__ = lambda x, o: x._get_current_object() % o
  99. __divmod__ = lambda x, o: x._get_current_object().__divmod__(o)
  100. __pow__ = lambda x, o: x._get_current_object() ** o
  101. __lshift__ = lambda x, o: x._get_current_object() << o
  102. __rshift__ = lambda x, o: x._get_current_object() >> o
  103. __and__ = lambda x, o: x._get_current_object() & o
  104. __xor__ = lambda x, o: x._get_current_object() ^ o
  105. __or__ = lambda x, o: x._get_current_object() | o
  106. __div__ = lambda x, o: x._get_current_object().__div__(o)
  107. __truediv__ = lambda x, o: x._get_current_object().__truediv__(o)
  108. __neg__ = lambda x: -(x._get_current_object())
  109. __pos__ = lambda x: +(x._get_current_object())
  110. __abs__ = lambda x: abs(x._get_current_object())
  111. __invert__ = lambda x: ~(x._get_current_object())
  112. __complex__ = lambda x: complex(x._get_current_object())
  113. __int__ = lambda x: int(x._get_current_object())
  114. __long__ = lambda x: long(x._get_current_object())
  115. __float__ = lambda x: float(x._get_current_object())
  116. __oct__ = lambda x: oct(x._get_current_object())
  117. __hex__ = lambda x: hex(x._get_current_object())
  118. __index__ = lambda x: x._get_current_object().__index__()
  119. __coerce__ = lambda x, o: x.__coerce__(x, o)
  120. __enter__ = lambda x: x.__enter__()
  121. __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)