
    +h@                     x   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	 dd
l
mZ ddlmZ  ej                  e      Zd ZdZdZ G d de      Z G d de      Z e       e_         G d dej.                  ee      Z G d de      Z G d de      Z G d de      Z G d de      Z G d dee      Zy)z@Path tracking utilities, representing mapper graph traversals.

    )chainN   )base   )exc)
inspection)util)visitors)HasCacheKeyc                 ,    t         j                  |       S N)PathRegistrydeserializepaths    O/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/orm/path_registry.py_unreduce_pathr      s    ##D))    *_sa_defaultc                   :   e Zd ZdZdZdZdZdej                  j                  fgZ
d Zd Zed        Zd Zd	 ZddZd Zd Zed        Zd Zd Zd Zd Zed        Zed        Zed        Zed        Zd Zed        Zed        Z ed        Z!d Z"d Z#d Z$y
)r   a  Represent query load paths and registry functions.

    Basically represents structures like:

    (<User mapper>, "orders", <Order mapper>, "items", <Item mapper>)

    These structures are generated by things like
    query options (joinedload(), subqueryload(), etc.) and are
    used to compose keys stored in the query._attributes dictionary
    for various options.

    They are then re-composed at query compile/result row time as
    the query is formed and as rows are fetched, where they again
    serve to compose keys to look up options in the context.attributes
    dictionary, which is copied from query._attributes.

    The path structure has a limited amount of caching, where each
    "root" ultimately pulls from a fixed registry associated with
    the first mapper, that also contains elements for each of its
    property keys.  However paths longer than two elements, which
    are the exception rather than the rule, are generated on an
    as-needed basis.

     Fr   c                     	 |d uxr | j                   |j                  k(  S # t        $ r$ t        j                  dt        |      z         Y yw xY w)N1Comparison of PathRegistry to %r is not supportedFr   _path_for_compareAttributeErrorr	   warntypeselfothers     r   __eq__zPathRegistry.__eq__C   sU    	$Me6M6M)MM 	IIC;  	   ! *AAc                     	 |d u xs | j                   |j                  k7  S # t        $ r$ t        j                  dt        |      z         Y yw xY w)Nr   Tr   r    s     r   __ne__zPathRegistry.__ne__M   sT    	D=HDII1H1H$HH 	IIC;  	r$   c                     | j                   S r   r   r!   s    r   r   zPathRegistry._path_for_compareW   s    yyr   c                 V    t         j                  d|| |       |||| j                  f<   y )Nzset '%s' on path '%s' to '%s')logdebugnatural_pathr!   
attributeskeyvalues       r   setzPathRegistry.set[   s*    		13eD/4
C**+,r   c                 p    t         j                  d|| |       |j                  || j                  f|       y )Nz$setdefault '%s' on path '%s' to '%s')r*   r+   
setdefaultr,   r-   s       r   r3   zPathRegistry.setdefault_   s0    		8#tUKsD$5$56>r   Nc                 4    || j                   f}||v r||   S |S r   )r,   r-   s       r   getzPathRegistry.getc   s)    D%%&*c?"Lr   c                 ,    t        | j                        S r   lenr   r(   s    r   __len__zPathRegistry.__len__j   s    499~r   c                     t        |       S r   )idr(   s    r   __hash__zPathRegistry.__hash__m   s    $xr   c                 ,    t        | j                        S r   r7   r(   s    r   lengthzPathRegistry.lengthp   s    499~r   c              #   x   K   | j                   }t        dt        |      d      D ]  }||   ||dz      f  y wNr   r   r   )r   ranger8   )r!   r   is      r   pairszPathRegistry.pairst   sB     yyq#d)Q' 	'Aq'4A;&&	's   8:c                     t        dt        | j                        d      D cg c]  }| j                  |    c}D ]  }|j                  j	                  |      s y yc c}w )Nr   r   TF)rA   r8   r   mapperisa)r!   rE   rB   path_mappers       r   contains_mapperzPathRegistry.contains_mappery   sW    273tyy>12MNQDIIaLN 	K!!%%f-	 	 Os   Ac                 "    || j                   f|v S r   r   )r!   r.   r/   s      r   containszPathRegistry.contains   s    TYY:--r   c                 0    t         | j                         ffS r   )r   	serializer(   s    r   
__reduce__zPathRegistry.__reduce__   s     0222r   c                    t        t        t        dt        |      d      D cg c]  }||   	 c}D cg c]1  }|j                  s|j
                  r|j                  n
