a
    xˆc@  ã                   @   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   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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   sB   z|d uo| j |jkW S  ty<   t dt|ƒ ¡ Y dS 0 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   sB   z|d u p| j |jkW S  ty<   t dt|ƒ ¡ Y dS 0 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}||v 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   ó    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|v 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   )r7   Ú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   Œ   r6   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 u rH|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   s8   |d u rd S |t jv r t j| S tj| dd�j| S d S rF   )rH   rI   rJ   rK   Úattrs)rL   r%   r   r   r   Ú_deserialize_key_token¡   s    

ÿþz>PathRegistry._deserialize_path.<locals>._deserialize_key_tokenc                    s"   g | ]\}}ˆ|ƒˆ ||ƒf‘qS r   r   )r4   rL   r%   ©rO   rM   r   r   r5   ­   s   ýþz2PathRegistry._deserialize_path.<locals>.<listcomp>éÿÿÿÿr   )Útupler   ©rD   r   Úpr   rP   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   )rE   ©r4   r%   r   r&   ©rD   r   r   r5   ¼   s   
ÿz7PathRegistry.serialize_context_dict.<locals>.<listcomp>c                    s.   g | ]&\}}t |tƒr|d  ˆ v r||f‘qS )r   )Ú
isinstancerR   )r4   ÚkÚv)Útokensr   r   r5   ¾   s   þ)Úitems)rD   Zdict_r[   r   )rD   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   )rR   rU   rV   rW   r   r   Ú	<genexpr>Ç   s   
ÿz8PathRegistry.deserialize_context_dict.<locals>.<genexpr>)r   ÚOrderedDict)rD   Z
serializedr   rW   r   Údeserialize_context_dictÅ   s    þz%PathRegistry.deserialize_context_dictc                 C   s   | j }|  |¡S r   )r   rE   )r   r   r   r   r   r<   Ì   s    zPathRegistry.serializec                 C   s    |d u rd S |   |¡}|  |¡S r   )rU   ÚcoercerS   r   r   r   r   Ð   s    
zPathRegistry.deserializec                 C   s"   |j rt| j|ƒS t| j|ƒS d S r   )r7   ÚCachingEntityRegistryÚrootÚSlotsEntityRegistry)rD   r9   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>à   r6   z%PathRegistry.coerce.<locals>.<lambda>)r   Úreducerc   )rD   Úrawr   r   r   ra   Þ   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_TOKENrc   r   ÚArgumentError)r   Útokenr   r   r   rr   â   s
    
zPathRegistry.tokenc                 C   s   t  dd„ |j| ¡S )Nc                 S   s   | | S r   r   rf   r   r   r   ri   ë   r6   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   r:   r;   r=   ÚclassmethodrE   rU   r]   r`   r<   r   re   ra   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v rt j| S |jS d S r   )rH   rI   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 )	rH   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 v r| j | S t|ƒ | j |< }|S d S r   )rI   rH   )rD   ZstrvalueÚresultr   r   r   Úintern  s    

zPathToken.internN)
ru   rw   rx   ry   rI   r†   r}   r   r~   rˆ   r   r   r   r   rH     s   
rH   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   )rH   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|   r9   Ziterate_to_rootro   rr   r�   Z_is_with_polymorphicZ_with_polymorphic_entities)r   Ú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v r‚|d  |j¡}|j| }|jr||j|j }d| _q´|}n2|jr´|jr´|j|jur´|j 	|j¡r´|j|j }|| _
|| _|j|f | _|j|f | _d|j| j
j f| _| j
j| _d| jf| _d S )NrQ   TÚloader)r   Úinspectr>   Z_use_mapper_pathr‰   Zwith_polymorphic_mappersZ_entity_for_mapperr9   Úis_unnaturalr8   Ú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^   �  r6   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’   r9   r   r   r   r   r9   —  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   )rX   ÚintÚslicer   rd   r€   r   r   r   r‚   Ÿ  s    
zPropRegistry.__getitem__N)ru   rw   rx   r‘   rƒ   rŠ   r•   r   Zmemoized_propertyr„   r�   r}   r9   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 )NrQ   )	r%   r‰   r>   r�   r   r‘   r9   r8   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�   r9   r   r   r   r   r9   ×  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 r2t| tj| ƒS t| |ƒS d S r   )rX   r—   r˜   r   rH   rI   ro   rŽ   r€   r   r   r   r‚   à  s
    

z"AbstractEntityRegistry.__getitem__N)ru   rw   rx   rz   r„   rŠ   r}   r–   r9   rš   Ú__nonzero__r‚   r   r   r   r   r™   ¦  s   (

r™   c                   @   s   e Zd ZdZdZdS )rd   T)r%   r‰   r>   r�   r   r"   N)ru   rw   rx   rƒ   rz   r   r   r   r   rd   é  s   rd   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )rb   Tc                 C   s(   t |ttfƒr| j| S t | |¡S d S r   )rX   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   rb   ø  s   rb   ) ry   Ú	itertoolsr   ÚloggingÚ r   rJ   r   r   r   Zsqlr	   Zsql.traversalsr
   Ú	getLoggerru   r    r   rn   rp   r   r   rc   ZInspectionAttrr@   rH   ro   rŽ   r™   rd   rœ   rb   r   r   r   r   Ú<module>   s,   
 R#fC