openmedialibrary_platform_l.../lib/python3.4/site-packages/sqlalchemy/event/attr.py

374 lines
12 KiB
Python
Raw Permalink Normal View History

2016-02-06 09:38:49 +00:00
# event/attr.py
2016-02-22 11:49:42 +00:00
# Copyright (C) 2005-2016 the SQLAlchemy authors and contributors
2016-02-06 09:38:49 +00:00
# <see AUTHORS file>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
"""Attribute implementation for _Dispatch classes.
The various listener targets for a particular event class are represented
as attributes, which refer to collections of listeners to be fired off.
These collections can exist at the class level as well as at the instance
level. An event is fired off using code like this::
some_object.dispatch.first_connect(arg1, arg2)
Above, ``some_object.dispatch`` would be an instance of ``_Dispatch`` and
``first_connect`` is typically an instance of ``_ListenerCollection``
if event listeners are present, or ``_EmptyListener`` if none are present.
The attribute mechanics here spend effort trying to ensure listener functions
are available with a minimum of function call overhead, that unnecessary
objects aren't created (i.e. many empty per-instance listener collections),
as well as that everything is garbage collectable when owning references are
lost. Other features such as "propagation" of listener functions across
many ``_Dispatch`` instances, "joining" of multiple ``_Dispatch`` instances,
as well as support for subclass propagation (e.g. events assigned to
``Pool`` vs. ``QueuePool``) are all implemented here.
"""
from __future__ import absolute_import, with_statement
from .. import util
from ..util import threading
from . import registry
from . import legacy
from itertools import chain
import weakref
2016-02-22 11:49:42 +00:00
import collections
2016-02-06 09:38:49 +00:00
2016-02-22 11:49:42 +00:00
class RefCollection(util.MemoizedSlots):
__slots__ = 'ref',
def _memoized_attr_ref(self):
2016-02-06 09:38:49 +00:00
return weakref.ref(self, registry._collection_gced)
2016-02-22 11:49:42 +00:00
class _ClsLevelDispatch(RefCollection):
"""Class-level events on :class:`._Dispatch` classes."""
__slots__ = ('name', 'arg_names', 'has_kw',
'legacy_signatures', '_clslevel', '__weakref__')
2016-02-06 09:38:49 +00:00
def __init__(self, parent_dispatch_cls, fn):
2016-02-22 11:49:42 +00:00
self.name = fn.__name__
2016-02-06 09:38:49 +00:00
argspec = util.inspect_getargspec(fn)
self.arg_names = argspec.args[1:]
self.has_kw = bool(argspec.keywords)
self.legacy_signatures = list(reversed(
sorted(
getattr(fn, '_legacy_signatures', []),
key=lambda s: s[0]
)
))
2016-02-22 11:49:42 +00:00
fn.__doc__ = legacy._augment_fn_docs(self, parent_dispatch_cls, fn)
2016-02-06 09:38:49 +00:00
self._clslevel = weakref.WeakKeyDictionary()
def _adjust_fn_spec(self, fn, named):
if named:
fn = self._wrap_fn_for_kw(fn)
if self.legacy_signatures:
try:
argspec = util.get_callable_argspec(fn, no_self=True)
except TypeError:
pass
else:
fn = legacy._wrap_fn_for_legacy(self, fn, argspec)
return fn
def _wrap_fn_for_kw(self, fn):
def wrap_kw(*args, **kw):
argdict = dict(zip(self.arg_names, args))
argdict.update(kw)
return fn(**argdict)
return wrap_kw
def insert(self, event_key, propagate):
target = event_key.dispatch_target
assert isinstance(target, type), \
"Class-level Event targets must be classes."
stack = [target]
while stack:
cls = stack.pop(0)
stack.extend(cls.__subclasses__())
if cls is not target and cls not in self._clslevel:
self.update_subclass(cls)
else:
if cls not in self._clslevel:
2016-02-22 11:49:42 +00:00
self._clslevel[cls] = collections.deque()
self._clslevel[cls].appendleft(event_key._listen_fn)
2016-02-06 09:38:49 +00:00
registry._stored_in_collection(event_key, self)
def append(self, event_key, propagate):
target = event_key.dispatch_target
assert isinstance(target, type), \
"Class-level Event targets must be classes."
stack = [target]
while stack:
cls = stack.pop(0)
stack.extend(cls.__subclasses__())
if cls is not target and cls not in self._clslevel:
self.update_subclass(cls)
else:
if cls not in self._clslevel:
2016-02-22 11:49:42 +00:00
self._clslevel[cls] = collections.deque()
2016-02-06 09:38:49 +00:00
self._clslevel[cls].append(event_key._listen_fn)
registry._stored_in_collection(event_key, self)
def update_subclass(self, target):
if target not in self._clslevel:
2016-02-22 11:49:42 +00:00
self._clslevel[target] = collections.deque()
2016-02-06 09:38:49 +00:00
clslevel = self._clslevel[target]
for cls in target.__mro__[1:]:
if cls in self._clslevel:
clslevel.extend([
fn for fn
in self._clslevel[cls]
if fn not in clslevel
])
def remove(self, event_key):
target = event_key.dispatch_target
stack = [target]
while stack:
cls = stack.pop(0)
stack.extend(cls.__subclasses__())
if cls in self._clslevel:
self._clslevel[cls].remove(event_key._listen_fn)
registry._removed_from_collection(event_key, self)
def clear(self):
"""Clear all class level listeners"""
to_clear = set()
for dispatcher in self._clslevel.values():
to_clear.update(dispatcher)
2016-02-22 11:49:42 +00:00
dispatcher.clear()
2016-02-06 09:38:49 +00:00
registry._clear(self, to_clear)
def for_modify(self, obj):
"""Return an event collection which can be modified.
2016-02-22 11:49:42 +00:00
For _ClsLevelDispatch at the class level of
2016-02-06 09:38:49 +00:00
a dispatcher, this returns self.
"""
return self
2016-02-22 11:49:42 +00:00
class _InstanceLevelDispatch(RefCollection):
__slots__ = ()
2016-02-06 09:38:49 +00:00
def _adjust_fn_spec(self, fn, named):
return self.parent._adjust_fn_spec(fn, named)
2016-02-22 11:49:42 +00:00
class _EmptyListener(_InstanceLevelDispatch):
"""Serves as a proxy interface to the events
served by a _ClsLevelDispatch, when there are no
2016-02-06 09:38:49 +00:00
instance-level events present.
Is replaced by _ListenerCollection when instance-level
events are added.
"""
2016-02-22 11:49:42 +00:00
propagate = frozenset()
listeners = ()
__slots__ = 'parent', 'parent_listeners', 'name'
2016-02-06 09:38:49 +00:00
def __init__(self, parent, target_cls):
if target_cls not in parent._clslevel:
parent.update_subclass(target_cls)
2016-02-22 11:49:42 +00:00
self.parent = parent # _ClsLevelDispatch
2016-02-06 09:38:49 +00:00
self.parent_listeners = parent._clslevel[target_cls]
2016-02-22 11:49:42 +00:00
self.name = parent.name
2016-02-06 09:38:49 +00:00
def for_modify(self, obj):
"""Return an event collection which can be modified.
For _EmptyListener at the instance level of
a dispatcher, this generates a new
_ListenerCollection, applies it to the instance,
and returns it.
"""
2016-02-22 11:49:42 +00:00
result = _ListenerCollection(self.parent, obj._instance_cls)
if getattr(obj, self.name) is self:
setattr(obj, self.name, result)
else:
assert isinstance(getattr(obj, self.name), _JoinedListener)
2016-02-06 09:38:49 +00:00
return result
def _needs_modify(self, *args, **kw):
raise NotImplementedError("need to call for_modify()")
exec_once = insert = append = remove = clear = _needs_modify
def __call__(self, *args, **kw):
"""Execute this event."""
for fn in self.parent_listeners:
fn(*args, **kw)
def __len__(self):
return len(self.parent_listeners)
def __iter__(self):
return iter(self.parent_listeners)
def __bool__(self):
return bool(self.parent_listeners)
__nonzero__ = __bool__
2016-02-22 11:49:42 +00:00
class _CompoundListener(_InstanceLevelDispatch):
__slots__ = '_exec_once_mutex', '_exec_once'
2016-02-06 09:38:49 +00:00
2016-02-22 11:49:42 +00:00
def _memoized_attr__exec_once_mutex(self):
2016-02-06 09:38:49 +00:00
return threading.Lock()
def exec_once(self, *args, **kw):
"""Execute this event, but only if it has not been
executed already for this collection."""
if not self._exec_once:
with self._exec_once_mutex:
if not self._exec_once:
try:
self(*args, **kw)
finally:
self._exec_once = True
def __call__(self, *args, **kw):
"""Execute this event."""
for fn in self.parent_listeners:
fn(*args, **kw)
for fn in self.listeners:
fn(*args, **kw)
def __len__(self):
return len(self.parent_listeners) + len(self.listeners)
def __iter__(self):
return chain(self.parent_listeners, self.listeners)
def __bool__(self):
return bool(self.listeners or self.parent_listeners)
__nonzero__ = __bool__
2016-02-22 11:49:42 +00:00
class _ListenerCollection(_CompoundListener):
2016-02-06 09:38:49 +00:00
"""Instance-level attributes on instances of :class:`._Dispatch`.
Represents a collection of listeners.
As of 0.7.9, _ListenerCollection is only first
created via the _EmptyListener.for_modify() method.
"""
2016-02-22 11:49:42 +00:00
__slots__ = (
'parent_listeners', 'parent', 'name', 'listeners',
'propagate', '__weakref__')
2016-02-06 09:38:49 +00:00
def __init__(self, parent, target_cls):
if target_cls not in parent._clslevel:
parent.update_subclass(target_cls)
2016-02-22 11:49:42 +00:00
self._exec_once = False
2016-02-06 09:38:49 +00:00
self.parent_listeners = parent._clslevel[target_cls]
self.parent = parent
2016-02-22 11:49:42 +00:00
self.name = parent.name
self.listeners = collections.deque()
2016-02-06 09:38:49 +00:00
self.propagate = set()
def for_modify(self, obj):
"""Return an event collection which can be modified.
For _ListenerCollection at the instance level of
a dispatcher, this returns self.
"""
return self
def _update(self, other, only_propagate=True):
"""Populate from the listeners in another :class:`_Dispatch`
object."""
existing_listeners = self.listeners
existing_listener_set = set(existing_listeners)
self.propagate.update(other.propagate)
other_listeners = [l for l
in other.listeners
if l not in existing_listener_set
and not only_propagate or l in self.propagate
]
existing_listeners.extend(other_listeners)
to_associate = other.propagate.union(other_listeners)
registry._stored_in_collection_multi(self, other, to_associate)
def insert(self, event_key, propagate):
2016-02-22 11:49:42 +00:00
if event_key.prepend_to_list(self, self.listeners):
2016-02-06 09:38:49 +00:00
if propagate:
self.propagate.add(event_key._listen_fn)
def append(self, event_key, propagate):
2016-02-22 11:49:42 +00:00
if event_key.append_to_list(self, self.listeners):
2016-02-06 09:38:49 +00:00
if propagate:
self.propagate.add(event_key._listen_fn)
def remove(self, event_key):
self.listeners.remove(event_key._listen_fn)
self.propagate.discard(event_key._listen_fn)
registry._removed_from_collection(event_key, self)
def clear(self):
registry._clear(self, self.listeners)
self.propagate.clear()
2016-02-22 11:49:42 +00:00
self.listeners.clear()
2016-02-06 09:38:49 +00:00
class _JoinedListener(_CompoundListener):
2016-02-22 11:49:42 +00:00
__slots__ = 'parent', 'name', 'local', 'parent_listeners'
2016-02-06 09:38:49 +00:00
def __init__(self, parent, name, local):
2016-02-22 11:49:42 +00:00
self._exec_once = False
2016-02-06 09:38:49 +00:00
self.parent = parent
self.name = name
self.local = local
self.parent_listeners = self.local
@property
def listeners(self):
return getattr(self.parent, self.name)
def _adjust_fn_spec(self, fn, named):
return self.local._adjust_fn_spec(fn, named)
def for_modify(self, obj):
self.local = self.parent_listeners = self.local.for_modify(obj)
return self
def insert(self, event_key, propagate):
self.local.insert(event_key, propagate)
def append(self, event_key, propagate):
self.local.append(event_key, propagate)
def remove(self, event_key):
self.local.remove(event_key)
def clear(self):
raise NotImplementedError()