t        |      3 c}t        dt        |      d      D cg c].  }||   j                  r||   j                  nt        ||         0 c}d gz               S c c}w c c}w c c}w r@   )
listziprA   r8   	is_mapperis_aliased_classclass_stris_propertyr/   )clsr   rB   ms       r   _serialize_pathzPathRegistry._serialize_path   s     05QD	1/EF!d1gF "#0B0BAHHQO #1c$i3 %)G$7$7DGKKc$q'lJ &	

 	
 Gs   C6C3Cc                     d }d }t        t        |D cg c]  \  }} ||       |||      f c}}       }|r
|d   |dd }|S c c}}w )Nc                 z    | t         j                  vrt        j                  | d      S t         j                  |    S NT)	configure)	PathToken_internorm_base_inspect_mapped_class)mclss    r   _deserialize_mapper_tokenzAPathRegistry._deserialize_path.<locals>._deserialize_mapper_token   s>    
 y000 ..ttD
 &&t,r   c                     |y |t         j                  v rt         j                  |   S t        j                  | d      j                  |   S r[   )r]   r^   r_   r`   attrs)ra   r/   s     r   _deserialize_key_tokenz>PathRegistry._deserialize_path.<locals>._deserialize_key_token   sM    {	))) ((--55D% r   r   )tupler   )rV   r   rb   re   ra   r/   ps          r   _deserialize_pathzPathRegistry._deserialize_path   st    		  &*
 "c 2$7.tS9

 2!BAs   Ac           	          |j                         D cg c]   \  }}t        |t              r|d   |v r||f" c}}D cg c]  \  \  }}}|| j                  |      f|f c}}}S c c}}w c c}}}w )Nr   )items
isinstancerg   rX   )rV   dict_tokenskvr/   r   r0   s           r   serialize_context_dictz#PathRegistry.serialize_context_dict   s     "KKM'Aqa'AaDFN A'
 
"dU 3&&t,-u5
 	
'
s   %A("A.c                 @     t        j                   fd|D              S )Nc              3   f   K   | ](  \  \  }}}|t        j                  |            f|f * y wr   )rg   ri   ).0r/   r   r0   rV   s       r   	<genexpr>z8PathRegistry.deserialize_context_dict.<locals>.<genexpr>   s;       
"dU 5..t456> 
s   .1)r	   OrderedDict)rV   
serializeds   ` r   deserialize_context_dictz%PathRegistry.deserialize_context_dict   s#      
&0 
 
 	
r   c                 <    | j                   }| j                  |      S r   )r   rX   )r!   r   s     r   rL   zPathRegistry.serialize   s    yy##D))r   c                 L    |y | j                  |      }| j                  |      S r   )ri   coerce)rV   r   rh   s      r   r   zPathRegistry.deserialize   s(    <!!$'zz!}r   c                 r    |j                   rt        | j                  |      S t        | j                  |      S r   )rQ   CachingEntityRegistryrootSlotsEntityRegistry)rV   rE   s     r   
per_mapperzPathRegistry.per_mapper   s-    (6::&sxx88r   c                 F    t        j                  d || j                        S )Nc                     | |   S r   r   prevnexts     r   <lambda>z%PathRegistry.coerce.<locals>.<lambda>   
    d4j r   )r	   reducer~   )rV   raws     r   r{   zPathRegistry.coerce   s    {{8#sxxHHr   c                     |j                  dt        z         rt        | |      S |j                  dt        z         rt        | j                  |      S t        j                  d|z        )N:zinvalid token: %s)endswith_WILDCARD_TOKENTokenRegistry_DEFAULT_TOKENr~   r   ArgumentError)r!   tokens     r   r   zPathRegistry.token   sY    >>#/0 u--^^C.01 E22##$7%$?@@r   c                 F    t        j                  d |j                  |       S )Nc                     | |   S r   r   r   s     r   r   z&PathRegistry.__add__.<locals>.<lambda>   r   r   )r	   r   r   r    s     r   __add__zPathRegistry.__add__   s    {{8%**dKKr   c                 N    | j                   j                  d| j                  dS )N())	__class____name__r   r(   s    r   __repr__zPathRegistry.__repr__   s    >>22DII>>r   r   )%r   
__module____qualname____doc__	__slots__is_tokenis_rootr
   ExtendedInternalTraversaldp_has_cache_key_list_cache_key_traversalr#   r&   propertyr   r1   r3   r5   r9   r<   r>   rC   rH   rJ   rM   classmethodrX   ri   rq   rx   rL   r   r   r{   r   r   r   r   r   r   r   r       s:   2 IHG 
33IIJ  5?  '
.3 
 
 ! !F 
 
 
 
*   9 9 I IAL?r   r   c                   .    e Zd ZdZdZdxZZdZdZdZ	d Z
y)RootRegistryzXRoot registry, defers to mappers so that
    paths are maintained per-root-mapper.

    Tr   Fc                 d    |t         j                  v rt         j                  |   S |j                  S r   )r]   r^   _path_registryr!   entitys     r   __getitem__zRootRegistry.__getitem__   s-    Y&&&$$V,,(((r   N)r   r   r   r   inherit_cacher   r,   
has_entityrR   r   r   r   r   r   r   r      s-    
 MD<JG)r   r   c                   :    e Zd ZdZi Zd Zed        Zed        Z	y)r]   zcacheable string tokenc                     t        |       fS r   rT   )r!   anon_map
