o
    i                     @   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 d S )Nnew)_makedir)selfchanger    r   R/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nbconvert/writers/files.py_build_directory_changed&   s   z$FilesWriter._build_directory_changedc                    s&   t  jdi | | d| ji dS )zInitialize the writer.r   Nr   )super__init__r   r   )r   kw	__class__r   r   r   ,   s   zFilesWriter.__init__  c              
   C   s   t j|s2| jd| z
t j||d W dS  ty1 } z|jtjkr& W Y d}~dS d}~ww t j	|s>t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 j||}t j|}| | | jdt|| t|d}|	| W d   n1 s7w   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p#|}	|di  }
|
rD| jdtj	|d	dd | 
|
|	 |d
i  }|rd| jdtj	|ddd | 
||	 |	r| jD ]C}t|D ];}|dkr~tj||}n|}tj	|	|}tj|}| | tj|tj|kr| jd|| t|| qpqi|dur|| n|}t|	| }| jdt|| t|trt|ddd}|| W d   |S 1 sw   Y  |S t|d}|| W d   |S 1 sw   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 %soutput_files_dirattachmentszAttachments will be in %sattachment_files_dirzCopying %s -> %sr&   wzutf-8)encodingr'   )	TypeErrorgetrelpathr   r.   r   r   r   r   r(   r4   filesglobr)   r   normpathr   r   r+   
isinstancestrr,   r-   )r   output	resourcesr5   r   msgr6   resource_pathr@   r   r.   r:   r0   matching_filenamedest_filenamer2   r   	dest_pathr3   r   r   r   r-   P   sf   








zFilesWriter.write)r   )N)__name__
__module____qualname____doc__r   tagr   r@   r   r   r   r   r4   r-   __classcell__r   r   r   r   r      s&    	

r   )rP   r"   rB   r   pathlibr   	traitletsr   r   nbconvert.utils.ior   baser   r   r   r   r   r   <module>   s    