
    g6F                       U 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
 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 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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  ddlm!Z! ddlmZ ddl"m#Z# dd l$m%Z% erdd!l&m'Z' dd"l"m(Z( dd#l"m)Z)  ed$e%      Z*ee+ee,d&f   f   Z- e.       Z/d'e0d(<   	 	 	 	 	 	 	 	 d>d)Z1	 	 	 	 	 	 	 	 d?d*Z2	 	 	 	 	 	 	 	 d@d+Z3 G d, d&      Z4 G d- d.e4      Z5 G d/ d0e4      Z6 G d1 d2      Z7 G d3 d4      Z8  e!jr                  e8      d5         G d6 d7      Z:dAd8Z; G d9 d:      Z<da=d;e0d<<   dBd=Z>y)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)boundClsRegistryTokenzSet[ClsRegistryToken]_registriesc                6   | |v r2||    }t        |t              s"t        |t        d|      g      x}|| <   n||| <   	 t        t        |d         }|j
                  j                  d      }|rQ|j                  d      }|j                  |      }|D ]  }|j                  |      } 	 |j                  | |       |rPyy# t        $ r t        dd      x|d<   }Y w xY w# t        $ r0}t        |t              st        j                  d|  d      | d}~ww xY w)z^Add a class to the _decl_class_registry associated with the
    given declarative class.

    	Type[Any]_sa_module_registryN.r   name "-" matches both a class name and a module name)
isinstance_MultipleClassMarkerr   _ModuleMarkerKeyError
__module__splitpop
get_module	add_classAttributeErrorr   InvalidRequestError)		classnameclsdecl_class_registryexistingroot_moduletokenstokenmoduleaes	            Z/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/sqlalchemy/orm/clsregistry.pyr6   r6   @   sQ    ''&y1($898Ld;129 H*95 *-I&
./DE
 ^^!!#&F 

1''.E&&u-F 		Y,   
/6	
12[
.  	fm4--YK (3 3 
 	s)   B? (C ?CC	D(+DDc                   | |v r*||    }t        |t              r|j                  |       n|| = 	 t        t        |d         }|j                  j                  d      }|rQ|j                  d      }|j                  |      }|D ]  }|j                  |      } 	 |j                  | |       |rPy y # t
        $ r Y y w xY w# t        $ r t        |t              sn Y 1w xY w)Nr*   r+   r   )r.   r/   remove_itemr   r0   r1   r2   r3   r4   r5   remove_classr7   )r9   r:   r;   r<   r=   r>   r?   r@   s           rB   rE   rE   v   s     ''&y1h 45  %#I../DE
 ^^!!#&F


1''.E&&u-F 		3/     	fm4 	s#   B2 C 2	B>=B>CCc                    | |vry||    }t        |t              r&|j                  D ]  } ||      s y t        d       ||       S )a  test 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.

    TFzunknown codepath)r.   r/   contentsNotImplementedError)keyr;   testthing	sub_things        rB   _key_is_emptyrM      sW    $ %%$E%-.II ( &&899;    c                      e Zd ZdZdZy)r&   zAan object that can be in the registry._class_registry as a value. N)__name__r2   __qualname____doc__	__slots__rP   rN   rB   r&   r&      s
    KIrN   c                  d    e Zd ZU dZdZded<   ded<   	 d	 	 	 ddZdd	Zdd
ZddZ	ddZ
ddZy)r/   zRrefers to multiple classes of the same name
    within _decl_class_registry.

    )	on_removerG   __weakref__zSet[weakref.ref[Type[Any]]]rG   z/CallableReference[Optional[Callable[[], None]]]rV   Nc                    || _         |D ch c]"  }t        j                  || j                        $ c}| _        t
        j                  |        y c c}w N)rV   weakrefref_remove_itemrG   r'   add)selfclassesrV   items       rB   __init__z_MultipleClassMarker.__init__   sJ    
 #=D
=DTGKKd//0W
 	
