B
    7dR              	   @   s  d 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mZ	m
Z
 ddlmZ ddlmZ ddlmZmZmZmZmZ ddlmZ ddlmZmZ dd	lmZ d
dlmZ d
dlmZ ddl m!Z! e" Z#dddddgZ$dYddZ%dddddde#ddf	ddZ&dZddZ'd[ddZ(d\ddZ)d]ddZ*d^d!d"Z+d_d#d$Z,d`d%d&Z-dad'd(Z.dbd)d*Z/dcd+d,Z0ddd-d.Z1ded/d0Z2dfd1d2Z3d
d3l4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= dd4l>m?Z?m@Z@ i ZAeABe= eABd5d6d iid7fd5d8d iid9fd: eAd8 eAd;< i ZCeCBe< eCBd<d=d>d? G d@d5 d5e5ZDG dAdB dBe5ZEG dCdD dDe5ZFG dEdF dFeFZGG dGdH dHeFZHG dIdJ dJe5ZIdKZJG dLdM dMe5ZKeKjLZMdgdNdOZNdhdPdQZOdRdS ZPdidTdUZQdVdW ZReSdXkreM  dS )jz?Utilities for installing Javascript extensions for the notebook    N)basenamejoinnormpath)urlparse)urlretrieve)jupyter_data_dirjupyter_config_pathjupyter_pathSYSTEM_JUPYTER_PATHENV_JUPYTER_PATH)ensure_dir_exists)string_typescast_unicode_py2)TemporaryDirectory   )__version__)BaseJSONConfigManager)import_itemcommonnotebooktreeZeditZterminalFc                sF   t ||||d tj s dS t| tr0| g} t fdd| D S )a  Check whether nbextension files have been installed

    Returns True if all files are found, False if any are missing.

    Parameters
    ----------

    files : list(paths)
        a list of relative paths within nbextensions.
    user : bool [default: False]
        Whether to check the user's .jupyter/nbextensions directory.
        Otherwise check a system-wide install (e.g. /usr/local/share/jupyter/nbextensions).
    prefix : str [optional]
        Specify install prefix, if it should differ from default (e.g. /usr/local).
        Will check prefix/share/jupyter/nbextensions
    nbextensions_dir : str [optional]
        Specify absolute path of nbextensions directory explicitly.
    sys_prefix : bool [default: False]
        Install into the sys.prefix, i.e. environment
    )user
sys_prefixprefixnbextensions_dirFc             3   s    | ]}t jt |V  qd S )N)ospathexistspjoin).0f)nbext c/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/nbclassic/nbextensions.py	<genexpr>B   s    z$check_nbextension.<locals>.<genexpr>)_get_nbextension_dirr   r   r   
isinstancer   all)filesr   r   r   r   r"   )r!   r#   check_nbextension$   s    
r)   c
          	   C   s  |t krddl}
