a
    ø£Bež  ã                   @   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   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/writers/files.pyÚ_build_directory_changed&   s    z$FilesWriter._build_directory_changedc                    s&   t ƒ jf i |¤Ž |  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 |¡sb| j d|¡ zt j||d� W qz ty^ } z|jtjkrJ‚ W Y d}~qzd}~0 0 nt j 	|¡szt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   s~   |D ]t\}}t j ||¡}t j |¡}|  |¡ | j dt|ƒ|¡ t|dƒ�}| 	|¡ W d  ƒ q1 sn0    Y  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u 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rþtj ||¡}n|}tj 	|	|¡}tj |¡}|  |¡ tj |¡tj |¡krâ| j d||¡ t||ƒ qâqÔ|du�rn|| n|}t|	ƒ| }| j dt|ƒ|¡ t|tƒ�rÚt|ddd��}| |¡ W d  ƒ n1 �sÎ0    Y  n6t|dƒ�}| |¡ W d  ƒ n1 �s0    Y  |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   