U
    kufS=                     @  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
m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 G dd dejZddddZddddZddddddZdddddZddddZdddddZddd d!Zed"kre  dS )#zgThe root `jupyter` command.

This does nothing other than dispatch to subcommands or output path info.
    )annotationsN)Path)which)Popen)Any   )paths)__version__c                   @  sF   e Zd ZdZeddddZejdddd	dZddd
dZdS )JupyterParserzA Jupyter argument parser.z
str | Nonereturnc                 C  s   dd t  S )z}Add subcommands to epilog on request

        Avoids searching PATH for subcommands unless help output is requested.
        zAvailable subcommands: %s )joinlist_subcommands)self r   8/tmp/pip-unpacked-wheel-a5h937xx/jupyter_core/command.pyepilog   s    zJupyterParser.epilogr   None)xr   c                 C  s   dS )z$Ignore epilog set in Parser.__init__Nr   )r   r   r   r   r   r   &   s    c                 C  s0   zddl }||  W n tk
r*   Y nX dS )z#Trigger auto-completion, if enabledr   N)argcompleteautocompleteImportError)r   r   r   r   r   r   *   s
    zJupyterParser.argcompleteN)__name__
__module____qualname____doc__propertyr   setterr   r   r   r   r   r
      s   r
   r   c                  C  s   t dd} | jdd}|jdddd |jd	td
dd}dd |_|jdddd |jdddd |jdddd |jdddd | jdddd | jdddd | S )zCreate a jupyter parser object.zJupyter: Interactive Computing)descriptionF)requiredz	--version
store_truez3show the versions of core jupyter packages and exit)actionhelp
subcommand?zthe subcommand to launch)typenargsr#   c                  _  s   t  S N)r   )argskwargsr   r   r   <lambda>B       z jupyter_parser.<locals>.<lambda>z--config-dirzshow Jupyter config dirz
--data-dirzshow Jupyter data dirz--runtime-dirzshow Jupyter runtime dirz--pathsz?show all Jupyter paths. Add --json for machine-readable format.z--jsonz%output paths as machine-readable jsonz--debugz$output debug information about paths)r
   add_mutually_exclusive_groupadd_argumentstrZ	completer)parsergroupZsubcommand_actionr   r   r   jupyter_parser4   s6         
r2   z	list[str]c               	     s   t  t D ]x} zt| }W n tk
r6   Y qY nX |D ]F}|dr<tjdrftj	|d }
t|ddd  q<qt  }D ]6 t fddtdt D s|
d  qt|S )	zList all jupyter subcommands

    searches PATH for `jupyter-name`

    Returns a list of jupyter's subcommand names, without the `jupyter-` prefix.
    Nested children (e.g. jupyter-sub-subsub) are not included.
    jupyter-winr   -r   Nc                 3  s   | ]} d | kV  qd S r(   r   ).0iZsub_tupZsubcommand_tuplesr   r   	<genexpr>k   s     z#list_subcommands.<locals>.<genexpr>)set_path_with_selfoslistdirOSError
startswithsysplatformpathsplitextaddtuplesplitanyrangelenr   sorted)dnamesnamesubcommandsr   r8   r   r   R   s     


 "r   r/   r   )cmdargvr   c                 C  s   t jdrpt| }|dkr,td|  tjt|g|dd  }ddl}||j	|j
 |  t |j nt| | dS )zexecvp, except on Windows where it uses Popen

    Python provides execvp on Windows, but its behavior is problematic (Python bug#9148).
    r4   Nz%r not foundr   r   )r@   rA   r?   r   r>   errnoENOENTr   signalSIGINTSIG_IGNwaitexit
returncoder<   execvp)rO   rP   Zcmd_pathprS   r   r   r   _execvpp   s    r[   )r$   r   c                 C  sf   t jt }d|  }t||d}|dkr@d| d}t|t |t jsbd| d}t||S )z^This method get the abspath of a specified jupyter-subcommand with no
    changes on ENV.
    r3   )rB   Nz