|
dt d}t||	||d}t| ttdsFd}t| t	t
fr\tdt| } | dr|rztd	t `}t| jd
d }tj||}|r|d|  d|  t| | t||||||d}W dQ R X n&| dst| r||rtd|rtd|r6|d|  d|  | drNt| }nt| rbt| }|| |  d}n|stt | }t|}t t!||}|r tj"|r |r|d|  tj#|rtj$|st%&| n
t'| |rJtj(| } tj)|s|r<|d| d|   t*| | ntj#| rt!tj(| d} xt+| D ]\}}}t!||t,| d }tj)|s|r|d|  t-| x0|D ](}t!||}t!||}t.|||d qW qvW n| }t.|||d |S )a  Install a Javascript extension for the notebook

    Stages files and/or directories into the nbextensions directory.
    By default, this compares modification time, and only stages files that need updating.
    If `overwrite` is specified, matching files are purged before proceeding.

    Parameters
    ----------

    path : path to file, directory, zip or tarball archive, or URL to install
        By default, the file will be installed with its base name, so '/path/to/foo'
        will install to 'nbextensions/foo'. See the destination argument below to change this.
        Archives (zip or tarballs) will be extracted into the nbextensions directory.
    overwrite : bool [default: False]
        If True, always install the files, regardless of what may already be installed.
    symlink : bool [default: False]
        If True, create a symlink in nbextensions, rather than copying files.
        Not allowed with URLs or archives. Windows support for symlinks requires
        Vista or above, Python 3, and a permission bit which only admin users
        have by default, so don't rely on it.
    user : bool [default: False]
        Whether to install to the user's nbextensions directory.
        Otherwise do a system-wide install (e.g. /usr/local/share/jupyter/nbextensions).
    prefix : str [optional]
        Specify install prefix, if it should differ from default (e.g. /usr/local).
        Will install to ``<prefix>/share/jupyter/nbextensions``
    nbextensions_dir : str [optional]
        Specify absolute path of nbextensions directory explicitly.
    destination : str [optional]
        name the nbextension is installed to.  For example, if destination is 'foo', then
        the source file will be installed to 'nbextensions/foo', regardless of the source name.
        This cannot be specified if an archive is given as the source.
    logger : Jupyter logger [optional]
        Logger instance to use
    r   Nzv`install_nbextension`'s `verbose` parameter is deprecated, it will have no effects and will be removed in Notebook 5.0)r   r   r   r   symlinkFzpath must be a string pointing to a single extension to install; call this function multiple times to install multiple extensions)zhttps://zhttp://zCannot symlink from URLs/zDownloading: z -> )	overwriter*   r   destinationloggerz.zipzCannot symlink from archivesz$Cannot give destination for archiveszExtracting: z
Removing: zSymlinking:  zMaking directory: )r/   )/DEPRECATED_ARGUMENTwarningswarnDeprecationWarningr%   r   hasattrr   r&   listtuple	TypeErrorr   
startswith
ValueErrorr   r   r   splitr   infor   install_nbextensionendswith_safe_is_tarfilezipfileZipFiletarfileopen
extractallcloser   r   r   lexistsisdirislinkshutilrmtreeremoveabspathr   r*   walklenmakedirs_maybe_copy)r   r-   r*   r   r   r   r.   verboser/   r   r2   	full_destr!   tdfilenameZ
local_patharchiveparentdirsr(   dest_dir	file_namesrc	dest_filer"   r"   r#   r=   E   s    (










r=   c             C   s   t | \}}	tj|jd }
g }xp|	D ]h}tj|
|d }|d }|r`|d| d|  t|||||||||d	}t||| |	| q(W |S )zInstall an nbextension bundled in a Python package.

    Returns a list of installed/updated directories.

    See install_nbextension for parameter information.r   rZ   destzInstalling z -> )r-   r*   r   r   r   r   r.   r/   )
_get_nbextension_metadatar   r   r;   __file__r   r<   r=   validate_nbextension_pythonappend)moduler-   r*   r   r   r   r   r/   mnbexts	base_path
full_destsr!   rZ   r\   rR   r"   r"   r#   install_nbextension_python   s    

rf   c             C   s   t ||||d}t| } t|| }tj|rp|rB|d|  tj|rftj|sft	
| n
t| tjt||dd}	t|	d}
|rx tD ]}|
|d|dii qW dS )a  Uninstall a Javascript extension of the notebook

    Removes staged files and/or directories in the nbextensions directory and
    removes the extension from the frontend config.

    Parameters
    ----------

    dest : str
        path to file, directory, zip or tarball archive, or URL to install
        name the nbextension is installed to.  For example, if destination is 'foo', then
        the source file will be installed to 'nbextensions/foo', regardless of the source name.
        This cannot be specified if an archive is given as the source.
    require : str [optional]
        require.js path used to load the extension.
        If specified, frontend config loading extension will be removed.
    user : bool [default: False]
        Whether to install to the user's nbextensions directory.
        Otherwise do a system-wide install (e.g. /usr/local/share/jupyter/nbextensions).
    prefix : str [optional]
        Specify install prefix, if it should differ from default (e.g. /usr/local).
        Will install to ``<prefix>/share/jupyter/nbextensions``
    nbextensions_dir : str [optional]
        Specify absolute path of nbextensions directory explicitly.
    logger : Jupyter logger [optional]
        Logger instance to use
    )r   r   r   r   z
