B
    3\+                 @   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   s0   t | }t|ds t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 {} does not contain a valid bundlerextension)r
   hasattrKeyErrorformatr   )modulembundlers r   Alib/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}	|rL|d|r@dnd| | |rn|	tt	| |||dii n|	tt	| d	ii |	
t
t	i 
| d	k	|kS )
aF  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{} {} bundler {}...ZEnablingZ	Disabling)labelmodule_namegroupN)ospathjoinr   r   infor   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 )aP  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>y   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_pythone   s    r.   c             C   s   t d| |||dS )aI  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 )aJ  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))Z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)r3   r   Z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)Z
extra_argssysexitlenZpythonr7   NotImplementedError)r3   r   r   r   start   s    
zToggleBundlerExtensionApp.start)__name__
__module____qualname____doc__r'   r   versiondescriptionr   r   r5   r4   r7   r=   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)r>   r?   r@   rA   r'   rC   r5   r   r   r   r   rD      s   rD   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)r>   r?   r@   rA   r'   rC   r5   r   r   r   r   rE      s   rE   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	}x`|d  D ]P\}}|d
}	|d}
|	dks|
dkrd|t}n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	    {} {}z    "{}" from {} {})r	   printr   r   r&   itemsr   r   )r3   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)rN   )r3   r   r   r   r=     s    zListBundlerExtensionApp.startN)
r>   r?   r@   rA   r'   r   rB   rC   rN   r=   r   r   r   r   rF      s   rF   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                s0   t t|   dt| j}td|  dS )z)Perform the App's functions as configuredz, z)Please supply at least one subcommand: %sN)superrO   r=   r!   sortedsubcommandsr9   r:   )r3   Zsubcmds)	__class__r   r   r=   '  s    zBundlerExtensionApp.start)r>   r?   r@   rA   r'   r   rB   rC   ZexamplesdictrD   rE   rF   rU   r=   __classcell__r   r   )rV   r   rO     s   rO   __main__)TFN)N)TFN)TFN) r9   r   
extensionsr   r   r   r   Z_versionr   Znotebook.config_managerr   Zjupyter_core.pathsr	   Ztraitlets.utils.importstringr
   Z	traitletsr   r$   r%   r   r+   r.   r/   r0   r1   rD   rE   rF   rO   Zlaunch_instancemainr>   r   r   r   r   <module>   s,   
;


+(
