attr.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. # event/attr.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. """Attribute implementation for _Dispatch classes.
  8. The various listener targets for a particular event class are represented
  9. as attributes, which refer to collections of listeners to be fired off.
  10. These collections can exist at the class level as well as at the instance
  11. level. An event is fired off using code like this::
  12. some_object.dispatch.first_connect(arg1, arg2)
  13. Above, ``some_object.dispatch`` would be an instance of ``_Dispatch`` and
  14. ``first_connect`` is typically an instance of ``_ListenerCollection``
  15. if event listeners are present, or ``_EmptyListener`` if none are present.
  16. The attribute mechanics here spend effort trying to ensure listener functions
  17. are available with a minimum of function call overhead, that unnecessary
  18. objects aren't created (i.e. many empty per-instance listener collections),
  19. as well as that everything is garbage collectable when owning references are
  20. lost. Other features such as "propagation" of listener functions across
  21. many ``_Dispatch`` instances, "joining" of multiple ``_Dispatch`` instances,
  22. as well as support for subclass propagation (e.g. events assigned to
  23. ``Pool`` vs. ``QueuePool``) are all implemented here.
  24. """
  25. from __future__ import absolute_import, with_statement
  26. from .. import util
  27. from ..util import threading
  28. from . import registry
  29. from . import legacy
  30. from itertools import chain
  31. import weakref
  32. import collections
  33. class RefCollection(util.MemoizedSlots):
  34. __slots__ = 'ref',
  35. def _memoized_attr_ref(self):
  36. return weakref.ref(self, registry._collection_gced)
  37. class _ClsLevelDispatch(RefCollection):
  38. """Class-level events on :class:`._Dispatch` classes."""
  39. __slots__ = ('name', 'arg_names', 'has_kw',
  40. 'legacy_signatures', '_clslevel', '__weakref__')
  41. def __init__(self, parent_dispatch_cls, fn):
  42. self.name = fn.__name__
  43. argspec = util.inspect_getargspec(fn)
  44. self.arg_names = argspec.args[1:]
  45. self.has_kw = bool(argspec.keywords)
  46. self.legacy_signatures = list(reversed(
  47. sorted(
  48. getattr(fn, '_legacy_signatures', []),
  49. key=lambda s: s[0]
  50. )
  51. ))
  52. fn.__doc__ = legacy._augment_fn_docs(self, parent_dispatch_cls, fn)
  53. self._clslevel = weakref.WeakKeyDictionary()
  54. def _adjust_fn_spec(self, fn, named):
  55. if named:
  56. fn = self._wrap_fn_for_kw(fn)
  57. if self.legacy_signatures:
  58. try:
  59. argspec = util.get_callable_argspec(fn, no_self=True)
  60. except TypeError:
  61. pass
  62. else:
  63. fn = legacy._wrap_fn_for_legacy(self, fn, argspec)
  64. return fn
  65. def _wrap_fn_for_kw(self, fn):
  66. def wrap_kw(*args, **kw):
  67. argdict = dict(zip(self.arg_names, args))
  68. argdict.update(kw)
  69. return fn(**argdict)
  70. return wrap_kw
  71. def insert(self, event_key, propagate):
  72. target = event_key.dispatch_target
  73. assert isinstance(target, type), \
  74. "Class-level Event targets must be classes."
  75. stack = [target]
  76. while stack:
  77. cls = stack.pop(0)
  78. stack.extend(cls.__subclasses__())
  79. if cls is not target and cls not in self._clslevel:
  80. self.update_subclass(cls)
  81. else:
  82. if cls not in self._clslevel:
  83. self._clslevel[cls] = collections.deque()
  84. self._clslevel[cls].appendleft(event_key._listen_fn)
  85. registry._stored_in_collection(event_key, self)
  86. def append(self, event_key, propagate):
  87. target = event_key.dispatch_target
  88. assert isinstance(target, type), \
  89. "Class-level Event targets must be classes."
  90. stack = [target]
  91. while stack:
  92. cls = stack.pop(0)
  93. stack.extend(cls.__subclasses__())
  94. if cls is not target and cls not in self._clslevel:
  95. self.update_subclass(cls)
  96. else:
  97. if cls not in self._clslevel:
  98. self._clslevel[cls] = collections.deque()
  99. self._clslevel[cls].append(event_key._listen_fn)
  100. registry._stored_in_collection(event_key, self)
  101. def update_subclass(self, target):
  102. if target not in self._clslevel:
  103. self._clslevel[target] = collections.deque()
  104. clslevel = self._clslevel[target]
  105. for cls in target.__mro__[1:]:
  106. if cls in self._clslevel:
  107. clslevel.extend([
  108. fn for fn
  109. in self._clslevel[cls]
  110. if fn not in clslevel
  111. ])
  112. def remove(self, event_key):
  113. target = event_key.dispatch_target
  114. stack = [target]
  115. while stack:
  116. cls = stack.pop(0)
  117. stack.extend(cls.__subclasses__())
  118. if cls in self._clslevel:
  119. self._clslevel[cls].remove(event_key._listen_fn)
  120. registry._removed_from_collection(event_key, self)
  121. def clear(self):
  122. """Clear all class level listeners"""
  123. to_clear = set()
  124. for dispatcher in self._clslevel.values():
  125. to_clear.update(dispatcher)
  126. dispatcher.clear()
  127. registry._clear(self, to_clear)
  128. def for_modify(self, obj):
  129. """Return an event collection which can be modified.
  130. For _ClsLevelDispatch at the class level of
  131. a dispatcher, this returns self.
  132. """
  133. return self
  134. class _InstanceLevelDispatch(RefCollection):
  135. __slots__ = ()
  136. def _adjust_fn_spec(self, fn, named):
  137. return self.parent._adjust_fn_spec(fn, named)
  138. class _EmptyListener(_InstanceLevelDispatch):
  139. """Serves as a proxy interface to the events
  140. served by a _ClsLevelDispatch, when there are no
  141. instance-level events present.
  142. Is replaced by _ListenerCollection when instance-level
  143. events are added.
  144. """
  145. propagate = frozenset()
  146. listeners = ()
  147. __slots__ = 'parent', 'parent_listeners', 'name'
  148. def __init__(self, parent, target_cls):
  149. if target_cls not in parent._clslevel:
  150. parent.update_subclass(target_cls)
  151. self.parent = parent # _ClsLevelDispatch
  152. self.parent_listeners = parent._clslevel[target_cls]
  153. self.name = parent.name
  154. def for_modify(self, obj):
  155. """Return an event collection which can be modified.
  156. For _EmptyListener at the instance level of
  157. a dispatcher, this generates a new
  158. _ListenerCollection, applies it to the instance,
  159. and returns it.
  160. """
  161. result = _ListenerCollection(self.parent, obj._instance_cls)
  162. if getattr(obj, self.name) is self:
  163. setattr(obj, self.name, result)
  164. else:
  165. assert isinstance(getattr(obj, self.name), _JoinedListener)
  166. return result
  167. def _needs_modify(self, *args, **kw):
  168. raise NotImplementedError("need to call for_modify()")
  169. exec_once = insert = append = remove = clear = _needs_modify
  170. def __call__(self, *args, **kw):
  171. """Execute this event."""
  172. for fn in self.parent_listeners:
  173. fn(*args, **kw)
  174. def __len__(self):
  175. return len(self.parent_listeners)
  176. def __iter__(self):
  177. return iter(self.parent_listeners)
  178. def __bool__(self):
  179. return bool(self.parent_listeners)
  180. __nonzero__ = __bool__
  181. class _CompoundListener(_InstanceLevelDispatch):
  182. __slots__ = '_exec_once_mutex', '_exec_once'
  183. def _memoized_attr__exec_once_mutex(self):
  184. return threading.Lock()
  185. def exec_once(self, *args, **kw):
  186. """Execute this event, but only if it has not been
  187. executed already for this collection."""
  188. if not self._exec_once:
  189. with self._exec_once_mutex:
  190. if not self._exec_once:
  191. try:
  192. self(*args, **kw)
  193. finally:
  194. self._exec_once = True
  195. def __call__(self, *args, **kw):
  196. """Execute this event."""
  197. for fn in self.parent_listeners:
  198. fn(*args, **kw)
  199. for fn in self.listeners:
  200. fn(*args, **kw)
  201. def __len__(self):
  202. return len(self.parent_listeners) + len(self.listeners)
  203. def __iter__(self):
  204. return chain(self.parent_listeners, self.listeners)
  205. def __bool__(self):
  206. return bool(self.listeners or self.parent_listeners)
  207. __nonzero__ = __bool__
  208. class _ListenerCollection(_CompoundListener):
  209. """Instance-level attributes on instances of :class:`._Dispatch`.
  210. Represents a collection of listeners.
  211. As of 0.7.9, _ListenerCollection is only first
  212. created via the _EmptyListener.for_modify() method.
  213. """
  214. __slots__ = (
  215. 'parent_listeners', 'parent', 'name', 'listeners',
  216. 'propagate', '__weakref__')
  217. def __init__(self, parent, target_cls):
  218. if target_cls not in parent._clslevel:
  219. parent.update_subclass(target_cls)
  220. self._exec_once = False
  221. self.parent_listeners = parent._clslevel[target_cls]
  222. self.parent = parent
  223. self.name = parent.name
  224. self.listeners = collections.deque()
  225. self.propagate = set()
  226. def for_modify(self, obj):
  227. """Return an event collection which can be modified.
  228. For _ListenerCollection at the instance level of
  229. a dispatcher, this returns self.
  230. """
  231. return self
  232. def _update(self, other, only_propagate=True):
  233. """Populate from the listeners in another :class:`_Dispatch`
  234. object."""
  235. existing_listeners = self.listeners
  236. existing_listener_set = set(existing_listeners)
  237. self.propagate.update(other.propagate)
  238. other_listeners = [l for l
  239. in other.listeners
  240. if l not in existing_listener_set
  241. and not only_propagate or l in self.propagate
  242. ]
  243. existing_listeners.extend(other_listeners)
  244. to_associate = other.propagate.union(other_listeners)
  245. registry._stored_in_collection_multi(self, other, to_associate)
  246. def insert(self, event_key, propagate):
  247. if event_key.prepend_to_list(self, self.listeners):
  248. if propagate:
  249. self.propagate.add(event_key._listen_fn)
  250. def append(self, event_key, propagate):
  251. if event_key.append_to_list(self, self.listeners):
  252. if propagate:
  253. self.propagate.add(event_key._listen_fn)
  254. def remove(self, event_key):
  255. self.listeners.remove(event_key._listen_fn)
  256. self.propagate.discard(event_key._listen_fn)
  257. registry._removed_from_collection(event_key, self)
  258. def clear(self):
  259. registry._clear(self, self.listeners)
  260. self.propagate.clear()
  261. self.listeners.clear()
  262. class _JoinedListener(_CompoundListener):
  263. __slots__ = 'parent', 'name', 'local', 'parent_listeners'
  264. def __init__(self, parent, name, local):
  265. self._exec_once = False
  266. self.parent = parent
  267. self.name = name
  268. self.local = local
  269. self.parent_listeners = self.local
  270. @property
  271. def listeners(self):
  272. return getattr(self.parent, self.name)
  273. def _adjust_fn_spec(self, fn, named):
  274. return self.local._adjust_fn_spec(fn, named)
  275. def for_modify(self, obj):
  276. self.local = self.parent_listeners = self.local.for_modify(obj)
  277. return self
  278. def insert(self, event_key, propagate):
  279. self.local.insert(event_key, propagate)
  280. def append(self, event_key, propagate):
  281. self.local.append(event_key, propagate)
  282. def remove(self, event_key):
  283. self.local.remove(event_key)
  284. def clear(self):
  285. raise NotImplementedError()