Removing: )r   r   nbconfig)
config_dirload_extensionsN)r%   r   r   r   r   rF   r<   rG   rH   rI   rJ   rK   r   _get_config_dirr   NBCONFIG_SECTIONSupdate)r\   requirer   r   r   r   r/   r!   rR   rh   cmsectionr"   r"   r#   uninstall_nbextension   s    



rp   c             C   sz   t | } xltdD ]`}t|| }tj|r|r@|d|  tj|rdtj|sdt	
| n
t| dS qW dS )zRemove nbextension files from the first location they are found.

    Returns True if files were removed, False otherwise.
    nbextensionsz
Removing: TF)r   r	   r   r   r   rF   r<   rG   rH   rI   rJ   rK   )rT   r/   r!   r   r"   r"   r#   _find_uninstall_nbextension  s    

rr   c          
   C   s^   t | \}}xL|D ]D}|d }	|d }
|r@|d|	 d|
  t|	|
|||||d qW dS )znUninstall an nbextension bundled in a Python package.

    See parameters of `install_nbextension_python`
    r\   rm   zUninstalling  )r   r   r   r   r/   N)r]   r<   rp   )ra   r   r   r   r   r/   rb   rc   r!   r\   rm   r"   r"   r#   uninstall_nbextension_python&  s    

rt   Tc             C   s   |rdn|}t jt||dd}t|d}|rT||r<dnd d|  d| d	 || d
||ii t||d || ||kS )a  Set whether the section's frontend should require the named nbextension

    Returns True if the final state is the one requested.

    Parameters
    ----------
    section : string
        The section of the server to change, one of NBCONFIG_SECTIONS
    require : string
        An importable AMD module inside the nbextensions static path
    state : bool
        The state in which to leave the extension
    user : bool [default: True]
        Whether to update the user's .jupyter/nbextensions directory
    sys_prefix : bool [default: False]
        Whether to update the sys.prefix, i.e. environment. Will override
        `user`.
    logger : Jupyter logger [optional]
        Logger instance to use
    F)r   r   rg   )rh   ZEnablingZ	Disablingrs   z extension z...ri   )r/   )	r   r   r   rj   r   r<   rl   validate_nbextensionget)ro   rm   stater   r   r/   rh   rn   r"   r"   r#   _set_nbextension_state7  s    
$rx   c                s$   t |\}} fdd|D S )ad  Enable or disable some nbextensions stored in a Python package

    Returns a list of whether the state was achieved (i.e. changed, or was
    already right)

    Parameters
    ----------

    state : Bool
        Whether the extensions should be enabled
    module : str
        Importable Python module exposing the
        magic-named `_jupyter_nbextension_paths` function
    user : bool
        Whether to enable in the user's nbextensions directory.
    sys_prefix : bool
        Enable/disable in the sys.prefix, i.e. environment
    logger : Jupyter logger [optional]
        Logger instance to use
    c          
      s(   g | ] }t |d  |d  dqS )ro   rm   )ro   rm   rw   r   r   r/   )rx   )r   r!   )r/   rw   r   r   r"   r#   
<listcomp>q  s   z1_set_nbextension_state_python.<locals>.<listcomp>)r]   )rw   ra   r   r   r/   rb   rc   r"   )r/   rw   r   r   r#   _set_nbextension_state_pythonZ  s    rz   c             C   s   t | |d|||dS )aX  Enable a named nbextension

    Returns True if the final state is the one requested.

    Parameters
    ----------

    section : string
        The section of the server to change, one of NBCONFIG_SECTIONS
    require : string
        An importable AMD module inside the nbextensions static path
    user : bool [default: True]
        Whether to enable in the user's nbextensions directory.
    sys_prefix : bool [default: False]
        Whether to enable in the sys.prefix, i.e. environment. Will override
        `user`
    logger : Jupyter logger [optional]
        Logger instance to use
    T)ro   rm   rw   r   r   r/   )rx   )ro   rm   r   r   r/   r"   r"   r#   enable_nbextensiony  s    r{   c             C   s   t | |d|||dS )aZ  Disable a named nbextension

    Returns True if the final state is the one requested.

    Parameters
    ----------

    section : string
        The section of the server to change, one of NBCONFIG_SECTIONS
    require : string
        An importable AMD module inside the nbextensions static path
    user : bool [default: True]
        Whether to enable in the user's nbextensions directory.
    sys_prefix : bool [default: False]
        Whether to enable in the sys.prefix, i.e. environment. Will override
        `user`.
    logger : Jupyter logger [optional]
        Logger instance to use
    F)ro   rm   rw   r   r   r/   )rx   )ro   rm   r   r   r/   r"   r"   r#   disable_nbextension  s    r|   c             C   sp   xjt  D ]`}ttj|dd}|| }|di |dr|rP|d|| || d|dii dS qW dS )zDisable an nbextension from the first config location where it is enabled.

    Returns True if it changed any config, False otherwise.
    rg   )rh   ri   NzDisabling %s extension in %sTF)r   r   r   r   r   rv   r<   rl   )ro   rm   r/   rh   rn   dr"   r"   r#   _find_disable_nbextension  s    
