o
    Rc"B                     @   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	 dd
l
mZ ddlmZ eeZdd ZdZdZG dd deZG dd deZe e_G dd deje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eZdS ) z@Path tracking utilities, representing mapper graph traversals.

    )chainN   )base   )exc)
inspection)util)visitors)HasCacheKeyc                 C   s
   t | S N)PathRegistrydeserializepath r   >D:\Flask\env\Lib\site-packages\sqlalchemy/orm/path_registry.py_unreduce_path      
r   *Z_sa_defaultc                   @   s  e Zd ZdZdZdZdZdejj	fgZ
dd Zdd Zed	d
 Zdd Zdd Zd6ddZdd Zdd Zedd Zdd Zdd Zdd Zdd Zed d! Zed"d# Zed$d% Zed&d' Zd(d) Zed*d+ Zed,d- Z ed.d/ Z!d0d1 Z"d2d3 Z#d4d5 Z$dS )7r   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.

    r   Fr   c                 C   s>   z|d uo
| j |jkW S  ty   tdt|  Y dS w )N1Comparison of PathRegistry to %r is not supportedFr   _path_for_compareAttributeErrorr   warntypeselfotherr   r   r   __eq__C      zPathRegistry.__eq__c                 C   s>   z|d u p
| j |jkW S  ty   tdt|  Y dS w )Nr   Tr   r   r   r   r   __ne__M   r   zPathRegistry.__ne__c                 C   s   | j S r   r   r   r   r   r   r   W   s   zPathRegistry._path_for_comparec                 C   s"   t d|| | |||| jf< d S )Nzset '%s' on path '%s' to '%s')logdebugnatural_pathr   
attributeskeyvaluer   r   r   set[   s   zPathRegistry.setc                 C   s&   t d|| | ||| jf| d S )Nz$setdefault '%s' on path '%s' to '%s')r"   r#   
setdefaultr$   r%   r   r   r   r*   _   s   zPathRegistry.setdefaultNc                 C   s   || j f}||v r|| S |S r   )r$   r%   r   r   r   getc   s   
zPathRegistry.getc                 C   
   t | jS r   lenr   r!   r   r   r   __len__j   r   zPathRegistry.__len__c                 C   s   t | S r   )idr!   r   r   r   __hash__m   s   zPathRegistry.__hash__c                 C   r,   r   r-   r!   r   r   r   lengthp      
zPathRegistry.lengthc                 c   s8    | j }tdt|dD ]}|| ||d  fV  qd S )Nr   r   r   )r   ranger.   )r   r   ir   r   r   pairst   s
   zPathRegistry.pairsc                    s@    fddt dt jdD D ]}|jr||r dS qdS )Nc                    s   g | ]} j | qS r   r   .0r5   r!   r   r   
<listcomp>z   s    z0PathRegistry.contains_mapper.<locals>.<listcomp>r   r   TF)r4   r.   r   	is_mapperisa)r   mapperZpath_mapperr   r!   r   contains_mappery   s
   $zPathRegistry.contains_mapperc                 C   s   || j f|v S r   r   )r   r&   r'   r   r   r   contains      zPathRegistry.containsc                 C   s   t |  ffS r   )r   	serializer!   r   r   r   
__reduce__   r?   zPathRegistry.__reduce__c                    sR   t tdd  fddtdt dD D  fddtdt dD d g S )Nc                 S   s&   g | ]}|j s
|jr|jnt|qS r   )r:   is_aliased_classclass_str)r8   mr   r   r   r9      s    z0PathRegistry._serialize_path.<locals>.<listcomp>c                    s   g | ]} | qS r   r   r7   r   r   r   r9      s    r   r   c                    s,   g | ]} | j r | jnt | qS r   )Zis_propertyr'   rD   r7   r   r   r   r9      s    r   )listzipr4   r.   )clsr   r   r   r   _serialize_path   s   
zPathRegistry._serialize_pathc                    sL   dd dd  t t fdd|D  }|r$|d d u r$|dd }|S )	Nc                 S   s"   | t jvrtj| ddS t j|  S NT)	configure)	PathToken_internorm_base_inspect_mapped_class)mclsr   r   r   _deserialize_mapper_token   s
   zAPathRegistry._deserialize_path.<locals>._deserialize_mapper_tokenc                 S   s4   |d u rd S |t jv rt j| S tj| ddj| S rJ   )rL   rM   rN   rO   attrs)rP   r'   r   r   r   _deserialize_key_token   s   

