api.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # event/api.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Public API functions for the event system.
  8. """
  9. from __future__ import absolute_import
  10. from .. import util, exc
  11. from .base import _registrars
  12. from .registry import _EventKey
  13. CANCEL = util.symbol('CANCEL')
  14. NO_RETVAL = util.symbol('NO_RETVAL')
  15. def _event_key(target, identifier, fn):
  16. for evt_cls in _registrars[identifier]:
  17. tgt = evt_cls._accept_with(target)
  18. if tgt is not None:
  19. return _EventKey(target, identifier, fn, tgt)
  20. else:
  21. raise exc.InvalidRequestError("No such event '%s' for target '%s'" %
  22. (identifier, target))
  23. def listen(target, identifier, fn, *args, **kw):
  24. """Register a listener function for the given target.
  25. e.g.::
  26. from sqlalchemy import event
  27. from sqlalchemy.schema import UniqueConstraint
  28. def unique_constraint_name(const, table):
  29. const.name = "uq_%s_%s" % (
  30. table.name,
  31. list(const.columns)[0].name
  32. )
  33. event.listen(
  34. UniqueConstraint,
  35. "after_parent_attach",
  36. unique_constraint_name)
  37. A given function can also be invoked for only the first invocation
  38. of the event using the ``once`` argument::
  39. def on_config():
  40. do_config()
  41. event.listen(Mapper, "before_configure", on_config, once=True)
  42. .. versionadded:: 0.9.4 Added ``once=True`` to :func:`.event.listen`
  43. and :func:`.event.listens_for`.
  44. .. note::
  45. The :func:`.listen` function cannot be called at the same time
  46. that the target event is being run. This has implications
  47. for thread safety, and also means an event cannot be added
  48. from inside the listener function for itself. The list of
  49. events to be run are present inside of a mutable collection
  50. that can't be changed during iteration.
  51. Event registration and removal is not intended to be a "high
  52. velocity" operation; it is a configurational operation. For
  53. systems that need to quickly associate and deassociate with
  54. events at high scale, use a mutable structure that is handled
  55. from inside of a single listener.
  56. .. versionchanged:: 1.0.0 - a ``collections.deque()`` object is now
  57. used as the container for the list of events, which explicitly
  58. disallows collection mutation while the collection is being
  59. iterated.
  60. .. seealso::
  61. :func:`.listens_for`
  62. :func:`.remove`
  63. """
  64. _event_key(target, identifier, fn).listen(*args, **kw)
  65. def listens_for(target, identifier, *args, **kw):
  66. """Decorate a function as a listener for the given target + identifier.
  67. e.g.::
  68. from sqlalchemy import event
  69. from sqlalchemy.schema import UniqueConstraint
  70. @event.listens_for(UniqueConstraint, "after_parent_attach")
  71. def unique_constraint_name(const, table):
  72. const.name = "uq_%s_%s" % (
  73. table.name,
  74. list(const.columns)[0].name
  75. )
  76. A given function can also be invoked for only the first invocation
  77. of the event using the ``once`` argument::
  78. @event.listens_for(Mapper, "before_configure", once=True)
  79. def on_config():
  80. do_config()
  81. .. versionadded:: 0.9.4 Added ``once=True`` to :func:`.event.listen`
  82. and :func:`.event.listens_for`.
  83. .. seealso::
  84. :func:`.listen` - general description of event listening
  85. """
  86. def decorate(fn):
  87. listen(target, identifier, fn, *args, **kw)
  88. return fn
  89. return decorate
  90. def remove(target, identifier, fn):
  91. """Remove an event listener.
  92. The arguments here should match exactly those which were sent to
  93. :func:`.listen`; all the event registration which proceeded as a result
  94. of this call will be reverted by calling :func:`.remove` with the same
  95. arguments.
  96. e.g.::
  97. # if a function was registered like this...
  98. @event.listens_for(SomeMappedClass, "before_insert", propagate=True)
  99. def my_listener_function(*arg):
  100. pass
  101. # ... it's removed like this
  102. event.remove(SomeMappedClass, "before_insert", my_listener_function)
  103. Above, the listener function associated with ``SomeMappedClass`` was also
  104. propagated to subclasses of ``SomeMappedClass``; the :func:`.remove`
  105. function will revert all of these operations.
  106. .. versionadded:: 0.9.0
  107. .. note::
  108. The :func:`.remove` function cannot be called at the same time
  109. that the target event is being run. This has implications
  110. for thread safety, and also means an event cannot be removed
  111. from inside the listener function for itself. The list of
  112. events to be run are present inside of a mutable collection
  113. that can't be changed during iteration.
  114. Event registration and removal is not intended to be a "high
  115. velocity" operation; it is a configurational operation. For
  116. systems that need to quickly associate and deassociate with
  117. events at high scale, use a mutable structure that is handled
  118. from inside of a single listener.
  119. .. versionchanged:: 1.0.0 - a ``collections.deque()`` object is now
  120. used as the container for the list of events, which explicitly
  121. disallows collection mutation while the collection is being
  122. iterated.
  123. .. seealso::
  124. :func:`.listen`
  125. """
  126. _event_key(target, identifier, fn).remove()
  127. def contains(target, identifier, fn):
  128. """Return True if the given target/ident/fn is set up to listen.
  129. .. versionadded:: 0.9.0
  130. """
  131. return _event_key(target, identifier, fn).contains()