B
    KôZ¸  ã               @   sh   d Z ddlZddlZddlZddlmZ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)ÚUnicodeÚobserve)Úlink_or_copyÚensure_dir_exists)Úunicode_typeé   )Ú
WriterBasec                   sh   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„ 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)Z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t |ƒ d S )NÚnew)r   )ÚselfZchanger   © r   ú6lib/python3.7/site-packages/nbconvert/writers/files.pyÚ_build_directory_changed%   s    z$FilesWriter._build_directory_changedc                s&   t t| ƒjf |Ž |  d| ji¡ d S )Nr   )Úsuperr	   Ú__init__r   r   )r   Úkw)Ú	__class__r   r   r   +   s    zFilesWriter.__init__c             C   s   |r| j  d|¡ t|ƒ dS )z,Make a directory if it doesn't already existzMaking directory %sN)ÚlogÚinfor   )r   Úpathr   r   r   Ú_makedir/   s    zFilesWriter._makedirNc          
   K   s  |dkrt dƒ‚| dd¡}| di ¡ dd¡}| jp8|}| jpB|}| di ¡ ¡ }	|	rx| j dtj 	| d	d¡d¡¡ xj|	D ]b\}
}tj 	||
¡}tj 
|¡}|  |¡ | j d
t|ƒ|¡ t |d¡}| |¡ W dQ R X q~W |r†xš| jD ]}
xŠt |
¡D ]|}|dkr tj ||¡}n|}tj 	||¡}tj 
|¡}|  |¡ tj |¡tj |¡ks| j d||¡ t||ƒ qW qòW |dk	rš|| }n|}tj 	||¡}| j dt|ƒ|¡ t|tƒrôtj|ddd}| |¡ W dQ R X n"t |d¡}| |¡ W d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_extensionZmetadatar   r
   ZoutputszSupport files will be in %sZoutput_files_dirz#Writing %i bytes to support file %sÚwbzCopying %s -> %szWriting %i bytes to %sÚwzutf-8)Úencoding)Ú	TypeErrorÚgetÚrelpathr   Úitemsr   r   Úosr   ÚjoinÚdirnamer   ÚdebugÚlenÚioÚopenÚwriteÚfilesÚglobÚnormpathr   Ú
isinstancer   )r   ÚoutputZ	resourcesr   r   r   Úresource_pathr!   r   r"   ÚfilenameÚdataÚdestr   ÚfZmatching_filenameZdest_filenamer   r   r   r*   5   sN    


 




zFilesWriter.write)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Útagr   r!   r   r   r   r   r*   Ú__classcell__r   r   )r   r   r	      s   r	   )r8   r(   r#   r,   Z	traitletsr   r   Zipython_genutils.pathr   r   Zipython_genutils.py3compatr   Úbaser   r	   r   r   r   r   Ú<module>   s   