o
    i+                     @   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re  dS 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   ^/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nbclassic/bundler/bundlerextensions.py_get_bundler_metadata   s
   
r   TFc           
   	   C   s   |rdn|}t jt||dd}t|d}	|r&|d|r dnd| | |r7|	tt	| |||dii n
|	tt	| d	ii |	
t
t	i 
| d	u|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 {}...Enabling	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-   ).0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   r0   r   _set_bundler_state_pythone   s   r2   c                 C      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+   r2   r   r   r   r+   r   r   r   enable_bundler_python      
r7   c                 C   r3   )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
    Fr4   r5   r6   r   r   r   disable_bundler_python   r8   r9   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 bundlerextensionsz1jupyter nbclassic-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.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_valuer7   r9   r   r   log)r?   r   toggler   r   r   toggle_bundler_python   s   
z/ToggleBundlerExtensionApp.toggle_bundler_pythonc                 C   sL   | j s	td nt| j dkrtd | jr"| | j d  d S td)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pythonrD   NotImplementedErrorr>   r   r   r   start   s   
zToggleBundlerExtensionApp.start)__name__
__module____qualname____doc__r)   r   versiondescriptionr   r   rA   r@   rD   rL   r   r   r   r   r:      s    r:   c                   @   s   e Zd ZdZdZdZdZdS )EnableBundlerExtensionAppz%An App that enables bundlerextensionsz)jupyter nbclassic-bundlerextension enablez
    Enable a bundlerextension in frontend configuration.
    
    Usage
        jupyter nbclassic-bundlerextension enable [--system|--sys-prefix]
    TNrM   rN   rO   rP   r)   rR   rA   r   r   r   r   rS      
    rS   c                   @   s   e Zd ZdZdZdZdZdS )DisableBundlerExtensionAppz&An App that disables bundlerextensionsz*jupyter nbclassic-bundlerextension disablez
    Disable a bundlerextension in frontend configuration.
    
    Usage
        jupyter nbclassic-bundlerextension disable [--system|--sys-prefix]
    NrT   r   r   r   r   rV      rU   rV   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 nbclassic-extension listz7List all nbextensions known by the configuration systemc                 C   s   dd t  D }td |D ]O}d|}d}t| |d}|d}d|v r]|s.t| d	}|d  D ](\}}|d
}	|d}
|	du sJ|
du rQd|t}nd|	|
t}t| q4qdS )zList all the nbextensionsc                 S   s   g | ]	}t j|d qS )r   )r!   r"   r#   )r.   pr   r   r   r1      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   )r?   config_dirsr   head
head_shownr,   data
bundler_idr$   r   r   msgr   r   r   list_nbextensions   s,   




z)ListBundlerExtensionApp.list_nbextensionsc                 C   s   |    dS ))Perform the App's functions as configuredN)rb   r>   r   r   r   rL     s   zListBundlerExtensionApp.startN)
rM   rN   rO   rP   r)   r   rQ   rR   rb   rL   r   r   r   r   rW      s    rW   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 )BundlerExtensionAppz;Base jupyter nbclassic-bundlerextension command entry pointz"jupyter nbclassic-bundlerextensionz$Work with Jupyter bundler extensionsa8  
jupyter nbclassic-bundlerextension list                          # list all configured bundlers
jupyter nbclassic-bundlerextension enable --py <packagename>     # enable all bundlers in a Python package
jupyter nbclassic-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 )rc   z, z)Please supply at least one subcommand: %sN)superrL   r#   sortedsubcommandsrG   rH   )r?   subcmds	__class__r   r   rL   '  s   
zBundlerExtensionApp.start)rM   rN   rO   rP   r)   r   rQ   rR   examplesdictrS   rV   rW   rj   rL   __classcell__r   r   rl   r   rd     s    rd   __main__)TFN)N) rG   r!   
extensionsr   r   r   r   _versionr   nbclassic.config_managerr   jupyter_core.pathsr	   traitlets.utils.importstringr
   	traitletsr   r&   r'   r   r-   r2   r7   r9   r:   rS   rV   rW   rd   launch_instancemainrM   r   r   r   r   <module>   s2   

<

+(