z>PathRegistry._deserialize_path.<locals>._deserialize_key_tokenc                    s"   g | ]\}}| ||fqS r   r   )r8   rP   r'   rS   rQ   r   r   r9      s    z2PathRegistry._deserialize_path.<locals>.<listcomp>r   )tupler   rH   r   pr   rT   r   _deserialize_path   s   	
zPathRegistry._deserialize_pathc                    s$    fddfdd|  D D S )Nc                    s&   g | ]\\}}}|  |f|fqS r   )rI   r8   r'   r   r(   rH   r   r   r9      s    
z7PathRegistry.serialize_context_dict.<locals>.<listcomp>c                    s.   g | ]\}}t |tr|d   v r||fqS )r   )
isinstancerV   )r8   kv)tokensr   r   r9      s    
)items)rH   Zdict_r_   r   )rH   r_   r   serialize_context_dict   s
   

z#PathRegistry.serialize_context_dictc                    s   t  fdd|D S )Nc                 3   s.    | ]\\}}}|t  |f|fV  qd S r   )rV   rY   rZ   r[   r   r   	<genexpr>   s
    

z8PathRegistry.deserialize_context_dict.<locals>.<genexpr>)r   OrderedDict)rH   Z
serializedr   r[   r   deserialize_context_dict   s   z%PathRegistry.deserialize_context_dictc                 C   s   | j }| |S r   )r   rI   )r   r   r   r   r   r@      s   
zPathRegistry.serializec                 C   s    |d u rd S |  |}| |S r   )rY   coercerW   r   r   r   r      s   

zPathRegistry.deserializec                 C   s   |j r	t| j|S t| j|S r   )r:   CachingEntityRegistryrootSlotsEntityRegistry)rH   r<   r   r   r   
per_mapper   s   zPathRegistry.per_mapperc                 C   s   t dd || jS )Nc                 S      | | S r   r   prevnextr   r   r   <lambda>       z%PathRegistry.coerce.<locals>.<lambda>)r   reducerg   )rH   rawr   r   r   re      s   zPathRegistry.coercec                 C   s@   | dt rt| |S | dt rt| j|S td| )N:zinvalid token: %s)endswith_WILDCARD_TOKENTokenRegistry_DEFAULT_TOKENrg   r   ArgumentError)r   tokenr   r   r   rx      s
   
zPathRegistry.tokenc                 C   s   t dd |j| S )Nc                 S   rj   r   r   rk   r   r   r   rn      ro   z&PathRegistry.__add__.<locals>.<lambda>)r   rp   r   r   r   r   r   __add__   s   zPathRegistry.__add__c                 C   s   d| j j| jf S )Nz%s(%r))	__class____name__r   r!   r   r   r   __repr__   s   zPathRegistry.__repr__r   )%r{   
__module____qualname____doc__	__slots__is_tokenis_rootr	   ZExtendedInternalTraversalZdp_has_cache_key_listZ_cache_key_traversalr   r    propertyr   r)   r*   r+   r/   r1   r2   r6   r=   r>   rA   classmethodrI   rY   ra   rd   r@   r   ri   re   rx   ry   r|   r   r   r   r   r       sP    







#





r   c                   @   s0   e Zd ZdZdZd ZZdZdZdZ	dd Z
dS )RootRegistryzXRoot registry, defers to mappers so that
    paths are maintained per-root-mapper.

    Tr   Fc                 C   s   |t jv r
t j| S |jS r   )rL   rM   Z_path_registryr   entityr   r   r   __getitem__   s   

zRootRegistry.__getitem__N)r{   r}   r~   r   inherit_cacher   r$   
has_entityrB   r   r   r   r   r   r   r      s    r   c                   @   s4   e Zd ZdZi Zdd Zedd Zedd Z	dS )	rL   zcacheable string tokenc                 C   s
   t | fS r   rD   )r   Zanon_mapZ
bindparamsr   r   r   _gen_cache_key  r   zPathToken._gen_cache_keyc                 C   s   d S r   r   r!   r   r   r   r        zPathToken._path_for_comparec                 C   s*   || j v r
| j | S t| | j |< }|S r   )rM   rL   )rH   Zstrvalueresultr   r   r   intern  s   

zPathToken.internN)
r{   r}   r~   r   rM   r   r   r   r   r   r   r   r   r   rL     s    
rL   c                   @   s4   e Zd ZdZdZdd ZdZdZdd Zdd	 Z	d
S )ru   )rx   parentr   r$   Tc                 C   s6   t |}|| _|| _|j|f | _|j|f | _d S r   )rL   r   rx   r   r   r$   )r   r   rx   r   r   r   __init__"  s
   
