a
    ø£Be@  ã                   @   s  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 ddl	m
Z
 ddlZddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ dd„ Zdd„ Zd"dd„Zd#dd„Zdd„ Zdd„ Zed$dd„ƒZ ed%dd„ƒZ!G dd„ deeƒZ"G d d!„ d!e"ƒZ#dS )&z9
Utilities for file-based Contents/Checkpoints managers.
é    N)ÚdecodebytesÚencodebytes)Úcontextmanager)Úpartial)Úrun_sync)Ú	HTTPError)ÚBool)ÚConfigurable)ÚLoggingConfigurable)ÚApiPathÚto_api_pathÚ
to_os_pathc                 C   s   t  | |¡ dS )zreplace dst with srcN)ÚosÚreplace©ÚsrcÚdst© r   új/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/services/contents/fileio.pyÚreplace_file   s    r   c                 Ã   s   t tj| |ƒI dH  dS )z#replace dst with src asynchronouslyN)r   r   r   r   r   r   r   Úasync_replace_file   s    r   c                 C   sH   t  | |¡ zt  | |¡ W n& tyB   |r>|jd|dd� Y n0 dS )zZcopy src to dst

    like shutil.copy2, but log errors in copystat instead of raising
    úcopystat on %s failedT©Úexc_infoN)ÚshutilÚcopyfileÚcopystatÚOSErrorÚdebug©r   r   Úlogr   r   r   Ú
copy2_safe!   s    r!   c                 Ã   sX   t tj| |ƒI dH  zt tj| |ƒI dH  W n& tyR   |rN|jd|dd� Y n0 dS )zicopy src to dst asynchronously

    like shutil.copy2, but log errors in copystat instead of raising
    Nr   Tr   )r   r   r   r   r   r   r   r   r   r   Úasync_copy2_safe.   s    r"   c                 C   s"   t j | ¡\}}t j |d| ¡S )ztName of the intermediate file used in atomic writes.

    The .~ prefix will make Dropbox ignore the temporary file.z.~©r   ÚpathÚsplitÚjoin©r$   ÚdirnameÚbasenamer   r   r   Úpath_to_intermediate;   s    r*   c                 C   s"   t j | ¡\}}t j ||d ¡S )zEName of invalid file after a failed atomic write and subsequent read.z.invalidr#   r'   r   r   r   Úpath_to_invalidC   s    r+   Túutf-8c                 k   sî   t j | ¡r(t j t j | ¡t  | ¡¡} t| ƒ}t j | ¡rJt| ||d� |rr| 	dd¡ t
| dfd|i|¤Ž}nt
| dfi |¤Ž}z
|V  W n& ty´   | ¡  t|| ƒ ‚ Y n0 | ¡  t  | ¡ ¡ | ¡  t j |¡rêt  |¡ dS )aÑ  Context manager to write to a file only if the entire write is successful.

    This works by copying the previous file contents to a temporary file in the
    same directory, and renaming that file back to the target if the context
    exits with an error. If the context is successful, the new data is synced to
    disk and the temporary file is removed.

    Parameters
    ----------
    path : str
        The target file to write to.
    text : bool, optional
        Whether to open the file in text mode (i.e. to write unicode). Default is
        True.
    encoding : str, optional
        The encoding to use for files opened in text mode. Default is UTF-8.
    **kwargs
        Passed to :func:`io.open`.
    ©r    ÚnewlineÚ
ÚwÚencodingÚwbN)r   r$   Úislinkr&   r(   Úreadlinkr*   Úisfiler!   Ú
setdefaultÚopenÚBaseExceptionÚcloser   ÚflushÚfsyncÚfilenoÚremove)r$   Útextr1   r    ÚkwargsÚtmp_pathÚfileobjr   r   r   Úatomic_writingI   s(    

rB   c                 k   s–   t j | ¡r(t j t j | ¡t  | ¡¡} |rP| dd¡ t| dfd|i|¤Ž}nt| dfi |¤Ž}z
|V  W n tyˆ   | 	¡  ‚ Y n0 | 	¡  dS )aÊ  Context manager to write file without doing atomic writing
    (for weird filesystem eg: nfs).

    Parameters
    ----------
    path : str
        The target file to write to.
    text : bool, optional
        Whether to open the file in text mode (i.e. to write unicode). Default is
        True.
    encoding : str, optional
        The encoding to use for files opened in text mode. Default is UTF-8.
    **kwargs
        Passed to :func:`io.open`.
    r.   r/   r0   r1   r2   N)
