U
    6‹Ñhã  ã                   @   sR   d dl Z d dlZd dlZd dlZd dlZG dd„ dejƒZG dd„ dejjƒZ	dS )é    Nc                       s^   e Zd ZdZdZd‡ fdd„	Zddd„Zdd	d
„Zdd„ Zddd„Z	dd„ Z
ddd„Z‡  ZS )ÚJupyterFileSystemz?View of the files as seen by a Jupyter server (notebook or lab))ZjupyterZjlabNc              
      s¤   d|krb|dkrRzt  d|¡d }W n, tk
rP } ztdƒ|‚W 5 d}~X Y nX | dd¡d }| d¡d | _t ¡ | _	|r’d	|› �| j	j
d
< tƒ jf |Ž dS )aD  

        Parameters
        ----------
        url : str
            Base URL of the server, like "http://127.0.0.1:8888". May include
            token in the string, which is given by the process when starting up
        tok : str
            If the token is obtained separately, can be given here
        kwargs
        ú?Nztoken=([a-z0-9]+)r   zCould not determine tokené   ú/z/api/contentsztoken ÚAuthorization)ÚreÚfindallÚ
IndexErrorÚ
ValueErrorÚsplitÚrstripÚurlÚrequestsÚSessionÚsessionÚheadersÚsuperÚ__init__)Úselfr   ÚtokÚkwargsÚe©Ú	__class__© úL/tmp/user/2202/pip-unpacked-wheel-org7786_/fsspec/implementations/jupyter.pyr      s    
zJupyterFileSystem.__init__Tc                 K   s¬   |   |¡}| j | j› d|› �¡}|jdkr4t|ƒS | ¡  | ¡ }|d dkrZ|d }n|g}|D ]0}| d¡|d< | d¡ |d dkrdd	|d< qd|rž|S d
d„ |D ƒS )Nr   é”  ÚtypeÚ	directoryÚcontentÚpathÚnameZnotebookÚfilec                 S   s   g | ]}|d  ‘qS )r!   r   )Ú.0Úor   r   r   Ú
<listcomp><   s     z(JupyterFileSystem.ls.<locals>.<listcomp>)	Ú_strip_protocolr   Úgetr   Ústatus_codeÚFileNotFoundErrorÚraise_for_statusÚjsonÚpop)r   r    Zdetailr   ÚrÚoutr$   r   r   r   Úls)   s"    




zJupyterFileSystem.lsc                 K   sx   |   |¡}| j | j› d|› �¡}|jdkr4t|ƒS | ¡  | ¡ }|d dkr^|d  ¡ }nt	 
|d ¡}|||… S )Nr   r   ÚformatÚtextr   )r&   r   r'   r   r(   r)   r*   r+   ÚencodeÚbase64Ú	b64decode)r   r    ÚstartÚendr   r-   r.   Úbr   r   r   Úcat_file>   s    

zJupyterFileSystem.cat_filec                 K   sV   |   |¡}| dd¡d |t|ƒt |¡ ¡ dddœ}| jj| j› d|› �|d� d S )Nr   r   éÿÿÿÿr3   r"   )r!   r    Úsizer   r0   r   ©r+   )	r&   ÚrsplitÚlenr3   Ú	b64encodeÚdecoder   Úputr   )r   r    ÚvalueÚ_r+   r   r   r   Ú	pipe_fileL   s    
úzJupyterFileSystem.pipe_filec                 K   sj   |   |¡}|r.d|kr.|  | dd¡d d¡ | dd¡d |d d ddœ}| jj| j› d|› �|d� d S )	Nr   r   r   Tr9   r   )r!   r    r:   r   r   r;   )r&   Úmkdirr<   r   r@   r   )r   r    Zcreate_parentsr   r+   r   r   r   rD   X   s    
ûzJupyterFileSystem.mkdirc                 C   s&   |   |¡}| j | j› d|› �¡ d S )Nr   )r&   r   Údeleter   )r   r    r   r   r   Ú_rme   s    
zJupyterFileSystem._rmÚrbc                 K   s8   |   |¡}|dkr&|  |¡}t |¡S t| |dd�S d S )NrG   Úwb)Úmode)r&   r8   ÚioÚBytesIOÚSimpleFileWriter)r   r    rI   r   Údatar   r   r   Ú_openi   s
    


zJupyterFileSystem._open)N)T)NN)T)rG   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úprotocolr   r/   r8   rC   rD   rF   rN   Ú__classcell__r   r   r   r   r   
   s   


r   c                   @   s   e Zd Zddd„ZdS )rL   Fc                 C   s6   |dkrdS | j  d¡ | j  ¡ }| j | j|¡ dS )zWNever uploads a chunk until file is done

        Not suitable for large files
        Fr   N)ÚbufferÚseekÚreadÚfsrC   r    )r   ÚfinalrM   r   r   r   Ú_upload_chunks   s
    
zSimpleFileWriter._upload_chunkN)F)rO   rP   rQ   rZ   r   r   r   r   rL   r   s   rL   )
r3   rJ   r   r   ZfsspecZAbstractFileSystemr   ÚspecZAbstractBufferedFilerL   r   r   r   r   Ú<module>   s   h