o
    [h6                     @   s   d Z ddlmZ ddlmZ ddl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 d	dlmZ G dd dejZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )a  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.

    )absolute_import)with_statementN)chain   )legacy)registry   )exc)util)	threadingc                   @      e Zd ZdZdd ZdS )RefCollection)refc                 C   s   t | tjS N)weakrefr   r   Z_collection_gcedself r   |/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/event/attr.py_memoized_attr_ref1      z RefCollection._memoized_attr_refN)__name__
__module____qualname__	__slots__r   r   r   r   r   r   .       r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )_empty_collectionc                 C      d S r   r   r   elementr   r   r   append6      z_empty_collection.appendc                 C   r   r   r   )r   otherr   r   r   extend9   r!   z_empty_collection.extendc                 C   r   r   r   r   r   r   r   remove<   r!   z_empty_collection.removec                 C   s   t g S r   )iterr   r   r   r   __iter__?      z_empty_collection.__iter__c                 C   r   r   r   r   r   r   r   clearB   r!   z_empty_collection.clearN)r   r   r   r    r#   r$   r&   r(   r   r   r   r   r   5   s    r   c                   @   sd   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )_ClsLevelDispatchz2Class-level events on :class:`._Dispatch` classes.)name	arg_nameshas_kwlegacy_signatures	_clslevel__weakref__c                 C   sn   |j | _t|}|jdd  | _t|j| _t	t
tt|dg dd d| _t| |||_t | _d S )Nr   Z_legacy_signaturesc                 S   s   | d S Nr   r   )sr   r   r   <lambda>Z   s    z,_ClsLevelDispatch.__init__.<locals>.<lambda>)key)r   r*   r
   Zinspect_getfullargspecargsr+   boolvarkwr,   listreversedsortedgetattrr-   r   Z_augment_fn_docs__doc__r   WeakKeyDictionaryr.   )r   Zparent_dispatch_clsfnargspecr   r   r   __init__R   s   
z_ClsLevelDispatch.__init__c                 C   sN   |r|  |}| jr%z	tj|dd}W n
 ty   Y |S w t| ||}|S )NT)Zno_self)_wrap_fn_for_kwr-   r
   Zget_callable_argspec	TypeErrorr   Z_wrap_fn_for_legacy)r   r=   namedr>   r   r   r   _adjust_fn_specb   s   
z!_ClsLevelDispatch._adjust_fn_specc                    s    fdd}|S )Nc                     s(   t tj| }||  di |S )Nr   )dictzipr+   update)r4   kwZargdictr=   r   r   r   wrap_kwo   s   
z2_ClsLevelDispatch._wrap_fn_for_kw.<locals>.wrap_kwr   )r   r=   rI   r   rH   r   r@   n   s   z!_ClsLevelDispatch._wrap_fn_for_kwc                 C      |j }t|tsJ dt|ddstd| |g}|rN|d}||  ||ur9|| j	vr9| 
| n|| j	vrC| | | j	| |j |st||  d S Nz*Class-level Event targets must be classes._sa_propagate_class_eventsTz.Can't assign an event directly to the %s classr   )dispatch_target
isinstancetyper:   r	   InvalidRequestErrorpopr#   __subclasses__r.   update_subclass_assign_cls_collection
appendleft
_listen_fnr   _stored_in_collectionr   	event_key	propagatetargetstackclsr   r   r   insertv   *   


	z_ClsLevelDispatch.insertc                 C   rJ   rK   )rM   rN   rO   r:   r	   rP   rQ   r#   rR   r.   rS   rT   r    rV   r   rW   rX   r   r   r   r       r_   z_ClsLevelDispatch.appendc                 C   s.   t |ddrt | j|< d S t | j|< d S )NrL   T)r:   collectionsdequer.   r   )r   r[   r   r   r   rT      s   z(_ClsLevelDispatch._assign_cls_collectionc                    s^   || j vr
| | | j |  |jdd  D ]}|| j v r,  fdd| j | D  qd S )Nr   c                    s   g | ]}| vr|qS r   r   ).0r=   Zclslevelr   r   
<listcomp>   s    z5_ClsLevelDispatch.update_subclass.<locals>.<listcomp>)r.   rT   __mro__r#   )r   r[   r]   r   rc   r   rS      s   