r   r$   r3   r&   r(   r4   r6   r7   r8   r9   )r$   r>   r1   r    r?   rA   r   r   r   Ú_simple_writing‚   s    
rC   c                   @   sx   e Zd ZdZedddd�Zedd„ ƒZedd„ ƒZedd
d„ƒZ	dd„ Z
dd„ Zddd„Zddd„Zdd„ Zdd„ ZdS )ÚFileManagerMixina›  
    Mixin for ContentsAPI classes that interact with the filesystem.

    Provides facilities for reading, writing, and copying files.

    Shared by FileContentsManager and FileCheckpoints.

    Note
    ----
    Classes using this mixin must provide the following attributes:

    root_dir : unicode
        A directory against against which API-style paths are to be resolved.

    log : logging.Logger
    Ta}  By default notebooks are saved on disk on a temporary file and then if succefully written, it replaces the old ones.
      This procedure, namely 'atomic_writing', causes some bugs on file system without operation order enforcement (like some networked fs).
      If set to False, the new notebook is written directly on the old one which could fail (eg: full filesystem or quota ))ÚconfigÚhelpc              	   o   sj   |   |¡�L t|g|¢R i |¤Ž�}|V  W d  ƒ n1 s>0    Y  W d  ƒ n1 s\0    Y  dS )z<wrapper around io.open that turns permission errors into 403N)Úperm_to_403r7   ©ÚselfÚos_pathÚargsr?   Úfr   r   r   r7   Ã   s    $zFileManagerMixin.openc              	   o   s¸   |   |¡�š | j|d< | jrZt|g|¢R i |¤Ž�}|V  W d  ƒ q–1 sN0    Y  n<t|g|¢R i |¤Ž�}|V  W d  ƒ n1 sŒ0    Y  W d  ƒ n1 sª0    Y  dS )záwrapper around atomic_writing that turns permission errors to 403.
        Depending on flag 'use_atomic_writing', the wrapper perform an actual atomic writing or
        simply writes the file (whatever an old exists or not)r    N)rG   r    Úuse_atomic_writingrB   rC   rH   r   r   r   rB   É   s    
&zFileManagerMixin.atomic_writingÚ c              
   c   sv   z
dV  W nf t yp } zN|jtjtjhv rZ|s:|jp8d}t|| jd�}tdd| ƒ|‚n‚ W Y d}~n
d}~0 0 dS )z7context manager for turning permission errors into 403.Nzunknown file)Úrooti“  zPermission denied: %s)r   ÚerrnoÚEPERMÚEACCESÚfilenamer   Úroot_dirr   )rI   rJ   Úer$   r   r   r   rG   ×   s    

zFileManagerMixin.perm_to_403c                 C   s   t ||| jd� dS ©zPcopy src to dest

        like shutil.copy2, but log errors in copystat
        r-   N)r!   r    ©rI   r   Údestr   r   r   Ú_copyè   s    zFileManagerMixin._copyc                 C   st   | j  d|¡ tj | j¡}tj |¡d r:tdd| ƒ‚tt	|ƒ|ƒ}tj |¡tjj
  |¡sptdd| ƒ‚|S )aS  Given an API path, return its file system path.

        Parameters
        ----------
        path : str
            The relative API path to the named file.

        Returns
        -------
        path : str
            Native, absolute OS path to for a file.

        Raises
        ------
        404: if path is outside root
        zReading path from disk: %sr   i”  z%s is not a relative API pathz%%s is outside root contents directory)r    r   r   r$   ÚabspathrT   Ú
splitdriver   r   r   ÚsepÚ
startswith)rI   r$   rO   rJ   r   r   r   Ú_get_os_pathï   s    zFileManagerMixin._get_os_pathé   Nc           	      C   sÚ   | j |ddd��¶}z tj|||d�W W  d  ƒ S  tyZ } z|}W Y d}~n
d}~0 0 t|ƒ}| jrvtj |¡sŒt	dd|› d|›�ƒ‚t
|ƒ}t||ƒ t||ƒ | j|||d	�W  d  ƒ S 1 sÌ0    Y  dS )
ú Read a notebook from an os path.Úrr,   ©r1   ©Ú
as_versionÚcapture_validation_errorNé�  úUnreadable Notebook: ú ©re   )r7   ÚnbformatÚreadÚ	Exceptionr*   rM   r   r$   Úexistsr   r+   r   Ú_read_notebook©	rI   rJ   rd   re   rL   rU   Ze_origr@   Zinvalid_filer   r   r   rn   
  s&    ÿþ

ÿzFileManagerMixin._read_notebookc                 C   sF   | j |dd��$}tj||tj|d� W d  ƒ n1 s80    Y  dS ©zSave a notebook to an os_path.r,   rb   )Úversionre   N)rB   rj   ÚwriteÚ
NO_CONVERT©rI   rJ   Únbre   rL   r   r   r   Ú_save_notebook'  s    üzFileManagerMixin._save_notebookc              
   C   sÂ   t j |¡stdd| ƒ‚|  |d¡�}| ¡ }W d  ƒ n1 sD0    Y  |du s^|dkr°z| d¡dfW S  ty® } z(|dkrštdd| dd	�|‚W Y d}~n
