o
    i                     @  s   d dl mZ d dlZd dlZd dlZd dlmZmZm	Z	 d dl
Z
d dlZd dlZer.d dlZeeZedZdZdZd"ddZd#ddZd$ddZd%ddZd&ddZd'ddZd'd d!ZdS )(    )annotationsN)TYPE_CHECKINGAnyOptionalz]^/namespaces/(?P<namespace>[^/]+)/workflows/(?P<workflow_id>[^/]+)/(?P<run_id>[^/]+)/history$eventID	eventTypehandle*temporalio.client.WorkflowHandle[Any, Any]return+temporalio.api.common.v1.Link.WorkflowEventc                 C  sZ   | j du rtd|  dtjjjjj| jj	| j
| j tjjjjjjdtjjjjjddS )z?Create a WorkflowEvent link corresponding to a started workflowNzWorkflow handle zR has no first execution run ID. Cannot create WorkflowExecutionStarted event link.   )event_id
event_type	namespaceworkflow_idrun_id	event_ref)first_execution_run_id
ValueError
temporalioapicommonv1LinkWorkflowEvent_clientr   idEventReferenceenums	EventType%EVENT_TYPE_WORKFLOW_EXECUTION_STARTED)r    r"   g/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/nexus/_link_conversion.py:workflow_execution_started_event_link_from_workflow_handle   s   

r$   workflow_eventnexusrpc.Linkc              	   C  sv   d}t j| j}t j| j}t j| j}d| d| d| d}t| j}tj	t j
|d|d|df| jjdS )zConvert a WorkflowEvent link into a nexusrpc link

    Used when propagating links from a StartWorkflow response to a Nexus start operation
    response.
    temporalz/namespaces/z/workflows//z/history )urltype)urllibparsequoter   r   r    _event_reference_to_query_paramsr   nexusrpcr   
urlunparse
DESCRIPTOR	full_name)r%   schemer   r   r   pathquery_paramsr"   r"   r#   workflow_event_to_nexus_link2   s   
r7   link5Optional[temporalio.api.common.v1.Link.WorkflowEvent]c              
   C  s   t j| j}t|j}|std|  dtj	  dS zt
|j}W n tyC } ztd|  d| d W Y d}~dS d}~ww | }tjjjjjt j|d t j|d t j|d	 |d
S )zConvert a nexus link into a WorkflowEvent link

    This is used when propagating links from a Nexus start operation request to a
    StartWorklow request.
    zInvalid Nexus link: z. Expected path to match NzFFailed to parse event reference from Nexus link URL query parameters: z ()r   r   r   r   )r,   r-   urlparser*   _LINK_URL_PATH_REGEXmatchr5   loggerwarningpattern _query_params_to_event_referencequeryr   	groupdictr   r   r   r   r   r   unquote)r8   r*   r=   r   errgroupsr"   r"   r#   nexus_link_to_workflow_eventF   s.   rG   r   :temporalio.api.common.v1.Link.WorkflowEvent.EventReferencestrc                 C  sB   t jjjj| j}|drt|	d}t
j| j|ddS )NEVENT_TYPE_r   )r   r   referenceType)r   r   r   r   r    Namer   
startswith(_event_type_constant_case_to_pascal_caseremoveprefixr,   r-   	urlencoder   )r   event_type_namer"   r"   r#   r/   f   s   
r/   raw_query_paramsc                 C  s   t j| }|dpdg\}|dkrtd| |tp!dg\}|s,td| |dr4|}ntd|rAdt	| }ntd| d	}|t
pQdg\}|rjzt|}W n tyi   td
| w tjjjjjjtjjjj||dS )zCReturn an EventReference from the query params or raise ValueError.rK   r)   r   zTExpected Nexus link URL query parameter referenceType to be EventReference but got: z(query params do not contain event type: rJ   z
[A-Z][a-z]zInvalid event type name: r   z'Query params contain invalid event id: )r   r   )r,   r-   parse_qsgetr   LINK_EVENT_TYPE_PARAM_NAMErM   rer=   (_event_type_pascal_case_to_constant_caseLINK_EVENT_ID_PARAM_NAMEintr   r   r   r   r   r   r   r   r    Value)rR   r6   reference_typeraw_event_type_namerQ   r   raw_event_idr"   r"   r#   rA   w   s8   
rA   sc                 C  s   t ddd |  S )zConvert a CONSTANT_CASE string to PascalCase.

    >>> _event_type_constant_case_to_pascal_case("NEXUS_OPERATION_SCHEDULED")
    "NexusOperationScheduled"
    z(\b|_)([a-z])c                 S  s   |   d  S )Nr   )rF   upper)mr"   r"   r#   <lambda>   s    z:_event_type_constant_case_to_pascal_case.<locals>.<lambda>)rV   sublowerr^   r"   r"   r#   rN      s   rN   c                 C  s   t dd| d S )zConvert a PascalCase string to CONSTANT_CASE.

    >>> _event_type_pascal_case_to_constant_case("NexusOperationScheduled")
    "NEXUS_OPERATION_SCHEDULED"
    z([A-Z])z_\1_)rV   rb   lstripr_   rd   r"   r"   r#   rW      s   rW   )r   r	   r
   r   )r%   r   r
   r&   )r8   r&   r
   r9   )r   rH   r
   rI   )rR   rI   r
   rH   )r^   rI   r
   rI   )
__future__r   loggingrV   urllib.parser,   typingr   r   r   r0   temporalio.api.common.v1r   temporalio.api.enums.v1temporalio.client	getLogger__name__r>   compiler<   rX   rU   r$   r7   rG   r/   rA   rN   rW   r"   r"   r"   r#   <module>   s.    




 

'	