zTokenRegistry.__init__Fc                 c   s    | j js| j js| j j D ]}t| j j | | jV  qd S | j jr@| j jjr@| V  | j jj	D ]}t| j j | | jV  q0d S | V  d S r   )
r   rB   r   r<   Ziterate_to_rootru   rx   r   Z_is_with_polymorphicZ_with_polymorphic_entities)r   entr   r   r   generate_for_superclasses.  s   
z'TokenRegistry.generate_for_superclassesc                 C   s   t  r   )NotImplementedErrorr   r   r   r   r   <  s   zTokenRegistry.__getitem__N)
r{   r}   r~   r   r   r   r   r   r   r   r   r   r   r   ru     s    ru   c                   @   s`   e Zd ZdZdZdd Zdd Zejdd Z	ejd	d
 Z
edd Zedd Zdd ZdS )PropRegistryFTc                 C   s
  t |d }|}|jr|jr|j|j  }}nB|jrA|jrA|j|jv rA|d |j}|j| }|jr>|j|j }d| _n|}n|jrZ|jrZ|j|jurZ|j	|jrZ|j|j }|| _
|| _|j|f | _|j|f | _d|j| j
j f| _| j
j| _d| jf| _d S )NrU   Tloader)r   inspectrB   Z_use_mapper_pathr   Zwith_polymorphic_mappersZ_entity_for_mapperr<   is_unnaturalr;   propr   r$   Z_wildcard_tokenZ_wildcard_path_loader_keyZ_default_path_loader_keyZ_loader_key)r   r   r   ZinspZnatural_parentZsubclass_entityr   r   r   r   D  sH   

 
zPropRegistry.__init__c                 C   s   d dd | jD S )Nz -> c                 s   s    | ]}t |V  qd S r   r   )r8   elemr   r   r   rb     s    z'PropRegistry.__str__.<locals>.<genexpr>)joinr   r!   r   r   r   __str__  s   zPropRegistry.__str__c                 C      | j jS r   )r   Z_links_to_entityr!   r   r   r   r        zPropRegistry.has_entityc                 C   r   r   )r   r   r!   r   r   r   r     r   zPropRegistry.entityc                 C   r   r   )r   r<   r!   r   r   r   r<     r   zPropRegistry.mapperc                 C   s
   | | j  S r   )r   r!   r   r   r   entity_path  r3   zPropRegistry.entity_pathc                 C   s"   t |ttfr| j| S t| |S r   )r\   intslicer   rh   r   r   r   r   r     s   

zPropRegistry.__getitem__N)r{   r}   r~   r   r   r   r   r   Zmemoized_propertyr   r   r   r<   r   r   r   r   r   r   r   @  s    H



r   c                   @   sH   e Zd ZdZdZdd Zedd Zedd Zd	d
 Z	e	Z
dd ZdS )AbstractEntityRegistryr   Tc                 C   s   || _ || _|j| _|| _|j|f | _|jr>| js|jr>|j|jd jr1|j|jf | _d S |j|jd jf | _d S | j| _d S )NrU   )	r'   r   rB   r   r   r   r<   r;   r$   )r   r   r   r   r   r   r     s   
zAbstractEntityRegistry.__init__c                 C   s   | S r   r   r!   r   r   r   r     r   z"AbstractEntityRegistry.entity_pathc                 C   s   t | jjS r   )r   r   r   r<   r!   r   r   r   r<     s   zAbstractEntityRegistry.mapperc                 C   s   dS )NTr   r!   r   r   r   __bool__  s   zAbstractEntityRegistry.__bool__c                 C   s<   t |ttfr| j| S |tjv rt| tj| S t| |S r   )r\   r   r   r   rL   rM   ru   r   r   r   r   r   r     s
   


z"AbstractEntityRegistry.__getitem__N)r{   r}   r~   r   r   r   r   r   r<   r   __nonzero__r   r   r   r   r   r     s    (

r   c                   @   s   e Zd ZdZdZdS )rh   T)r'   r   rB   r   r   r$   N)r{   r}   r~   r   r   r   r   r   r   rh     s    rh   c                   @   s    e Zd ZdZdd Zdd ZdS )rf   Tc                 C   s$   t |ttfr| j| S t| |S r   )r\   r   r   r   dictr   r   r   r   r   r     s   
z!CachingEntityRegistry.__getitem__c                 C   s   t | | | |< }|S r   )r   )r   r'   itemr   r   r   __missing__  s   z!CachingEntityRegistry.__missing__N)r{   r}   r~   r   r   r   r   r   r   r   rf     s    rf   ) r   	itertoolsr   logging r   rN   r   r   r   sqlr	   Zsql.traversalsr
   	getLoggerr{   r"   r   rt   rv   r   r   rg   ZInspectionAttrrD   rL   ru   r   r   rh   r   rf   r   r   r   r   <module>   s.   
 R#fC