d}~0 0 t|ƒ d
¡dfS ©a*  Read a non-notebook file.

        os_path: The path to be read.
        format:
          If 'text', the contents will be decoded as UTF-8.
          If 'base64', the raw bytes contents will be encoded as base64.
          If not specified, try to decode as UTF-8, and fall back to base64
        rf   zCannot read non-file %sÚrbNr>   Úutf8z%s is not UTF-8 encodedz
bad format)ÚreasonÚasciiÚbase64)	r   r$   r5   r   r7   rk   ÚdecodeÚUnicodeErrorr   ©rI   rJ   ÚformatrL   ÚbcontentrU   r   r   r   Ú
_read_file1  s"    	&ýüzFileManagerMixin._read_filec              
   C   s¶   |dvrt ddƒ‚z*|dkr(| d¡}n| d¡}t|ƒ}W n< tyx } z$t dd|› d|› �ƒ|‚W Y d	}~n
d	}~0 0 | j|d
d��}| |¡ W d	  ƒ n1 s¨0    Y  d	S ©zSave content of a generic file.>   r|   r>   rf   z:Must specify format of file contents as 'text' or 'base64'r>   ry   r{   zEncoding error saving z: NF)r>   )r   Úencoder   rl   rB   rr   ©rI   rJ   Úcontentr€   r�   Z	b64_bytesrU   rL   r   r   r   Ú
_save_fileN  s    þ
.zFileManagerMixin._save_file)rN   )r_   N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rM   r   r7   rB   rG   rY   r^   rn   rv   r‚   r‡   r   r   r   r   rD   ©   s$   ý




rD   c                   @   s<   e Zd ZdZdd„ Zddd„Zddd	„Zd
d„ Zdd„ ZdS )ÚAsyncFileManagerMixinzY
    Mixin for ContentsAPI classes that interact with the filesystem asynchronously.
    c                 Ã   s   t ||| jd�I dH  dS rV   )r"   r    rW   r   r   r   rY   g  s    zAsyncFileManagerMixin._copyr_   Nc           	      Ã   sö   | j |dd��Ô}z,tttj||d�|ƒI dH W W  d  ƒ S  tyd } z|}W Y d}~n
d}~0 0 t|ƒ}| jr€tj	 
|¡s–tdd|› d|›�ƒ‚t|ƒ}t||ƒI dH  t||ƒI dH  | j|||d�I dH W  d  ƒ S 1 sè0    Y  dS )	r`   r,   rb   rc   Nrf   rg   rh   ri   )r7   r   r   rj   rk   rl   r*   rM   r   r$   rm   r   r+   r   rn   ro   r   r   r   rn   n  s0    ýúþÿz$AsyncFileManagerMixin._read_notebookc                 Ã   sR   | j |dd��0}tttjtj|d�||ƒI dH  W d  ƒ n1 sD0    Y  dS rp   )rB   r   r   rj   rr   rs   rt   r   r   r   rv   �  s    ýùz$AsyncFileManagerMixin._save_notebookc              
   Ã   sÊ   t j |¡stdd| ƒ‚|  |d¡� }t|jƒI dH }W d  ƒ n1 sL0    Y  |du sf|dkr¸z| d¡dfW S  ty¶ } z(|dkr¢tdd| dd	�|‚W Y d}~n
d}~0 0 t	|ƒ d
¡dfS rw   )
r   r$   r5   r   r7   r   rk   r}   r~   r   r   r   r   r   r‚   �  s"    	.ýüz AsyncFileManagerMixin._read_filec              
   Ã   s¾   |dvrt ddƒ‚z*|dkr(| d¡}n| d¡}t|ƒ}W n< tyx } z$t dd|› d|› �ƒ|‚W Y d	}~n
d	}~0 0 | j|d
d��"}t|j|ƒI d	H  W d	  ƒ n1 s°0    Y  d	S rƒ   )r   r„   r   rl   rB   r   rr   r…   r   r   r   r‡   º  s    þ
.z AsyncFileManagerMixin._save_file)r_   N)N)	rˆ   r‰   rŠ   r‹   rY   rn   rv   r‚   r‡   r   r   r   r   rŒ   b  s   
"
rŒ   )N)N)Tr,   N)Tr,   N)$r‹   rP   r   r   r|   r   r   Ú
contextlibr   Ú	functoolsr   rj   Zanyio.to_threadr   Ztornado.webr   Z	traitletsr   Ztraitlets.configr	   Ztraitlets.config.configurabler
   Zjupyter_server.utilsr   r   r   r   r   r!   r"   r*   r+   rB   rC   rD   rŒ   r   r   r   r   Ú<module>   s4   

8& :