U
    nuf                     @   sd   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ G dd	 d	eZdS )
z;Contains writer for writing nbconvert output to filesystem.    N)Path)Unicodeobserve)link_or_copy   )
WriterBasec                       sr   e Zd ZdZedddjddZeddjddZedd	d
 Z	 fddZ
dddZdd ZdddZ  ZS )FilesWriterz-Consumes nbconvert output and produces files. a  Directory to write output(s) to. Defaults
                              to output to the directory of each notebook. To recover
                              previous default behaviour (outputting to the current
                              working directory) use . as the flag value.)helpT)configaU  When copying files that the notebook depends on, copy them in
        relation to this path, such that the destination filename will be
        os.path.relpath(filename, relpath). If FilesWriter is operating on a
        notebook that already exists elsewhere on disk, then the default will be
        the directory containing that notebook.build_directoryc                 C   s   |d }|r|  | d S )Nnew)_makedir)selfZchanger    r   ;/tmp/pip-unpacked-wheel-nxpdnluh/nbconvert/writers/files.py_build_directory_changed&   s    z$FilesWriter._build_directory_changedc                    s"   t  jf | | d| ji dS )zInitialize the writer.r   N)super__init__r   r   )r   kw	__class__r   r   r   ,   s    zFilesWriter.__init__  c              
   C   s|   t j|s`| jd| zt j||d W qx tk
r\ } z|jtjkrL W 5 d}~X Y qxX nt j	|sxtd| dS )a  ensure that a directory exists

        If it doesn't exist, try to create it and protect against a race condition
        if another process is doing the same.

        The default permissions are 755, which differ from os.makedirs default of 777.
        zMaking directory %s)modeNz %r exists but is not a directory)
ospathexistsloginfomakedirsOSErrorerrnoEEXISTisdir)r   r   r   er   r   r   r   1   s    zFilesWriter._makedirc              
   C   sj   |D ]`\}}t j||}t j|}| | | jdt|| t|d}|	| W 5 Q R X qdS )z-Write a dict containing filename->binary dataWriting %i bytes to %swbN)
r   r   joindirnamer   r   debuglenopenwrite)r   items	build_dirfilenamedatadestr   fr   r   r   _write_itemsC   s    
zFilesWriter._write_itemsNc              	   K   s  |dkrd}t ||dd}|di dd}| jp<|}| jpF|}	|di  }
|
r| jdtj	|d	dd | 
|
|	 |d
i  }|r| jdtj	|ddd | 
||	 |	r\| jD ]}t|D ]v}|dkrtj||}n|}tj	|	|}tj|}| | tj|tj|kr| jd|| t|| qq|dk	rn|| n|}t|	| }| jdt|| t|trt|ddd}|| W 5 Q R X n t|d}|| W 5 Q R X |S )z
        Consume and write Jinja output to the file system.  Output directory
        is set via the 'build_directory' variable of this instance (a
        configurable).

        See base for more...
        Nnotebook_nameoutput_extensionmetadatar   r	   outputszSupport files will be in %sZoutput_files_dirattachmentszAttachments will be in %sZattachment_files_dirzCopying %s -> %sr%   wzutf-8)encodingr&   )	TypeErrorgetrelpathr   r-   r   r   r   r   r'   r3   filesglobr(   r   normpathr   r   r*   
isinstancestrr+   r,   )r   output	resourcesr4   r   msgr5   resource_pathr=   r   r-   r8   r/   Zmatching_filenameZdest_filenamer1   r   	dest_pathr2   r   r   r   r,   P   sT    




zFilesWriter.write)r   )N)__name__
__module____qualname____doc__r   tagr   r=   r   r   r   r   r3   r,   __classcell__r   r   r   r   r      s$   	

r   )rK   r!   r?   r   pathlibr   Z	traitletsr   r   Znbconvert.utils.ior   baser   r   r   r   r   r   <module>   s   