
    g[=                     <   d 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
 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 ddlmZ ddlmZ ddlmZ dZ	 d ZegZ	  G d de      Z e       xe_        Zee_         G d d      Z G d de      Zd Zd Zd Zy)a  Extensible class instrumentation.

The :mod:`sqlalchemy.ext.instrumentation` package provides for alternate
systems of class instrumentation within the ORM.  Class instrumentation
refers to how the ORM places attributes on the class which maintain
data and track changes to that data, as well as event hooks installed
on the class.

.. note::
    The extension package is provided for the benefit of integration
    with other object management packages, which already perform
    their own instrumentation.  It is not intended for general use.

For examples of how the instrumentation extension is used,
see the example :ref:`examples_instrumentation`.

    N   )util)
attributes)base)collections)exc)instrumentation)_default_dict_getter)_default_manager_getter)_default_opt_manager_getter)_default_state_getter)ClassManager)InstrumentationFactory__sa_instrumentation_manager__c                 $    t        | t        d      S )z;Find user-specified instrumentation management for a class.N)getattrINSTRUMENTATION_MANAGERclss    ]/var/www/html/AdoubleTech/venv/lib/python3.12/site-packages/sqlalchemy/ext/instrumentation.py%find_native_user_instrumentation_hookr   E   s    3/66    c                        e Zd ZdZ ej
                         Z ej
                         Z ej
                         ZdZ	d Z
d Zd Zd Z fdZd Zd	 Zd
 Zd Z xZS )ExtendedInstrumentationRegistryzExtends :class:`.InstrumentationFactory` with additional
    bookkeeping, to accommodate multiple types of
    class managers.

    Fc                 ^    t         D ]$  } ||      }|| j                  ||      }||fc S  y)N)NN)instrumentation_finders_extended_class_manager)selfclass_finderfactorymanagers        r   _locate_extended_factoryz8ExtendedInstrumentationRegistry._locate_extended_factoryg   sB    - 	FVnG"66vwG''		 r   c                     | j                  |      j                  |g      }|r$t        d|j                  dt	        |            y )Nz6multiple instrumentation implementations specified in z inheritance hierarchy: )!_collect_management_factories_for
difference	TypeError__name__list)r   r   r!   existing_factoriess       r   _check_conflictsz0ExtendedInstrumentationRegistry._check_conflictsp   sQ    !CC

*gY
 	  ??D);$<>  r   c                 H    ||      }t        |t              st        ||      }|t        k7  r| j                  sd| _        t	                |j                         | j                  |<   |j                         | j                  |<   |j                         | j                  |<   |S )NT)
isinstancer   _ClassInstrumentationAdapter	_extended_install_instrumented_lookupsmanager_getter_manager_findersstate_getter_state_findersdict_getter_dict_finders)r   r   r!   r"   s       r   r   z7ExtendedInstrumentationRegistry._extended_class_manager{   s    &/'<0267CGl"4>> "DN)+(/(>(>(@f%&-&:&:&<F#%,%8%8%:6"r   c                 &   t        j                  |      }t               }|D ][  }| j                  |      }||j	                  |j
                         2t        D ]  } ||      }| n d}|j	                  |       ] |j                  d       |S )ac  Return a collection of factories in play or specified for a
        hierarchy.

        Traverses the entire inheritance graph of a cls and returns a
        collection of instrumentation factories for those classes. Factories
        are extracted from active ClassManagers, if available, otherwise
        instrumentation_finders is consulted.

        N)r   class_hierarchysetopt_manager_of_classaddr!   r   discard)r   r   	hierarchy	factoriesmemberr"   r    r!   s           r   r%   zAExtendedInstrumentationRegistry._collect_management_factories_for   s     ((-	E	 	'F//7G"goo.5 #F$VnG*#
 #Gg&	' 	$r   c                     t         |   |       || j                  v r(| j                  |= | j                  |= | j                  |= y y N)super
