
    bia)                         d Z ddlZddlmZmZmZ ddlmZmZ ddl	m
Z
mZmZ ddlmZ  G d d	e ee            Z G d
 deee      Zy)zKernel Provisioner Classes    N)ABCABCMetaabstractmethod)AnyUnion)InstanceLoggingConfigurableUnicode   )KernelConnectionInfoc                       e Zd Zy)KernelProvisionerMetaN)__name__
__module____qualname__     g/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jupyter_client/provisioning/provisioner_base.pyr   r      s    r   r   c                   *   e Zd ZU dZ edd      Zeed<    edd      Z	e
eef   ed<   i Zeed<   eed	efd
              Zed	edz  fd       Zed	edz  fd       Zeded	dfd       Zed%ded	dfd       Zed%ded	dfd       Zedee   ded	efd       Zed%ded	dfd       Zd%ded	dfdZded	eeef   fdZded	dfdZd	eeef   fdZded	dfdZ d&de!d	e!fdZ"d'de!d	e!fdZ#ded	edz  fd Z$d!eeef   d	dfd"Z%d#eeef   d	eeef   fd$Z&y)(KernelProvisionerBasea  
    Abstract base class defining methods for KernelProvisioner classes.

    A majority of methods are abstract (requiring implementations via a subclass) while
    some are optional and others provide implementations common to all instances.
    Subclasses should be aware of which methods require a call to the superclass.

    Many of these methods model those of :class:`subprocess.Popen` for parity with
    previous versions where the kernel process was managed directly.
    z$jupyter_client.kernelspec.KernelSpecT)
allow_nonekernel_specN	kernel_idconnection_inforeturnc                      y)z
        Returns true if this provisioner is currently managing a process.

        This property is asserted to be True immediately following a call to
        the provisioner's :meth:`launch_kernel` method.
        Nr   selfs    r   has_processz!KernelProvisionerBase.has_process#   s     	r   c                    K   yw)z
        Checks if kernel process is still running.

        If running, None is returned, otherwise the process's integer-valued exit code is returned.
        This method is called from :meth:`KernelManager.is_alive`.
        Nr   r   s    r   pollzKernelProvisionerBase.poll.         	   c                    K   yw)z
        Waits for kernel process to terminate.

        This method is called from `KernelManager.finish_shutdown()` and
        `KernelManager.kill_kernel()` when terminating a kernel gracefully or
        immediately, respectively.
        Nr   r   s    r   waitzKernelProvisionerBase.wait8   s      	r#   signumc                    K   yw)z
        Sends signal identified by signum to the kernel process.

        This method is called from `KernelManager.signal_kernel()` to send the
        kernel process a signal.
        Nr   )r   r&   s     r   send_signalz!KernelProvisionerBase.send_signalC   r"   r#   restartc                    K   yw)aL  
        Kill the kernel process.

        This is typically accomplished via a SIGKILL signal, which cannot be caught.
        This method is called from `KernelManager.kill_kernel()` when terminating
        a kernel immediately.

        restart is True if this operation will precede a subsequent launch_kernel request.
        Nr   r   r)   s     r   killzKernelProvisionerBase.killM   s      	r#   c                    K   yw)a  
        Terminates the kernel process.

        This is typically accomplished via a SIGTERM signal, which can be caught, allowing
        the kernel provisioner to perform possible cleanup of resources.  This method is
        called indirectly from `KernelManager.finish_shutdown()` during a kernel's
        graceful termination.

        restart is True if this operation precedes a start launch_kernel request.
        Nr   r+   s     r   	terminatezKernelProvisionerBase.terminateZ   s      	r#   cmdkwargsc                    K   yw)z
        Launch the kernel process and return its connection information.

        This method is called from `KernelManager.launch_kernel()` during the
        kernel manager's start kernel sequence.
        Nr   )r   r/   r0   s      r   launch_kernelz#KernelProvisionerBase.launch_kernelh   r"   r#   c                    K   yw)a"  
        Cleanup any resources allocated on behalf of the kernel provisioner.

        This method is called from `KernelManager.cleanup_resources()` as part of
        its shutdown kernel sequence.

        restart is True if this operation precedes a start launch_kernel request.
        Nr   r+   s     r   cleanupzKernelProvisionerBase.cleanupr         	r#   c                    K   yw)a  
        Allows the provisioner to determine if the kernel's shutdown has been requested.

        This method is called from `KernelManager.request_shutdown()` as part of
        its shutdown sequence.

        This method is optional and is primarily used in scenarios where the provisioner
        may need to perform other operations in preparation for a kernel's shutdown.
        Nr   r+   s     r   shutdown_requestedz(KernelProvisionerBase.shutdown_requested~   r5   r#   c                    K   |j                  dt        j                        j                         }|j	                  | j                  |             | j                  |       ||d<   |S w)a(  
        Perform any steps in preparation for kernel process launch.

        This includes applying additional substitutions to the kernel launch command
        and environment. It also includes preparation of launch parameters.

        NOTE: Subclass implementations are advised to call this method as it applies
        environment variable substitutions from the local environment and calls the
        provisioner's :meth:`_finalize_env()` method to allow each provisioner the
        ability to cleanup the environment variables that will be used by the kernel.

        This method is called from `KernelManager.pre_start_kernel()` as part of its
        start kernel sequence.

        Returns the (potentially updated) keyword arguments that are passed to
        :meth:`launch_kernel()`.
        env)poposenvironcopyupdate/_KernelProvisionerBase__apply_env_substitutions_finalize_env)r   r0   r9   s      r   