r~   c             C   s   t d| |||dS )a_  Enable some nbextensions associated with a Python module.

    Returns a list of whether the state was achieved (i.e. changed, or was
    already right)

    Parameters
    ----------

    module : str
        Importable Python module exposing the
        magic-named `_jupyter_nbextension_paths` function
    user : bool [default: True]
        Whether to enable in the user's nbextensions directory.
    sys_prefix : bool [default: False]
        Whether to enable in the sys.prefix, i.e. environment. Will override
        `user`
    logger : Jupyter logger [optional]
        Logger instance to use
    T)r/   )rz   )ra   r   r   r/   r"   r"   r#   enable_nbextension_python  s    
r   c             C   s   t d| |||dS )a  Disable some nbextensions associated with a Python module.

    Returns True if the final state is the one requested.

    Parameters
    ----------

    module : str
        Importable Python module exposing the
        magic-named `_jupyter_nbextension_paths` function
    user : bool [default: True]
        Whether to enable in the user's nbextensions directory.
    sys_prefix : bool [default: False]
        Whether to enable in the sys.prefix, i.e. environment
    logger : Jupyter logger [optional]
        Logger instance to use
    F)r/   )rz   )ra   r   r   r/   r"   r"   r#   disable_nbextension_python  s    
r   c       	      C   s   g }g }d}x@t dD ]4}tjj|f| d  d}tj|}|rP qW d}|rj||t|  n||t	|  |r|r|
d x|D ]}|
| qW x(|D ]}|| qW n|dt  |S )a  Validate a named nbextension.

    Looks across all of the nbextension directories.

    Returns a list of warnings.

    require : str
        require.js path used to load the extension
    logger : Jupyter logger [optional]
        Logger instance to use
    Frq   r+   z.jsz        - require? {} {}z#      - Validating: problems found:z      - Validating: )r	   r   r   r   r;   r   r`   formatGREEN_OKRED_Xwarningr<   )	rm   r/   r2   infosZ	js_existsextsjsZrequire_tmplmsgr"   r"   r#   ru     s*    