z!_ClsLevelDispatch.update_subclassc                 C   sX   |j }|g}|r$|d}||  || jv r"| j| |j |st||  d S r0   )	rM   rQ   r#   rR   r.   r$   rV   r   _removed_from_collection)r   rY   r[   r\   r]   r   r   r   r$      s   

z_ClsLevelDispatch.removec                 C   s8   t  }| j D ]}|| |  qt| | dS )zClear all class level listenersN)setr.   valuesrF   r(   r   _clear)r   Zto_clear
dispatcherr   r   r   r(      s
   

z_ClsLevelDispatch.clearc                 C      | S )zReturn an event collection which can be modified.

        For _ClsLevelDispatch at the class level of
        a dispatcher, this returns self.

        r   r   objr   r   r   
for_modify      z_ClsLevelDispatch.for_modifyN)r   r   r   r;   r   r?   rC   r@   r^   r    rT   rS   r$   r(   rn   r   r   r   r   r)   F   s    	

	r)   c                   @   r   )_InstanceLevelDispatchr   c                 C      | j ||S r   )parentrC   r   r=   rB   r   r   r   rC      r   z&_InstanceLevelDispatch._adjust_fn_specN)r   r   r   r   rC   r   r   r   r   rp      r   rp   c                   @   sr   e Zd ZdZe ZdZdZdd Zdd Z	dd	 Z
e
 Z Z Z Z ZZd
d Zdd Zdd Zdd ZeZdS )_EmptyListenerzServes as a proxy interface to the events
    served by a _ClsLevelDispatch, when there are no
    instance-level events present.

    Is replaced by _ListenerCollection when instance-level
    events are added.

    r   )rr   parent_listenersr*   c                 C   s2   ||j vr
|| || _|j | | _|j| _d S r   )r.   rS   rr   ru   r*   r   rr   Z
target_clsr   r   r   r?      s
   

z_EmptyListener.__init__c                 C   sJ   t | j|j}t|| j| u rt|| j| |S tt|| jts#J |S )zReturn 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.

        )_ListenerCollectionrr   Z_instance_clsr:   r*   setattrrN   _JoinedListener)r   rm   resultr   r   r   rn      s   	z_EmptyListener.for_modifyc                 O   s   t d)Nzneed to call for_modify()NotImplementedErrorr   r4   rG   r   r   r   _needs_modify   r'   z_EmptyListener._needs_modifyc                 O   s   | j D ]	}||i | qdS zExecute this event.N)ru   r   r4   rG   r=   r   r   r   __call__  s   
z_EmptyListener.__call__c                 C   
   t | jS r   )lenru   r   r   r   r   __len__     
z_EmptyListener.__len__c                 C   r   r   )r%   ru   r   r   r   r   r&   
  r   z_EmptyListener.__iter__c                 C   r   r   )r5   ru   r   r   r   r   __bool__  r   z_EmptyListener.__bool__N)r   r   r   r;   	frozensetrZ   	listenersr   r?   rn   r~   	exec_onceexec_once_unless_exceptionr^   r    r$   r(   r   r   r&   r   __nonzero__r   r   r   r   rt      s,    	rt   c                   @   sT   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZeZdS )_CompoundListenerZ_exec_once_mutex
_exec_oncec                 C   s   t  S r   )r   Lockr   r   r   r   _memoized_attr__exec_once_mutex  r'   z1_CompoundListener._memoized_attr__exec_once_mutexc              	   O   s   | j 8 | js+zz| |i | d}W n   d} W |r|s"d| _n|r'|s*d| _w W d    d S W d    d S 1 s>w   Y  d S )NFTr   )r   Zretry_on_exceptionr4   rG   	exceptionr   r   r   _exec_once_impl  s$   " z!_CompoundListener._exec_once_implc                 O   &   | j s| jdg|R i | dS dS )z]Execute this event, but only if it has not been
        executed already for this collection.FNr   r   r}   r   r   r   r   &  s   z_CompoundListener.exec_oncec                 O   r   )at  Execute this event, but only if it has not been
        executed already for this collection, or was called
        by a previous exec_once_unless_exception call and
        raised an exception.

        If exec_once was already called, then this method will never run
        the callable regardless of whether it raised or not.

        .. versionadded:: 1.3.8

        TNr   r}   r   r   r   r   -  s   z,_CompoundListener.exec_once_unless_exceptionc                 O   s8   | j D ]	}||i | q| jD ]	}||i | qdS r   )ru   r   r   r   r   r   r   <  s
   