s   'Ac                L    | j                  t        j                  |             y rY   )r\   rZ   r[   r^   r:   s     rB   rD   z _MultipleClassMarker.remove_item   s    '++c*+rN   c                (    d | j                   D        S )Nc              3  *   K   | ]  } |         y wrY   rP   ).0r[   s     rB   	<genexpr>z0_MultipleClassMarker.__iter__.<locals>.<genexpr>   s     /#s   rG   )r^   s    rB   __iter__z_MultipleClassMarker.__iter__   s    ///rN   c                    t        | j                        dkD  r+t        j                  ddj	                  ||gz         z        t        | j                        d   } |       }|t        |      |S )Nr   zxMultiple classes found for path "%s" in the registry of this declarative base. Please use a fully module-qualified path.r+   r   )lenrG   r   r8   joinlist	NameError)r^   pathrI   r[   r:   s        rB   attempt_getz _MultipleClassMarker.attempt_get   su    t}}!))B 88DC5L)+  t}}%a(C%C{n$JrN   c                    | j                   j                  |       | j                   s3t        j                  |        | j                  r| j                          y y y rY   )rG   discardr'   rV   )r^   r[   s     rB   r\   z!_MultipleClassMarker._remove_item   sD    c"}}%~~   rN   c                   t        | j                        D cg c]	  } |        c}D ch c]  }||j                   }}|j                  |v r0t        j                  d|j                  d|j
                  d       | j                  j                  t        j                  || j                               y c c}w c c}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.)
rm   rG   r2   r   warnrQ   r]   rZ   r[   r\   )r^   r`   r[   r:   moduless        rB   add_itemz_MultipleClassMarker.add_item   s     *.dmm)<=)<#)<=
= NN= 	 

 ??g%II ??DMM3 	'++dD,=,=>? >
s
   B:B?rY   )r_   zIterable[Type[Any]]rV   zOptional[Callable[[], None]])r:   r)   returnNone)rw   z*Generator[Optional[Type[Any]], None, None])ro   	List[str]rI   strrw   r)   )r[   zweakref.ref[Type[Any]]rw   rx   )r`   r)   rw   rx   )rQ   r2   rR   rS   rT   __annotations__ra   rD   ri   rp   r\   rv   rP   rN   rB   r/   r/      sP    
 7I))>>
 37	$	 0	,0!@rN   r/   c                  ~    e Zd ZU dZdZded<   ded<   ded<   d	ed
<   ddZddZddZddZ	ddZ
ddZddZddZy)r0   z>Refers to a module name within
    _decl_class_registry.

    )parentnamerG   mod_nsro   rW   Optional[_ModuleMarker]r}   z5Dict[str, Union[_ModuleMarker, _MultipleClassMarker]]rG   _ModNSr   ry   ro   c                    || _         || _        i | _        t        |       | _        | j                   r*| j                   j
                  | j                  gz   | _        ng | _        t        j                  |        y rY   )r}   r~   rG   r   r   ro   r'   r]   )r^   r~   r}   s      rB   ra   z_ModuleMarker.__init__  sX    	Tl;;((DII;6DIDIrN   c                    || j                   v S rY   rh   r^   r~   s     rB   __contains__z_ModuleMarker.__contains__  s    t}}$$rN   c                     | j                   |   S rY   rh   r   s     rB   __getitem__z_ModuleMarker.__getitem__  s    }}T""rN   c                    | j                   j                  |d        | j                   sG| j                  %| j                  j                  | j                         t
        j                  |        y y rY   )rG   r4   r}   r\   r~   r'   rr   r   s     rB   r\   z_ModuleMarker._remove_item   sP    $%}}{{&((3% rN   c                8    | j                   j                  |      S rY   )r   __getattr__r^   rI   s     rB   resolve_attrz_ModuleMarker.resolve_attr'  s    {{&&s++rN   c                    || j                   vrt        ||       }|| j                   |<   |S t        t        | j                   |         }|S rY   )rG   r0   r   )r^   r~   markers      rB   r5   z_ModuleMarker.get_module*  sK    t}}$"4.F"(DMM$  -t)<=FrN   c                >     j                   v r0t        t         j                            }	 |j                  |       y t        |g fd      x} j                   <   y # t        $ r0}t        |t              st        j                  d d      | d }~ww xY w)Nr,   r-   c                 &    j                         S rY   )r\   )r~   r^   s   rB   <lambda>z)_ModuleMarker.add_class.<locals>.<lambda>A  s    ):):4)@rN   )rV   )rG   r   r/   rv   r7   r.   r   r8   )r^   r~   r:   r<   rA   s   ``   rB   r6   z_ModuleMarker.add_class2  s    4== 0$--2EFH	!!#& .B!@. Ht}}T* " !(,@A11  '7 7 
 s   A# #	B,+BBc                ~    || j                   v r/t        t        | j                   |         }|j                  |       y y rY   )rG   r   r/   rD   )r^   r~   r:   r<   s       rB   rE   z_ModuleMarker.remove_classD  s7    4== 0$--2EFH  % !rN   N)r~   rz   r}   r   )r~   rz   rw   bool)r~   rz   rw   r&   )r~   rz   rw   rx   rI   rz   rw   zUnion[_ModNS, Type[Any]])r~   rz   rw   r0   )r~   rz   r:   r)   rw   rx   )rQ   r2   rR   rS   rT   r{   ra   r   r   r\   r   r5   r6   rE   rP   rN   rB   r0   r0     sM    
 NI##CCN
