
    Mh                     r    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dS )
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                              d          Z ed                              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                 H    |d         }|r|                      |           d S d S )Nnew)_makedir)selfchanger   s      W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/nbconvert/writers/files.py_build_directory_changedz$FilesWriter._build_directory_changed&   s5    Um 	MM#	 	    c                 r     t                      j        di | |                     d| j        i           dS )zInitialize the writer.r   N )super__init__r   r   )r   kw	__class__s     r   r   zFilesWriter.__init__,   sB    2%%ud.B&CDDDDDr     c                 j   t           j                            |          sb| j                            d|           	 t          j        ||           dS # t          $ r!}|j        t          j        k    r Y d}~dS d}~ww xY wt           j        	                    |          st          d|z            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   es       r   r   zFilesWriter._makedir1   s     w~~d## 	EHMM/666Dt,,,,,,   7el** +***** t$$ 	E<tCDDD	E 	Es   A 
A?A::A?c                    |D ]\  }}t           j                            ||          }t           j                            |          }|                     |           | j                            dt          |          |           t          |d          5 }|	                    |           ddd           n# 1 swxY w Y   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!   fs           r   _write_itemszFilesWriter._write_itemsC   s    # 		 		NHd7<<	844D7??4((DMM$ HNN3SYYEEEdD!! Q              		 		s   B77B;	>B;	Nc           	         |d}t          |          |                    dd          }|                    di                               dd          }| j        p|}| j        p|}	|                    di                                           }
|
rc| j                            dt          j        	                    |                    d	d          d                     | 
                    |
|	           |                    d
i                                           }|rc| j                            dt          j        	                    |                    dd          d                     | 
                    ||	           |	r| j        D ]}t          j        |          D ]}|dk    r!t          j                            ||          }n|}t          j        	                    |	|          }t          j                            |          }|                     |           t          j                            |          t          j                            |          k    r,| j                            d||           t!          ||           |||z   n|}t#          |	          |z  }| j                            dt%          |          |           t'          |t(                    r@t+          |dd          5 }|                    |           ddd           n# 1 swxY w Y   n=t+          |d          5 }|                    |           ddd           n# 1 swxY 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   r4   r#   r$   r    r!   r.   r:   filesglobr/   r   normpathr   r   r1   
isinstancestrr2   r3   )r   output	resourcesr<   r   msgr=   resource_pathrG   r   r4   rA   r6   matching_filenamedest_filenamer8   r!   	dest_pathr9   s                      r   r3   zFilesWriter.writeP   s     !CC..  %==);TBB "j"5599&"EE,/-.?- i,,2244 	6HMM-Y]]+=rBBBGG   e_555  mmM266<<>> 	<HMM+Y]]+A2FFKK   k?;;;  	> J > >)-8)<)< > >%"}}(*8I7(S(S(9 7<<GGD7??400DMM$''' w''--1A1ABS1T1TTT&8:KTRRR$%6===>$ 4D3O}///Ub))D0	 	.FYGGGfc"" 	 iw777  1                              i&&  !                              s$   :LL #L 8MM!M)r   )N)__name__
__module____qualname____doc__r   tagr   rG   r   r   r   r   r:   r3   __classcell__)r   s   @r   r
   r
      s       77g
M   
cc  g3   
cc  W   
E E E E E
E E E E$  N N N N N N N Nr   r
   )rW   r'   rI   r    pathlibr   	traitletsr   r   nbconvert.utils.ior   baser   r
   r   r   r   <module>r^      s    A A
   				       & & & & & & & & + + + + + +      L L L L L* L L L L Lr   