unregisterr2   r4   r6   )r   r   	__class__s     r   rC   z*ExtendedInstrumentationRegistry.unregister   sM    6"T***%%f-##F+""6* +r   c                 r    	 | j                   j                  |t              } ||      S # t        $ r Y y w xY wrA   )r2   getr   r'   )r   r   r    s      r   r:   z4ExtendedInstrumentationRegistry.opt_manager_of_class   sC    	**..0F #;	  		s    * 	66c                     	 | j                   j                  |t              } ||      }|t        j                  |d|       |S # t
        $ r t        j                  |d|       w xY w)Nz2Can't locate an instrumentation manager for class )r2   rF   r   orm_excUnmappedClassErrorr'   )r   r   r    r"   s       r   manager_of_classz0ExtendedInstrumentationRegistry.manager_of_class   s    	**..s4KLF SkG00HN  N  	,,I#O 	s    A #A*c                 |    |t        d       | j                  j                  |j                  t              |      S NzNone has no persistent state.)AttributeErrorr4   rF   rD   r   r   instances     r   state_ofz(ExtendedInstrumentationRegistry.state_of   sE     !@AA
t""&& 5

 	r   c                 |    |t        d       | j                  j                  |j                  t              |      S rL   )rM   r6   rF   rD   r
   rN   s     r   dict_ofz'ExtendedInstrumentationRegistry.dict_of   sE     !@AA
t!!%% 4

 	r   )r(   
__module____qualname____doc__weakrefWeakKeyDictionaryr2   r4   r6   r/   r#   r+   r   r%   rC   r:   rJ   rP   rR   __classcell__rD   s   @r   r   r   [   sm     1w002.W..0N-G--/MI	"6+	"r   r   c                   v    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)InstrumentationManageraW  User-defined class instrumentation extension.

    :class:`.InstrumentationManager` can be subclassed in order
    to change
    how class instrumentation proceeds. This class exists for
    the purposes of integration with other object management
    frameworks which would like to entirely modify the
    instrumentation methodology of the ORM, and is not intended
    for regular usage.  For interception of class instrumentation
    events, see :class:`.InstrumentationEvents`.

    The API for this class should be considered as semi-stable,
    and may change slightly with new releases.

    c                      y rA    r   r   s     r   __init__zInstrumentationManager.__init__       r   c                     t        |d|       y N_default_class_managersetattrr   r   r"   s      r   managezInstrumentationManager.manage   s    0':r   c                     t        |d       y rb   delattrrf   s      r   rC   z!InstrumentationManager.unregister   s    01r   c                     d }|S )Nc                     | j                   S rA   )rc   r   s    r   rF   z2InstrumentationManager.manager_getter.<locals>.get   s    ---r   r]   )r   r   rF   s      r   r1   z%InstrumentationManager.manager_getter   s    	. 
r   c                      y rA   r]   r   r   keyinsts       r   instrument_attributez+InstrumentationManager.instrument_attribute  r`   r   c                      y rA   r]   rn   s       r   post_configure_attributez/InstrumentationManager.post_configure_attribute  r`   r   c                     t        |||       y rA   rd   rn   s       r   install_descriptorz)InstrumentationManager.install_descriptor	  s    T"r   c                     t        ||       y rA   ri   r   r   ro   s      r   uninstall_descriptorz+InstrumentationManager.uninstall_descriptor      r   c                     t        |||       y rA   rd   )r   r   ro   implementations       r   install_memberz%InstrumentationManager.install_member  s    ^,r   c                     t        ||       y rA   ri   rw   s      r   uninstall_memberz'InstrumentationManager.uninstall_member  ry   r   c                 ,    t        j                  |      S rA   )r   prepare_instrumentation)r   r   ro   collection_classs       r   instrument_collection_classz2InstrumentationManager.instrument_collection_class  s    223CDDr   c                     |j                   S rA   )__dict__r   r   rO   s      r   get_instance_dictz(InstrumentationManager.get_instance_dict  s       r   c                      y rA   r]   r   s      r   initialize_instance_dictz/InstrumentationManager.initialize_instance_dict  r`   r   c                     t        |d|       y N_default_staterd   )r   r   rO   states       r   install_statez$InstrumentationManager.install_state  s    *E2r   c                     t        |d       y r   ri   r   s      r   remove_statez#InstrumentationManager.remove_state!  s    *+r   c                     d S )Nc                     t        | d      S r   )r   )rO   s    r   <lambda>z5InstrumentationManager.state_getter.<locals>.<lambda>%  s    2B C r   r]   r^   s     r   r3   z#InstrumentationManager.state_getter$  s    CCr   c                       fdS )Nc                 (    j                  |       S rA   )r   )rp   r   r   s    r   r   z4InstrumentationManager.dict_getter.<locals>.<lambda>(  s    D2264@ r   r]   r^   s   ``r   r5   z"InstrumentationManager.dict_getter'  s	    @@r   N)r(   rS   rT   rU   r_   rg   rC   r1   rq   rs   ru   rx   r|   r~   r   r   r   r   r   r3   r5   r]   r   r   r[   r[      s_    (;2#-E!3,DAr   r[   c                        e Zd ZdZd Zd Zd Zd ZddZ fdZ	d Z
d	 Zd
 Zd Zd Zd ZddZd ZddZd Zd Zd Zd Z xZS )r.   z?Adapts a user-defined InstrumentationManager to a ClassManager.c                     || _         | j                   j                  |      | _        | j                   j                  |      | _        t        j                  | |       y rA   )_adaptedr3   
