
    	]j7              
       L   d Z ddlZddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ  ej                   e      Z e       Zdded	ed
eeee   f   fdZdee   deeef   d
efdZdee   d
eeef   fdZdee   deeef   d
eeee   f   fdZded
eeeef      fdZy)z
Utility functions for working with the declarative transition system.

This module provides helper functions to make it easier to integrate
the new Pydantic-based transition system with existing Label Studio code.
    N)AnyDictListType)Model)transition_registry)get_state_manager)BaseTransitionTransitionValidationErrorentityvalidatereturnc                    | j                   j                  j                         }t        j                  |      }|s|S i }|j                         D ]  \  }}	 t        j                  |       }|r|j                  nd}	ddl	m
}
 	  |       } |
| |||	dt        | dd            }|j                  |      } |
| |||	|t        | dd            }|j                  |      r|||<    |S # t        t        f$ r |||<   Y w xY w# t         $ r Y t"        $ rE}t$        j'                  dd|| j                   j                  t)        |      dd	
       Y d}~d}~ww xY w)a  
    Get available transitions for an entity.

    Args:
        entity: The entity to get transitions for
        user: User context for validation (only used when validate=True)
        validate: Whether to validate each transition against current state.
                 When False, returns all registered transitions for the entity type.
                 When True, filters to only transitions valid from current state (may be expensive).

    Returns:
        Dictionary mapping transition names to transition classes.
        When validate=False: All registered transitions for the entity type.
        When validate=True: Only transitions valid for the current state.
    N   TransitionContextorganization_idr   current_usercurrent_state_objectcurrent_statetarget_stater   z&Unexpected error validating transitionzfsm.transition_validation_error)eventtransition_nameentity_typeerrorT)extraexc_info)_meta
model_namelowerr   get_transitions_for_entityitemsStateManagerget_current_state_objectstatetransitionsr   getattrget_target_state	TypeError
ValueErrorcan_transition_from_stater   	Exceptionloggerwarningstr)r   userr   entity_name	availablevalid_transitionsnametransition_classr   r   r   temp_instanceminimal_contextr   contextes                   K/root/env/lib/python3.12/site-packages/label_studio/fsm/transition_utils.pyget_available_transitionsr<      s     ,,))//1K#>>{KI"+//"3 98	#/#H#H#P :N066TXM 7 0 2"3!!%)="/!%$+F4Et$L#  -==oN (!%9+) '0A4 HG  99'B*:!$'Q9v I z*  +;!$'	& ) 	 	NN8>'+#)<<#:#: V	   	 	sB   +D1C'4/D'C>;D=C>>D	EE:EEr6   datac                 l    	  | di |S # t         $ r }t        d| j                   d|       d}~ww xY w)am  
    Create a transition instance from a dictionary of data.

    This handles Pydantic validation and provides clear error messages.

    Args:
        transition_class: The transition class to instantiate
        data: Dictionary of transition data

    Returns:
        Validated transition instance

    Raises:
        ValueError: If data validation fails
    zFailed to create z: N )r-   r+   __name__)r6   r=   r:   s      r;   create_transition_from_dictrA   k   sI     O'$'' O,-=-F-F,Gr!MNNOs   
 	3.3c                 "    | j                         S )z
    Get the JSON schema for a transition class.

    Useful for generating API documentation or frontend forms.

    Args:
        transition_class: The transition class

    Returns:
        JSON schema dictionary
    )model_json_schema)r6   s    r;   get_transition_schemarD      s     --//    c                 $   	  | di | i S # t         $ rz}i }t        |d      rQ|j                         D ]=  }dj                  d |d   D              }||vrg ||<   ||   j	                  |d          ? nt        |      g|d<   |cY d}~S d}~ww xY w)	z
    Validate transition data without creating an instance.

    Args:
        transition_class: The transition class
        data: Data to validate

    Returns:
        Dictionary of field names to error messages (empty if valid)
    errors.c              3   2   K   | ]  }t        |        y w)N)r0   ).0locs     r;   	<genexpr>z+validate_transition_data.<locals>.<genexpr>   s      BcS Bs   rK   msg__root__Nr?   )r-   hasattrrG   joinappendr0   )r6   r=   r:   rG   r   fields         r;   validate_transition_datarS      s     4 	 1h 3 BU5\ BB&$&F5Mu$$U5\2	3 #&a&F:s   	 	BA/B
B
Bc                 ,   | j                   j                  j                         }t        j                  |      }t               }g }|j                         D ]  \  }}	 ddlm}  |       }t        | d      r || ddddt        | dd            nd}	|j                  |	      }
|j                  |
       |j                  ||j                  |
|j                  xs dt!        |j"                  j%                               d        |S # t&        $ r Y w xY w)	z
    Get a summary of the state flow for an entity type.

    This analyzes all registered transitions and builds a flow diagram.

    Args:
        entity: Example entity instance

    Returns:
        List of state flow information
    r   r   pkNr   r    )r   r6   r   descriptionfields)r   r    r!   r   r"   setr#   r'   r   rO   r(   r)   addrQ   r@   __doc__listmodel_fieldskeysr-   )r   r2   r'   statesflowsr   r6   r   
transitionr8   targets              r;   get_entity_state_flowrc      s$    ,,))//1K%@@MK UFE-8->->-@  ))	6)+J 64( "!!%)-"&!%$+F4Et$L    00AFJJvLL'6(8(A(A$*#3#;#;#Ar"#3#@#@#E#E#GH- D L  		s   B&D	DD)NF)r[   loggingtypingr   r   r   r   django.db.modelsr   fsm.registryr   fsm.state_managerr	   fsm.transitionsr
   r   	getLoggerr@   r.   r$   boolr0   r<   rA   rD   rS   rc   r?   rE   r;   <module>rl      s    ( ( " , / E			8	$ "Se S$ SSWX[]abp]qXqSr SlO$~2F OdSVX[S[n Oao O,0D,@ 0T#s(^ 0tN/C 4PSUXPX> ^bcfhlmphqcq^r 85% 5Dc3h,@ 5rE   