ru   c       	      C   s*  g }g }|  dd}|tkr4|dt d|  n|dt d|  |  dd}|dk	rtj|dt| d   | d	}tj	|r|dt d
|  n|dt d|  |r&|r|
d x|D ]}|
| qW x|D ]}|| qW |
d|   n|dt  |S )aP  Assess the health of an installed nbextension

    Returns a list of warnings.

    Parameters
    ----------

    spec : dict
        A single entry of _jupyter_nbextension_paths():
            [{
                'section': 'notebook',
                'src': 'mockextension',
                'dest': '_mockdestination',
                'require': '_mockdestination/index'
            }]
    full_dest : str
        The on-disk location of the installed nbextension: this should end
        with `nbextensions/<dest>`
    logger : Jupyter logger [optional]
        Logger instance to use
    ro   Nz  z
 section: z  section: rm   r   r\   z.jsz
 require: z  require: z- Validating: problems found:zFull spec: z- Validating: )rv   rk   r`   r   r   r   r   r   rN   r   r   r<   )	specrR   r/   r   r2   ro   rm   Zrequire_pathr   r"   r"   r#   r_     s0    


r_   )	BaseExtensionApprj   GREEN_ENABLEDRED_DISABLEDr   r   ArgumentConflict_base_aliases_base_flags)BoolUnicodeInstallNBExtensionAppr-   z!Force overwrite of existing filesr*   z'Create symlink instead of copying files)r-   r*   szInstallNBExtensionApp.prefixz&InstallNBExtensionApp.nbextensions_dirz!InstallNBExtensionApp.destination)r   rq   r.   c               @   s~   e Zd ZdZdZdZeZeZeddddZ	eddddZ
ed	dd
dZed	dddZed	dddZdd Zdd Zdd ZdS )r   z.Entry point for installing notebook extensionsa  Install Jupyter notebook extensions

    Usage

        jupyter nbclassic-extension install path|url [--user|--sys-prefix]

    This copies a file or a folder into the jupyter nbclassic-extensions directory.
    If a URL is given, it will be downloaded.
    If an archive is given, it will be extracted into nbextensions.
    If the requested files are already up to date, no action is taken
    unless --overwrite is specified.
    zB
    jupyter nbclassic-extension install /path/to/myextension
    FTz!Force overwrite of existing files)confighelpz(Create symlinks instead of copying filesr0   zInstallation prefixz;Full path to nbextensions dir (probably use prefix or user)z#Destination for the copy or symlinkc             C   s   dS )zThe default config file name.jupyter_notebook_configr"   )selfr"   r"   r#   _config_file_name_default  s    z/InstallNBExtensionApp._config_file_name_defaultc          
   C   s   t | jdkrtd| jr&t}i }nt}d| ji}|| jd f| j| j| j	| j
| j| j| jd|}|r| jd| jr| jd nd | j	rdnd	 | jrd
nd	 | j
rdnd	 d dS )z*Perform the installation of nbextension(s)r   z[Only one nbextension allowed at a time. Call multiple times to install multiple extensions.r.   r   )r-   r*   r   r   r   r   r/   z
To initialize this nbextension in the browser every time the notebook (or other app) loads:

      jupyter nbclassic-extension enable z<the entry point>z --userr0   z --pyz --sys-prefix
N)rN   
extra_argsr:   pythonrf   r=   r.   r-   r*   r   r   r   r   logr<   )r   installkwargsre   r"   r"   r#   install_extensions  s&    
z(InstallNBExtensionApp.install_extensionsc          
   C   sT   | j std n>y|   W n0 tk
rN } ztt| W dd}~X Y nX dS )z(Perform the App's function as configuredz(Please specify an nbextension to installN)r   sysexitr   r   str)r   er"   r"   r#   start  s    zInstallNBExtensionApp.startN)__name__
__module____qualname____doc__descriptionexamplesaliasesflagsr   r-   r*   r   r   r   r.   r   r   r   r"   r"   r"   r#   r   u  s   "c               @   s   e Zd ZdZeZdZdZddddZe	j
 Z
d dd	iid
fe
d< edd	ddZedd	ddZedd	ddZedd	d
dZdd Zdd Zdd Zdd ZdS )UninstallNBExtensionAppz0Entry point for uninstalling notebook extensionsab  Uninstall Jupyter notebook extensions

    Usage

        jupyter nbclassic-extension uninstall path/url path/url/entrypoint
        jupyter nbclassic-extension uninstall --py pythonPackageName

    This uninstalls an nbextension. By default, it uninstalls from the
    first directory on the search path where it finds the extension, but you can
    uninstall from a specific location using the --user, --sys-prefix or
    --system flags, or the --prefix option.

    If you specify the --require option, the named extension will be disabled,
    e.g.::

        jupyter nbclassic-extension uninstall myext --require myext/main

    If you use the --py or --python flag, the name should be a Python module.
    It will uninstall nbextensions listed in that module, but not the module
    itself (which you should uninstall using a package manager such as pip).
    z
    jupyter nbclassic-extension uninstall dest/dir dest/dir/extensionjs
    jupyter nbclassic-extension uninstall --py extensionPyPackage
    zUninstallNBExtensionApp.prefixz(UninstallNBExtensionApp.nbextensions_dirzUninstallNBExtensionApp.require)r   rq   rm   systemTz=Uninstall specifically from systemwide installation directoryr0   z@Installation prefix. Overrides --user, --sys-prefix and --system)r   r   z;Full path to nbextensions dir (probably use prefix or user)z$require.js module to disable loadingFc             C   s   dS )zThe default config file name.r   r"   )r   r"   r"   r#   r     s    z1UninstallNBExtensionApp._config_file_name_defaultc             C   sZ   | j | j| j| j| jd}| jr4t| jd f| n"| jrD| j|d< t	| jd f| dS )z1Uninstall an nbextension from a specific location)r   r   r   r   r/   r   rm   N)
r   r   r   r   r   r   rt   r   rm   rp   )r   r   r"   r"   r#   uninstall_extension  s    

z+UninstallNBExtensionApp.uninstall_extensionc             C   s   | j d }| jrht|\}}d}xT|D ]<}t|d | jdr@d}x tD ]}t||d | jd qFW q&W nt|| jd}|std|d | jrxtD ]}t|| j| jd qW d	S )
z5Uninstall an nbextension from an unspecified locationr   Fr\   )r/   Trm   zNo installed extension z found.N)	r   r   r]   rr   r   rk   r~   printrm   )r   name_rc   changedr!   ro   r"   r"   r#   find_uninstall_extension
  s"    




z0UninstallNBExtensionApp.find_uninstall_extensionc          
   C   s   | j std nt| j dkr,td nf| jsJ| jsJ| jsJ| jsJ| jry| 	  W q t
k
r } ztt| W d d }~X Y qX n|   d S )Nz*Please specify an nbextension to uninstallr   z]Only one nbextension allowed at a time. Call multiple times to uninstall multiple extensions.)r   r   r   rN   r   r   r   r   r   r   r   r   r   )r   r   r"   r"   r#   r   $  s    "zUninstallNBExtensionApp.startN)r   r   r   r   r   versionr   r   r   r   r   copyr   r   r   rm   r   r   r   r   r   r   r"   r"   r"   r#   r     s*   


r   c               @   sd   e Zd ZdZdZeZdZeddddZ	e
ddddZd	d
iZdZdd Zdd Zdd Zdd ZdS )ToggleNBExtensionAppz4A base class for apps that enable/disable extensionsz*jupyter nbclassic-extension enable/disablez/Enable/disable an nbextension in configuration.r   TzMWhich config section to add the extension to, 'common' will affect all pages.)r   r   z;Apply the configuration only for the current user (default)ro   zToggleNBExtensionApp.sectionNc             C   s   dS )zThe default config file name.r   r"   )r   r"   r"   r#   r   E  s    z.ToggleNBExtensionApp._config_file_name_defaultc             C   s$   | j r
tnt}||| j| j| jdS )aN  Toggle some extensions in an importable Python module.

        Returns a list of booleans indicating whether the state was changed as
        requested.

        Parameters
        ----------
        module : str
            Importable Python module exposing the
            magic-named `_jupyter_nbextension_paths` function
        )r   r   r/   )_toggle_valuer   r   r   r   r   )r   ra   toggler"   r"   r#   toggle_nbextension_pythonI  s    
z.ToggleNBExtensionApp.toggle_nbextension_pythonc             C   s(   | j r
tnt}|| j|| j| j| jdS )zToggle some a named nbextension by require-able AMD module.

        Returns whether the state was changed as requested.

        Parameters
        ----------
        require : str
            require.js path used to load the nbextension
        )r   r   r/   )r   r{   r|   ro   r   r   r   )r   rm   r   r"   r"   r#   toggle_nbextension\  s
    

z'ToggleNBExtensionApp.toggle_nbextensionc             C   sV   | j std nt| j dkr*td | jrB| | j d  n| | j d  d S )Nz:Please specify an nbextension/package to enable or disabler   z0Please specify one nbextension/package at a timer   )r   r   r   rN   r   r   r   )r   r"   r"   r#   r   l  s    
zToggleNBExtensionApp.start)r   r   r   r   r   r   r   r   r   ro   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r   6  s   r   c               @   s   e Zd ZdZdZdZdZdS )EnableNBExtensionAppz An App that enables nbextensionsz"jupyter nbclassic-extension enablez
    Enable an nbextension in frontend configuration.

    Usage
        jupyter nbclassic-extension enable [--system|--sys-prefix]
    TN)r   r   r   r   r   r   r   r"   r"   r"   r#   r   w  s   r   c               @   s   e Zd ZdZdZdZdZdS )DisableNBExtensionAppz!An App that disables nbextensionsz#jupyter nbclassic-extension disablez
    Disable an nbextension in frontend configuration.

    Usage
        jupyter nbclassic-extension disable [--system|--sys-prefix]
    N)r   r   r   r   r   r   r   r"   r"   r"   r#   r     s   r   c               @   s,   e Zd ZdZdZeZdZdd Zdd Z	dS )	ListNBExtensionsAppz,An App that lists and validates nbextensionsz jupyter nbclassic-extension listz7List all nbextensions known by the configuration systemc       
      C   s   dd t  D }td x|D ]}d| }d}t| |d}xtD ]~}||}d|krB|sht| d}td	| d
 xF|d  D ]6\}}	td| d|	rtnt  |	rt|| j	d qW qBW qW dS )zList all the nbextensionsc             S   s   g | ]}t j|d qS )rg   )r   r   r   )r   pr"   r"   r#   ry     s    z9ListNBExtensionsApp.list_nbextensions.<locals>.<listcomp>zKnown nbextensions:z  config dir: F)rV   rh   ri   Tz    z sectionz      rs   )r/   N)
r   r   r   rk   rv   itemsr   r   ru   r   )
r   Zconfig_dirsrh   headZ
head_shownrn   ro   datarm   enabledr"   r"   r#   list_nbextensions  s"    



z%ListNBExtensionsApp.list_nbextensionsc             C   s   |    dS )z)Perform the App's functions as configuredN)r   )r   r"   r"   r#   r     s    zListNBExtensionsApp.startN)
r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r     s   r   a  
jupyter nbclassic-extension list                          # list all configured nbextensions
jupyter nbclassic-extension install --py <packagename>    # install an nbextension from a Python package
jupyter nbclassic-extension enable --py <packagename>     # enable all nbextensions in a Python package
jupyter nbclassic-extension disable --py <packagename>    # disable all nbextensions in a Python package
jupyter nbclassic-extension uninstall --py <packagename>  # uninstall an nbextension in a Python package
c                   sV   e Zd ZdZdZeZdZeZ	e
edfedfedfedfedfd	Z fd
dZ  ZS )NBExtensionAppz4Base jupyter nbclassic-extension command entry pointzjupyter nbclassic-extensionz%Work with Jupyter notebook extensionszInstall an nbextensionzEnable an nbextensionzDisable an nbextensionzUninstall an nbextensionzList nbextensions)r   enabledisable	uninstallr6   c                s.   t    dt| j}td|  dS )z)Perform the App's functions as configuredz, z'Please supply at least one subcommand: N)superr   r   sortedsubcommandsr   r   )r   Zsubcmds)	__class__r"   r#   r     s    
zNBExtensionApp.start)r   r   r   r   r   r   r   r   	_examplesr   dictr   r   r   r   r   r   r   __classcell__r"   r"   )r   r#   r     s   r   c             C   s\   t j|sdS t | jt |j dkrD|r@|d|  dS |rX|d|  dS )am  Should a file be copied, if it doesn't exist, or is newer?

    Returns whether the file needs to be updated.

    Parameters
    ----------

    src : string
        A path that should exist from which to copy a file
    src : string
        A path that might exist to which to copy a file
    logger : Jupyter logger [optional]
        Logger instance to use
    Tgư>zOut of date: zUp to date: F)r   r   r   statst_mtimer3   r<   )rZ   r\   r/   r"   r"   r#   _should_copy  s    r   c             C   s8   t | ||dr4|r(|d|  d|  t| | dS )a!  Copy a file if it needs updating.

    Parameters
    ----------

    src : string
        A path that should exist from which to copy a file
    src : string
        A path that might exist to which to copy a file
    logger : Jupyter logger [optional]
        Logger instance to use
    )r/   z	Copying: z -> N)r   r<   rI   copy2)rZ   r\   r/   r"   r"   r#   rP     s    rP   c             C   s$   y
t | S  tk
r   dS X dS )zSafe version of is_tarfile, return False on IOError.

    Returns whether the file exists and is a tarfile.

    Parameters
    ----------

    path : string
        A path that might not exist and or be a tarfile
    FN)rB   
is_tarfileOSError)r   r"   r"   r#   r?   	  s    
r?   c             C   s   d| fd|fd|fd|fg}dd |D }t |dkrJtdd	| | r\tt d
}n@|rpttd d
}n,|rt|ddd
}n|r|}nttd d
}|S )a  Return the nbextension directory specified

    Parameters
    ----------

    user : bool [default: False]
        Get the user's .jupyter/nbextensions directory
    sys_prefix : bool [default: False]
        Get sys.prefix, i.e. ~/.envs/my-env/share/jupyter/nbextensions
    prefix : str [optional]
        Get custom prefix
    nbextensions_dir : str [optional]
        Get what you put in
    r   r   r   r   c             S   s"   g | ]\}}|r| d |qS )=r"   )r   nvr"   r"   r#   ry   /  s    z(_get_nbextension_dir.<locals>.<listcomp>r   zXcannot specify more than one of user, sys_prefix, prefix, or nbextensions_dir, but got: z, rq   r   Zsharejupyter)rN   r   r   r   r   r   r
   )r   r   r   r   conflictingZconflicting_setr!   r"   r"   r#   r%     s$    
r%   c             C   s2   t | }t|ds"td|  d| }||fS )a  Get the list of nbextension paths associated with a Python module.

    Returns a tuple of (the module,             [{
        'section': 'notebook',
        'src': 'mockextension',
        'dest': '_mockdestination',
        'require': '_mockdestination/index'
    }])

    Parameters
    ----------

    module : str
        Importable Python module exposing the
        magic-named `_jupyter_nbextension_paths` function
    _jupyter_nbextension_pathszThe Python module zU is not a valid nbextension, it is missing the `_jupyter_nbextension_paths()` method.)r   r5   KeyErrorr   )ra   rb   rc   r"   r"   r#   r]   B  s    
r]   __main__)FNNF)FFFFNNN)NFFNNN)N)FFNNN)TFN)N)TFN)TFN)N)TFN)TFN)N)N)N)N)FFNN)Tr   r   rI   r   rB   r@   os.pathr   r   r   r   urllib.parser   urllib.requestr   Zjupyter_core.pathsr   r   r	   r
   r   Zjupyter_core.utilsr   Zipython_genutils.py3compatr   r   Zipython_genutils.tempdirr   _versionr   Zconfig_managerr   Ztraitlets.utils.importstringr   objectr1   rk   r)   r=   rf   rp   rr   rt   rx   rz   r{   r|   r~   r   r   ru   r_   
extensionsr   rj   r   r   r   r   r   r   r   Z	traitletsr   r   r   rl   r   r   r   r   r   r   r   r   r   Zlaunch_instancemainr   rP   r?   r%   r]   r   r"   r"   r"   r#   <module>   s   
!| 
 
0
 

#
 
 

 
 

*
;,


NsA*


(
