o
    2wiz&                     @  s,  d 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Zddl	Z	ddl
Z
ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ejjZd	e	jd  Zeeejd
Z				d1d2ddZ	d3d4ddZ				d5d6ddZdd eddddd fd7d,d-Z G d.d/ d/eZ!e"d0kre!#  dS dS )8z#The IPython kernel spec for Jupyter    )annotationsN)Path)Any)KernelSpecManager)Unicode)Applicationzpython%i	resourcesipykernel_launchermodstr
executable
str | Noneextra_argumentslist[str] | Nonepython_argumentsreturn	list[str]c                 C  s<   |du rt j}|p
g }|pg }|g|d| dd|S )a  Build Popen command list for launching an IPython kernel.

    Parameters
    ----------
    mod : str, optional (default 'ipykernel')
        A string of an IPython module whose __main__ starts an IPython kernel
    executable : str, optional (default sys.executable)
        The Python executable to use for the kernel process.
    extra_arguments : list, optional
        A list of extra arguments to pass when executing the launch code.

    Returns
    -------
    A Popen command list
    Nz-mz-fz{connection_file})sysr   )r
   r   r   r    r   Q/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/ipykernel/kernelspec.pymake_ipkernel_cmd   s
   r   dict[str, Any]c                 C  s&   t | |ddtjd  dddiddS )	zConstruct dict for kernel.jsonr   r   zPython %i (ipykernel)r   pythondebuggerTz5.5)argvdisplay_namelanguagemetadatakernel_protocol_version)r   r   version_infor   r   r   r   get_kernel_dict;   s   r!   pathPath | str | None	overridesdict[str, Any] | Nonec                 C  s   | du rt tjddt } tt|  t |  j}|tj	@ s*t | 
|tj	B  t||}|r6|| tt| dd}tj||dd W d   t| S 1 sTw   Y  t| S )zWrite a kernel spec directory to `path`

    If `path` is not specified, a temporary directory is created.
    If `overrides` is given, the kernelspec JSON is updated before writing.

    The path to the kernelspec is always returned.
    N_kernels)suffixzkernel.jsonw   )indent)r   tempfilemkdtempKERNEL_NAMEshutilcopytree	RESOURCESstatst_modeS_IWUSRchmodr!   updateopenpjoinjsondumpr   )r"   r$   r   r   maskkernel_dictfr   r   r   write_kernel_specJ   s   



r=   Fkernel_spec_managerKernelSpecManager | Noneuserboolkernel_namer   prefixprofileenvdict[str, str] | Nonefrozen_modulesc                 C  s   | du rt  } |du ri }|tkr|du r|}i }|r||d< |r3d|g}	|s2dtjd |f |d< nd}	d}
tjdkrPt dkrP|sHdg}
nd	|vrPd
|d	< |rV||d< t||	|
d}| j||||d}t	| |S )a  Install the IPython kernelspec for Jupyter

    Parameters
    ----------
    kernel_spec_manager : KernelSpecManager [optional]
        A KernelSpecManager to use for installation.
        If none provided, a default instance will be created.
    user : bool [default: False]
        Whether to do a user-only install, or system-wide.
    kernel_name : str, optional
        Specify a name for the kernelspec.
        This is needed for having multiple IPython kernels for different environments.
    display_name : str, optional
        Specify the display name for the kernelspec
    profile : str, optional
        Specify a custom profile to be loaded by the kernel.
    prefix : str, optional
        Specify an install prefix for the kernelspec.
        This is needed to install into a non-default location, such as a conda/virtual-env.
    env : dict, optional
        A dictionary of extra environment variables for the kernel.
        These will be added to the current environment variables before the
        kernel is started
    frozen_modules : bool, optional
        Whether to use frozen modules for potentially faster kernel startup.
        Using frozen modules prevents debugging inside of some built-in
        Python modules, such as io, abc, posixpath, ntpath, or stat.
        The frozen modules are used in CPython for faster interpreter startup.
        Ignored for cPython <3.11 and for other Python implementations.

    Returns
    -------
    The path where the kernelspec was installed.
    Nr   	--profilezPython %i [profile=%s]r   )      CPythonz-Xfrozen_modules=offPYDEVD_DISABLE_FILE_VALIDATION1rE   )r$   r   r   )rB   r@   rC   )
r   r-   r   r    platformpython_implementationr=   install_kernel_specr.   rmtree)r>   r@   rB   r   rC   rD   rE   rG   r$   r   r   r"   destr   r   r   installm   s>   ,
rS   c                   @  s.   e Zd ZdZedZdddd	Zdd
dZdS )InstallIPythonKernelSpecAppzDummy app wrapping argparsezipython-kernel-installNr   r   r   Nonec                 C  s    |du rt jdd }|| _dS )zInitialize the app.Nr)   )r   r   )selfr   r   r   r   
initialize   s   
z&InstallIPythonKernelSpecApp.initializec              
   C  sZ  ddl }|j| jdd}|jdddd |jd	ttd
d |jdtdd |jdtdd |jdtdd |jddtjddtj d |jdddddd |jdddd || j	}|j
rdt|j
|_
zt|j|j|j|j|j|j
d }W n) ty } z|jtjkrt|tjd! |jrtd"tjd! | d#  d}~ww td$|j d%|  dS )&zStart the app.r   Nz Install the IPython kernel spec.)progdescriptionz--user
store_truez3Install for the current user instead of system-wide)actionhelpz--namezdSpecify a name for the kernelspec. This is needed to have multiple IPython kernels at the same time.)typedefaultr\   z--display-namezdSpecify the display name for the kernelspec. This is helpful when you have multiple IPython kernels.)r]   r\   rH   z]Specify an IPython profile to load. This can be used to create custom versions of the kernel.z--prefixzSpecify an install prefix for the kernelspec. This is needed to install into a non-default location, such as a conda/virtual-env.z--sys-prefixstore_constrC   z[Install to Python's sys.prefix. Shorthand for --prefix='%s'. For use in conda/virtual-envs.)r[   constrR   r\   z--envappend   )ENVVALUEz)Set environment variables for the kernel.)r[   nargsmetavarr\   z--frozen_moduleszEnable frozen modules for potentially faster startup. This has a downside of preventing the debugger from navigating to certain built-in modules.)r@   rB   rD   rC   r   rE   )filez$Perhaps you want `sudo` or `--user`?r)   zInstalled kernelspec z in )argparseArgumentParsernameadd_argumentr   r-   r   rC   
parse_argsr   rE   dictrS   r@   rD   r   OSErrorerrnoEACCESprintstderrexit)rV   rh   parseroptsrR   er   r   r   start   s   

z!InstallIPythonKernelSpecApp.start)N)r   r   r   rU   )r   rU   )__name__
__module____qualname____doc__r   rj   rW   rw   r   r   r   r   rT      s
    rT   __main__)r	   NNN)
r
   r   r   r   r   r   r   r   r   r   )NN)r   r   r   r   r   r   )NNNN)
r"   r#   r$   r%   r   r   r   r   r   r   )r>   r?   r@   rA   rB   r   r   r   rC   r   rD   r   rE   rF   rG   rA   r   r   )$r{   
__future__r   ro   r8   osrN   r.   r1   r   r+   pathlibr   typingr   jupyter_client.kernelspecr   	traitletsr   traitlets.configr   r"   joinr7   r    r-   __file__parentr0   r   r!   r=   rS   rT   rx   launch_instancer   r   r   r   <module>   sV    $][