O	%#&,$&rN   r0   c                  ,    e Zd ZU dZded<   ddZddZy)	r   )__parentr0   _ModNS__parentc                    || _         y rY   )r   )r^   r}   s     rB   ra   z_ModNS.__init__O  s	    rN   c                J   	 | j                   j                  |   }|Tt        |t              r|j                  S t        |t
              sJ |j                  | j                   j                  |      S t        d| j                   j                  d|      # t        $ r Y 0w xY w)NzModule z1 has no mapped classes registered under the name )r   rG   r.   r0   r   r/   rp   ro   r1   rn   r~   )r^   rI   values      rB   r   z_ModNS.__getattr__R  s    
	FMM**3/E  e]3 <<'%e-ABBB ,,T]]-?-?EE.2mm.@.@#G
 	
  		s   B 	B"!B"N)r}   r0   r   rQ   r2   rR   rT   r{   ra   r   rP   rN   rB   r   r   J  s    I
rN   r   c                  ,    e Zd ZU dZded<   ddZddZy)	_GetColumnsr:   r)   r:   c                    || _         y rY   r   rc   s     rB   ra   z_GetColumns.__init__i  s	    rN   c                   t        | j                  d      }|r||j                  vrt        d| j                  d|      |j                  |   }|j                  t
        j                  j                  u rmt        |t        j                        sJ |j                  }t        |t              r|j                  }n(t        |t              st        j                   d|z        t#        | j                  |      S )NF)	configurezClass 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).)r   r:   all_orm_descriptorsr7   extension_typer   NotExtensionNOT_EXTENSIONr.   r   QueryableAttributepropertyr   r~   r   r   r8   getattr)r^   rI   mpdescprops        rB   r   z_GetColumns.__getattr__l  s    $((e4"000$xx& 
 ))#.D""j&=&=&K&KK!$
(E(EFFF}}dO4))C#D.9112478 
 txx%%rN   N)r:   r)   rI   rz   rw   r   r   rP   rN   rB   r   r   d  s    I	N&rN   r   c                @    t        j                  | j                        S rY   )r   inspectr:   )targets    rB   r   r     s    :%%fjj1rN   c                  6    e Zd ZU dZded<   ded<   d	dZd
dZy)	_GetTablerI   metadatarz   rI   r"   r   c                     || _         || _        y rY   r   )r^   rI   r   s      rB   ra   z_GetTable.__init__  s     rN   c                \    | j                   j                  t        || j                           S rY   )r   tablesr   rI   r   s     rB   r   z_GetTable.__getattr__  s"    }}##N3$ABBrN   N)rI   rz   r   r"   )rI   rz   rw   r#   r   rP   rN   rB   r   r     s    !I	H!CrN   r   c                \    t        |t              r|j                  g |       }t        |      S rY   )r.   r/   rp   r   )rI   r   s     rB   _determine_containerr     s*    %-.!!"c*urN   c                      e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   	 d	 	 	 	 	 	 	 	 	 ddZddZddZddZddZ	y)_class_resolver)r:   r   argfallback_dict
_resolversfavor_tablesr)   r:   RelationshipProperty[Any]r   Mapping[str, Any]r   rz   r   r   r   z Tuple[Callable[[str], Any], ...]r   c                    || _         || _        || _        || _        t	        j
                  | j                        | _        d| _        || _	        y )NrP   )
r:   r   r   r   r   PopulateDict_access_clsr   r   r   )r^   r:   r   r   r   r   s         rB   ra   z_class_resolver.__init__  sG     	 &&t'7'78
(rN   c                   | j                   }t        j                  |      }|j                  }|J |j                  }|j
                  }| j                  rB||j                  v r|j                  |   S ||j                  v rt        |t        |d|            S ||v rt        |||         S | j                  sB||j                  v r|j                  |   S ||j                  v rt        |t        |d|            S d|v r9|t        t        |d         v r$t        t        |d         }|j                  |      S | j                  r| j                  D ]  } ||      }	|	|	c S  | j                   |   S )Nr   r*   )r:   r   manager_of_classregistry_class_registryr   r   r   _schemasr   r   r   r   r0   r   r   r   )
