o
    [h5                     @   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	 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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   )class_mapper   )exc)
inspection)utilc                 C   s
   t | S N)PathRegistrydeserializepath r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/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d Zdd Zdd	 Z	d
d Z
d1d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d+d, Zd-d. Zd/d0 ZdS )2r
   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   Fc                 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   AttributeErrorr   warntypeselfotherr   r   r   __eq__>      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__H   r   zPathRegistry.__ne__c                 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   setR   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&   V   s   zPathRegistry.setdefaultNc                 C   s   || j f}||v r|| S |S r	   )r    r!   r   r   r   getZ   s   
zPathRegistry.getc                 C   
   t | jS r	   lenr   r   r   r   r   __len__a   r   zPathRegistry.__len__c                 C   r(   r	   r)   r+   r   r   r   lengthd      
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   pairsh   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   .0r0   r+   r   r   
<listcomp>n       z0PathRegistry.contains_mapper.<locals>.<listcomp>r   r   TF)r/   r*   r   	is_mapperisa)r   mapperZpath_mapperr   r+   r   contains_mapperm   s
   $zPathRegistry.contains_mapperc                 C   s   || j f|v S r	   r   )r   r"   r#   r   r   r   containst      zPathRegistry.containsc                 C   s   t |  ffS r	   )r   	serializer+   r   r   r   
__reduce__w   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 qS r   )class_)r3   mr   r   r   r4   ~   s    z0PathRegistry._serialize_path.<locals>.<listcomp>c                    s   g | ]} | qS r   r   r2   r   r   r   r4   ~   s    r   r   c                    s   g | ]} | j qS r   )r#   r2   r   r   r   r4      r5   r   )listzipr/   r*   )clsr   r   r   r   _serialize_pathz   s   &"zPathRegistry._serialize_pathc                 C   s6   t tdd |D  }|r|d d u r|dd }|S )Nc                 S   s2   g | ]\}}t ||d urt |j| nd fqS r	   )r   attrs)r3   mclsr#   r   r   r   r4      s    z2PathRegistry._deserialize_path.<locals>.<listcomp>r   )tupler   rB   r   pr   r   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   )rC   r3   r#   r   r$   rB   r   r   r4      s    
z7PathRegistry.serialize_context_dict.<locals>.<listcomp>c                    s.   g | ]\}}t |tr|d   v r||fqS )r   )
isinstancerG   )r3   kv)tokensr   r   r4      s    )items)rB   Zdict_rP   r   )rB   rP   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	   )rG   rJ   rK   rL   r   r   	<genexpr>   s
    

z8PathRegistry.deserialize_context_dict.<locals>.<genexpr>)r   OrderedDict)rB   Z
serializedr   rL   r   deserialize_context_dict   s   z%PathRegistry.deserialize_context_dictc                 C   s   | j }| |S r	   )r   rC   )r   r   r   r   r   r<      s   
zPathRegistry.serializec                 C   s    |d u rd S |  |}| |S r	   )rJ   coercerH   r   r   r   r      s   

zPathRegistry.deserializec                 C   s   |j r	t| j|S t| j|S r	   )r6   CachingEntityRegistryrootSlotsEntityRegistry)rB   r8   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   reducerX   )rB   rawr   r   r   rV      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_TOKENrX   r   ArgumentError)r   tokenr   r   r   ri      s
   
