o
    ŠågŸ  ã                   @  sú   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 d�ZG dd„ deƒZG dd„ deƒZG d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                   @  s   e Zd ZdS )Ú_StateChangeStateN)Ú__name__Ú
__module__Ú__qualname__© r   r   úX/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/orm/state_changes.pyr   !   s    r   c                   @  s   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    r   c                   @  sb   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d„Zeddd„ƒZejd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                 C  s   t jd|› d|›�dd�‚)NzCan't run operation 'z()' when Session is in state Úisce©Úcode)Úsa_excÚIllegalStateChangeError)Úselfr#   r%   r   r   r   Ú_raise_for_prerequisite_state9   s   ÿýz*_StateChange._raise_for_prerequisite_stateÚprerequisite_statesúEUnion[Literal[_StateChangeStates.ANY], Tuple[_StateChangeState, ...]]Úmoves_toúCallable[[_F], _F]c                   sH   |sJ dƒ‚|t ju‰td|ƒ‰ˆt ju‰ tjd‡ ‡‡‡fd
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   r,   r   ÚargÚkwr&   c           	        sv  |j }ˆr|ˆvr| | j|¡ |j}|j}ˆ rˆn|}|tjurTˆ rT||urT|rC|tjtjfv rCt	j
d| j› d|j› dˆ›�dd�‚t	j
d| j› dˆ›d|›�dd�‚| |_tj|_zXz| |g|¢R i |¤Ž}W n   ‚ |j |u r||W ||_||_S |j |u r�t	j
d| j› d	ˆ›d
�dd�‚|r¥t	j
d|j› d| j› d|j ›�dd�‚t	j
d| j› d|j ›�dd�‚||_||_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!   r-   r   r    r"   r   r   r   r   r*   r+   )	r2   r,   r3   r4   Úcurrent_stateÚ
next_stateÚexisting_fnÚexpect_stateÚ	ret_value©Úexpect_state_changeÚhas_prerequisite_statesr0   Úprerequisite_state_collectionr   r   Ú_go_   s„   ÿ
þþ
ÿýû
ÿþü

ç
þü
þüú	
ÿýz(_StateChange.declare_states.<locals>._goN)
r2   r   r,   r   r3   r   r4   r   r&   r   )r   r   r   r   r   Ú	decorator)Úclsr.   r0   r>   r   r:   r   Údeclare_statesB   s   ÿÿ
Mz_StateChange.declare_statesÚexpectedúIterator[Any]c                 c  sj   � | j tju sJ dƒ‚|| _ z!zdV  W n   ‚ | j|ur)tjd| j›�dd�‚W tj| _ dS tj| _ 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!   r*   r+   )r,   rB   r   r   r   Ú_expect_state¯   s   €ÿ

ÿÿz_StateChange._expect_state)r#   r$   r%   r   r&   r   )r.   r/   r0   r   r&   r1   )rB   r   r&   rC   )r   r   r   Ú__doc__r   r   r    Ú__annotations__r   r!   r"   r-   ÚclassmethodrA   Ú
contextlibÚcontextmanagerrD   r   r   r   r   r   +   s   
 	
	lr   )rE   Ú
__future__r   rH   Úenumr   Útypingr   r   r   r   r   r	   r
   r   r   Ú r   r*   r   Úutil.typingr   r   r   r   r   r   r   r   r   Ú<module>   s(   