a
    •˜ëj   ã                   @  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 )z7State 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   ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/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                   @  sx   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	d
œdd„Zeddddœdd„ƒZejdd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Ústrr   )Úoperation_nameÚstateÚreturnc                 C  s   t jd|› d|›�dd�‚d S )NúCan't run operation 'z()' when Session is in state Úisce©Úcode)Úsa_excÚIllegalStateChangeError)Úselfr$   r%   r   r   r   Ú_raise_for_prerequisite_state7   s    ÿýz*_StateChange._raise_for_prerequisite_statezEUnion[Literal[_StateChangeStates.ANY], Tuple[_StateChangeState, ...]]zCallable[[_F], _F])Úprerequisite_statesÚmoves_tor&   c                   sT   |sJ dƒ‚|t ju‰td|ƒ‰ˆt ju‰ tjddddd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 prerequisite states sentzTuple[_StateChangeState, ...]r   r   )Úfnr-   ÚargÚkwr&   c           	        sœ  |j }ˆr |ˆvr | | j|¡ |j}|j}ˆ r4ˆn|}|tjurªˆ rª||urª|rˆ|tjtjfv rˆt	j
d| j› d|j› dˆ›�dd�‚n"t	j
d| j› dˆ›d|›�dd�‚| |_tj|_zÐz| |g|¢R i |¤Ž}W n   ‚ Y n˜0 |j |u �r|W ||_||_S |j |u �r.t	j
d| j› d	ˆ›d
�dd�‚nL|�r\t	j
d|j› d| j› d|j ›�dd�‚nt	j
d| j› d|j ›�dd�‚W ||_||_n||_||_0 d S )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)   r'   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,   )	r1   r-   r2   r3   Zcurrent_stateÚ
next_stateZexisting_fnZexpect_stateZ	ret_value©Zexpect_state_changeZhas_prerequisite_statesr0   Zprerequisite_state_collectionr   r   Ú_go]   s�    ÿþþüúþ
ÿýû
ÿþüå
þü
þüú	
ÿýÿz(_StateChange.declare_states.<locals>._go)r   r   r   r   r   Ú	decorator)Úclsr/   r0   r6   r   r5   r   Údeclare_states@   s    ÿÿ
"Mz_StateChange.declare_stateszIterator[Any])Úexpectedr&   c                 c  sp   | j tju sJ dƒ‚|| _ zFz
dV  W n   ‚ Y n"0 | j|urVtjd| j›�dd�‚W tj| _ n
tj| _ 0 dS )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-   r:   r   r   r   Ú_expect_state­   s    ÿ

ÿz_StateChange._expect_state)r   r   r   Ú__doc__r   r   r    Ú__annotations__r   r!   r"   r.   Úclassmethodr9   Ú
contextlibÚcontextmanagerr;   r   r   r   r   r   )   s   
		lr   )r<   Ú
__future__r   r?   Úenumr   Útypingr   r   r   r   r   r	   r
   r   r   Ú r   r+   r   Zutil.typingr   r   r   r   r   r   r   r   r   Ú<module>   s&   