o
    y¬´\Á2  ã                   @   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   Ú_collection_gced©Úself© r   úG/var/www/html/env/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   Ú_legacy_signaturesc                 S   s   | d S ©Nr   r   )Úsr   r   r   Ú<lambda>Z   s    z,_ClsLevelDispatch.__init__.<locals>.<lambda>)Úkey)r   r+   r
   Úinspect_getargspecÚargsr,   ÚboolÚkeywordsr-   ÚlistÚreversedÚsortedÚgetattrr.   r   Ú_augment_fn_docsÚ__doc__r   ÚWeakKeyDictionaryr/   )r   Ú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)Úno_self)Ú_wrap_fn_for_kwr.   r
   Úget_callable_argspecÚ	TypeErrorr   Ú_wrap_fn_for_legacy)r   rB   ÚnamedrC   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)r7   ÚkwÚargdict©rB   r   r   r   Úwrap_kwo   s   
z2_ClsLevelDispatch._wrap_fn_for_kw.<locals>.wrap_kwr   )r   rB   rR   r   rQ   r   rF   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   rS   rT   )rV   rW   rX   r=   r	   rY   rZ   r$   r[   r/   r\   r]   r!   r_   r   r`   ra   r   r   r   r!   ‹   rh   z_ClsLevelDispatch.appendc                 C   s.   t |ddƒrt ¡ | j|< d S tƒ | j|< d S )NrU   T)r=   ÚcollectionsÚdequer/   r   )r   rd   r   r   r   r]       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   )Ú.0rB   ©Úclslevelr   r   Ú
<listcomp>­   s    z5_ClsLevelDispatch.update_subclass.<locals>.<listcomp>)r/   r]   Ú__mro__r$   )r   rd   rf   r   rl   r   r\   ¦   s   



ÿ€þz!_ClsLevelDispatch.update_subclassc                 C   sX   |j }|g}|r$| d¡}| | ¡ ¡ || jv r"| j|  |j¡ |st || ¡ d S r2   )	rV   rZ   r$   r[   r/   r%   r_   r   Ú_removed_from_collection)r   rb   rd   re   rf   r   r   r   r%   °   s   

üz_ClsLevelDispatch.removec                 C   s8   t ƒ }| j ¡ D ]}| |¡ | ¡  qt | |¡ dS )zClear all class level listenersN)Úsetr/   ÚvaluesrN   r)   r   Ú_clear)r   Úto_clearÚ
dispatcherr   r   r   r)   º   s
   

z_ClsLevelDispatch.clearc                 C   ó   | S )z™Return 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   rD   rK   rF   rg   r!   r]   r\   r%   r)   ry   r   r   r   r   r*   F   s    	

	r*   c                   @   r   )Ú_InstanceLevelDispatchr   c                 C   ó   | j  ||¡S r   )ÚparentrK   ©r   rB   rJ   r   r   r   rK   Ð   r   z&_InstanceLevelDispatch._adjust_fn_specN)r   r   r   r   rK   r   r   r   r   r{   Í   r   r{   c                   @   sn   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d
d„ Zdd„ Zdd„ Zdd„ ZeZdS )Ú_EmptyListenerzÙServes 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   )r}   Úparent_listenersr+   c                 C   s2   ||j vr
| |¡ || _|j | | _|j| _d S r   )r/   r\   r}   r€   r+   ©r   r}   Ú
target_clsr   r   r   rD   ã   s
   

z_EmptyListener.__init__c                 C   sJ   t | j|jƒ}t|| jƒ| u rt|| j|ƒ |S tt|| jƒtƒs#J ‚|S )zìReturn 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.

        )Ú_ListenerCollectionr}   Ú_instance_clsr=   r+   ÚsetattrrW   Ú_JoinedListener)r   rx   Úresultr   r   r   ry   ê   s   	ÿz_EmptyListener.for_modifyc                 O   s   t dƒ‚)Nzneed to call for_modify()©ÚNotImplementedError©r   r7   rO   r   r   r   Ú_needs_modifyú   r(   z_EmptyListener._needs_modifyc                 O   s   | j D ]	}||i |¤Ž qdS ©zExecute this event.N)r€   ©r   r7   rO   rB   r   r   r   Ú__call__ÿ   s   
ÿz_EmptyListener.__call__c                 C   ó
   t | jƒS r   )Úlenr€   r   r   r   r   Ú__len__  ó   
z_EmptyListener.__len__c                 C   r�   r   )r&   r€   r   r   r   r   r'     r’   z_EmptyListener.__iter__c                 C   r�   r   )r8   r€   r   r   r   r   Ú__bool__  r’   z_EmptyListener.__bool__N)r   r   r   r?   Ú	frozensetrc   Ú	listenersr   rD   ry   r‹   Ú	exec_oncerg   r!   r%   r)   rŽ   r‘   r'   r“   Ú__nonzero__r   r   r   r   r   Ô   s    	r   c                   @   sD   e Zd Z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)Ú_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   sn   | j s5| j�% | j sz| |i |¤Ž W d| _ nd| _ w W d  ƒ dS W d  ƒ dS 1 s.w   Y  dS dS )z]Execute this event, but only if it has not been
        executed already for this collection.TN)rš   r™   rŠ   r   r   r   r–     s   üÿ"ûÿz_CompoundListener.exec_oncec                 O   s8   | j D ]	}||i |¤Ž q| jD ]	}||i |¤Ž qdS rŒ   )r€   r•   r�   r   r   r   rŽ   #  s
   

ÿz_CompoundListener.__call__c                 C   s   t | jƒt | jƒ S r   )r�   r€   r•   r   r   r   r   r‘   +  s   z_CompoundListener.__len__c                 C   ó   t | j| jƒS r   )r   r€   r•   r   r   r   r   r'   .  r   z_CompoundListener.__iter__c                 C   s   t | jp| jƒS r   )r8   r•   r€   r   r   r   r   r“   1  ó   z_CompoundListener.__bool__N)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 )rƒ   zÛInstance-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.

    )r€   r}   r+   r•   rc   r0   c                 C   sJ   ||j vr
| |¡ d| _|j | | _|| _|j| _t ¡ | _t	ƒ | _
d S ©NF)r/   r\   rš   r€   r}   r+   ri   rj   r•   rq   rc   r�   r   r   r   rD   J  s   


z_ListenerCollection.__init__c                 C   rv   )zžReturn an event collection which can be modified.

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

        r   rw   r   r   r   ry   T  rz   z_ListenerCollection.for_modifyTc                    s\   ˆj }t|ƒ‰ ˆj |j¡ ‡ ‡‡fdd„|j D ƒ}| |¡ |j |¡}t ˆ||¡ dS )zMPopulate from the listeners in another :class:`_Dispatch`
            object.c                    s&   g | ]}|ˆ vr
ˆr|ˆj v r|‘qS r   )rc   )rk   Úl©Úexisting_listener_setÚonly_propagater   r   r   rn   d  s    ÿ
üz/_ListenerCollection._update.<locals>.<listcomp>N)r•   rq   rc   rN   r$   Úunionr   Ú_stored_in_collection_multi)r   r#   r£   Úexisting_listenersÚother_listenersÚto_associater   r¡   r   Ú_update]  s   þ
z_ListenerCollection._updatec                 C   ó,   |  | | j¡r|r| j |j¡ d S d S d S r   )Úprepend_to_listr•   rc   Úaddr_   ©r   rb   rc   r   r   r   rg   q  ó
   þz_ListenerCollection.insertc                 C   rª   r   )Úappend_to_listr•   rc   r¬   r_   r­   r   r   r   r!   v  r®   z_ListenerCollection.appendc                 C   s,   | j  |j¡ | j |j¡ t || ¡ d S r   )r•   r%   r_   rc   Údiscardr   rp   ©r   rb   r   r   r   r%   {  s   z_ListenerCollection.removec                 C   s&   t  | | j¡ | j ¡  | j ¡  d S r   )r   rs   r•   rc   r)   r   r   r   r   r)   €  s   
z_ListenerCollection.clearN)T)r   r   r   r?   r   rD   ry   r©   rg   r!   r%   r)   r   r   r   r   rƒ   7  s    		

	rƒ   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 )r†   )r}   r+   Úlocalr€   c                 C   s$   d| _ || _|| _|| _| j| _d S rŸ   )rš   r}   r+   r²   r€   )r   r}   r+   r²   r   r   r   rD   ‰  s
   z_JoinedListener.__init__c                 C   r�   r   )r=   r}   r+   r   r   r   r   r•   �  s   z_JoinedListener.listenersc                 C   r|   r   )r²   rK   r~   r   r   r   rK   ”  r   z_JoinedListener._adjust_fn_specc                 C   s   | j  |¡ | _ | _| S r   )r²   ry   r€   rw   r   r   r   ry   —  s   z_JoinedListener.for_modifyc                 C   ó   | j  ||¡ d S r   )r²   rg   r­   r   r   r   rg   ›  ó   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   rD   Úpropertyr•   rK   ry   rg   r!   r%   r)   r   r   r   r   r†   †  s    
r†   )r?   Ú
__future__r   r   ri   Ú	itertoolsr   r   Ú r   r   r	   r
   r   ÚMemoizedSlotsr   Úobjectr   r*   r{   r   r˜   rƒ   r†   r   r   r   r   Ú<module>   s(    =&O