U kf@e@sUdZddlmZddlmZddlmZddlZddlZddl m Z ddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlmZerddlm Z ddl!m"Z"ddl#m$Z$ddl%m&Z&ddlm'Z'ddlm(Z(ddl)m*Z*dd l+m,Z,dd!l-m.Z.dd"l-m/Z/d#d$d%d&d'Z0d#d(d%d)d*Z1ne2d'Z0e2d*Z1ee Z3e4Z5ee5d+d,fZ6ee6d-fZ7ed+Z8eed,e4fZ9e:e;Zd2e?d3<d4Z@Gd5d#d#eZAGd6d7d7eAZBGd8d9d9eBZCeCeA_DGd:d;d;ejEee4ZFGdd?d?eAZHGd@dAdAeBZIGdBdCdCeIZJGdDdEdEe e e fZKGdFdGdGeIZLerd#d(d%dHdIZMd#dJd%dKdLZNne2d*ZMe2dMZNdS)Nz@Path tracking utilities, representing mapper graph traversals. ) annotations)reduce)chainN)Any)cast)Dict)Iterator)List)Optional)overload)Sequence)Tuple) TYPE_CHECKING)Union)base)insp_is_mapper_property)exc)util)visitors) HasCacheKey)_InternalEntityType)StrategizedProperty)Mapper)RelationshipProperty) AliasedInsp)_CacheKeyTraversalType) BindParameter)anon_map) _LiteralStar) TypeGuard PathRegistryzTypeGuard[RootRegistry]pathreturncCsdSNr$r'r'L/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/path_registry.pyis_root0r*z!TypeGuard[AbstractEntityRegistry]cCsdSr&r'r(r'r'r) is_entity2r+r,_InternalEntityType[Any]StrategizedProperty[Any]._SerializedPathcCs t|Sr&)r" deserializer(r'r'r)_unreduce_pathPsr1*r _WILDCARD_TOKENZ _sa_defaultc@seZdZUdZdZdZdZdZdZdZ de d<de d<de d<d e d <d e d <de j j fgZd e d<dddddZdddddZeddddZdddddZddddd d!d"Zddddd d#d$Zdgddd&dd d'd(Zddd)d*Zddd+d,Zed-d.d/d0d1Zedd2d/d3d1Zed4dd/d5d1Zedd6d/d7d1Zed8d9d/d:d1Zd;dd?Zd@ddAdBZdCddDdEdFZddddGdHdIZdddJdKZe ddLdMdNdOZ!e dLddMdPdQZ"dLddRdSZ#e dLddMdTdUZ$ee dCdVdDdWdXZ%ee dYdZdDd[dXZ%e dd6dDd\dXZ%e ddd]d^d_Z&dddd`daZ'dbddcddZ(dbddedfZ)d%S)hr"aRepresent query load paths and registry functions. Basically represents structures like: (, "orders", , "items", ) 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'Fbool is_unnatural_PathRepresentationr$ natural_pathzOptional[PathRegistry]parent RootRegistryrootr_cache_key_traversalr)otherr%cCsDz|dk o|j|jkWStk r>tdt|YdSXdS)N1Comparison of PathRegistry to %r is not supportedFr$_path_for_compareAttributeErrorrwarntypeselfr<r'r'r)__eq__szPathRegistry.__eq__cCsDz|dkp|j|jkWStk r>tdt|YdSXdS)Nr=Tr>rCr'r'r)__ne__szPathRegistry.__ne__Optional[_PathRepresentation]r%cCs|jSr&r(rDr'r'r)r?szPathRegistry._path_for_compareintr-)indexr%cCs |j|Sr&r()rDrKr'r'r) odd_elementszPathRegistry.odd_elementzDict[Any, Any]None) attributeskeyvaluer%cCs"td|||||||jf<dS)Nzset '%s' on path '%s' to '%s')logdebugr7rDrNrOrPr'r'r)setszPathRegistry.setcCs&td||||||jf|dS)Nz$setdefault '%s' on path '%s' to '%s')rQrR setdefaultr7rSr'r'r)rUszPathRegistry.setdefaultNz Optional[Any]cCs"||jf}||kr||S|SdSr&)r7rSr'r'r)gets zPathRegistry.getcCs t|jSr&lenr$rIr'r'r)__len__szPathRegistry.__len__cCst|Sr&)idrIr'r'r)__hash__szPathRegistry.__hash__ _StrPathToken TokenRegistryentityr%cCsdSr&r'rDr_r'r'r) __getitem__szPathRegistry.__getitem___PathElementTypecCsdSr&r'r`r'r'r)rasslicecCsdSr&r'r`r'r'r)rasAbstractEntityRegistrycCsdSr&r'r`r'r'r)rasr. PropRegistrycCsdSr&r'r`r'r'r)raszTUnion[_StrPathToken, int, slice, _InternalEntityType[Any], StrategizedProperty[Any]]zaUnion[TokenRegistry, _PathElementType, _PathRepresentation, PropRegistry, AbstractEntityRegistry]cCs tdSr&)NotImplementedErrorr`r'r'r)rascCs t|jSr&rWrIr'r'r)lengthszPathRegistry.lengthzOIterator[Tuple[_InternalEntityType[Any], Union[str, StrategizedProperty[Any]]]]ccsFtt|j}tt|}tdt|dD]}||||dfVq&dS)Nrrr)r_OddPathRepresentationr$_EvenPathRepresentationrangerX)rDZodd_pathZ even_pathir'r'r)pairss  zPathRegistry.pairs Mapper[Any])mapperr%csFtt|jfddtdtdDD]}|j|r*dSq*dS)Ncsg|] }|qSr'r'.0rk_m_pathr'r) sz0PathRegistry.contains_mapper..rrTF)rrhr$rjrXrnisa)rDrnZ path_mapperr'rqr)contains_mappers  " zPathRegistry.contains_mapper)rNrOr%cCs||jf|kSr&r()rDrNrOr'r'r)containsszPathRegistry.containscCst|ffSr&)r1 serializerIr'r'r) __reduce__szPathRegistry.__reduce__r/r#c svtt|tt|tttddfddtdtdDDtddfddtd tdDDd S) Ncss(|] }|js|jr|jnt|VqdSr&) is_mapperis_aliased_classclass_str)rpmr'r'r) sz/PathRegistry._serialize_path..csg|] }|qSr'r'rorqr'r)rssz0PathRegistry._serialize_path..rrcss$|]}t|r|jnt|VqdSr&)rrOr|)rppr'r'r)r~scsg|] }|qSr'r'ro)_p_pathr'r)rssrr&)rrhrilistziptuplerjrX)clsr$r')rrrr)_serialize_paths  zPathRegistry._serialize_pathcs^dddddddddddttfdd |D}|rZ|d dkrZ|d d }|S) Nr)mclsr%cSs"|tjkrtj|ddStj|SNT) configure) PathToken_internorm_base_inspect_mapped_class)rr'r'r)_deserialize_mapper_tokenszAPathRegistry._deserialize_path.._deserialize_mapper_token)rrOr%cSsH|dkr dS|tjkr tj|Stj|dd}|dk s:t|j|SdSr)rrrrAssertionErrorattrs)rrOmpr'r'r)_deserialize_key_tokens   z>PathRegistry._deserialize_path.._deserialize_key_tokencs"g|]\}}|||fqSr'r')rprrOrrr'r)rs%sz2PathRegistry._deserialize_path..r)rrrr$rr'rr)_deserialize_paths     zPathRegistry._deserialize_pathcCs|j}||Sr&)r$r)rDr$r'r'r)rw2szPathRegistry.serializecCs |dk s t||}||Sr&)rrcoercerr'r'r)r06s  zPathRegistry.deserializeCachingEntityRegistrycCsdSr&r'rrnr'r'r) per_mapper<szPathRegistry.per_mapperAliasedInsp[Any]SlotsEntityRegistrycCsdSr&r'rr'r'r)r@scCs"|jrt|j|St|j|SdSr&)ryrr:rrr'r'r)rDs )rawr%cCs dddddd}t|||jS)Nr"rbprevnext_r%cSs||Sr&r'rrr'r'r)_redOsz!PathRegistry.coerce.._red)rr:)rrrr'r'r)rMszPathRegistry.coercecCs dddddd}t||j|S)Nr"rbrcSs||Sr&r'rr'r'r)rVsz"PathRegistry.__add__.._red)rr$)rDr<rr'r'r)__add__UszPathRegistry.__add__r|cCsdddd|jDdS)Nz ORM Path[z -> css|]}t|VqdSr&r|)rpelemr'r'r)r~\sz'PathRegistry.__str__..])joinr$rIr'r'r)__str__[szPathRegistry.__str__cCs|jjd|jdS)N()) __class____name__r$rIr'r'r)__repr__^szPathRegistry.__repr__)N)*r __module__ __qualname____doc__ __slots__is_tokenr* has_entity is_propertyr,__annotations__rZExtendedInternalTraversalZdp_has_cache_key_listr;rErFpropertyr?rLrTrUrVrYr[r rargrlrurvrx classmethodrrrwr0rrrrrr'r'r'r)r"Xs|      #c@s2eZdZUdZded<ded<ddddd Zd S) CreatesTokenr'r4rzr*r\r])tokenr%cCsJ|dtrt||S|dtr6t|j|Std|dS)N:zinvalid token: )endswithr3r]_DEFAULT_TOKENr:r ArgumentError)rDrr'r'r)rhs   zCreatesToken.tokenN)rrrrrrr'r'r'r)rbs rc@sVeZdZdZdZdZdZZdZdZ dZ dZ ddddd Z dd d d Z esRe Zd S)r9zXRoot registry, defers to mappers so that paths are maintained per-root-mapper. r'TFrz,Union[TokenRegistry, AbstractEntityRegistry]r^cCs\|tjkr,trt|tstt|tj|Sz|jWStk rVt d|YnXdS)Nz/invalid argument for RootRegistry.__getitem__: ) rrr isinstancer\rr]Z_path_registryr@ IndexErrorr`r'r'r)_getitems zRootRegistry._getitemrHcCs|Sr&r'rIr'r'r)_truncate_recursivesz RootRegistry._truncate_recursiveN)rrrrr inherit_cacher$r7rrzr*r5rrrrar'r'r'r)r9qsr9c@sVeZdZUdZiZded<dddddd Zed d d d Ze dddddZ dS)rzcacheable string tokenzDict[str, PathToken]rrzList[BindParameter[Any]]zTuple[Any, ...])r bindparamsr%cCs t|fSr&r)rDrrr'r'r)_gen_cache_keyszPathToken._gen_cache_keyrGrHcCsdSr&r'rIr'r'r)r?szPathToken._path_for_comparer|)strvaluer%cCs.||jkr|j|St||j|<}|SdSr&)rr)rrresultr'r'r)interns  zPathToken.internN) rrrrrrrrr?rrr'r'r'r)rs  rc@sreZdZUdZdZded<ded<ddddd Zd ZdZd d d dZ dd ddZ dddddZ e sne Z dS)r])rr8r$r7Tr\rrr8)r8rcCs6t|}||_||_|j|f|_|j|f|_dSr&)rrrr8r$r7)rDr8rr'r'r)__init__s  zTokenRegistry.__init__FzIterator[PathRegistry]rHccs|j}t|r|VdStr*t|ts*t|jsV|jD]}t |j||j Vq:nJ|jrt d|j j r|Vt d|j jD]}t |j||j Vq~n|VdSNr)r8r*rrrdrrzrniterate_to_rootr]rrr__is_with_polymorphic_with_polymorphic_entitiesrDr8Zmp_entZentr'r'r)generate_for_superclassess. z'TokenRegistry.generate_for_superclasseszIterator[_PathRepresentation]ccs|j}t|r|jVdStr,t|ts,t|jD]}t |j||j jVq6|j rt d|j jr|jVt d|j jD]}t |j||j jVq|n|jVdSr)r8r*r7rrrdrrnrr]rrzrr_rrrr'r'r)"_generate_natural_for_superclassess. z0TokenRegistry._generate_natural_for_superclassesrr^c Cs@z |j|WStk r:}zt||W5d}~XYnXdSr&)r$ TypeErrorr)rDr_errr'r'r)rs zTokenRegistry._getitemN)rrrrrrrrrrrrrrar'r'r'r)r]s r]c@szeZdZUdZdZdZded<ded<ded<d dd d d Zdd ddZe d d ddZ dddddZ e sve Z dS)re) propr8r$r7rr_rn_wildcard_path_loader_key_default_path_loader_key _loader_keyr5Tr.rzOptional[Mapper[Any]]rnz"Optional[_InternalEntityType[Any]]r_rd)r8rcCsdtd|d}|}|jjp$t|jj|_|jr4|jrF|j|j}}n|jr|jr|j|jkr|d |j}|j|}|jr|j|j}d|_q|}n2|jr|jr|j|jk r|j |jr|j|j}||_ ||_|j |f|_ |j |f|_ |j|_|jr*trt|tst|j|_|j|_n d|_d|_d|j |j jf|_|j j|_d|j f|_dS)Nr-rTloader)rr8r5r4rnZinheritsrzZ_use_mapper_pathZwith_polymorphic_mappersZ_entity_for_mapperrtrr$r7Z_links_to_entityrZ_is_relationshiprrrrr_Z_wildcard_tokenrrr)rDr8rZinspZnatural_parentZsubclass_entityr'r'r)rs^          zPropRegistry.__init__rHcCs\d}tt|jddD]\}}||jkr|}q|dkr>|S||jd|d SdS)Nrrr) enumeratereversedr$rr)rDZearliestrkrr'r'r)rvs z PropRegistry._truncate_recursivecCs|jdk st||jSr&)r_rrIr'r'r) entity_pathszPropRegistry.entity_pathz+Union[int, slice, _InternalEntityType[Any]]zDUnion[AbstractEntityRegistry, _PathElementType, _PathRepresentation]r^cCs&t|ttfr|j|St||SdSr&)rrJrcr$rr`r'r'r)rs zPropRegistry._getitemN)rrrrrrrrrrrrrrar'r'r'r)res  Z rec@seZdZUdZdZdZded<ded<ded<ded <ddd d d Zdd ddZe dd ddZ e dd ddZ e dd ddZ dd ddZ dddddZeseZdS) rd)rOr8rzr$r_r7T!Union[RootRegistry, PropRegistry]r8r-rOr_r4rzr8r_cCs||_||_|j|_||_|j|f|_|jrx|js:|jrx|j|jdjr`|j|jf|_q|j|jdjf|_n|j|_dS)Nr) rOr8rzr_r$r5rnrtr7rDr8r_r'r'r)rs zAbstractEntityRegistry.__init__rHcCs|j|jSr&)r8rr_rIr'r'r)rsz*AbstractEntityRegistry._truncate_recursivecCs |dS)Nr)rLrIr'r'r) root_entitysz"AbstractEntityRegistry.root_entityr"cCs|Sr&r'rIr'r'r)rsz"AbstractEntityRegistry.entity_pathrmcCs|jjSr&)r_rnrIr'r'r)rnszAbstractEntityRegistry.mappercCsdS)NTr'rIr'r'r)__bool__szAbstractEntityRegistry.__bool__rz:Union[_PathElementType, _PathRepresentation, PathRegistry]r^cCs@t|ttfr|j|S|tjkr2t|tj|St||SdSr&)rrJrcr$rrr]rer`r'r'r)rs   zAbstractEntityRegistry._getitemN)rrrrrr,rrrrrrrnrrrrar'r'r'r)rds&  . rdc@seZdZdZdS)rTN)rrrrr'r'r'r)rsrc@s*eZdZddddZddddd Zd S) _ERDictrregistrycCs ||_dSr&r)rDrr'r'r)rsz_ERDict.__init__rre)rOr%cCst|j|||<}|Sr&)rer)rDrOitemr'r'r) __missing__sz_ERDict.__missing__N)rrrrrr'r'r'r)rsrcsVeZdZdZdZdddfdd Zdddd d d Zddd d dZesNeZ Z S)r)_cacheTrr-rcst||t||_dSr&)superrrrrrr'r)r szCachingEntityRegistry.__init__r)rOdefaultr%cCs|j||Sr&)rpop)rDrOrr'r'r)rszCachingEntityRegistry.popr^cCs:t|ttfr|j|St|tr,t||S|j|SdSr&)rrJrcr$rr]rr`r'r'r)rs    zCachingEntityRegistry._getitem) rrrrrrrrrra __classcell__r'r'rr)rsrcCsdSr&r'r(r'r'r)path_is_entity#srzTypeGuard[PropRegistry]cCsdSr&r'r(r'r'r)path_is_property'r+rr)Or __future__r functoolsr itertoolsrloggingoperatortypingrrrrr r r r r rrrrZ_typingrrrsqlrZ sql.cache_keyrrZ interfacesrrnrZ relationshipsrrrZ sql.elementsrZ sql.visitorsrZ util.typingr r!r*r, attrgetterr/r|r\rbr6rhri getLoggerrrQr1r3rrr"rr9r:ZInspectionAttrrr]rerdrrrrrr'r'r'r)s                                    &V `