
    Mh%                         d Z ddlZddlZddlmZmZ ddlmZmZm	Z	 ej
        dk     r	ddlmZmZ n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          ZdS )zKernel Provisioner Classes    N)getenvpath)AnyDictList)   
   )
EntryPointentry_points)SingletonConfigurableUnicodedefault   )KernelProvisionerBasec                   H    e Zd ZU dZdZi Zeeef         e	d<   dZ
 edd          Z ed          d	efd
            Zded	df fdZded	efdZdededed	efdZded	efdZded	eeef         fdZd	eeef         fdZed	ee         fd            Zded	efdZ xZS )KernelProvisionerFactorya  
    :class:`KernelProvisionerFactory` is responsible for creating provisioner instances.

    A singleton instance, `KernelProvisionerFactory` is also used by the :class:`KernelSpecManager`
    to validate `kernel_provisioner` references found in kernel specifications to confirm their
    availability (in cases where the kernel specification references a kernel provisioner that has
    not been installed into the current Python environment).

    It's ``default_provisioner_name`` attribute can be used to specify the default provisioner
    to use when a kernel_spec is found to not reference a provisioner.  It's value defaults to
    `"local-provisioner"` which identifies the local provisioner implemented by
    :class:`LocalProvisioner`.
    z"jupyter_client.kernel_provisionersprovisioners JUPYTER_DEFAULT_PROVISIONER_NAMETzIndicates the name of the provisioner to use when no kernel_provisioner
                                       entry is present in the kernelspec.)confighelpdefault_provisioner_namereturnc                 ,    t          | j        d          S )zThe default provisioner name.local-provisioner)r   default_provisioner_name_env)selfs    c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_client/provisioning/factory.py!_default_provisioner_name_defaultz:KernelProvisionerFactory._default_provisioner_name_default-   s     d79LMMM    kwargsNc                      t                      j        di | t                                          D ]}|| j        |j        <   dS )z(Initialize a kernel provisioner factory.N )super__init__r   _get_all_provisionersr   name)r   r    ep	__class__s      r   r$   z!KernelProvisionerFactory.__init__2   sW    ""6"""*@@BB 	, 	,B)+Dbg&&	, 	,r   kernel_specc                     d}|                      |          }t          |                    d                    }|                     |          s(d}| j                            d|j         d| d           |S )aO  
        Reads the associated ``kernel_spec`` to determine the provisioner and returns whether it
        exists as an entry_point (True) or not (False).  If the referenced provisioner is not
        in the current cache or cannot be loaded via entry_points, a warning message is issued
        indicating it is not available.
        Tprovisioner_nameFzKernel 'z(' is referencing a kernel provisioner ('zW') that is not available.  Ensure the appropriate package has been installed and retry.)_get_provisioner_configstrget_check_availabilitylogwarningdisplay_name)r   r)   is_availableprovisioner_cfgr+   s        r   is_provisioner_availablez1KernelProvisionerFactory.is_provisioner_available9   s     "66{CC223EFFGG''(899 	 LHP;3 P P!1P P P  
 r   	kernel_idparentc                    |                      |          }t          |                    d                    }|                     |          sd| d}t	          |          | j                            d|j         d|            | j        |         	                                }|                    d          } |d|||d|}	|	S )	a$  
        Reads the associated ``kernel_spec`` to see if it has a `kernel_provisioner` stanza.
        If one exists, it instantiates an instance.  If a kernel provisioner is not
        specified in the kernel specification, a default provisioner stanza is fabricated
        and instantiated corresponding to the current value of ``default_provisioner_name`` trait.
        The instantiated instance is returned.

        If the provisioner is found to not exist (not registered via entry_points),
        `ModuleNotFoundError` is raised.
        r+   zKernel provisioner 'z' has not been registered.zInstantiating kernel 'z' with kernel provisioner: r   )r6   r)   r7   r"   )
r,   r-   r.   r/   ModuleNotFoundErrorr0   debugr2   r   load)
r   r6   r)   r7   r4   r+   msgprovisioner_classprovisioner_configprovisioners
             r   create_provisioner_instancez4KernelProvisionerFactory.create_provisioner_instanceL   s    66{CC223EFFGG''(899 	+U)9UUUC%c***6[%= 6 6#36 6	
 	
 	
 !-.>?DDFF,00::->-> .
[.
 .
K].
 .
 r   r+   c                     d}|| j         vr3	 |                     |          }|| j         |<   n# t          $ r d}Y nw xY w|S )a.  
        Checks that the given provisioner is available.

        If the given provisioner is not in the current set of loaded provisioners an attempt
        is made to fetch the named entry point and, if successful, loads it into the cache.

        :param provisioner_name:
        :return:
        TF)r   _get_provisioner	Exception)r   r+   r3   r'   s       r   r/   z,KernelProvisionerFactory._check_availabilityj   sk     4#444%**+;<<68!"233 % % %$%s   - <<c                     |j                             di           }d|v rd|vr|                    di i           |S | j        i dS )a
  
        Return the kernel_provisioner stanza from the kernel_spec.

        Checks the kernel_spec's metadata dictionary for a kernel_provisioner entry.
        If found, it is returned, else one is created relative to the DEFAULT_PROVISIONER
        and returned.

        Parameters
        ----------
        kernel_spec : Any - this is a KernelSpec type but listed as Any to avoid circular import
            The kernel specification object from which the provisioner dictionary is derived.

        Returns
        -------
        dict
            The provisioner portion of the kernel_spec.  If one does not exist, it will contain
            the default information.  If no `config` sub-dictionary exists, an empty `config`
            dictionary will be added.
        kernel_provisionerr+   r   )r+   r   )metadatar.   updater   )r   r)   env_provisioners      r   r,   z0KernelProvisionerFactory._get_provisioner_config}   sb    ( &.223GLL00//&&"~666""$($ARPPPr   c                 \    i }| j                                         D ]\  }}|j        ||<   |S )z
        Returns a dictionary of provisioner entries.

        The key is the provisioner name for its entry point.  The value is the colon-separated
        string of the entry point's module name and object name.
        )r   itemsvalue)r   entriesr&   r'   s       r   get_provisioner_entriesz0KernelProvisionerFactory.get_provisioner_entries   s=     )//11 	% 	%HD"HGDMMr   c                  6    t          t          j                  S )zaWrapper around entry_points (to fetch the set of provisioners) - primarily to facilitate testing.)group)r   r   
GROUP_NAMEr"   r   r   r%   z.KernelProvisionerFactory._get_all_provisioners   s     ":"EFFFFr   r&   c                 6   t          t          j        |          }|r|d         S |dk    rnt          j        t	          j        t	          j        t                               d          }| j                            d|            t          ddd          S  )z^Wrapper around entry_points (to fetch a single provisioner) - primarily to facilitate testing.)rO   r&   r   r   z-*a  Kernel Provisioning: The 'local-provisioner' is not found.  This is likely due to the presence of multiple jupyter_client distributions and a previous distribution is being used as the source for entrypoints - which does not include 'local-provisioner'.  That distribution should be removed such that only the version-appropriate distribution remains (version >= 7).  Until then, a 'local-provisioner' entrypoint will be automatically constructed and used.
The candidate distribution locations are: zjupyter_client.provisioningLocalProvisioner)
r   r   rP   globr   dirname__file__r0   r1   r
   )r   r&   epsdistross       r   rB   z)KernelProvisionerFactory._get_provisioner   s    !9!D4PPP 	q6M &&&i4<X0F0F#G#G K K KLLGHR IPR R   #%BDV   	r   )__name__
__module____qualname____doc__rP   r   r   r-   r
   __annotations__r   r   r   r   r   r   r$   boolr5   r   r@   r/   r,   rM   staticmethodr   r%   rB   __classcell__)r(   s   @r   r   r      s          6J*,L$sJ',,,#E &wN      W'((N3 N N N )(N, , , , , , , ,C D    &+.8;	   <C D    &Q3 Q4S> Q Q Q Q:
c3h 
 
 
 
 G4
#3 G G G \GS Z        r   r   )r[   rS   sysosr   r   typingr   r   r   version_infoimportlib_metadatar
   r   importlib.metadatatraitlets.configr   r   r   provisioner_baser   r   r"   r   r   <module>rh      s        



         " " " " " " " " " " g;;;;;;;;;;;;;;;;; D D D D D D D D D D 3 3 3 3 3 3t t t t t4 t t t t tr   