zPathRegistry.tokenc                 C   s   t dd |j| S )Nc                 S   r[   r	   r   r\   r   r   r   r_      r`   z&PathRegistry.__add__.<locals>.<lambda>)r   ra   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	   )rl   
__module____qualname____doc__	__slots__is_tokenis_rootr   r   r%   r&   r'   r,   propertyr-   r1   r9   r:   r=   classmethodrC   rJ   rR   rU   r<   r   rZ   rV   ri   rj   rm   r   r   r   r   r
      sF    











r
   c                   @   s,   e 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.

    r   FTc                 C   s   |j S r	   )Z_path_registryr   entityr   r   r   __getitem__      zRootRegistry.__getitem__N)
rl   rn   ro   rp   r   r    
has_entityis_aliased_classrs   ry   r   r   r   r   rv      s    rv   c                   @   s0   e Zd ZdZdd ZdZdZdd Zdd	 Zd
S )rf   ri   parentr   r    c                 C   s,   || _ || _|j|f | _|j|f | _d S r	   r}   )r   r~   ri   r   r   r   __init__   s   zTokenRegistry.__init__FTc                 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~   r|   rs   r8   Ziterate_to_rootrf   ri   rx   Z_is_with_polymorphicZ_with_polymorphic_entities)r   Zentr   r   r   generate_for_superclasses   s   
z'TokenRegistry.generate_for_superclassesc                 C   s   t  r	   )NotImplementedErrorrw   r   r   r   ry      rz   zTokenRegistry.__getitem__N)	rl   rn   ro   rq   r   r{   rr   r   ry   r   r   r   r   rf      s    rf   c                   @   s\   e 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 )PropRegistryFc                 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 )NrF   Tloader)r   inspectr|   Z_use_mapper_pathr~   Zwith_polymorphic_mappersZ_entity_for_mapperr8   is_unnaturalr7   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     sD   
 
zPropRegistry.__init__c                 C   s   d dd | jD S )Nz -> c                 s   s    | ]}t |V  qd S r	   )str)r3   elemr   r   r   rS   J  s    z'PropRegistry.__str__.<locals>.<genexpr>)joinr   r+   r   r   r   __str__I  s   zPropRegistry.__str__c                 C   s   t | jdS )Nr8   )hasattrr   r+   r   r   r   r{   L  s   zPropRegistry.has_entityc                 C   s   | j jS r	   )r   r8   r+   r   r   r   rx   P  s   zPropRegistry.entityc                 C   s   | j S r	   rx   r+   r   r   r   r8   T  s   zPropRegistry.mapperc                 C   s
   | | j  S r	   r   r+   r   r   r   entity_pathX  r.   zPropRegistry.entity_pathc                 C   "   t |ttfr| j| S t| |S r	   )rM   intslicer   rY   rw   r   r   r   ry   \     

zPropRegistry.__getitem__N)rl   rn   ro   r   r   r   r   Zmemoized_propertyr{   rx   rt   r8   r   ry   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 )NrF   )	r#   r~   r|   rx   r   r   r8   r7   r    )r   r~   rx   r   r   r   r   h  s   
zAbstractEntityRegistry.__init__c                 C   s   | S r	   r   r+   r   r   r   r     s   z"AbstractEntityRegistry.entity_pathc                 C   s   t | jjS r	   )r   r   rx   r8   r+   r   r   r   r8     s   zAbstractEntityRegistry.mapperc                 C   s   dS )NTr   r+   r   r   r   __bool__  s   zAbstractEntityRegistry.__bool__c                 C   r   r	   )rM   r   r   r   r   rw   r   r   r   ry     r   z"AbstractEntityRegistry.__getitem__N)rl   rn   ro   rq   r{   r   rt   r   r8   r   __nonzero__ry   r   r   r   r   r   c  s     

r   c                   @   s   e Zd ZdZdS )rY   )r#   r~   r|   rx   r   r    N)rl   rn   ro   rq   r   r   r   r   rY     s    rY   c                   @   s   e Zd Zdd Zdd ZdS )rW   c                 C   s$   t |ttfr| j| S t| |S r	   )rM   r   r   r   dictry   rw   r   r   r   ry     s   
z!CachingEntityRegistry.__getitem__c                 C   s   t | | | |< }|S r	   )r   )r   r#   itemr   r   r   __missing__  s   z!CachingEntityRegistry.__missing__N)rl   rn   ro   ry   r   r   r   r   r   rW     s    rW   )rp   	itertoolsr   loggingbaser    r   r   r   	getLoggerrl   r   r   re   rg   objectr
   rv   rX   rf   r   r   rY   r   rW   r   r   r   r   <module>   s(   
 /e9