
    fL                    f   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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# ddl$m%Z%  ed e!      Z& ed"e!      Z' G d# d$ee&         Z( G d% d&e(e&         Z)d'd(	 	 	 	 	 d1d)Z* G d* d+      Z+d'd(	 	 	 	 	 d2d,Z,d'd(	 	 	 	 	 d3d-Z- G d. d/ee&e'f         Z.	 	 	 	 	 	 d4d0Z/e.Z0	 e-Z1	 e,Z2	 e*Z3y)5    )annotationsN)Any)Callable)Dict)Generic)List)Optional)Sequence)Tuple)Type)TYPE_CHECKING)TypeVar)Union   )base)
collection)collection_adapter   )exc)util)	coercions)
expression)roles)Missing)	MissingOr)Literal)AttributeEventToken)Mapper)CollectionAdapter)ColumnElement_KT)bound_VTc                  8    e Zd ZdZdZddZ	 	 d	dZd
dZddZy)_PlainColumnGetterzPlain column getter, stores collection of Column objects
    directly.

    Serializes to a :class:`._SerializableColumnGetterV2`
    which has more expensive __call__() performance
    and some rare caveats.

    )cols	compositec                8    || _         t        |      dkD  | _        y Nr   )r&   lenr'   )selfr&   s     a/var/www/highground/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/mapped_collection.py__init__z_PlainColumnGetter.__init__:   s    	TQ    c                @    t         j                  | j                        S N)_SerializableColumnGetterV2_reduce_from_colsr&   r+   s    r,   
__reduce__z_PlainColumnGetter.__reduce__>   s     +<<TYYGGr.   c                    | j                   S r0   )r&   )r+   mappers     r,   _colsz_PlainColumnGetter._colsF   s    yyr.   c                   t        j                  |      }t        j                  |      }| j                  |      D cg c]  }|j	                  ||j
                  |      ! }}| j                  rt        |      S |d   }|t        S |S c c}w )Nr   )	r   instance_state_state_mapperr7   _get_state_attr_by_columndictr'   tupler   )r+   valuestatemcolkeyobjs          r,   __call__z_PlainColumnGetter.__call__I   s    ##E*u% zz!}
 ''uzz3?
 
 >>:a&C{

s   $B
N)r&   Sequence[ColumnElement[_KT]]returnNonerF   cTuple[Type[_SerializableColumnGetterV2[_KT]], Tuple[Sequence[Tuple[Optional[str], Optional[str]]]]]r6   zMapper[_KT]rF   rE   )r>   r!   rF   z&MissingOr[Union[_KT, Tuple[_KT, ...]]])	__name__
__module____qualname____doc__	__slots__r-   r4   r7   rD    r.   r,   r%   r%   .   s.     &I'H
Hr.   r%   c                  J    e Zd ZdZdZ	 	 	 	 ddZ	 	 d	dZed
d       ZddZ	y)r1   a<  Updated serializable getter which deals with
    multi-table mapped classes.

    Two extremely unusual cases are not supported.
    Mappings which have tables across multiple metadata
    objects, or which are mapped to non-Table selectables
    linked across inheriting mappers may fail to function
    here.

    )colkeysc                8    || _         t        |      dkD  | _        y r)   )rR   r*   r'   )r+   rR   s     r,   r-   z$_SerializableColumnGetterV2.__init__i   s     W)r.   c                4    | j                   | j                  ffS r0   )	__class__rR   r3   s    r,   r4   z&_SerializableColumnGetterV2.__reduce__o   s     ~~..r.   c                h    dd}|D cg c]  }|j                    ||      f }}t        |ffS c c}w )Nc                x    t        | j                  t        j                        sy | j                  j                  S r0   )
isinstancetabler   TableClauserB   )cs    r,   
_table_keyzA_SerializableColumnGetterV2._reduce_from_cols.<locals>._table_key|   s'    aggz'='=>ww{{"r.   )r[   zColumnElement[_KT]rF   zOptional[str])rB   r1   )clsr&   r\   r[   rR   s        r,   r2   z-_SerializableColumnGetterV2._reduce_from_colsw   s;    
	# 488aAEE:a=)88*WJ66 9s   /c                   g }t        |j                  dd       }| j                  D ]a  \  }}||||vr)|j                  |j                  j                  |          7|j                  |j
                  |   j                  |          c |S )Nmetadata)getattrlocal_tablerR   appendr[   tables)r+   r6   r&   r_   ckeytkeys         r,   r7   z!_SerializableColumnGetterV2._cols   s    )+6--z4@,, 	;JD$|x/4x3GF..0067HOOD133D9:		;
 r.   N)rR   z-Sequence[Tuple[Optional[str], Optional[str]]]rF   rG   rH   )r&   rE   rF   rI   rJ   )
