
    i                        S SK Jr  S SKrS SKJrJr  S SKJrJrJ	r	  S SK
Jr  S SKJr   " S S\5      r " S	 S
\5      rSS jrSS jr " S S\5      rg)    )annotationsN)AnyType)	BaseModelValidationErrormodel_validator)!import_module_from_qualified_name)Eventc                  v    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   / 4SS jjr\ S     SS jj5       rSr	g)EventEnvelopeWithMetadata   z
Client readable representation of an Event. Includes class metadata in order to support
matching event types semantically in an extendable manner (e.g. "StartEvent", "StopEvent", etc.).
zdict[str, Any]value
str | Nonequalified_namestrtypelist[str] | Nonetypesc                    U Vs0 s H  o"R                   U_M     nn[        U R                  U R                  U R                  S9R                  5       n[        R                  XCS9$ s  snf )z
Attempts to load the event data as a python class based on the envelope metadata.
Looks up the event from the registry, if provided. Falls back to the qualified_name, attempting to load from the module path.
)r   r   r   )client_dataregistry)__name__EventEnveloper   r   r   
model_dumpparse)selfr   eregistry_lookupas_event_envelopes        `/home/james-whalen/.local/lib/python3.13/site-packages/workflows/protocol/serializable_events.py
load_event$EventEnvelopeWithMetadata.load_event   si    
 3;;(Q::q=(;)**499T=P=P

*, 	 "") # 
 	
	 <s   A'c           	         UR                  SS9n[        UU(       a  [        [        U5      5      OS[	        [        U5      5      [        U5      R
                  S9nU$ )z
Build a backward-compatible envelope for an Event, preserving existing
fields (e.g., qualified_name, value) while adding metadata useful for
type-safe clients.

jsonmodeN)r   r   r   r   )r   r   _get_qualified_namer   _get_event_subtypesr   )clseventinclude_qualified_namer   envelopes        r    
from_event$EventEnvelopeWithMetadata.from_event*   sX       f -,% /tE{;%d5k2e%%
      N)r   zlist[Type[Event]]returnr
   )T)r*   r
   r+   boolr1   r   )
r   
__module____qualname____firstlineno____doc____annotations__r!   classmethodr-   __static_attributes__r0   r/   r    r   r      s[    
   I79 
 :>37	" r/   r   c                      \ rS rSr% SrS\S'   SrS\S'   SrS\S'   \" S	S
9\	SS j5       5       r
\	SS j5       r\	  S       SS jj5       rSrg)r   B   z
Client write representation of an Event. Includes class metadata in order to support
matching event types semantically in an extendable manner (e.g. "StartEvent", "StopEvent", etc.).
z
Any | Noner   Nr   r   r   beforer%   c                    [        U[        5      (       a   [        R                  " U5      n[        U[
        5      (       a  SU;  a  SU;   a  0 UESUS   0EnU$ ! [        R                   a     NDf = f)Nr   data)
isinstancer   r$   loadsJSONDecodeErrordict)r)   r>   s     r    _format_compatibility#EventEnvelope._format_compatibilityL   su     dC  zz$' dD!!d"v~6$6f6 '' s   A A21A2c                P    U " UR                  SS9[        U5      R                  S9$ )Nr$   r%   )r   r   )r   r   r   )r)   r*   s     r    r-   EventEnvelope.from_eventZ   s-    """/e%%
 	
r/   c           	        U=(       d    0 n/ n [        U[        5      (       a  [        R                  " U5      OUn[        U[
        5      (       d  [        S5      eSU;  =(       d    SU;  =(       a    SU;  nU(       a8  U(       a1  UR                  U;  a  0 UEUR                  U0EnUR                  US.n [        R                  U5      nUR                  (       av  UR                  U;  a?  UR                  SUR                   SSR                  UR                  5       5       35        O'X'R                     R                  UR                  5      $ UR                  (       ae  [!        UR                  5      n[#        U[$        5      (       d   UR                  S	UR                   S
35        OUR                  UR                  5      $  U(       a  UOS/n[        SR                  U5      5      e! [        R                   a    Un GNf = f! [&         a'  n	UR                  S[        U	5       35         Sn	A	NoSn	A	ff = f)ao  
Parse client data into an Event. Raises an EventValidationError if the client data is invalid.

Args:
    client_data: The client data to parse. Can be a dictionary, a string, or an explicit Event class.
    registry: The registry of event type names to Event classes.
    explicit_event: An explicit Event class to treat the dict as

Returns:
    The parsed Event.
zNFailed to deserialize event. Must be a json object, or stringified json objectr   r   r   )r   r   zInvalid event type: z. Expected one of z, z$Invalid client data. Qualified name z) does not correspond to an Event subclasszFailed to deserialize event: NzFInvalid client data. Must have a type or a qualified name, got {event} )r?   r   r$   r@   rA   rB   EventValidationErrorr   r   model_validater   appendjoinkeysr   r   r	   
issubclassr
   r   )
r)   r   r   explicit_eventerrorsas_dictmissing_qualifiersr*   module_classr   s
             r    r   EventEnvelope.parsea   s   $ >r	"+5k3+G+G

;'[ 
 '4((&`  G+DvW/D%W$ 	 .&&h6PhP(?(?P&// G	D!009Ezz::X-MM.uzzl:LTYYW_WdWdWfMgLhi $JJ/>>u{{KK##@AUAUV!,66MM>u?S?S>TT}~ (66u{{CC $   Y 	 #388F#344W ## 	"!G	"D  	DMM9#a&BCC	Ds7   -G2 9BH AH 0H 2HH
I H;;I r0   )r>   r   r1   r   )r*   r
   r1   r   )NN)r   zdict[str, Any] | strr   zdict[str, Type[Event]] | NonerO   zType[Event] | Noner1   r
   )r   r3   r4   r5   r6   r7   r   r   r   r8   rC   r-   r   r9   r0   r/   r    r   r   B   s    
 D*!%NJ%(#
  $
 
 
  37-1	B5)B5 0B5 +	B5
 
B5 B5r/   r   c                    / nU R                  5       SS  H@  nU[        L a    O6[        U[        5      (       d  M%  UR                  UR                  5        MB     U(       d  gU$ )zg
Traverses the MRO (Module Resolution Order) of a class and returns the list of only Event subclasses.
   N)mror
   rN   rK   r   )r)   namescs      r    r(   r(      sS     EWWYqr]:aLL$	 
 Lr/   c                8    U R                    SU R                   3$ )N.)r3   r   )r*   s    r    r'   r'      s    q 011r/   c                      \ rS rSrSrSrg)rI      z'Raised when the client data is invalid.r0   N)r   r3   r4   r5   r6   r9   r0   r/   r    rI   rI      s    1r/   rI   )r)   Type[Event]r1   r   )r*   r^   r1   r   )
__future__r   r$   typingr   r   pydanticr   r   r   workflows.context.utilsr	   workflows.eventsr
   r   r   r(   r'   	ExceptionrI   r0   r/   r    <module>re      sN    #   @ @ E "1	 1hb5I b5J 229 2r/   