bindparamss      r   _gen_cache_keyzPathToken._gen_cache_key  s    D	|r   c                      y r   r   r(   s    r   r   zPathToken._path_for_compare  s    r   c                 t    || j                   v r| j                   |   S t        |      x| j                   |<   }|S r   )r^   r]   )rV   strvalueresults      r   internzPathToken.intern  s:    s{{";;x((-6x-@@CKK!FMr   N)
r   r   r   r   r^   r   r   r   r   r   r   r   r   r]   r]     s7     G    r   r]   c                   .    e Zd ZdZdZd ZdZdZd Zd Z	y)r   )r   parentr   r,   Tc                     t         j                  |      }|| _        || _        |j                  |fz   | _        |j
                  |fz   | _        y r   )r]   r   r   r   r   r,   )r!   r   r   s      r   __init__zTokenRegistry.__init__"  sG      '
KK5(*	"//5(:r   Fc              #   <  K   | j                   j                  so| j                   j                  sY| j                   j                  j	                         D ]1  }t        | j                   j                   |   | j                         3 y | j                   j                  ry| j                   j                  j                  rY|  | j                   j                  j                  D ]1  }t        | j                   j                   |   | j                         3 y |  y wr   )
r   rR   r   rE   iterate_to_rootr   r   r   _is_with_polymorphic_with_polymorphic_entities)r!   ents     r   generate_for_superclassesz'TokenRegistry.generate_for_superclasses.  s     {{++DKK4G4G{{))99; I#DKK$6$6s$;TZZHHI KK((""77J{{))DD I#DKK$6$6s$;TZZHHI Js   DDc                     t               r   )NotImplementedErrorr   s     r   r   zTokenRegistry.__getitem__<  s    !##r   N)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s'    ;IM; JH$r   r   c                       e Zd ZdZdZd Zd Zej                  d        Z	ej                  d        Z
ed        Zed        Zd	 Zy
)PropRegistryFTc                    t        j                  |d         }|}|j                  r|j                  r|j                  |j                     x}}n|j                  r|j
                  ru|j                  |j
                  v r]|d   j                  |j                        }|j                  |   }|j                  r!|j                  |j                     }d| _        nq|}nn|j                  rb|j                  rV|j                  |j                  ur>|j                  j                  |j                        r|j                  |j                     }|| _
        || _        |j                  |fz   | _        |j                  |fz   | _        d|j                  | j                  j                  z   f| _        | j                  j                  | _        d| j                  f| _        y )Nrf   Tloader)r   inspectrR   _use_mapper_pathr   with_polymorphic_mappers_entity_for_mapperrE   is_unnaturalrF   propr   r,   _wildcard_token_wildcard_path_loader_key_default_path_loader_key_loader_key)r!   r   r   inspnatural_parentsubclass_entitys         r   r   zPropRegistry.__init__D  s    !!&*-$$(=(=&,mmDKK&@@F^!!--t<<<$Rj;;DKKHO]]?3F@ }}!'/E/E!F$(!!'!!%%;;,#]]4;;7N	KK4')	*774'A KK$))333*
