o
    [h&                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ dd	lm	Z	 e	
eZd
d ZG dd deZG dd deZG dd deZdd Zdd ZG dd de	eeZG dd deZG dd deZdS )a  Base implementation classes.

The public-facing ``Events`` serves as the base class for an event interface;
its public attributes represent different kinds of events.   These attributes
are mirrored onto a ``_Dispatch`` class, which serves as a container for
collections of listener functions.   These collections are represented both
at the class level of a particular ``_Dispatch`` class as well as within
instances of ``_Dispatch``.

    )absolute_importN   )_ClsLevelDispatch)_EmptyListener)_JoinedListener   )utilc                 C   s   |  d o	| dkS )N_dispatch)
startswith)name r   |/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/event/base.py_is_event_name   s   r   c                   @      e Zd ZdZdd ZdS )_UnpickleDispatchzSerializable callable that re-generates an instance of
    :class:`_Dispatch` given a particular :class:`.Events` subclass.

    c                 C   s4   |j D ]}d|jv r|jd j|  S qtd)Nr
   z*No class with a 'dispatch' member present.)__mro____dict__r
   
_for_classAttributeError)self_instance_clsclsr   r   r   __call__)   s   

z_UnpickleDispatch.__call__N)__name__
__module____qualname____doc__r   r   r   r   r   r   #   s    r   c                   @   sx   e Zd ZdZdZe ZdddZdd Z	e
dd	 Ze
d
d Zdd Zdd Zdd Zdd ZdddZdd ZdS )	_Dispatcha;  Mirror the event listening definitions of an Events class with
    listener collections.

    Classes which define a "dispatch" member will return a
    non-instantiated :class:`._Dispatch` subclass when the member
    is accessed at the class level.  When the "dispatch" member is
    accessed at the instance level of its owner, an instance
    of the :class:`._Dispatch` class is returned.

    A :class:`._Dispatch` class is generated for each :class:`.Events`
    class defined, by the :func:`._create_dispatcher_class` function.
    The original :class:`.Events` classes remain untouched.
    This decouples the construction of :class:`.Events` subclasses from
    the implementation used by the event internals, and allows
    inspecting tools like Sphinx to work in an unsurprising
    way against the public API.

    )_parentr   r   _empty_listenersNc                    sb   || _  | _ r,z	| j  | _W d S  ty+    fdd|jD  | _| j < Y d S w i | _d S )Nc                    s   i | ]	}|j t| qS r   )r   r   ).0lsinstance_clsr   r   
<dictcomp>W   s    z&_Dispatch.__init__.<locals>.<dictcomp>)r   r   _empty_listener_regr    KeyError_event_descriptors)r   parentr$   r   r#   r   __init__M   s   


z_Dispatch.__init__c                 C   s8   z| j | }W n ty   t|w t| |j| |S N)r    r'   r   setattrr   )r   r   r"   r   r   r   __getattr__^   s   z_Dispatch.__getattr__c                 c   s    | j D ]}t| |V  qd S r+   )_event_namesgetattr)r   kr   r   r   r(   i   s   
z_Dispatch._event_descriptorsc                 C      | j jS r+   )_events_listenr   r   r   r   r3   p      z_Dispatch._listenc                 C   s   |  | |S r+   )	__class__)r   r$   r   r   r   r   t   s   z_Dispatch._for_classc                 C   s   |j }| |S r+   )r6   r   )r   instancer$   r   r   r   _for_instancew   s   
z_Dispatch._for_instancec                 C   s<   d| j jvrtd| j j tfd| ji}|| j _| | |S )zCreate a 'join' of this :class:`._Dispatch` and another.

        This new dispatcher will dispatch events to both
        :class:`._Dispatch` objects.

        _joined_dispatch_clszJoined%s	__slots__)r6   r   typer   _JoinedDispatcherr.   r9   )r   otherr   r   r   r   _join{   s   
z_Dispatch._joinc                 C   s   t  | jffS r+   )r   r   r4   r   r   r   
__reduce__      z_Dispatch.__reduce__Tc                 C   s8   |j D ]}t|trqt| |j| j||d qdS )zIPopulate from the listeners in another :class:`_Dispatch`
        object.)only_propagateN)r(   
isinstancer   r/   r   
for_modify_update)r   r=   rA   r"   r   r   r   rD      s   

z_Dispatch._updatec                 C   s   | j D ]	}||   qd S r+   )r(   rC   clear)r   r"   r   r   r   _clear   s   
z_Dispatch._clearr+   )T)r   r   r   r   r:   weakrefWeakKeyDictionaryr&   r*   r-   propertyr(   r3   r   r8   r>   r?   rD   rF   r   r   r   r   r   3   s     




r   c                   @   r   )
_EventMetazKIntercept new Event subclasses and create
    associated _Dispatch classes.c                 C   s"   t | ||| t| ||| d S r+   )_create_dispatcher_classr;   r*   )r   	classnamebasesdict_r   r   r   r*      s   z_EventMeta.__init__N)r   r   r   r   r*   r   r   r   r   rJ      s    rJ   c                 C   s   t | dr
