3
‰dœ
  ã               @   sj   d dl mZ d dl mZ d dl mZ dZddgZG dd„ deƒZG dd„ deƒZd	d
„ Z	e
dkrfe	ƒ  dS )é    )Úabsolute_import)Údivision)Úprint_functionaš  
Dirt Simple Events

A Dispatcher (or a subclass of Dispatcher) stores event handlers that
are 'fired' simple event objects when interesting things happen.

Create a dispatcher:

  >>> d = Dispatcher()

Now create a handler for the event and subscribe it to the dispatcher
to handle Event events.  A handler is a simple function or method that
accepts the event as an argument:

  >>> def handler1(event): print(repr(event))
  >>> d.subscribe(Event, handler1)

Now dispatch a new event into the dispatcher, and see handler1 get
fired:

  >>> d.dispatch(Event(foo='bar', data='yours', used_by='the event handlers'))
  <rdflib.events.Event ['data', 'foo', 'used_by']>
ÚEventÚ
Dispatcherc               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   aÂ  
    An event is a container for attributes.  The source of an event
    creates this object, or a subclass, gives it any kind of data that
    the events handlers need to handle the event, and then calls
    notify(event).

    The target of an event registers a function to handle the event it
    is interested with subscribe().  When a sources calls
    notify(event), each subscriber to that event will be called in no
    particular order.
    c             K   s   | j j|ƒ d S )N)Ú__dict__Úupdate)ÚselfÚkw© r   ú//tmp/pip-build-7vycvbft/rdflib/rdflib/events.pyÚ__init__-   s    zEvent.__init__c             C   s"   t | jjƒ ƒ}ddd„ |D ƒf S )Nz<rdflib.events.Event %s>c             S   s   g | ]}|‘qS r   r   )Ú.0Úar   r   r   ú
<listcomp>2   s    z"Event.__repr__.<locals>.<listcomp>)Úsortedr   Úkeys)r	   Úattrsr   r   r   Ú__repr__0   s    zEvent.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r       s   c               @   s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )r   z]
    An object that can dispatch events to a privately managed group of
    subscribers.
    Nc             C   s
   || _ d S )N)Ú_dispatch_map)r	   Zamapr   r   r   Úset_map=   s    zDispatcher.set_mapc             C   s   | j S )N)r   )r	   r   r   r   Úget_map@   s    zDispatcher.get_mapc             C   sJ   | j dkr| ji ƒ | j j|dƒ}|dkr2|g}n
|j|ƒ || j |< dS )zv Subscribe the given handler to an event_type.  Handlers
        are called in the order they are subscribed.
        N)r   r   ÚgetÚappend)r	   Z
event_typeÚhandlerÚlstr   r   r   Ú	subscribeC   s    


zDispatcher.subscribec             C   sN   | j dk	rJ| j jt|ƒdƒ}|dkr4tdt|ƒ ƒ‚x|D ]}||ƒ q:W dS )zQ Dispatch the given event to the subscribed handlers for
        the event's typeNzunknown event type: %s)r   r   ÚtypeÚ
ValueError)r	   Úeventr   Úlr   r   r   ÚdispatchP   s    

zDispatcher.dispatch)	r   r   r   r   r   r   r   r    r%   r   r   r   r   r   5   s   c              C   s   dd l } | jƒ  d S )Nr   )ÚdoctestÚtestmod)r&   r   r   r   Útest[   s    r(   Ú__main__N)Ú
__future__r   r   r   r   Ú__all__Úobjectr   r   r(   r   r   r   r   r   Ú<module>   s   &