B
    7d                 @   s   d 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
mZ ddlmZ ddlmZ ddlmZ d	d
lmZ dd ZG dd deZde  efgZdS )z'Tornado handler for bundling notebooks.    N)nbclassic_path)import_item)webgen)url2path)JupyterHandler)ConfigManager   )toolsc             C   sF   t | rt| S t| tjjr,t| S t }|	|  |S dS )zxLike tornado's deprecated gen.maybe_future

    but more compatible with asyncio for recent versions
    of tornado
    N)
inspectisawaitableasyncioensure_future
isinstance
concurrentfuturesFuturewrap_future
set_result)objf r   g/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/nbclassic/bundler/handlers.pymaybe_future   s    



r   c               @   s0   e Zd Zdd Zdd Zejejdd Z	dS )BundlerHandlerc             C   s
   t | _ dS )z~Make tools module available on the handler instance for compatibility
        with existing bundler API and ease of reference.N)r
   )selfr   r   r   
initialize(   s    zBundlerHandler.initializec             C   s   t  }|ddi | S )a  
        Get bundler metadata from config given a bundler ID.
        
        Parameters
        ----------
        bundler_id: str
            Unique bundler ID within the notebook/bundlerextensions config section
        
        Returns
        -------
        dict
            Bundler metadata with label, group, and module_name attributes
        
        
        Raises
        ------
        KeyError
            If the bundler ID is unknown
        notebookZbundlerextensions)r   get)r   
bundler_idcmr   r   r   get_bundler-   s    zBundlerHandler.get_bundlerc          
   c   s   |  d}| jjt|d}y| |}W n4 tk
r^ } ztdd| |W dd}~X Y nX |d }yt|}W n t	k
r   |
d}Y nX yt|}W n4 tk
r } ztdd	| |W dd}~X Y nX t|| |V  dS )
zBundle the given nbclassic.
        
        Parameters
        ----------
        path: str
            Path to the notebook (path parameter)
        bundler: str
            Bundler ID to use (query parameter)
        bundler)pathi  zBundler %s not enabledNmodule_namezutf-8i  zCould not import bundler %s )Zget_query_argumentZcontents_managerr   r   r!   KeyErrorr   	HTTPErrorstrUnicodeEncodeErrorencoder   ImportErrorr   Zbundle)r   r#   r   modelr"   er$   Zbundler_modr   r   r   r   D   s$    
zBundlerHandler.getN)
__name__
__module____qualname__r   r!   r   authenticatedr   	coroutiner   r   r   r   r   r   '   s   r   z%s/bundle/(.*))__doc__r   r   concurrent.futuresr   Z	nbclassicr   Ztraitlets.utils.importstringr   tornador   r   Zjupyter_server.utilsr   Zjupyter_server.base.handlersr   Zjupyter_server.services.configr    r
   r   r   Zdefault_handlersr   r   r   r   <module>   s   F