a
    ø£Bel   ã                
   @  sX  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
mZ ddlmZ ddlmZ zddlmZ W n ey’   d	ZY n0 ejjZd
ejd  Zeej e¡dƒZd&dddddœdd„Zd'dddœdd„Zd(dddddœdd„Zdd	eddddfdddddddddœd d!„Zdd"lm Z  G d#d$„ d$e ƒZ!e"d%k�rTe! #¡  dS ))z#The IPython kernel spec for Jupyteré    )ÚannotationsN)ÚAny)ÚKernelSpecManager)ÚUnicodeé   )Ú_is_debugpy_availableFzpython%iÚ	resourcesÚipykernel_launcherÚstrz
str | Noneúlist[str] | Nonez	list[str])ÚmodÚ
executableÚextra_argumentsÚreturnc                 C  s2   |du rt j}|pg }|d| ddg}| |¡ |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   Úextend)r   r   r   Ú	arguments© r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/ipykernel/kernelspec.pyÚmake_ipkernel_cmd!   s    
r   zdict[str, Any])r   r   c                 C  s"   t | d�dtjd  ddtidœS )zConstruct dict for kernel.json©r   zPython %i (ipykernel)r   ÚpythonÚdebugger)ÚargvÚdisplay_nameÚlanguageÚmetadata)r   r   Úversion_infor   r   r   r   r   Úget_kernel_dict>   s
    ür   zdict[str, Any] | None)ÚpathÚ	overridesr   r   c                 C  s¬   | du rt j tjdd�t¡} t t| ¡ t  	| ¡j
}|t	j@ sRt  | |t	jB ¡ t|ƒ}|rh| |¡ tt| dƒdƒ� }tj||dd� W d  ƒ n1 sž0    Y  | S )zïWrite 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.
    NZ_kernels)Úsuffixzkernel.jsonÚwr   )Úindent)Úosr   ÚjoinÚtempfileÚmkdtempÚKERNEL_NAMEÚshutilÚcopytreeÚ	RESOURCESÚstatÚst_modeÚS_IWUSRÚchmodr   ÚupdateÚopenÚpjoinÚjsonÚdump)r   r    r   ÚmaskZkernel_dictÚfr   r   r   Úwrite_kernel_specH   s    

.r7   zKernelSpecManager | NoneÚboolzdict[str, str] | None)Úkernel_spec_managerÚuserÚkernel_namer   ÚprefixÚprofileÚenvr   c                 C  s–   | du rt ƒ } |tkr"|du r"|}i }|r2||d< |rZd|g}|s^dtjd |f |d< nd}|rj||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

    Returns
    -------
    The path where the kernelspec was installed.
    Nr   ú	--profilezPython %i [profile=%s]r   r>   )r    r   )r;   r:   r<   )r   r(   r   r   r7   Zinstall_kernel_specr)   Úrmtree)r9   r:   r;   r   r<   r=   r>   r    r   r   Údestr   r   r   Úinstallj   s(    %ÿ
rB   )ÚApplicationc                   @  s8   e Zd ZdZedƒZddddœdd„Zdd	œd
d„ZdS )ÚInstallIPythonKernelSpecAppzDummy app wrapping argparsezipython-kernel-installNr   ÚNone)r   r   c                 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.initialize)r   c              
   C  s\  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	¡}|j
r¸t|j
ƒ|_
z$t|j|j|j|j|j|j
d�}W nd t�y@ } zJ|jtjk�r*t|tjd� |j�r td tjd� |  d!¡ ‚ W Y d}~n
d}~0 0 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ÚdefaultrL   z--display-namezdSpecify the display name for the kernelspec. This is helpful when you have multiple IPython kernels.)rM   rL   r?   z]Specify an IPython profile to load. This can be used to create custom versions of the kernel.z--prefixz�Specify 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_constr<   z[Install to Python's sys.prefix. Shorthand for --prefix='%s'. For use in conda/virtual-envs.)rK   ÚconstrA   rL   z--envÚappendé   )ÚENVZVALUEz)Set environment variables for the kernel.)rK   ÚnargsÚmetavarrL   )r:   r;   r=   r<   r   r>   )Úfilez$Perhaps you want `sudo` or `--user`?r   zInstalled kernelspec z in )ÚargparseÚArgumentParserÚnameÚadd_argumentr
   r(   r   r<   Ú
parse_argsr   r>   ÚdictrB   r:   r=   r   ÚOSErrorÚerrnoÚEACCESÚprintÚstderrÚexit)rF   rW   ÚparserÚoptsrA   Úer   r   r   Ústart»   s„    ÿýüýýýÿûûú

z!InstallIPythonKernelSpecApp.start)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rY   rG   rf   r   r   r   r   rD   °   s   rD   Ú__main__)r	   NN)N)NNN)$rj   Ú
__future__r   r^   r3   r$   r)   r,   r   r&   Útypingr   Zjupyter_client.kernelspecr   Z	traitletsr   r   r   ÚImportErrorr   r%   r2   r   r(   ÚdirnameÚ__file__r+   r   r   r7   rB   Ztraitlets.configrC   rD   rg   Zlaunch_instancer   r   r   r   Ú<module>   sN   
   ý   ý#ùCU