r^   rI   r:   manager	decl_baser;   r   r   resolvr   s
             rB   r   z_class_resolver._access_cls  s~   hh--c2$$	$$$'77%%hoo%s++))) gc:x&HII%%'-@-EFF  hoo%s++))) gc:x&HII $77C4./DED
 =
 23HIH ((--__//s$ L *
 }}S!!rN   c                   t        j                  d|      }|r|j                  d      j                  d      }t	        j
                  d| j                  j                   d| j                  d| j                  j                   d|j                  d       d	| d
      |t	        j
                  d| j                  j                  d| j                  d|d| j                  d	      |)Nz(.+)\[(.+)\]r   'zWhen 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[r   z['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.)rematchgroupstripr   r8   r   r}   r   rI   r:   )r^   r~   errgeneric_matchclsargs        rB   _raise_for_namez_class_resolver._raise_for_name  s    $7"((+11#6F))+DII,<,<+= >,,0HH< 8 IIMM?)M,?,?,B+CVH24  ))
 99##TXXtTXX	? rN   c                   | j                   }| j                  }d }	 |j                  d      D ]  }|||   }t        ||      } 	 t	        |t
              r|j                  S t        rt	        |t        t        t        f      sJ |S # t        $ r}| j                  ||       Y d }~y d }~wt        $ r)}| j                  |j                  d   |       Y d }~y d }~ww xY w)Nr+   r   )r   r   r3   r   r.   r   r:   r   typer#   r   r1   r   rn   args)r^   r~   drvalr?   r   ns          rB   _resolve_namez_class_resolver._resolve_name  s    xxJJ	C<U8D"4/D	 ) $,xx %dT5&,ABBB  	,  s++ 	/  A..	/s#   *B 	CB((C4CCc                    	 t        | j                  t               | j                        }t	        |t
              r|j                  S |S # t        $ r)}| j                  |j                  d   |       Y d }~y d }~ww xY w)Nr   )
evalr   globalsr   r.   r   r:   rn   r   r   )r^   xr   s      rB   __call__z_class_resolver.__call__  sb    	/TXXwy$**5A![)uu 	/  A..	/s   AA	 A	 		A;A66A;NF)
r:   r)   r   r   r   r   r   rz   r   r   r   )r~   rz   r   	Exceptionrw   r   )rw   zUnion[Table, Type[Any], _ModNS])rw   r   )
rQ   r2   rR   rT   r{   ra   r   r   r   r   rP   rN   rB   r   r     s|    I 
N
##	H00 #)) () $	)
 ) ) %"N.,	/rN   r   r   _fallback_dictc                     t         Add l}ddlm} ddlm} t        j                  |j                        j                  ||d      a dd	 fd}	 	 	 	 d
 fd}||fS )Nr   r   )foreign)remote)r   r   c                ,    t        t        | |      S )N)r   )r   r   )r   r   r:   r   s     rB   resolve_argz_resolver.<locals>.resolve_arg1  s    ~s
 	
rN   c                <    t        t        |       j                  S rY   )r   r   r   )r   r:   r   s    rB   resolve_namez_resolver.<locals>.resolve_name6  s     sD.#>LLLrN   r   )r   rz   r   r   rw   r   )r   rz   rw   z-Callable[[], Union[Type[Any], Table, _ModNS]])	r   
sqlalchemy r   r   r   immutabledict__dict__union)r:   r   r   r   r   r   r   s   ``     rB   	_resolverr   "  sg     ++J,?,?@FF62


MM	6M
 $$rN   )r9   rz   r:   zType[_T]r;   _ClsRegistryTyperw   rx   )r9   rz   r:   r)   r;   r   rw   rx   )rI   rz   r;   r   rJ   zCallable[[Any], bool]rw   r   )rI   rz   r   r   rw   r   )r:   r)   r   r   rw   zmTuple[Callable[[str], Callable[[], Union[Type[Any], Table, _ModNS]]], Callable[[str, bool], _class_resolver]])?rS   
__future__r   r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   rZ   r   r   r   descriptor_propsr   
propertiesr   r   r   r   r   
sql.schemar   util.typingr    relationshipsr!   r"   r#   r$   rz   r   r   setr'   r{   r6   rE   rM   r&   r/   r0   r   r   	_inspectsr   r   r   r   r   rP   rN   rB   <module>r      s   # 	         !             - &     ' +3%"T!#uT3E-E'F"FG  &)U" *33!38H3	3l"9I	B	)   
	@ B@+ B@JE&$ E&P
 
4& &@ " 
  [ !1
C C/ /D %)! (%rN   