rK   rL   rM   rN   rO   r-   r4   classmethodr2   r7   rP   r.   r,   r1   r1   [   sK    	 I*D*	*/
/ 7 7r.   r1   Fignore_unpopulated_attributec                   t        j                  |       D cg c](  }t        j                  t        j
                  |d      * }}t        |      }t        ||      S c c}w )a  A dictionary-based collection type with column-based keying.

    .. versionchanged:: 2.0 Renamed :data:`.column_mapped_collection` to
       :class:`.column_keyed_dict`.

    Returns a :class:`.KeyFuncDict` factory which will produce new
    dictionary keys based on the value of a particular :class:`.Column`-mapped
    attribute on ORM mapped instances to be added to the dictionary.

    .. note:: the value of the target attribute must be assigned with its
       value at the time that the object is being added to the
       dictionary collection.   Additionally, changes to the key attribute
       are **not tracked**, which means the key in the dictionary is not
       automatically synchronized with the key value on the target object
       itself.  See :ref:`key_collections_mutations` for further details.

    .. seealso::

        :ref:`orm_dictionary_collection` - background on use

    :param mapping_spec: a :class:`_schema.Column` object that is expected
     to be mapped by the target mapper to a particular attribute on the
     mapped class, the value of which on a particular instance is to be used
     as the key for a new dictionary entry for that instance.
    :param ignore_unpopulated_attribute:  if True, and the mapped attribute
     indicated by the given :class:`_schema.Column` target attribute
     on an object is not populated at all, the operation will be silently
     skipped.  By default, an error is raised.

     .. versionadded:: 2.0 an error is raised by default if the attribute
        being used for the dictionary key is determined that it was never
        populated with any value.  The
        :paramref:`_orm.column_keyed_dict.ignore_unpopulated_attribute`
        parameter may be set which will instead indicate that this condition
        should be ignored, and the append operation silently skipped.
        This is in contrast to the behavior of the 1.x series which would
        erroneously populate the value in the dictionary with an arbitrary key
        value of ``None``.


    mapping_spec)argnamerg   )r   to_listr   expectr   ColumnArgumentRoler%   _mapped_collection_cls)rj   rh   qr&   keyfuncs        r,   column_keyed_dictrr      sd    ` l+ 	111nMD  !&G!%A s   -Ac                  (    e Zd ZdZddZddZddZy)	_AttrGetter)	attr_namegetterc                F    || _         t        j                  |      | _        y r0   )ru   operator
attrgetterrv   )r+   ru   s     r,   r-   z_AttrGetter.__init__   s    ")))4r.   c                *   | j                  |      }|t        j                  |      }|j                  }| j                  |j
                  v r@|j                  }|j                  | j                  t        j                        }|t        S |S t        S |S r0   )
rv   r   r9   r6   ru   attrsr<   getNO_VALUEr   )r+   mapped_objectrC   r?   mpdict_s         r,   rD   z_AttrGetter.__call__   s|    kk-(;''6EB~~)

ii>;"N 
 
r.   c                (    t         | j                  ffS r0   )rt   ru   r3   s    r,   r4   z_AttrGetter.__reduce__   s    T^^---r.   N)ru   str)r~   r   rF   r   )rF   z$Tuple[Type[_AttrGetter], Tuple[str]])rK   rL   rM   rO   r-   rD   r4   rP   r.   r,   rt   rt      s    'I5.r.   rt   c               .    t        t        |       |      S )ad  A dictionary-based collection type with attribute-based keying.

    .. versionchanged:: 2.0 Renamed :data:`.attribute_mapped_collection` to
       :func:`.attribute_keyed_dict`.

    Returns a :class:`.KeyFuncDict` factory which will produce new
    dictionary keys based on the value of a particular named attribute on
    ORM mapped instances to be added to the dictionary.

    .. note:: the value of the target attribute must be assigned with its
       value at the time that the object is being added to the
       dictionary collection.   Additionally, changes to the key attribute
       are **not tracked**, which means the key in the dictionary is not
       automatically synchronized with the key value on the target object
       itself.  See :ref:`key_collections_mutations` for further details.

    .. seealso::

        :ref:`orm_dictionary_collection` - background on use

    :param attr_name: string name of an ORM-mapped attribute
     on the mapped class, the value of which on a particular instance
     is to be used as the key for a new dictionary entry for that instance.
    :param ignore_unpopulated_attribute:  if True, and the target attribute
     on an object is not populated at all, the operation will be silently
     skipped.  By default, an error is raised.

     .. versionadded:: 2.0 an error is raised by default if the attribute
        being used for the dictionary key is determined that it was never
        populated with any value.  The
        :paramref:`_orm.attribute_keyed_dict.ignore_unpopulated_attribute`
        parameter may be set which will instead indicate that this condition
        should be ignored, and the append operation silently skipped.
        This is in contrast to the behavior of the 1.x series which would
        erroneously populate the value in the dictionary with an arbitrary key
        value of ``None``.


    rg   )ro   rt   )ru   rh   s     r,   attribute_keyed_dictr      s    V "I%A r.   c                   t        | |      S )a  A dictionary-based collection type with arbitrary keying.

    .. versionchanged:: 2.0 Renamed :data:`.mapped_collection` to
       :func:`.keyfunc_mapping`.

    Returns a :class:`.KeyFuncDict` factory with a keying function
    generated from keyfunc, a callable that takes an entity and returns a
    key value.

    .. note:: the given keyfunc is called only once at the time that the
       target object is being added to the collection.   Changes to the
       effective value returned by the function are not tracked.


    .. seealso::

        :ref:`orm_dictionary_collection` - background on use

    :param keyfunc: a callable that will be passed the ORM-mapped instance
     which should then generate a new key to use in the dictionary.
     If the value returned is :attr:`.LoaderCallableStatus.NO_VALUE`, an error
     is raised.
    :param ignore_unpopulated_attribute:  if True, and the callable returns
     :attr:`.LoaderCallableStatus.NO_VALUE` for a particular instance, the
     operation will be silently skipped.  By default, an error is raised.

     .. versionadded:: 2.0 an error is raised by default if the callable
        being used for the dictionary key returns
        :attr:`.LoaderCallableStatus.NO_VALUE`, which in an ORM attribute
        context indicates an attribute that was never populated with any value.
        The :paramref:`_orm.mapped_collection.ignore_unpopulated_attribute`
        parameter may be set which will instead indicate that this condition
        should be ignored, and the append operation silently skipped. This is
        in contrast to the behavior of the 1.x series which would erroneously
        populate the value in the dictionary with an arbitrary key value of
        ``None``.


    rg   )ro   )rq   rh   s     r,   keyfunc_mappingr     s    X ".J r.   c                  P    e Zd ZdZdd	 	 	 	 	 	 	 d fdZe	 d	 	 	 	 	 	 	 dd       Z	 	 ddZ ej                  d      	 d	 	 	 	 	 	 	 dd       Z
ej                  ej                  	 d	 	 	 	 	 dd	              Zej                  ej                  	 d	 	 	 	 	 dd
              Z xZS )KeyFuncDicta  Base for ORM mapped dictionary classes.

    Extends the ``dict`` type with additional methods needed by SQLAlchemy ORM
    collection classes. Use of :class:`_orm.KeyFuncDict` is most directly
    by using the :func:`.attribute_keyed_dict` or
    :func:`.column_keyed_dict` class factories.
    :class:`_orm.KeyFuncDict` may also serve as the base for user-defined
    custom dictionary classes.

    .. versionchanged:: 2.0 Renamed :class:`.MappedCollection` to
       :class:`.KeyFuncDict`.

    .. seealso::

        :func:`_orm.attribute_keyed_dict`

        :func:`_orm.column_keyed_dict`

        :ref:`orm_dictionary_collection`

        :ref:`orm_custom_collection`


    Frg   c               :    || _         || _        t        |   |  y)ah  Create a new collection with keying provided by keyfunc.

        keyfunc may be any callable that takes an object and returns an object
        for use as a dictionary key.

        The keyfunc will be called every time the ORM needs to add a member by
        value-only (such as when loading instances from the database) or
        remove a member.  The usual cautions about dictionary keying apply-
        ``keyfunc(object)`` should return the same output for the life of the
        collection.  Keying based on mutable properties can result in
        unreachable instances "lost" in the collection.

        N)rq   rh   superr-   )r+   rq   rh   	dict_argsrU   s       r,   r-   zKeyFuncDict.__init___  s!    & ,H))$r.   c                >    t        |      }|j                  |       |S r0   )r   update)r]   rq   valuesadapterr   s        r,   	_unreducezKeyFuncDict._unreducev  s!     %0$8
		& 	r.   c                d    t         j                  | j                  t        |       t	        |       ffS r0   )r   r   rq   r<   r   r3   s    r,   r4   zKeyFuncDict.__reduce__  s2     !!T
"4(
 	
r.   zsqlalchemy.orm.attributesc                  t        j                  |      j                  }t        j                  j
                  }t        ||j                        sd}n?|j                  |j                  v r|j                  |j                      }n|j                  }|rt        j                  d| d       y t        j                  d| dt        j                  |       dt        j                  |       d| d	      )	Nzunknown relationshipz0Attribute keyed dictionary value for attribute 'z' was None; this will raise in a future release. To skip this assignment entirely, Set the "ignore_unpopulated_attribute=True" parameter on the mapped collection factory.z1In event triggered from population of attribute 'za' (potentially from a backref), can't populate value in KeyFuncDict; dictionary key derived from z= is not populated. Ensure appropriate state is set up on the z  object before assigning to the z attribute. To skip this assignment entirely, Set the "ignore_unpopulated_attribute=True" parameter on the mapped collection factory.)r   r9   r6   r   	preloadedorm_attributesrX   r   rB   r{   warnsa_excInvalidRequestErrorinstance_str)r+   r>   	initiator	warn_onlyr6   
attributesrelationships          r,   _raise_for_unpopulatedz"KeyFuncDict._raise_for_unpopulated  s     $$U+22^^22
)Z%C%CD1L]]fll*$ll9==9:L$==LII*^ ,>? ,,*^ ,  !% 1 1% 89 :((/0 1++7. 9>? r.   c                   | j                  |      }|t        j                  u r"| j                  s| j	                  ||d       n-y|t
        u r$| j                  s| j	                  ||d       d}ny| j                  |||       y)z9Add an item by value, consulting the keyfunc for the key.Fr   NT)rq   r   r}   rh   r   r   __setitem__r+   r>   _sa_initiatorrB   s       r,   setzKeyFuncDict.set  s     ll5!$--44++=E ,  G^44++=D ,  e]3r.   c           	     \   | j                  |      }|t        j                  u r!| j                  s| j	                  ||d       y|t
        u r$| j                  s| j	                  ||d       d}ny| |   |k7  r"t        j                  d|d| |   d|d      | j                  ||       y)	z<Remove an item by value, consulting the keyfunc for the key.Fr   NTzCan not remove 'z': collection holds 'z' for key 'z'. Possible cause: is the KeyFuncDict key function based on mutable properties or properties that only obtain values after flush?)	rq   r   r}   rh   r   r   r   r   __delitem__r   s       r,   removezKeyFuncDict.remove  s     ll5!$--44++=E ,  G^44++=D ,   9,, */S	3@  	m,r.   )rq   Callable[[Any], Any]r   r   rh   boolrF   rG   r0   )rq   r   r   zDict[_KT, _KT]r   zOptional[CollectionAdapter]rF   z'KeyFuncDict[_KT, _KT]')rF   zxTuple[Callable[[_KT, _KT], KeyFuncDict[_KT, _KT]], Tuple[Any, Union[Dict[_KT, _KT], Dict[_KT, _KT]], CollectionAdapter]])r>   r!   r   0Union[AttributeEventToken, Literal[None, False]]r   r   rF   rG   )r>   r!   r   r   rF   rG   )rK   rL   rM   rN   r-   rf   r   r4   r   preload_moduler   r   appenderinternally_instrumentedr   removerr   __classcell__)rU   s   @r,   r   r   E  so   : .3	%%% % '+	%
 
%. 
 04	
%
 
 -	

 
!
 



 T45 GK)) D)
 ) 
) 6)V '' KO44 H4 
	4 ( 46 '' KO!-!- H!- 
	!- ( !-r.   r   c                H      G  fddt         t        t        f         }|S )Nc                  &     e Zd Zd fdZ xZS )/_mapped_collection_cls.<locals>._MKeyfuncMappedc                ,    t        |   g|di y )Nrh   )r   r-   )r+   r   rU   rh   rq   s     r,   r-   z8_mapped_collection_cls.<locals>._MKeyfuncMapped.__init__  s&    G .Jr.   )r   r   rF   rG   )rK   rL   rM   r-   r   )rU   rh   rq   s   @r,   _MKeyfuncMappedr     s    	 	r.   r   )r   r!   )rq   rh   r   s   `` r,   ro   ro     s     +c3h/  r.   )rj   z&Union[Type[_KT], Callable[[_KT], _VT]]rh   r   rF   Type[KeyFuncDict[_KT, _KT]])ru   r   rh   r   rF   zType[KeyFuncDict[Any, Any]])rq   r   rh   r   rF   zType[KeyFuncDict[_KT, Any]])rq   r   rh   r   rF   r   )4
__future__r   rx   typingr   r   r   r   r   r	   r
   r   r   r   r   r    r   collectionsr   r   r   r   r   sqlr   r   r   util.langhelpersr   r   util.typingr   r   r   r   sql.elementsr    r!   r#   r%   r1   rr   rt   r   r   r   ro   MappedCollectionmapped_collectionattribute_mapped_collectioncolumn_mapped_collectionrP   r.   r,   <module>r      s   #                # +      & ( !%.,e3e3* *Z2"4S"9 2p */686 #'6 !	6r. .6 =B..59. .h */.!. #'. !	.by-$sCx. y-x!AE    $  3  - r.   