You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 line
6.6KB

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