B
    3¢\  ã               @   s>   d dl Z d dlZd dlZd dlm  mZ dd„ Zdd„ ZdS )é    Nc               C   s   dddddœgS )z&Metadata for notebook bundlerextensionZnotebook_zip_downloadzIPython Notebook bundle (.zip)znotebook.bundler.zip_bundlerZdownload)ÚnameZlabelZmodule_nameÚgroup© r   r   r   ú;lib/python3.7/site-packages/notebook/bundler/zip_bundler.pyÚ_jupyter_bundlerextension_paths   s    r   c             C   sÜ   t j | jd j|d ¡}|d }t j |¡d }t j |¡d d }|  |¡ |  dd¡ t 	|d¡}t
 ¡ }tj|d	tjd
}| ||¡ t j |¡}	x&|D ]}
t j |	|
¡}| ||
¡ q W | ¡  |  | ¡ ¡ dS )aÍ  Create a zip file containing the original notebook and files referenced
    from it. Retain the referenced files in paths relative to the notebook.
    Return the zip as a file download. 
    
    Assumes the notebook and other files are all on local disk.
    
    Parameters
    ----------
    handler : tornado.web.RequestHandler
        Handler that serviced the bundle request
    model : dict
        Notebook model from the configured ContentManager
    Zcontents_managerÚpathr   r   z.zipzContent-Typezapplication/zipé   Úw)ÚmodeZcompressionN)Úosr   ÚjoinZsettingsZroot_dirÚsplitextZset_attachment_headerZ
set_headerÚtoolsZget_file_referencesÚioÚBytesIOÚzipfileZZipFileZZIP_DEFLATEDÚwriteÚdirnameÚcloseZfinishÚgetvalue)ZhandlerZmodelZabs_nb_pathZnotebook_filenameZnotebook_nameZzip_filenameZref_filenamesZ
zip_bufferZzipfZnotebook_dirZnb_relative_filenameZabs_fnr   r   r   Úbundle   s"    


r   )r   r   r   Znotebook.bundler.toolsZbundlerr   r   r   r   r   r   r   Ú<module>   s
   	