diff options
Diffstat (limited to 'lib/sqlalchemy/event.py')
-rw-r--r-- | lib/sqlalchemy/event.py | 61 |
1 files changed, 10 insertions, 51 deletions
diff --git a/lib/sqlalchemy/event.py b/lib/sqlalchemy/event.py index 5fae8aa24..28ed7f563 100644 --- a/lib/sqlalchemy/event.py +++ b/lib/sqlalchemy/event.py @@ -6,54 +6,11 @@ and :mod:`sqlalchemy.orm` packages. """ from sqlalchemy import util -import inspect -def listen(fn, identifier, target, *args, **kw): - """Listen for events, passing to fn. - - Event listener functions are in a consistent format:: - - def listen(event_name, args): - # ... - - Where ``event_name`` is the string name, the same - as ``identifier``, and ``args`` is a dict containing - an entry for each argument. The names match those - of the event declaration. - - """ - - for evt_cls in _registrars[identifier]: - for tgt in evt_cls.accept_with(target): - fn = _create_wrapper(evt_cls, fn, identifier) - tgt.dispatch.listen(fn, identifier, tgt, *args, **kw) - break +CANCEL = util.symbol('CANCEL') +NO_RETVAL = util.symbol('NO_RETVAL') -def _create_wrapper(evt_cls, fn, identifier): - argspec = inspect.getargspec(getattr(evt_cls, identifier)) - arg, varargs, keywords, defaults = argspec - def go(*args, **kw): - # here we are coercing the *arg, **kw to a single - # dictionary. - - # TODO: defaults - if keywords: - kw = {keywords:kw} - for n, v in zip(arg[1:], args): - kw[n] = v - if varargs: - kw[varargs] = arg[len(args)+1:] - - fn(identifier, kw) - - # then here, we ask the Events subclass to interpret - # the dictionary back to what it wants for a return. - - return evt_cls.unwrap(identifier, kw) - - return util.update_wrapper(go, fn) - -def listen_raw(fn, identifier, target, *args, **kw): +def listen(fn, identifier, target, *args, **kw): """Listen for events, accepting an event function that's "raw". Only the exact arguments are received in order. @@ -85,7 +42,7 @@ class _Dispatch(object): @property def descriptors(self): return (getattr(self, k) for k in dir(self) if k.startswith("on_")) - + def update(self, other): """Populate from the listeners in another :class:`Events` object.""" @@ -137,9 +94,11 @@ class Events(object): def listen(cls, fn, identifier, target): getattr(target.dispatch, identifier).append(fn, target) - @classmethod - def unwrap(cls, identifier, event): - return None +# def update(self, other): +# """Populate from the listeners in another :class:`Events` object.""" + +# for ls in other.events: +# getattr(self, ls.name).listeners.extend(ls.listeners) class _DispatchDescriptor(object): """Class-level attributes on _Dispatch classes.""" @@ -183,7 +142,7 @@ class _ListenerCollection(object): if not self._exec_once: self(*args, **kw) self._exec_once = True - + def exec_until_return(self, *args, **kw): """Execute listeners for this event until one returns a non-None value. |