o
    i                      @  sz  d Z ddlmZ ddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZmZmZmZmZ ddlmZ ddlZddlZddlZddlZddlZddlmZ ed	Zed
Zedje
jdZ ej!j"d+i ddddddde dg ddddde	ddde de	ddde ddddddd d!d"d#d$dd%ej#j$j%d&dZ&G d'd( d(Z'G d)d* d*Z(dS ),zActivity test environment.    )annotationsN)contextmanager)datetime	timedeltatimezone)AnyCallableOptionalSetTypeVar)	ParamSpec)Client_Params_Return)tzinfoactivity_idtestactivity_typeunknownattempt   current_attempt_scheduled_timeheartbeat_detailsheartbeat_timeoutis_localFschedule_to_close_timeout)secondsscheduled_timestart_to_close_timeoutstarted_time
task_queue
task_tokens   testworkflow_idworkflow_namespacedefaultworkflow_run_idztest-runworkflow_typepriorityretry_policyc                   @  sH   e Zd ZdZddddZejjd	d
fdddZdddZ	dddZ
dS )ActivityEnvironmenta  Activity environment for testing activities.

    This environment is used for running activity code that can access the
    functions in the :py:mod:`temporalio.activity` module. Use :py:meth:`run` to
    run an activity function or any function within an activity context.

    Attributes:
        info: The info that is returned from :py:func:`temporalio.activity.info`
            function.
        on_heartbeat: Function called on each heartbeat invocation by the
            activity.
        payload_converter: Payload converter set on the activity context. This
            must be set before :py:meth:`run`. Changes after the activity has
            started do not take effect.
        metric_meter: Metric meter set on the activity context. This must be set
            before :py:meth:`run`. Changes after the activity has started do not
            take effect. Default is noop.
    NclientOptional[Client]returnNonec                 C  sT   t | _dd | _tjjjj| _tjj	j
| _d| _d| _t | _|| _tj | _dS )z8Create an ActivityEnvironment for running activity code.c                  W  s   d S N argsr/   r/   b/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/testing/_activity.py<lambda>G   s    z.ActivityEnvironment.__init__.<locals>.<lambda>FN)_default_infoinfoon_heartbeat
temporalio	converterDataConverterr$   payload_convertercommonMetricMeternoopmetric_meter
_cancelled_worker_shutdownset_activities_clientactivity"_ActivityCancellationDetailsHolder_cancellation_details)selfr*   r/   r/   r2   __init__D   s   

zActivityEnvironment.__init__T)cancel_requestedcancellation_details/temporalio.activity.ActivityCancellationDetailsc                 C  s0   | j rdS d| _ || j_| jD ]}|  qdS )a2  Cancel the activity.

        Args:
            cancellation_details: details about the cancellation. These will
                be accessible through temporalio.activity.cancellation_details()
                in the activity after cancellation.

        This only has an effect on the first call.
        NT)r?   rF   detailsrB   cancel)rG   rJ   actr/   r/   r2   rM   T   s   

zActivityEnvironment.cancelc                 C  s(   | j rdS d| _ | jD ]}|  qdS )zrNotify the activity that the worker is shutting down.

        This only has an effect on the first call.
        NT)r@   rB   worker_shutdown)rG   rN   r/   r/   r2   rO   j   s   

z#ActivityEnvironment.worker_shutdownfnCallable[_Params, _Return]r1   _Params.argskwargs_Params.kwargsr   c                 O  s   t | || jj|i |S )a   Run the given callable in an activity context.

        Args:
            fn: The function/callable to run.
            args: All positional arguments to the callable.
            kwargs: All keyword arguments to the callable.

        Returns:
            The callable's result.
        )	_ActivityrC   run)rG   rP   r1   rS   r/   r/   r2   rV   u   s   zActivityEnvironment.runr.   )r*   r+   r,   r-   )rJ   rK   r,   r-   r,   r-   )rP   rQ   r1   rR   rS   rT   r,   r   )__name__
__module____qualname____doc__rH   r7   rD   ActivityCancellationDetailsrM   rO   rV   r/   r/   r/   r2   r)   0   s    
r)   c                   @  s4   e Zd Zdd	d
ZdddZdddZdddZdS )rU   envr)   rP   r   r*   r+   r,   r-   c                   s    | _ || _t|pt|j| _d | _| js+tjj	
|}|r$|js+tjj | _tjj fdd fddtjjt | jrEt nd dtjjt | jrUt nd d| js]d n| jj j j| jri|nd  jd	| _d | _d S )Nc                     s    j S r.   )r5   r/   r]   r/   r2   r3      s    z$_Activity.__init__.<locals>.<lambda>c                    s
    j |  S r.   )r6   r0   r^   r/   r2   r3      s   
 )thread_eventasync_event)	r5   	heartbeatcancelled_eventworker_shutdown_eventshield_thread_cancel_exception#payload_converter_class_or_instanceruntime_metric_meterr*   rJ   )r]   rP   inspectiscoroutinefunction__call__is_asynccancel_thread_raiserr7   rD   _Definitionfrom_callableno_thread_cancel_exceptionworker	_activity_ThreadExceptionRaiser_Context_CompositeEvent	threadingEventasyncioshieldedr:   r>   rF   contexttask)rG   r]   rP   r*   defnr/   r^   r2   rH      s@   




z_Activity.__init__r   c                   s   j rt j}|d urj | tfdd jr) fdd}| S    ji W  d    S 1 s?w   Y  d S )Nc               	   3  s     j jr jj   j jr jj   j j  t	j
j j} zd V  W t	j
j|   j j  d S t	j
j|   j j  w r.   )r]   r?   rx   rb   rA   r@   rc   rB   addr7   rD   rr   resetremove)tokenrG   r/   r2   activity_context   s   z'_Activity.run.<locals>.activity_contextc                     sb     # t ji _jjrj  jI d H W  d    S 1 s*w   Y  d S r.   )rv   create_taskrP   ry   r]   r?   rM   r/   r   r1   rS   rG   r/   r2   	run_async   s   

$z _Activity.run.<locals>.run_async)rk   rt   current_threadidentset_thread_idr   rj   rP   )rG   r1   rS   	thread_idr   r/   r   r2   rV      s   
$z_Activity.runc                 C  sT   | j j s| j j  | jr| jtjj | j	r&| j	
 s(| j	  d S d S d S r.   )rx   rb   is_setrA   rk   raise_in_threadr7   
exceptionsCancelledErrorry   donerM   r   r/   r/   r2   rM      s   z_Activity.cancelc                 C  s    | j j s| j j  d S d S r.   )rx   rc   r   rA   r   r/   r/   r2   rO      s   z_Activity.worker_shutdownN)r]   r)   rP   r   r*   r+   r,   r-   )r,   r   rW   )rX   rY   rZ   rH   rV   rM   rO   r/   r/   r/   r2   rU      s
    

.
'
rU   r/   ))r[   
__future__r   rv   rg   rt   
contextlibr   r   r   r   typingr   r   r	   r
   r   typing_extensionsr   temporalio.activityr7   temporalio.commontemporalio.convertertemporalio.exceptionstemporalio.worker._activitytemporalio.clientr   r   r   fromtimestampreplaceutc	_utc_zerorD   Infor;   Priorityr$   r4   r)   rU   r/   r/   r/   r2   <module>   sv    
	


Y