Events

Event Objects

class traity.events.Event(snitch, target, dispatcher=None, **kwargs)

An event should never need to be created directly. Use events(obj).etrigger to start a chain of events.

Parameters:
  • event_type – type of event
  • target
dispatch(listener)

call a listener object

target

The target this event will trigger action for.

Snitch Objects

class traity.events.Snitch(instance)

Snitch object handles events. and propegates them to the object’s upstream nodes.

etrigger(target, **metadata)

trigger an event.

Parameters:target – only listeners listening to this target will fire
listen(target, listener, weak=None)

Add a listener to listen to events with target ‘target’.

queue(*args, **kwds)

queue listened to events for this object.

quiet(*args, **kwds)

Context manager to stop events being propegated.

trigger(event)
Parameters:eventEvent object to trigger
unique(*args, **kwds)

remove duplicate events.

unlisten(target, listener=None)

Remove a listener.

Functions

traity.events.connect(upstream, downstream, target, init=True)

Connect two objects together so that events will bubble upstream.

traity.events.disconnect(upstream, downstream, target, init=True)

Discnnect two objects.

traity.events.connected(upstream, downstream)

Test if to objects are connected.

See also

connect()

traity.events.events(instance, init=False)

Get the events class associated with this object. If init is true. call init_events if events are uninitialized, otherwize, raise an exception.

traity.events.init_events(*instances)

Initialize the events for an object

traity.events.init_events(*instances)

Initialize the events for an object

Global liteners

traity.events.add_global_listener(listener, target=None)

Add a listener to events on all objects.

traity.events.remove_global_listener(listener, target=None)

remove a add_global_listener

traity.events.global_listener(*args, **kwds)
Parameters:
  • listener – callable object, signature must be listener(event)
  • target – target tuple

Context manager for global listeners

def print_event(event):
    print event
    
with global_listener(print_event):
    ...

ok

Global dispatchers

traity.events.add_global_dispatcher(dispatcher)

Add a dispatcher to the stack.

Parameters:dispatcher – callable object. signature must be dispatcher(event, listener)
traity.events.pop_global_dispatcher()

remove the last global disopatcher from the stack

traity.events.remove_global_dispatcher(dispatcher)

remove the last dispatcher from the stack

Parameters:dispatcher – specific dispatcher to remove
traity.events.global_dispatcher(*args, **kwds)
Parameters:dispatcher – callable object. signature must be dispatcher(event, listener)

Add a global dispatcher in a context:

with global_dispatcher(myfunc):
    #Trigger events

ok

traity.events.quiet(*args, **kwds)

Do not dispatch any events

traity.events.queue(*args, **kwds)

Put all events into a queue. example:

with queue() as todo:
    ...

print "I have cought %i events" % len(todo)

this

traity.events.unique(*args, **kwds)

Only process unique events eg:

num_calls = 0
with global_listener(inc_num_calls):
    with unique():
        triggers_event()
        triggers_event()

assert num_calls == 1 

and how