& )-		(J(J%$d&7&78r   c                 F    dj                  d | j                  D              S )Nz -> c              3   2   K   | ]  }t        |        y wr   r   )rt   elems     r   ru   z'PropRegistry.__str__.<locals>.<genexpr>  s     ;3t9;s   )joinr   r(   s    r   __str__zPropRegistry.__str__  s    {{;;;;r   c                 .    | j                   j                  S r   )r   _links_to_entityr(   s    r   r   zPropRegistry.has_entity  s    yy)))r   c                 .    | j                   j                  S r   )r   r   r(   s    r   r   zPropRegistry.entity      yyr   c                 .    | j                   j                  S r   )r   rE   r(   s    r   rE   zPropRegistry.mapper  r   r   c                      | | j                      S r   )r   r(   s    r   entity_pathzPropRegistry.entity_path  s    DKK  r   c                 d    t        |t        t        f      r| j                  |   S t	        | |      S r   )rl   intslicer   r   r   s     r   r   zPropRegistry.__getitem__  s,    fsEl+99V$$&tV44r   N)r   r   r   r   r   r   r   r	   memoized_propertyr   r   r   rE   r   r   r   r   r   r   r   @  sy    LMF9P< 
* * 
        ! !5r   r   c                   J    e Zd ZdZdZd Zed        Zed        Zd Z	e	Z
d Zy)	AbstractEntityRegistryr   Tc                    || _         || _        |j                  | _        || _        |j                  |fz   | _        |j                  r| j                  s|j
                  r|j                  j                  |j                  d   j                        r |j                  |j                  fz   | _        y |j                  |j                  d   j                  fz   | _        y | j                  | _        y )Nrf   )	r/   r   rR   r   r   r   rE   rF   r,   )r!   r   r   s      r   r   zAbstractEntityRegistry.__init__  s     & 7 7KK6)+	 ;;D11V5H5H }}  !4!4R!8!?!?@$*$7$76==:J$J!$*$7$7''+22; %! !%		Dr   c                     | S r   r   r(   s    r   r   z"AbstractEntityRegistry.entity_path  s    r   c                 T    t        j                  | j                        j                  S r   )r   r   r   rE   r(   s    r   rE   zAbstractEntityRegistry.mapper  s    !!$++.555r   c                      y)NTr   r(   s    r   __bool__zAbstractEntityRegistry.__bool__  s    r   c                     t        |t        t        f      r| j                  |   S |t        j
                  v rt        | t        j
                  |         S t        | |      S r   )rl   r   r   r   r]   r^   r   r   r   s     r   r   z"AbstractEntityRegistry.__getitem__  sR    fsEl+99V$$y((( y'8'8'@AAf--r   N)r   r   r   r   r   r   r   r   rE   r   __nonzero__r   r   r   r   r   r     sK    IJ&*P   6 6 K.r   r   c                       e Zd ZdZdZy)r   T)r/   r   rR   r   r   r,   N)r   r   r   r   r   r   r   r   r   r     s     MIr   r   c                       e Zd ZdZd Zd Zy)r}   Tc                 x    t        |t        t        f      r| j                  |   S t        j                  | |      S r   )rl   r   r   r   dictr   r   s     r   r   z!CachingEntityRegistry.__getitem__  s2    fsEl+99V$$##D&11r   c                 (    t        | |      x| |<   }|S r   )r   )r!   r/   items      r   __missing__z!CachingEntityRegistry.__missing__  s    'c22S	Dr   N)r   r   r   r   r   r   r   r   r   r}   r}     s     M2r   r}   ) r   	itertoolsr   logging r   r_   r   r   r	   sqlr
   sql.traversalsr   	getLoggerr   r*   r   r   r   r   r   r~   InspectionAttrrT   r]   r   r   r   r   r   r}   r   r   r   <module>r      s           (g!* N?; N?b)< )( !N ''c * $L  $Fc5< c5L@.\ @.F0 2D r   