U
    ÌZj@  ã                   @   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   ú_/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/sqlalchemy/orm/path_registry.pyÚ_unreduce_path   s    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   sD   z|d k	o| j |jkW S  tk
r>   t dt|ƒ ¡ Y dS X d S )Nú1Comparison of PathRegistry to %r is not supportedF©r   Ú_path_for_compareÚAttributeErrorr   ÚwarnÚtype©ÚselfÚotherr   r   r   Ú__eq__C   s    ÿÿzPathRegistry.__eq__c                 C   sD   z|d kp| j |jkW S  tk
r>   t dt|ƒ ¡ Y dS X d S )Nr   Tr   r   r   r   r   Ú__ne__M   s    ÿÿ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}||kr|| S |S d S r   )r"   r#   r   r   r   Úgetc   s    
zPathRegistry.getc                 C   s
   t | jƒS r   ©Úlenr   r   r   r   r   Ú__len__j   s    zPathRegistry.__len__c                 C   s   t | ƒS r   )Úidr   r   r   r   Ú__hash__m   s    zPathRegistry.__hash__c                 C   s
   t | jƒS r   r*   r   r   r   r   Úlengthp   s    zPathRegistry.lengthc                 c   s6   | 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   ©Ú.0r1   r   r   r   Ú
<listcomp>z   s     z0PathRegistry.contains_mapper.<locals>.<listcomp>r   r   TF)r0   r+   r   Ú	is_mapperÚisa)r   ÚmapperZpath_mapperr   r   r   Úcontains_mappery   s    $zPathRegistry.contains_mapperc                 C   s   || j f|kS r   r   )r   r$   r%   r   r   r   Úcontains€   s    zPathRegistry.containsc                 C   s   t |  ¡ ffS r   )r   Ú	serializer   r   r   r   Ú
__reduce__ƒ   s    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   )r6   Úis_aliased_classÚclass_Ústr)r4   Úmr   r   r   r5   Š   s   ÿz0PathRegistry._serialize_path.<locals>.<listcomp>c                    s   g | ]}ˆ | ‘qS r   r   r3   r   r   r   r5   Œ   s     r   r   c                    s,   g | ]$}ˆ | j rˆ | jn
tˆ | ƒ‘qS r   )Zis_propertyr%   r?   r3   r   r   r   r5   Ž   s   ÿr   )ÚlistÚzipr0   r+   )Úclsr   r   r   r   Ú_serialize_path†   s    þ
þüûÿzPathRegistry._serialize_pathc                    sL   dd„ ‰dd„ ‰ t t‡ ‡fdd„|D ƒŽ ƒ}|rH|d d krH|dd… }|S )	Nc                 S   s"   | t jk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   s8   |d krd S |t jkr t j| S tj| dd�j| S d S rE   )rG   rH   rI   rJ   Úattrs)rK   r%   r   r   r   Ú_deserialize_key_token¡   s    

 ÿþz>PathRegistry._deserialize_path.<locals>._deserialize_key_tokenc                    s"   g | ]\}}ˆ|ƒˆ ||ƒf‘qS r   r   )r4   rK   r%   ©rN   rL   r   r   r5   ­   s   ýþz2PathRegistry._deserialize_path.<locals>.<listcomp>éÿÿÿÿr   )Útupler   ©rC   r   Úpr   rO   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   )rD   ©r4   r%   r   r&   ©rC   r   r   r5   ¼   s   
ÿz7PathRegistry.serialize_context_dict.<locals>.<listcomp>c                    s.   g | ]&\}}t |tƒr|d  ˆ kr||f‘qS )r   )Ú
isinstancerQ   )r4   ÚkÚv)Útokensr   r   r5   ¾   s   
 þ)Úitems)rC   Zdict_rZ   r   )rC   rZ   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   )rQ   rT   rU   rV   r   r   Ú	<genexpr>Ç   s   
ÿz8PathRegistry.deserialize_context_dict.<locals>.<genexpr>)r   ÚOrderedDict)rC   Z
serializedr   rV   r   Údeserialize_context_dictÅ   s    þz%PathRegistry.deserialize_context_dictc                 C   s   | j }|  |¡S r   )r   rD   )r   r   r   r   r   r;   Ì   s    zPathRegistry.serializec                 C   s    |d krd S |   |¡}|  |¡S r   )rT   ÚcoercerR   r   r   r   r   Ð   s    
zPathRegistry.deserializec                 C   s"   |j rt| j|ƒS t| j|ƒS d S r   )r6   ÚCachingEntityRegistryÚrootÚSlotsEntityRegistry)rC   r8   r   r   r   Ú
per_mapper×   s    zPathRegistry.per_mapperc                 C   s   t  dd„ || j¡S )Nc                 S   s   | | S r   r   ©ÚprevÚnextr   r   r   Ú<lambda>à   ó    z%PathRegistry.coerce.<locals>.<lambda>)r   Úreducerb   )rC   Úrawr   r   r   r`   Þ   s    zPathRegistry.coercec                 C   sD   |  dt ¡rt| |ƒS |  dt ¡r2t| j|ƒS t d| ¡‚d S )Nú:zinvalid token: %s)ÚendswithÚ_WILDCARD_TOKENÚTokenRegistryÚ_DEFAULT_TOKENrb   r   ÚArgumentError)r   Útokenr   r   r   rr   â   s
    
zPathRegistry.tokenc                 C   s   t  dd„ |j| ¡S )Nc                 S   s   | | S r   r   re   r   r   r   rh   ë   ri   z&PathRegistry.__add__.<locals>.<lambda>)r   rj   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__)N)%ru   Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__Úis_tokenÚis_rootr	   ZExtendedInternalTraversalZdp_has_cache_key_listZ_cache_key_traversalr   r   Úpropertyr   r'   r(   r)   r,   r.   r/   r2   r9   r:   r<   ÚclassmethodrD   rT   r\   r_   r;   r   rd   r`   rr   rs   rv   r   r   r   r   r       sN   
ÿ