| jj}nt}dd |D }td| |fd|i}||_| | |}|jD ]}t||t| ||  t	| 
|  q*|jD ]!}	t|	trc|	turc|	jjjD ]}
t||
j|
 |j
|
j qRqBt| ddrrt| | j_dS dS )zRCreate a :class:`._Dispatch` class corresponding to an
    :class:`.Events` class.r
   c                 S   s   g | ]}t |r|qS r   )r   )r!   r0   r   r   r   
<listcomp>   s    z,_create_dispatcher_class.<locals>.<listcomp>z
%sDispatchr:   _dispatch_targetN)hasattrr
   r6   r   r;   r.   _set_dispatchr,   r   _registrarsappend	__bases__
issubclassr2   r(   r   r/   
dispatcherrP   )r   rL   rM   rN   Zdispatch_baseZevent_namesdispatch_clsZdispatch_instr0   Zsuper_r"   r   r   r   rK      s*   



rK   c                 C   s.   | j jD ]}t| |  t| st|= qd S r+   )r
   r.   rS   remove)r   r0   r   r   r   _remove_dispatcher   s   rZ   c                   @   sN   e Zd ZdZedd Zedd ZedddZed	d
 Z	edd Z
dS )Eventsz>Define event listening functions for a particular target type.c                 C   s   |d | _ | |_| j S r+   )r
   r2   )r   rX   r   r   r   rR      s   
zEvents._set_dispatchc                    s`    fdd} fdd}t  dr*|| jjs(|t| jjs(|tr,|| jjr. S d S d S d S )Nc                     s   t  fdd| D S )Nc                 3   s    | ]	}t  j|V  qd S r+   )rB   r
   )r!   ttargetr   r   	<genexpr>   s    z;Events._accept_with.<locals>.dispatch_is.<locals>.<genexpr>)all)typesr]   r   r   dispatch_is   s   z(Events._accept_with.<locals>.dispatch_isc                    s   t  jj| S r+   )rB   r
   r)   )r\   r]   r   r   dispatch_parent_is   r@   z/Events._accept_with.<locals>.dispatch_parent_isr
   )rQ   r
   r6   r;   r<   )r   r^   rb   rc   r   r]   r   _accept_with   s   


zEvents._accept_withFc                 C   s   |j |||d d S )N)	propagateinsertnamed)Zbase_listen)r   	event_keyre   rf   rg   r   r   r   r3      s   zEvents._listenc                 C   s   |   d S r+   )rY   )r   rh   r   r   r   _remove   s   zEvents._removec                 C   s   | j   d S r+   )r
   rF   )r   r   r   r   rF      s   zEvents._clearN)FFF)r   r   r   r   staticmethodrR   classmethodrd   r3   ri   rF   r   r   r   r   r[      s    



r[   c                   @   s<   e Zd ZdZdZdd Zdd Zedd Zed	d
 Z	dS )r<   z5Represent a connection between two _Dispatch objects.localr)   r   c                 C   s   || _ || _| j j| _d S r+   rl   )r   rm   r)   r   r   r   r*     s   z_JoinedDispatcher.__init__c                 C   s.   t | j|}t| j|j|}t| |j| |S r+   )r/   rm   r   r)   r   r,   )r   r   r"   jlr   r   r   r-   
  s   z_JoinedDispatcher.__getattr__c                 C   r1   r+   )r)   r3   r4   r   r   r   r3     r5   z_JoinedDispatcher._listenc                 C   r1   r+   )r)   r2   r4   r   r   r   r2     r5   z_JoinedDispatcher._eventsN)
r   r   r   r   r:   r*   r-   rI   r3   r2   r   r   r   r   r<      s    
r<   c                   @   s    e Zd ZdZdd Zdd ZdS )rW   zDescriptor used by target classes to
    deliver the _Dispatch class at the class level
    and produce new _Dispatch instances for target
    instances.

    c                 C   s   |j | _ || _d S r+   )r
   events)r   ro   r   r   r   r*   #  s   
zdispatcher.__init__c                 C   s(   |d u r| j S | j | |jd< }|S )Nr
   )r
   r8   r   )r   objr   Zdispr   r   r   __get__'  s   zdispatcher.__get__N)r   r   r   r   r*   rq   r   r   r   r   rW     s    rW   )r   
__future__r   rG   attrr   r   r    r   defaultdictlistrS   r   objectr   r   r;   rJ   rK   rZ   with_metaclassr[   r<   rW   r   r   r   r   <module>   s"   

k	"0