pre_launchz KernelProvisionerBase.pre_launch   sX     $ jj

+002

411#673us   A'A)c                    K   yw)z
        Perform any steps following the kernel process launch.

        This method is called from `KernelManager.post_start_kernel()` as part of its
        start kernel sequence.
        Nr   )r   r0   s     r   post_launchz!KernelProvisionerBase.post_launch   s      	r#   c                 N   K   i }| j                   |d<   | j                  |d<   |S w)a  
        Captures the base information necessary for persistence relative to this instance.

        This enables applications that subclass `KernelManager` to persist a kernel provisioner's
        relevant information to accomplish functionality like disaster recovery or high availability
        by calling this method via the kernel manager's `provisioner` attribute.

        NOTE: The superclass method must always be called first to ensure proper serialization.
        r   r   r   r   r   provisioner_infos     r   get_provisioner_infoz*KernelProvisionerBase.get_provisioner_info   s5      ,.(,%.2.B.B*+s   #%rG   c                 4   K   |d   | _         |d   | _        yw)a  
        Loads the base information necessary for persistence relative to this instance.

        The inverse of `get_provisioner_info()`, this enables applications that subclass
        `KernelManager` to re-establish communication with a provisioner that is managing
        a (presumably) remote kernel from an entirely different process that the original
        provisioner.

        NOTE: The superclass method must always be called first to ensure proper deserialization.
        r   r   NrE   rF   s     r   load_provisioner_infoz+KernelProvisionerBase.load_provisioner_info   s"      *+6/0ABs   recommendedc                     |S )aE  
        Returns the time allowed for a complete shutdown. This may vary by provisioner.

        This method is called from `KernelManager.finish_shutdown()` during the graceful
        phase of its kernel shutdown sequence.

        The recommended value will typically be what is configured in the kernel manager.
        r   r   rK   s     r   get_shutdown_wait_timez,KernelProvisionerBase.get_shutdown_wait_time   s
     r   c                     |S )z
        Returns the expected upper bound for a kernel (re-)start to complete.
        This may vary by provisioner.

        The recommended value will typically be what is configured in the kernel restarter.
        r   rM   s     r   get_stable_start_timez+KernelProvisionerBase.get_stable_start_time   s
     r   pathc                      y)a1  
        Returns the path resolved relative to kernel working directory.

        For example, path `my_code.py` for a kernel started in `/tmp/`
        should result in `/tmp/my_code.py`, while path `~/test.py` for
        a kernel started in `/home/my_user/` should resolve to the
        (fully specified) `/home/my_user/test.py` path.

        The provisioner may choose not to resolve any paths, or restrict
        the resolution to paths local to the kernel working directory
        to prevent path traversal and exposure of file system layout.
        Nr   )r   rQ   s     r   resolve_pathz"KernelProvisionerBase.resolve_path   s     r   r9   c                     | j                   j                  rG| j                   j                  j                         j                  d      r|j	                  dd       yyy)z
        Ensures env is appropriate prior to launch.

        This method is called from `KernelProvisionerBase.pre_launch()` during the kernel's
        start sequence.

        NOTE: Subclasses should be sure to call super()._finalize_env(env)
        pythonPYTHONEXECUTABLEN)r   languagelower
startswithr:   )r   r9   s     r   r@   z#KernelProvisionerBase._finalize_env   sP     $$)9)9)B)B)H)H)J)U)UV^)_ GG&- *`$r   substitution_valuesc                     i }| j                   r\ddlm} | j                   j                  }|j	                         D ]-  \  }}|j                  | ||      j                  |      i       / |S )aq  
        Walks entries in the kernelspec's env stanza and applies substitutions from current env.

        This method is called from `KernelProvisionerBase.pre_launch()` during the kernel's
        start sequence.

        Returns the substituted list of env entries.

        NOTE: This method is private and is not intended to be overridden by provisioners.
        r   )Template)r   stringr\   r9   itemsr>   safe_substitute)r   rZ   substituted_envr\   templated_envkvs          r   __apply_env_substitutionsz/KernelProvisionerBase.__apply_env_substitutions   so     '
 !,,00M%++- ^1&&8A;+F+FGZ+['\]^r   )F)g      @)g      $@)'r   r   r   __doc__r   r   r   __annotations__r
   r   r   strr   r   propertyr   boolr   intr!   r%   r(   r,   r.   listr2   r4   r7   dictrA   rC   rH   rJ   floatrN   rP   rS   r@   r?   r   r   r   r   r      sA   	   FSWXKX%,Td%CIuS'\"C,.O).T    C$J   C$J       
$ 
4 
 
 t    tCy C DX   	T 	d 	 	
 
 
 c3h 2# $  DcN  CD CT C	% 	% 	 %  t .c3h .D .T#s(^ PTUXZ]U]P^ r   r   )	metaclass)re   r;   abcr   r   r   typingr   r   traitlets.configr   r	   r
   connectr   typer   r   r   r   r   <module>rt      sG      
 , ,  C C *	GT*=%> 	}C!4@U }r   