
    [h                    6   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  ededef                   Z G d de          Z G d de          Z G d d          ZdS )z9State tracking utilities used by :class:`_orm.Session`.

    )annotationsN)Enum)Any)Callable)cast)Iterator)NoReturn)Optional)Tuple)TypeVar)Union   )exc)util)Literal_F.)boundc                      e Zd ZdS )_StateChangeStateN)__name__
__module____qualname__     k/var/www/api.easyaligner.net/htdocs/venv_linux/lib/python3.11/site-packages/sqlalchemy/orm/state_changes.pyr   r   !   s        Dr   r   c                      e Zd ZdZdZdZdS )_StateChangeStates   r      N)r   r   r   ANY	NO_CHANGECHANGE_IN_PROGRESSr   r   r   r   r   %   s         
CIr   r   c                      e Zd ZU dZej        Zded<   ej        Z	ded<   dZ
ded<   ddZedd            Zej        dd            ZdS )_StateChangea!  Supplies state assertion decorators.

    The current use case is for the :class:`_orm.SessionTransaction` class. The
    :class:`_StateChange` class itself is agnostic of the
    :class:`_orm.SessionTransaction` class so could in theory be generalized
    for other systems as well.

    r   _next_state_stateNzOptional[Callable[..., Any]]_current_fnoperation_namestrstatereturnr	   c                :    t          j        d| d|d          )NzCan't run operation 'z()' when Session is in state iscecode)sa_excIllegalStateChangeError)selfr(   r*   s      r   _raise_for_prerequisite_statez*_StateChange._raise_for_prerequisite_state9   s?     ,%N % % % %
 
 
 	
r   prerequisite_statesEUnion[Literal[_StateChangeStates.ANY], Tuple[_StateChangeState, ...]]moves_toCallable[[_F], _F]c                    |s
J d            |t           j        ut          d|          t           j        ut          j        dfd
            }|S )a  Method decorator declaring valid states.

        :param prerequisite_states: sequence of acceptable prerequisite
         states.   Can be the single constant _State.ANY to indicate no
         prerequisite state

        :param moves_to: the expected state at the end of the method, assuming
         no exceptions raised.   Can be the constant _State.NO_CHANGE to
         indicate state should not change at the end of the method.

        zno prequisite states sentzTuple[_StateChangeState, ...]fnr   r2   r   argkwr+   c                &   |j         }
r|vr|                    | j        |           |j        }|j        }	rn|}|t
          j        uro	rm||uri|rC|t
          j        t
          j        fv r)t          j
        d| j         d|j         dd          t          j
        d| j         dd|d          | |_        t
          j        |_        	  | |g|R i |}|j         |u r|||_        ||_        S |j         |u r"t          j
        d| j         d	d
d          |r.t          j
        d|j         d| j         d|j         d          t          j
        d| j         d|j         d          #   xY w# ||_        ||_        w xY w)NzMethod 'z"()' can't be called here; method 'zN()' is already in progress and this would cause an unexpected state change to r-   r.   zCant run operation 'z()' here; will move to state z where we are expecting z()' failed to change state to z as expectedzWhile method 'z()' was running, method 'z)()' caused an unexpected state change to )r&   r3   r   r%   r'   r   r    r!   r"   r0   r1   )r9   r2   r:   r;   current_state
next_stateexisting_fnexpect_state	ret_valueexpect_state_changehas_prerequisite_statesr6   prerequisite_state_collections            r   _goz(_StateChange.declare_states.<locals>._go_   s    KM (O!)FFF222;NNN)J*K':M88L "4"888' 9 l22 :&0&92 $ $ !882; 8 8#.#78 8 ,48 8 $    !84r{ 4 4.64 4%/4 4 $	     "D1DD!/Bt0c000R00	 ;,..$4 $. #.  3 ;-// 872; 7 7&7 7 7 $	    !  8;)= ; ;#%;; ; ,0;	; ;
 $    !8;2; ; ;+/;; ;#   /: $. #. ....s%   E:  
F  9BF  :E==F   F)
r9   r   r2   r   r:   r   r;   r   r+   r   )r   r    r   r!   r   	decorator)clsr4   r6   rE   rB   rC   rD   s     ` @@@r   declare_statesz_StateChange.declare_statesB   s    & #??$????'9'== 	  )-+-@)
 )
% '.@.JJ	K	/ K	/ K	/ K	/ K	/ K	/ K	/ K	/ 
K	/Z 
r   expectedIterator[Any]c              #    K   | j         t          j        u s
J d            || _         	 dV  | j        |urt	          j        d| j        d          n#   xY w	 t          j        | _         dS # t          j        | _         w xY w)zxcalled within a method that changes states.

        method must also use the ``@declare_states()`` decorator.

        zAUnexpected call to _expect_state outside of state-changing methodNzUnexpected state change to r-   r.   )r%   r   r"   r&   r0   r1   )r2   rI   s     r   _expect_statez_StateChange._expect_state   s       #5#HHHH$ IHH
 $
	EEEE {(**4A$+AA    +	*
  2DD1DDDDDDs   A (A. AA. .B)r(   r)   r*   r   r+   r	   )r4   r5   r6   r   r+   r7   )rI   r   r+   rJ   )r   r   r   __doc__r   r    r%   __annotations__r!   r&   r'   r3   classmethodrH   
contextlibcontextmanagerrL   r   r   r   r$   r$   +   s           &8%;K;;;; 2 <F<<<<04K4444
 
 
 
 j j j [jX E E E E E Er   r$   )rM   
__future__r   rP   enumr   typingr   r   r   r   r	   r
   r   r   r    r   r0   r   util.typingr   r   r   r   r$   r   r   r   <module>rW      s    # " " " " "                                                                             ! ! ! ! ! !WT#s(+,,,	 	 	 	 	 	 	 	    *   [E [E [E [E [E [E [E [E [E [Er   