B
    9dh*                 @   s  d dl Z d dlZddlmZmZmZ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 d	Zd
Zdd Zd"ddZd#ddZd$ddZd%ddZG dd deZG dd deZG dd deZG dd deZG dd  d eZejZed!kre  dS )&    N   )BaseExtensionApp_get_config_dirGREEN_ENABLEDRED_DISABLED)__version__)BaseJSONConfigManager)jupyter_config_path)import_item)Boolnotebookbundlerextensionsc             C   s2   t | }t|ds"td|  d| }||fS )a  Gets the list of bundlers associated with a Python package.

    Returns a tuple of (the module, [{
        'name': 'unique name of the bundler',
        'label': 'file menu item label for the bundler',
        'module_name': 'dotted package/module name containing the bundler',
        'group': 'download or deploy parent menu item'
    }])

    Parameters
    ----------

    module : str
        Importable Python module exposing the
        magic-named `_jupyter_bundlerextension_paths` function
    _jupyter_bundlerextension_pathszThe Python module z* does not contain a valid bundlerextension)r
   hasattrKeyErrorr   )modulembundlers r   o/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/notebook/bundler/bundlerextensions.py_get_bundler_metadata   s
    
r   TFc       
   	   C   s   |rdn|}t jt||dd}t|d}	|rT||r<dnd d|  d| d	 |rv|	tt| |||d
ii n|	tt| dii |		t	ti 	| dk	|kS )a>  Set whether a bundler is enabled or disabled.

    Returns True if the final state is the one requested.

    Parameters
    ----------
    name : string
        Unique name of the bundler
    label : string
        Human-readable label for the bundler menu item in the notebook UI
    module_name : string
        Dotted module/package name containing the bundler
    group : string
        'download' or 'deploy' indicating the parent menu containing the label
    state : bool
        The state in which to leave the extension
    user : bool [default: True]
        Whether to update the user's .jupyter/nbconfig 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)user
sys_prefixnbconfig)
config_dirZEnablingZ	Disabling z	 bundler z...)labelmodule_namegroupN)
ospathjoinr   r   infoupdateBUNDLER_SECTIONBUNDLER_SUBSECTIONget)
namer   r   r   stater   r   loggerr   cmr   r   r   _set_bundler_state)   s&    
$
r+   c                s$   t |\}} fdd|D S )aH  Enables or disables bundlers defined in a Python package.

    Returns a list of whether the state was achieved for each bundler.

    Parameters
    ----------
    state : Bool
        Whether the extensions should be enabled
    module : str
        Importable Python module exposing the
        magic-named `_jupyter_bundlerextension_paths` function
    user : bool
        Whether to enable in the user's nbconfig directory.
    sys_prefix : bool
        Enable/disable in the sys.prefix, i.e. environment
    logger : Jupyter logger [optional]
        Logger instance to use
    c                s4   g | ],}t |d  |d |d |d  dqS )r'   r   r   r   )r'   r   r   r   r(   r   r   r)   )r+   ).0Zbundler)r)   r(   r   r   r   r   
<listcomp>u   s   z-_set_bundler_state_python.<locals>.<listcomp>)r   )r(   r   r   r   r)   r   r   r   )r)   r(   r   r   r   _set_bundler_state_pythona   s    r.   c             C   s   t d| |||dS )aA  Enables bundlers defined in a Python package.

    Returns whether each bundle defined in the packaged was enabled or not.

    Parameters
    ----------
    module : str
        Importable Python module exposing the
        magic-named `_jupyter_bundlerextension_paths` function
    user : bool [default: True]
        Whether to enable in the user's nbconfig 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)   )r.   )r   r   r   r)   r   r   r   enable_bundler_python~   s    
r/   c             C   s   t d| |||dS )aB  Disables bundlers defined in a Python package.

    Returns whether each bundle defined in the packaged was enabled or not.

    Parameters
    ----------
    module : str
        Importable Python module exposing the
        magic-named `_jupyter_bundlerextension_paths` function
    user : bool [default: True]
        Whether to enable in the user's nbconfig 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)r)   )r.   )r   r   r   r)   r   r   r   disable_bundler_python   s    