#





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krt j| S |jS d S r   )rG   rH   Z_path_registry©r   Úentityr   r   r   Ú__getitem__þ   s    

zRootRegistry.__getitem__N)ru   rw   rx   ry   Úinherit_cacher   r"   Ú
has_entityr=   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 )	rG   zcacheable string tokenc                 C   s
   t | ƒfS r   ©r?   )r   Zanon_mapZ
bindparamsr   r   r   Ú_gen_cache_key  s    zPathToken._gen_cache_keyc                 C   s   d S r   r   r   r   r   r   r     s    zPathToken._path_for_comparec                 C   s.   || j kr| j | S t|ƒ | j |< }|S d S r   )rH   rG   )rC   ZstrvalueÚresultr   r   r   Úintern  s    

zPathToken.internN)
ru   rw   rx   ry   rH   r†   r}   r   r~   rˆ   r   r   r   r   rG     s   
rG   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 )ro   )rr   Úparentr   r"   Tc                 C   s6   t  |¡}|| _|| _|j|f | _|j|f | _d S r   )rG   rˆ   rr   r‰   r   r"   )r   r‰   rr   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nF| j jrz| j jjrz| V  | j jj	D ]}t| j j | | jƒV  q\n| V  d S r   )
r‰   r=   r|   r8   Ziterate_to_rootro   rr   r�   Z_is_with_polymorphicZ_with_polymorphic_entities)r   Zentr   r   r   Úgenerate_for_superclasses.  s    ÿþz'TokenRegistry.generate_for_superclassesc                 C   s
   t ƒ ‚d S r   )ÚNotImplementedErrorr€   r   r   r   r‚   <  s    zTokenRegistry.__getitem__N)
ru   rw   rx   rz   rƒ   rŠ   r„   r{   r‹   r‚   r   r   r   r   ro     s   ro   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r0|j|j  }}n„|jr‚|jr‚|j|jkr‚|d  |j¡}|j| }|jr||j|j }d| _q´|}n2|jr´|jr´|j|jk	r´|j 	|j¡r´|j|j }|| _
|| _|j|f | _|j|f | _d|j| j
j f| _| j
j| _d| jf| _d S )NrP   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Š   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…   )r4   Úelemr   r   r   r]   �  s     z'PropRegistry.__str__.<locals>.<genexpr>)Újoinr   r   r   r   r   Ú__str__Œ  s    zPropRegistry.__str__c                 C   s   | j jS r   )r‘   Z_links_to_entityr   r   r   r   r„   �  s    zPropRegistry.has_entityc                 C   s   | j jS r   )r‘   r�   r   r   r   r   r�   “  s    zPropRegistry.entityc                 C   s   | j jS r   )r‘   r8   r   r   r   r   r8   —  s    zPropRegistry.mapperc                 C   s
   | | j  S r   )r�   r   r   r   r   Úentity_path›  s    zPropRegistry.entity_pathc                 C   s&   t |ttfƒr| j| S t| |ƒS d S r   )rW   ÚintÚslicer   rc   r€   r   r   r   r‚   Ÿ  s    
zPropRegistry.__getitem__N)ru   rw   rx   r�   rƒ   rŠ   r”   r   Zmemoized_propertyr„   r�   r}   r8   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rx| js:|jrx|j |jd j¡r`|j|jf | _q€|j|jd jf | _n| j| _d S )NrP   )	r%   r‰   r=   r�   r   r�   r8   r7   r"   )r   r‰   r�   r   r   r   rŠ   «  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�   r�   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   s@   t |ttfƒr| j| S |tjkr2t| tj| ƒS t| |ƒS d S r   )rW   r–   r—   r   rG   rH   ro   r�   r€   r   r   r   r‚   à  s
    

z"AbstractEntityRegistry.__getitem__N)ru   rw   rx   rz   r„   rŠ   r}   r•   r8   r™   Ú__nonzero__r‚   r   r   r   r   r˜   ¦  s   (

r˜   c                   @   s   e Zd ZdZdZdS )rc   T)r%   r‰   r=   r�   r   r"   N)ru   rw   rx   rƒ   rz   r   r   r   r   rc   é  s   rc   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ra   Tc                 C   s(   t |ttfƒr| j| S t | |¡S d S r   )rW   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)ru   rw   rx   rƒ   r‚   r�   r   r   r   r   ra   ø  s   ra   ) ry   Ú	itertoolsr   ÚloggingÚ r   rI   r   r   r   Úsqlr	   Zsql.traversalsr
   Ú	getLoggerru   r    r   rn   rp   r   r   rb   ZInspectionAttrr?   rG   ro   r�   r˜   rc   r›   ra   r   r   r   r   Ú<module>   s,   
 R#fC