
    f                     j    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	e      Zy)
z;Contains writer for writing nbconvert output to filesystem.    N)Path)Unicodeobserve)link_or_copy   )
WriterBasec                        e Zd ZdZ edd      j                  d      Z ed      j                  d      Z ed      d	        Z	 fd
Z
ddZd ZddZ x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                 6    |d   }|r| j                  |       y y )Nnew)_makedir)selfchanger   s      R/var/www/cvtools/html/venv/lib/python3.12/site-packages/nbconvert/writers/files.py_build_directory_changedz$FilesWriter._build_directory_changed&   s    UmMM#     c                 ^    t        |   di | | j                  d| j                  i       y)zInitialize the writer.r   N )super__init__r   r   )r   kw	__class__s     r   r   zFilesWriter.__init__,   s,    2%%ud.B.B&CDr   c                 n   t         j                  j                  |      s5| j                  j	                  d|       	 t        j
                  ||       yt         j                  j                  |      st        d|z        y# t        $ r(}|j                  t        j                  k7  r Y d}~yd}~ww xY w)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   es       r   r   zFilesWriter._makedir1   s     ww~~d#HHMM/6Dt, t$<tCDD %  77ell* +s   B 	B4B//B4c                 h   |D ]  \  }}t         j                  j                  ||      }t         j                  j                  |      }| j	                  |       | j
                  j                  dt        |      |       t        |d      5 }|j                  |       ddd        y# 1 sw Y   xY w)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    fs           r   _write_itemszFilesWriter._write_itemsC   s    # 		NHd77<<	84D77??4(DMM$ HHNN3SYEdD! Q 		 s   B((B1	c           	      P   |d}t        |      |j                  dd      }|j                  di       j                  dd      }| j                  xs |}| j                  xs |}	|j                  di       j	                         }
|
r\| j
                  j                  dt        j                  j                  |j                  d	d      d             | j                  |
|	       |j                  d
i       j	                         }|r\| j
                  j                  dt        j                  j                  |j                  dd      d             | j                  ||	       |	r| j                  D ]  }t        j                  |      D ]  }|dk7  r!t        j                  j                  ||      }n|}t        j                  j                  |	|      }t        j                  j                  |      }| j                  |       t        j                  j                  |      t        j                  j                  |      k7  s| j
                  j                  d||       t!        ||         |||z   n|}t#        |	      |z  }| j
                  j                  dt%        |      |       t'        |t(              r*t+        |dd      5 }|j-                  |       ddd       |S t+        |d      5 }|j-                  |       ddd       |S # 1 sw Y   |S xY w# 1 sw Y   |S xY w)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   r3   r"   r#   r   r    r-   r9   filesglobr.   r   normpathr   r   r0   
isinstancestrr1   r2   )r   output	resourcesr;   r   msgr<   resource_pathrF   r   r3   r@   r5   matching_filenamedest_filenamer7   r    	dest_pathr8   s                      r   r2   zFilesWriter.writeP   s     !CC.  %==);TB "j"599&"E,,/-..?- i,224HHMM-Y]]+=rBBG e_5  mmM26<<>HHMM+Y]]+A2FK k?;  JJ >)-8)< >%"}(*8I7(S(9 77<<GD77??40DMM$' ww''-1A1ABS1TT&8:KTR$%6=>>( 4D3O}//Ub)D0	 	.FYGfc"iw7  1   i&  !      s   
L2LLL%)i  )N)__name__
__module____qualname____doc__r   tagr   rF   r   r   r   r   r9   r2   __classcell__)r   s   @r   r
   r
      s}    7
M 
cc  3 
cc    
E
E$Nr   r
   )rV   r&   rH   r   pathlibr   	traitletsr   r   nbconvert.utils.ior   baser   r
   r   r   r   <module>r]      s-    A
   	  & + L* Lr   