signals.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """
  2. Implements signals based on blinker if available, otherwise
  3. falls silently back to a noop. Shamelessly stolen from flask.signals:
  4. https://github.com/mitsuhiko/flask/blob/master/flask/signals.py
  5. """
  6. signals_available = False
  7. try:
  8. from blinker import Namespace
  9. signals_available = True
  10. except ImportError: # noqa
  11. class Namespace:
  12. def signal(self, name, doc=None):
  13. return _FakeSignal(name, doc)
  14. class _FakeSignal:
  15. """If blinker is unavailable, create a fake class with the same
  16. interface that allows sending of signals but will fail with an
  17. error on anything else. Instead of doing anything on send, it
  18. will just ignore the arguments and do nothing instead.
  19. """
  20. def __init__(self, name, doc=None):
  21. self.name = name
  22. self.__doc__ = doc
  23. def _fail(self, *args, **kwargs):
  24. raise RuntimeError('signalling support is unavailable '
  25. 'because the blinker library is '
  26. 'not installed.')
  27. send = lambda *a, **kw: None
  28. connect = disconnect = has_receivers_for = receivers_for = \
  29. temporarily_connected_to = connected_to = _fail
  30. del _fail
  31. # The namespace for code signals. If you are not oauthlib code, do
  32. # not put signals in here. Create your own namespace instead.
  33. _signals = Namespace()
  34. # Core signals.
  35. scope_changed = _signals.signal('scope-changed')