Jupyter command `z` not found.z` is not executable.)r<   pathsepr   r;   r   	ExceptionaccessX_OK)r$   search_pathZjupyter_subcommandabs_pathmsgr   r   r   _jupyter_abspath   s    
rc   c                  C  s   t jdpt jt j} ztd}W n tk
r<   Y nX | 	| t
jd g}t|d  rz|	t j|d  |D ]8}tt|j}t| r~t |t jr~| d| q~| S )zPut `jupyter`'s dir at the front of PATH

    Ensures that /path/to/jupyter subcommand
    will do /path/to/jupyter-subcommand
    even if /other/jupyter-subcommand is ahead of it on PATH
    PATHscriptsr   )r<   environgetdefpathrF   r\   	sysconfigget_pathKeyErrorappendr@   rP   r   
is_symlinkrB   realpathr/   parentis_dirr^   r_   insert)	path_listZbindirre   scriptr   r   r   r;      s    	
r;   )r0   r   c                 C  sz   zLddl m}m} | }|rBt|dkrB|d dsB|  |W S |   W n tk
rh   |   Y nX d}t|dS )a  If argcomplete is enabled, trigger autocomplete or return current words

    If the first word looks like a subcommand, return the current command
    that is attempting to be completed so that the subcommand can evaluate it;
    otherwise auto-complete using the main parser.
    r   )get_argcomplete_cwordsincrement_argcomplete_indexr   r5   z3Control flow should not reach end of autocomplete()N)Z#traitlets.config.argcomplete_configrt   ru   rI   r?   r   r   AssertionError)r0   rt   ru   cwordsrb   r   r   r   _evaluate_argcomplete   s    rx   c               
   C  s<  t  } tj}d}dtjkr.t| }|d }n2t|dkrT|d dsT|d }n|  \}}|j	}|j
rtd dD ]P}z |dkrt}nt|}|j}W n tk
r   d}Y nX t|d	d
| qxdS |jr|jstd |jr|jstd |jr|jrtd |jr,tt  dS |jrDtt  dS |jr\tt  dS |jr`i }t g|d< t |d< t |d< |jrtt| n|jr tj}	t rtd ntd t rtd ntd |	drtd ntd |	dr0td|	d d ntd |	dr\td|	d d ntd tj rtd t!  d! ntd"t!  d! |	d#rtd$|	d# d% ntd& |	d'rtd(|	d' d) ntd* |	d+rtd,|	d+ d- ntd. t  t"|D ]2}
||
 }td/|
  |D ]}td0|  qDq(dS |s~| j#tj$d1 td2 zt%|}W nT t&k
r } z4| j#tj$d1 |d3krW Y dS tt'| W 5 d}~X Y nX zt(||g|d4d   W n: t)k
r6 } ztd5|d6|  W 5 d}~X Y nX dS )7zThe command entry point.NZ_ARGCOMPLETEr   r5   z!Selected Jupyter core packages...)ZIPythonZ	ipykernelZ
ipywidgetsZjupyter_clientjupyter_coreZjupyter_serverZ
jupyterlabZnbclientZ	nbconvertZnbformatZnotebookZ	qtconsoleZ	traitletsry   znot installedz<17:z --json is only used with --pathsz!--debug is only used with --pathsz"--debug cannot be used with --jsonZruntimeconfigdatazjJUPYTER_PLATFORM_DIRS is set to a true value, so we use platformdirs to find platform-specific directorieszJUPYTER_PLATFORM_DIRS is set to a false value, or is not set, so we use hardcoded legacy paths for platform-specific directorieszJUPYTER_PREFER_ENV_PATH is set to a true value, or JUPYTER_PREFER_ENV_PATH is not set and we detected a virtual environment, making the environment-level path preferred over the user-level path for data and configzJUPYTER_PREFER_ENV_PATH is set to a false value, or JUPYTER_PREFER_ENV_PATH is not set and we did not detect a virtual environment, making the user-level path preferred over the environment-level path for data and configZJUPYTER_NO_CONFIGzWJUPYTER_NO_CONFIG is set, making the config path list only a single temporary directoryzEJUPYTER_NO_CONFIG is not set, so we use the full path list for configZJUPYTER_CONFIG_PATHzJUPYTER_CONFIG_PATH is set to 'zO', which is prepended to the config path list (unless JUPYTER_NO_CONFIG is set)zQJUPYTER_CONFIG_PATH is not set, so we do not prepend anything to the config pathsZJUPYTER_CONFIG_DIRzJUPYTER_CONFIG_DIR is set to 'z5', overriding the default user-level config directoryzPJUPYTER_CONFIG_DIR is not set, so we use the default user-level config directoryzKPython's site.ENABLE_USER_SITE is True, so we add the user site directory ''z]Python's site.ENABLE_USER_SITE is not True, so we do not add the Python site user directory 'ZJUPYTER_PATHzJUPYTER_PATH is set to 'z'', which is prepended to the data pathszHJUPYTER_PATH is not set, so we do not prepend anything to the data pathsZJUPYTER_DATA_DIRzJUPYTER_DATA_DIR is set to 'z3', overriding the default user-level data directoryzLJUPYTER_DATA_DIR is not set, so we use the default user-level data directoryZJUPYTER_RUNTIME_DIRzJUPYTER_RUNTIME_DIR is set to 'z+', overriding the default runtime directoryzGJUPYTER_RUNTIME_DIR is not set, so we use the default runtime directoryz%s:z    )filez>
Please specify a subcommand or one of the optional arguments.r#      z Error executing Jupyter command z: )*r2   r@   rP   r<   rf   rx   rI   r?   parse_known_argsr$   versionprintr	   
__import__r   jsonr   rW   debugZ
config_dirZjupyter_config_dirdata_dirZjupyter_data_dirZruntime_dirZjupyter_runtime_dirZjupyter_config_pathZjupyter_pathdumpsZuse_platform_dirsZprefer_environment_over_userrg   siteENABLE_USER_SITEgetuserbaserJ   
print_helpstderrrc   r]   r/   r[   r>   )r0   rP   r$   r)   optspackager   modr|   envrM   rB   rZ   commander   r   r   main   s   










 r   __main__) r   
__future__r   argparserQ   r   r<   r   r@   ri   pathlibr   shutilr   
subprocessr   typingr    r   r   r	   ArgumentParserr
   r2   r   r[   rc   r;   rx   r   r   r   r   r   r   <module>   s2   & 5