r0   c               @   sF   e Zd ZdZdZeZdZeddddZ	dZ
dd	 Zd
d Zdd ZdS )ToggleBundlerExtensionAppz;A base class for apps that enable/disable bundlerextensionsz'jupyter bundlerextension enable/disablez3Enable/disable a bundlerextension in configuration.Tz;Apply the configuration only for the current user (default))confighelpNc             C   s   dS )zThe default config file name.Zjupyter_notebook_configr   )selfr   r   r   _config_file_name_default   s    z3ToggleBundlerExtensionApp._config_file_name_defaultc             C   s$   | j r
tnt}||| j| j| jdS )aS  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_bundlerextension_paths` function
        )r   r   r)   )_toggle_valuer/   r0   r   r   log)r4   r   toggler   r   r   toggle_bundler_python   s    
z/ToggleBundlerExtensionApp.toggle_bundler_pythonc             C   sN   | j std nt| j dkr*td | jrB| | j d  ntdd S )Nz?Please specify an bundlerextension/package to enable or disable   z5Please specify one bundlerextension/package at a timer   z0Cannot install bundlers from non-Python packages)
extra_argssysexitlenpythonr9   NotImplementedError)r4   r   r   r   start   s    
zToggleBundlerExtensionApp.start)__name__
__module____qualname____doc__r'   r   versiondescriptionr   r   r6   r5   r9   rA   r   r   r   r   r1      s   r1   c               @   s   e Zd ZdZdZdZdZdS )EnableBundlerExtensionAppz%An App that enables bundlerextensionszjupyter bundlerextension enablez
    Enable a bundlerextension in frontend configuration.

    Usage
        jupyter bundlerextension enable [--system|--sys-prefix]
    TN)rB   rC   rD   rE   r'   rG   r6   r   r   r   r   rH      s   rH   c               @   s   e Zd ZdZdZdZdZdS )DisableBundlerExtensionAppz&An App that disables bundlerextensionsz jupyter bundlerextension disablez
    Disable a bundlerextension in frontend configuration.

    Usage
        jupyter bundlerextension disable [--system|--sys-prefix]
    N)rB   rC   rD   rE   r'   rG   r6   r   r   r   r   rI      s   rI   c               @   s,   e Zd ZdZdZeZdZdd Zdd Z	dS )	ListBundlerExtensionAppz,An App that lists and validates nbextensionszjupyter nbextension listz7List all nbextensions known by the configuration systemc             C   s   dd t  D }td x|D ]}d| }d}t| |d}|d}d|kr|s^t| d	}xl|d  D ]\\}}|d
}	|d}
|	dks|
dkrd| dt }nd|	 d|
 dt }t| qlW qW dS )zList all the nbextensionsc             S   s   g | ]}t j|d qS )r   )r   r    r!   )r,   pr   r   r   r-      s    z=ListBundlerExtensionApp.list_nbextensions.<locals>.<listcomp>zKnown bundlerextensions:z  config dir: F)parentr   r   r   Tr   r   Nz    r   z    "z" from )r	   printr   r&   itemsr   r   )r4   Zconfig_dirsr   headZ
head_shownr*   dataZ
bundler_idr"   r   r   msgr   r   r   list_nbextensions   s$    




z)ListBundlerExtensionApp.list_nbextensionsc             C   s   |    dS )z)Perform the App's functions as configuredN)rR   )r4   r   r   r   rA     s    zListBundlerExtensionApp.startN)
rB   rC   rD   rE   r'   r   rF   rG   rR   rA   r   r   r   r   rJ      s   rJ   c                   sJ   e Zd ZdZdZeZdZdZe	e
dfedfedfdZ fd	d
Z  ZS )BundlerExtensionAppz1Base jupyter bundlerextension command entry pointzjupyter bundlerextensionz$Work with Jupyter bundler extensionsa  
jupyter bundlerextension list                          # list all configured bundlers
jupyter bundlerextension enable --py <packagename>     # enable all bundlers in a Python package
jupyter bundlerextension disable --py <packagename>    # disable all bundlers in a Python package
zEnable a bundler extensionzDisable a bundler extensionzList bundler extensions)enabledisablelistc                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)superrA   r!   sortedsubcommandsr<   r=   )r4   Zsubcmds)	__class__r   r   rA   !  s    
zBundlerExtensionApp.start)rB   rC   rD   rE   r'   r   rF   rG   ZexamplesdictrH   rI   rJ   rY   rA   __classcell__r   r   )rZ   r   rS     s   rS   __main__)TFN)N)TFN)TFN) r<   r   
extensionsr   r   r   r   _versionr   Znotebook.config_managerr   Zjupyter_core.pathsr	   Ztraitlets.utils.importstringr
   Z	traitletsr   r$   r%   r   r+   r.   r/   r0   r1   rH   rI   rJ   rS   Zlaunch_instancemainrB   r   r   r   r   <module>   s,   
7


+&