_get_stater5   	_get_dictr   r_   )r   r   overrides      r   r_   z%_ClassInstrumentationAdapter.__init__.  sE     --44V<226:dF+r   c                 P    | j                   j                  | j                  |        y rA   )r   rg   r   r   s    r   rg   z#_ClassInstrumentationAdapter.manage5  s    T[[$/r   c                 P    | j                   j                  | j                  |        y rA   )r   rC   r   r   s    r   rC   z'_ClassInstrumentationAdapter.unregister8  s      d3r   c                 L    | j                   j                  | j                        S rA   )r   r1   r   r   s    r   r1   z+_ClassInstrumentationAdapter.manager_getter;  s    }}++DKK88r   c                     t        j                  | |||       |s(| j                  j                  | j                  ||       y y rA   )r   rq   r   r   )r   ro   rp   
propagateds       r   rq   z1_ClassInstrumentationAdapter.instrument_attribute>  s8    ))$T:FMM..t{{CF r   c                 x    t         |   |       | j                  j                  | j                  || |          y rA   )rB   rs   r   r   )r   ro   rD   s     r   rs   z5_ClassInstrumentationAdapter.post_configure_attributeC  s.    (-..t{{CcKr   c                 R    | j                   j                  | j                  ||       y rA   )r   ru   r   )r   ro   rp   s      r   ru   z/_ClassInstrumentationAdapter.install_descriptorG  s    ((c4@r   c                 P    | j                   j                  | j                  |       y rA   )r   rx   r   r   ro   s     r   rx   z1_ClassInstrumentationAdapter.uninstall_descriptorJ  s    **4;;<r   c                 R    | j                   j                  | j                  ||       y rA   )r   r|   r   )r   ro   r{   s      r   r|   z+_ClassInstrumentationAdapter.install_memberM  s    $$T[[#~Fr   c                 P    | j                   j                  | j                  |       y rA   )r   r~   r   r   s     r   r~   z-_ClassInstrumentationAdapter.uninstall_memberP  s    &&t{{C8r   c                 P    | j                   j                  | j                  ||      S rA   )r   r   r   )r   ro   r   s      r   r   z8_ClassInstrumentationAdapter.instrument_collection_classS  s%    }}88KK.
 	
r   c                 x    t        | j                  dd       }|r
 ||||      S t        j                  | |||      S )Ninitialize_collection)r   r   r   r   )r   ro   r   r!   delegates        r   r   z2_ClassInstrumentationAdapter.initialize_collectionX  sC    4==*A4HC0055c5' r   c                 t    | j                   j                  | j                         }| j                  ||       |S rA   )r   __new__setup_instance)r   r   rO   s      r   new_instancez)_ClassInstrumentationAdapter.new_instancea  s/    ;;&&t{{3He,r   c                 H    | j                  |      ry| j                  |      S )zInstall a default InstanceState if none is present.

        A private convenience method used by the __init__ decorator.
        F)	has_stater   rN   s     r   _new_state_if_nonez/_ClassInstrumentationAdapter._new_state_if_nonef  s$    
 >>(#&&x00r   c                     | j                   j                  | j                  |       || j                  ||       }| j                   j	                  | j                  ||       |S rA   )r   r   r   _state_constructorr   )r   rO   r   s      r   r   z+_ClassInstrumentationAdapter.setup_instancep  sR    ..t{{HE=++Hd;E 	##DKK5Ar   c                 P    | j                   j                  | j                  |       y rA   )r   r   r   rN   s     r   teardown_instancez._ClassInstrumentationAdapter.teardown_instancez  s    ""4;;9r   c                 Z    	 | j                  |       y# t        j                  $ r Y yw xY w)NTF)r   rH   NO_STATErN   s     r   r   z&_ClassInstrumentationAdapter.has_state}  s2    	OOH%   		s    **c                     | j                   S rA   )r   r   s    r   r3   z)_ClassInstrumentationAdapter.state_getter  s    r   c                     | j                   S rA   )r   r   s    r   r5   z(_ClassInstrumentationAdapter.dict_getter  s    ~~r   )FrA   )r(   rS   rT   rU   r_   rg   rC   r1   rq   rs   ru   rx   r|   r~   r   r   r   r   r   r   r   r3   r5   rX   rY   s   @r   r.   r.   +  si    I,049G
LA=G9


1:r   r.   c                      t        t        t        j                  t        j                  t        j
                  t        j                               y)a  Replace global class/object management functions
    with ExtendedInstrumentationRegistry implementations, which
    allow multiple types of class managers to be present,
    at the cost of performance.

    This function is called only by ExtendedInstrumentationRegistry
    and unit tests specific to this behavior.

    The _reinstall_default_lookups() function can be called
    after this one to re-establish the default functions.

    instance_stateinstance_dictrJ   r:   N)_install_lookupsdict_instrumentation_factoryrP   rR   rJ   r:   r]   r   r   r0   r0     s8     3<<2::5FF!9!N!N		
r   c                  j    t        t        t        t        t        t
                     dt        _        y)zRestore simplified lookups.r   FN)r   r   r   r
   r   r   r   r/   r]   r   r   _reinstall_default_lookupsr     s)    0.4!<		
 */&r   c                 j   | d   a | d   a| d   a| d   at         xt        _         xt
        _         t        _         t        xt        _        xt
        _        t        _        t        xt        _        xt
        _        t        _        t        xt        _        xt        _        xt
        _        t        _        y )Nr   r   rJ   r:   )r   r   rJ   r:   orm_baser   orm_instrumentationorm_util)lookupss    r   r   r     s     -.NO,M12"#9: 	H j7* 	H Z5) 	H 
 ;, 4HHH! HH$A H'0r   )rU   rV    r   ormr   r   r   r   r   rH   r	   r   r   orm.instrumentationr
   r   r   r   r   r   r   r   r   r   r   r[   r.   r0   r   r   r]   r   r   <module>r      s   "    "    8 " 6 9 = 7 . 8 ; 27
 AA  {&< {~ $%  ,/G /F  +IA IAX^< ^B.
/Hr   