o
    h                     @  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   p/var/www/html/construction_image-detection-poc/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_statesEUnion[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 rt	j
d| j d	d
dd|rt	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(   