z_CompoundListener.__call__c                 C   s   t | jt | j S r   )r   ru   r   r   r   r   r   r   D  s   z_CompoundListener.__len__c                 C      t | j| jS r   )r   ru   r   r   r   r   r   r&   G  r   z_CompoundListener.__iter__c                 C   s   t | jp| jS r   )r5   r   ru   r   r   r   r   r   J     z_CompoundListener.__bool__N)r   r   r   r   r   r   r   r   r   r   r&   r   r   r   r   r   r   r     s    r   c                   @   sN   e Zd ZdZdZdd Zdd Zddd	Zd
d Zdd Z	dd Z
dd ZdS )rw   zInstance-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.

    )ru   rr   r*   r   rZ   r/   c                 C   sJ   ||j vr
|| d| _|j | | _|| _|j| _t | _t	 | _
d S NF)r.   rS   r   ru   rr   r*   r`   ra   r   rg   rZ   rv   r   r   r   r?   c  s   


z_ListenerCollection.__init__c                 C   rk   )zReturn an event collection which can be modified.

        For _ListenerCollection at the instance level of
        a dispatcher, this returns self.

        r   rl   r   r   r   rn   m  ro   z_ListenerCollection.for_modifyTc                    s\   j }t| j|j  fdd|j D }|| |j|}t|| dS )zIPopulate from the listeners in another :class:`_Dispatch`
        object.c                    s&   g | ]}| vr
r|j v r|qS r   )rZ   )rb   lZexisting_listener_setonly_propagater   r   r   rd   }  s    
z/_ListenerCollection._update.<locals>.<listcomp>N)r   rg   rZ   rF   r#   unionr   Z_stored_in_collection_multi)r   r"   r   Zexisting_listenersZother_listenersZto_associater   r   r   _updatev  s   
z_ListenerCollection._updatec                 C   ,   | | | jr|r| j|j d S d S d S r   )Zprepend_to_listr   rZ   addrV   r   rY   rZ   r   r   r   r^     
   z_ListenerCollection.insertc                 C   r   r   )Zappend_to_listr   rZ   r   rV   r   r   r   r   r      r   z_ListenerCollection.appendc                 C   s,   | j |j | j|j t||  d S r   )r   r$   rV   rZ   discardr   rf   r   rY   r   r   r   r$     s   z_ListenerCollection.removec                 C   s&   t | | j | j  | j  d S r   )r   ri   r   rZ   r(   r   r   r   r   r(     s   
z_ListenerCollection.clearN)T)r   r   r   r;   r   r?   rn   r   r^   r    r$   r(   r   r   r   r   rw   P  s    		

	rw   c                   @   sT   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )ry   )rr   r*   localru   c                 C   s$   d| _ || _|| _|| _| j| _d S r   )r   rr   r*   r   ru   )r   rr   r*   r   r   r   r   r?     s
   z_JoinedListener.__init__c                 C   r   r   )r:   rr   r*   r   r   r   r   r     s   z_JoinedListener.listenersc                 C   rq   r   )r   rC   rs   r   r   r   rC     r   z_JoinedListener._adjust_fn_specc                 C   s   | j | | _ | _| S r   )r   rn   ru   rl   r   r   r   rn     s   z_JoinedListener.for_modifyc                 C      | j || d S r   )r   r^   r   r   r   r   r^        z_JoinedListener.insertc                 C   r   r   )r   r    r   r   r   r   r      r   z_JoinedListener.appendc                 C   s   | j | d S r   )r   r$   r   r   r   r   r$     r   z_JoinedListener.removec                 C   s   t  r   r{   r   r   r   r   r(     s   z_JoinedListener.clearN)r   r   r   r   r?   propertyr   rC   rn   r^   r    r$   r(   r   r   r   r   ry     s    
ry   )r;   
__future__r   r   r`   	itertoolsr   r    r   r   r	   r
   r   ZMemoizedSlotsr   objectr   r)   rp   rt   r   rw   ry   r   r   r   r   <module>   s(    ?=O