L i FUdZddlmZddlZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlmZddl"m#Z#dd l$m%Z%erdd!l&m'Z'dd"l"m(Z(dd#l"m)Z)ed$e%Z*e e+ee,d&ffZ-e.Z/d'e0d(< d>d)Z1 d?d*Z2 d@d+Z3Gd,d&Z4Gd-d.e4Z5Gd/d0e4Z6Gd1d2Z7Gd3d4Z8e!jre8d5Gd6d7Z:dAd8Z;Gd9d:Zy)CzRoutines to handle the string class registry used by declarative. This system allows specification of classes and expressions used in :func:`_orm.relationship` using strings. ) annotationsN)Any)Callable)cast)Dict) Generator)Iterable)List)Mapping)MutableMapping)NoReturn)Optional)Set)Tuple)Type) TYPE_CHECKING)TypeVar)Union) attributes) interfaces)SynonymProperty)ColumnProperty) class_mapper)exc) inspection)util)_get_table_key)CallableReference)RelationshipProperty)MetaData)Table_T)boundClsRegistryTokenzSet[ClsRegistryToken] _registriesc2||vr0||}t|ts t|td|g||<n|||< tt|d}|j j d}|rQ|jd}|j|}|D]}|j|} |j|||rPyy#t$rtddx|d<}YwxYw#t$r0}t|tstjd|d|d}~wwxYw)z^Add a class to the _decl_class_registry associated with the given declarative class. Type[Any]_sa_module_registryN.rname "-" matches both a class name and a module name) isinstance_MultipleClassMarkerr _ModuleMarkerKeyError __module__splitpop get_module add_classAttributeErrorrInvalidRequestError) classnameclsdecl_class_registryexisting root_moduletokenstokenmoduleaes `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/orm/clsregistry.pyr6r6@sO''&y1($89-Ad;12.  **-I&  ./DE ^^ ! !# &F  1 ''. .E&&u-F .    Y ,   / 6 12[ . fm4--YK(33  s)B=&C=CC D&+DDc||vr*||}t|tr|j|n||= tt|d}|j jd}|rQ|jd}|j|}|D]}|j|} |j|||rPyy#t $rYywxYw#t$rt|tsnY1wxYw)Nr*r+r) r.r/ remove_itemrr0r1r2r3r4r5 remove_classr7)r9r:r;r<r=r>r?r@s rBrErEvs''&y1 h 4 5   %#I. ./DE ^^ ! !# &F  1 ''. .E&&u-F .     3 /   fm4 s#B2C2 B>=B>CCc||vry||}t|tr&|jD] }||s ytd|| S)atest if a key is empty of a certain object. used for unit tests against the registry to see if garbage collection is working. "test" is a callable that will be passed an object should return True if the given object is the one we were looking for. We can't pass the actual object itself b.c. this is for testing garbage collection; the caller will have to have removed references to the object itself. TFzunknown codepath)r.r/contentsNotImplementedError)keyr;testthing sub_things rB _key_is_emptyrMs\$ %%  $E%-. :II :&&89 9;ceZdZdZdZy)r&zAan object that can be in the registry._class_registry as a value.N)__name__r2 __qualname____doc__ __slots__rPrNrBr&r&s KIrNcdeZdZUdZdZded<ded< d ddZdd Zdd Zdd Z dd Z dd Z y)r/zRrefers to multiple classes of the same name within _decl_class_registry. ) on_removerG __weakref__zSet[weakref.ref[Type[Any]]]rGz/CallableReference[Optional[Callable[[], None]]]rVNc||_|Dchc]"}tj||j$c}|_t j |ycc}wN)rVweakrefref _remove_itemrGr'add)selfclassesrVitems rB__init__z_MultipleClassMarker.__init__sF #=D 59GKKd// 0    s'AcL|jtj|yrY)r\rZr[r^r:s rBrDz _MultipleClassMarker.remove_items '++c*+rNc(d|jDS)Nc3*K|] }| ywrYrP).0r[s rB z0_MultipleClassMarker.__iter__..s/#/srG)r^s rB__iter__z_MultipleClassMarker.__iter__s///rNct|jdkDr+tjddj ||gzzt |jd}|}| t ||S)NrzxMultiple classes found for path "%s" in the registry of this declarative base. Please use a fully module-qualified path.r+r)lenrGrr8joinlist NameError)r^pathrIr[r:s rB attempt_getz _MultipleClassMarker.attempt_getsu t}}  !))B88DC5L)+ t}}%a(C%C{n$JrNc|jj||js3tj||jr|jyyyrY)rGdiscardr'rV)r^r[s rBr\z!_MultipleClassMarker._remove_itemsD c"}}    %~~ rNct|jDcgc] }| c}Dchc]}| |j}}|j|vr0tjd|jd|j d|jj tj||jycc}wcc}w)Nz[This declarative base already contains a class with the same class name and module name as r+z2, and will be replaced in the string-lookup table.) rmrGr2rwarnrQr]rZr[r\)r^r`r[r:moduless rBadd_itemz_MultipleClassMarker.add_items *.dmm)<=#=  NN  ??g % II??DMM3  '++dD,=,=>?> s B:B?rY)r_zIterable[Type[Any]]rVzOptional[Callable[[], None]])r:r)returnNone)rwz*Generator[Optional[Type[Any]], None, None])ro List[str]rIstrrwr))r[zweakref.ref[Type[Any]]rwrx)r`r)rwrx) rQr2rRrSrT__annotations__rarDrirpr\rvrPrNrBr/r/sP 7I))>> 37 $ 0 ,0 !@rNr/c~eZdZUdZdZded<ded<ded<d ed <dd Zdd Zdd ZddZ ddZ ddZ ddZ ddZ y)r0z>Refers to a module name within _decl_class_registry. )parentnamerGmod_nsrorWOptional[_ModuleMarker]r}z5Dict[str, Union[_ModuleMarker, _MultipleClassMarker]]rG_ModNSrryroc||_||_i|_t||_|jr*|jj |jgz|_ng|_t j|yrY)r}r~rGrrror'r])r^r~r}s rBraz_ModuleMarker.__init__sX   Tl ;; ((DII;6DIDIrNc||jvSrYrhr^r~s rB __contains__z_ModuleMarker.__contains__st}}$$rNc |j|SrYrhrs rB __getitem__z_ModuleMarker.__getitem__s}}T""rNc|jj|d|jsG|j%|jj|jt j |yyrY)rGr4r}r\r~r'rrrs rBr\z_ModuleMarker._remove_item sP $%}}{{& ((3    %rNc8|jj|SrY)r __getattr__r^rIs rB resolve_attrz_ModuleMarker.resolve_attr's{{&&s++rNc||jvrt||}||j|<|Stt|j|}|SrY)rGr0r)r^r~markers rBr5z_ModuleMarker.get_module*sK t}} $"4.F"(DMM$  -t)<=F rNc:jvr0ttj} |j|yt|gfdj<y#t$r0}t |tst jdd|d}~wwxYw)Nr,r-c&jSrY)r\)r~r^srBz)_ModuleMarker.add_class..As):):4)@rN)rV)rGrr/rvr7r.rr8)r^r~r:r<rAs`` rBr6z_ModuleMarker.add_class2s 4== 0$--2EFH !!#&#7!@#DMM$ " !(,@A11 '77  sA!! B*+BBc~||jvr/tt|j|}|j|yyrY)rGrr/rD)r^r~r:r<s rBrEz_ModuleMarker.remove_classDs7 4== 0$--2EFH   % !rNN)r~rzr}r)r~rzrwbool)r~rzrwr&)r~rzrwrxrIrzrwzUnion[_ModNS, Type[Any]])r~rzrwr0)r~rzr:r)rwrx)rQr2rRrSrTr{rarrr\rr5r6rErPrNrBr0r0sM NI ##CC N O %#&,$&rNr0c,eZdZUdZded<ddZddZy) r)__parentr0_ModNS__parentc||_yrY)r)r^r}s rBraz_ModNS.__init__Os  rNcJ |jj|}|Tt|tr |jSt|t sJ|j |jj|Std|jjd|#t$rY0wxYw)NzModule z1 has no mapped classes registered under the name ) rrGr.r0rr/rpror1rnr~)r^rIvalues rBrz_ModNS.__getattr__Rs FMM**3/E e]3 <<'%e-ABBB ,,T]]-?-?EE.2mm.@.@# G     sB B"!B"N)r}r0rrQr2rRrTr{rarrPrNrBrrJsI rNrc,eZdZUdZded<ddZddZy) _GetColumnsr:r)r:c||_yrYrrcs rBraz_GetColumns.__init__is rNct|jd}|r||jvrtd|jd||j|}|jt j jurmt|tjsJ|j}t|tr |j}n(t|tstj d|zt#|j|S)NF) configurezClass z% does not have a mapped column named zaProperty %r is not an instance of ColumnProperty (i.e. does not correspond directly to a Column).)rr:all_orm_descriptorsr7extension_typer NotExtension NOT_EXTENSIONr.rQueryableAttributepropertyrr~rrr8getattr)r^rImpdescprops rBrz_GetColumns.__getattr__ls $((e 4 "000$xx& ))#.D""j&=&=&K&KK!$ (E(EFFF}}dO4))C#D.9112478 txx%%rNN)r:r)rIrzrwrrrPrNrBrrdsI N&rNrc@tj|jSrY)rinspectr:)targets rBrrs:%%fjj1rNc6eZdZUdZded<ded<d dZd dZy) _GetTablerImetadatarzrIr"rc ||_||_yrYr)r^rIrs rBraz_GetTable.__init__s  rNc\|jjt||jSrY)rtablesrrIrs rBrz_GetTable.__getattr__s"}}##N3$ABBrNN)rIrzrr")rIrzrwr#rrPrNrBrrs!I H!CrNrc\t|tr|jg|}t|SrY)r.r/rpr)rIrs rB_determine_containerrs*%-.!!"c* u rNceZdZUdZded<ded<ded<ded <d ed <d ed < d ddZddZddZddZddZ y)_class_resolver)r:rargfallback_dict _resolvers favor_tablesr)r:RelationshipProperty[Any]rMapping[str, Any]rrzrrrz Tuple[Callable[[str], Any], ...]rc||_||_||_||_t j |j |_d|_||_ y)NrP) r:rrrr PopulateDict _access_clsrrr)r^r:rrrrs rBraz_class_resolver.__init__sG   &&t'7'78 (rNc|j}tj|}|j}|J|j}|j }|j rB||jvr|j|S||jvrt|t|d|S||vrt|||S|j sB||jvr|j|S||jvrt|t|d|Sd|vr9|tt|dvr$tt|d}|j|S|jr|jD]}||} | | cS|j |S)Nrr*)r:rmanager_of_classregistry_class_registryrrr_schemasrrrrr0rrr) r^rIr:manager decl_baser;rrresolvrs rBrz_class_resolver._access_clsshh--c2$$ $$$'77%%   hoo%s++))) gc:x&HII % %'-@-EF F  hoo%s++))) gc:x&HII $7 7C4 ./DED = 23HIH((- - __// !s $ L ! }}S!!rNc tjd|}|r|jdjd}t j d|j jd|jd|j jd|jdd |d |t j d|j jd |jd |d |jd |)Nz (.+)\[(.+)\]r'zWhen initializing mapper z, expression "relationship(z)" seems to be using a generic class as the argument to relationship(); please state the generic argument using an annotation, e.g. "z : Mapped[rz['z']] = relationship()"z , expression z failed to locate a name (zG). If this is a class name, consider adding this relationship() to the z6 class after both dependent classes have been defined.) rematchgroupstriprr8rr}rrIr:)r^r~err generic_matchclsargs rB_raise_for_namez_class_resolver._raise_for_names$7 "((+11#6F))+DII,<,<+=>,,0HH<8IIMM?)M,?,?,B+CVH2 4 )) 99##TXXtTXX ?   rNc|j}|j}d} |jdD]}|||} t||} t |t r |j Strt |tttfsJ|S#t$r}|j||Yd}~yd}~wt$r)}|j|jd|Yd}~yd}~wwxYw)Nr+r)rrr3rr.rr:rtyper#rr1rrnargs)r^r~drvalr?rns rB _resolve_namez_class_resolver._resolve_namesxx JJ C 0<U8D"4/D  0$ ,xx %dT5&,ABBB  ,  s + + /  A . . /s#*B CB(( C4CCc t|jt|j}t |t r |j S|S#t$r)}|j|jd|Yd}~yd}~wwxYw)Nr) evalrglobalsrr.rr:rnrr)r^xrs rB__call__z_class_resolver.__call__sb /TXXwy$**5A![)uu  /  A . . /sAA A A;A66A;NF) r:r)rrrrrrzrrr)r~rzr Exceptionrwr )rwzUnion[Table, Type[Any], _ModNS])rwr) rQr2rRrTr{rarrrrrPrNrBrrs|I N ## H00# ) )()$ )  )  ) %"N., /rNrr_fallback_dictctAddl}ddlm}ddlm}t j |jj||dadd fd } d fd }||fS) Nrr)foreign)remote)rrc,tt||S)N)r)rr)rrr:rs rB resolve_argz_resolver..resolve_arg1s ~s  rNc<tt|jSrY)rrr)rr:rs rB resolve_namez_resolver..resolve_name6ssD.#>LLLrNr)rrzrrrwr)rrzrwz-Callable[[], Union[Type[Any], Table, _ModNS]]) r sqlalchemyrrr immutabledict__dict__union)r:rrrrrrs`` rB _resolverr"sg ++J,?,?@FF6 2  M M 6M  $$rN)r9rzr:zType[_T]r;_ClsRegistryTyperwrx)r9rzr:r)r;rrwrx)rIrzr;rrJzCallable[[Any], bool]rwr)rIrzrrrwr)r:r)rrrwzmTuple[Callable[[str], Callable[[], Union[Type[Any], Table, _ModNS]]], Callable[[str, bool], _class_resolver]])?rS __future__rrtypingrrrrrr r r r r rrrrrrrrZrrrdescriptor_propsr propertiesrrrrr sql.schemar util.typingr relationshipsr!r"r#r$rzrrsetr'r{r6rErMr&r/r0rr _inspectsrrrrrrPrNrBrs# ! -&'+3%" T!#uT3E-E'F"FG &)U "*33!38H3 3l"9I B )  @ B@+B@JE&$E&P  4&&@" [!1 C C //D%)!(%rN