o
    h                  
   @   s  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
 d dlmZ d dlmZmZmZmZmZmZmZmZ d dlmZmZ d dlZd d	lmZmZmZ d d
lmZ d dl m!Z! ddl"m#Z# ddl$m%Z%m&Z& ddl'm(Z(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5 e6dej7Z8eG dd dZ9G dd dej:Z;G dd dej<j=Z>G dd dej<j=Z?de@de@fddZAd e@de@fd!d"ZBd#e@d$eeC defd%d&ZDd'e;d#e@d(e@d)eEd*e@f
d+d,ZFdS )-    N)deque)	dataclassfield)datetime)chain)Path)AnyDictIteratorListNoReturnOptionalTupleUnion)quoteunquote)_DEFAULT_CALLBACKNoOpCallbackTqdmCallback)
isfilelike)Response   )	constants)CommitOperationCopyCommitOperationDelete)EntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundError)
hf_hub_urlhttp_get)HfApiLastCommitInfoRepoFile)HFValidationErrorhf_raise_for_statushttp_backoffzy
    (^refs\/convert\/\w+)     # `refs/convert/parquet` revisions
    |
    (^refs\/pr\/\d+)          # PR revisions
    c                   @   sX   e Zd ZU dZeed< eed< eed< eed< edddZee ed	< d
efddZ	dS )HfFileSystemResolvedPathzUData structure containing information about a resolved Hugging Face file system path.	repo_typerepo_idrevisionpath_in_repoNF)defaultrepr_raw_revisionreturnc                 C   s~   t j| jd| j }| jr| d| j d| j dS | jt j	kr4| dt
| j d| j dS | d| j dS )N @/)r   REPO_TYPES_URL_PREFIXESgetr'   r(   r-   r*   rstripr)   DEFAULT_REVISIONsafe_revision)self	repo_path r9   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/hf_file_system.py	unresolve0   s   "z"HfFileSystemResolvedPath.unresolve)
__name__
__module____qualname____doc__str__annotations__r   r-   r   r;   r9   r9   r9   r:   r&   $   s   
 r&   c                       s  e Zd ZdZdZdZddddee dee	edf f fdd	Z
d
ededee dee	ee f fddZdGdedee defddZdGdee ddfddZ			dHdededee dee ddf
ddZdGdedee ddfddZ			dIdede	dee dee ddf
d d!Z	dJded#e	d$e	dee deeeeeef f  f
d%d&Z				"dKdede	d$e	dee d'e	f
d(d)Zdedeeeee ee f  f fd*d+Zdedee f fd,d-Z					dLdedee d.e	d#e	d$e	dee deee eeeeef f f f fd/d0ZdGd1ed2edee ddfd3d4Zdedefd5d6Z dMded$e	dee deeef fd7d8Z!d9d: Z"d;d< Z#d=d> Z$dedefd?d@Z%e&dfdN fdAdBZ'e(dCdD Z)dEdF Z*  Z+S )OHfFileSystema8  
    Access a remote Hugging Face Hub repository as if were a local file system.

    <Tip warning={true}>

        [`HfFileSystem`] provides fsspec compatibility, which is useful for libraries that require it (e.g., reading
        Hugging Face datasets directly with `pandas`). However, it introduces additional overhead due to this compatibility
        layer. For better performance and reliability, it's recommended to use `HfApi` methods when possible.

    </Tip>

    Args:
        token (`str` or `bool`, *optional*):
            A valid user access token (string). Defaults to the locally saved
            token, which is the recommended method for authentication (see
            https://huggingface.co/docs/huggingface_hub/quick-start#authentication).
            To disable authentication, pass `False`.
        endpoint (`str`, *optional*):
            Endpoint of the Hub. Defaults to <https://huggingface.co>.
    Usage:

    ```python
    >>> from huggingface_hub import HfFileSystem

    >>> fs = HfFileSystem()

    >>> # List files
    >>> fs.glob("my-username/my-model/*.bin")
    ['my-username/my-model/pytorch_model.bin']
    >>> fs.ls("datasets/my-username/my-dataset", detail=False)
    ['datasets/my-username/my-dataset/.gitattributes', 'datasets/my-username/my-dataset/README.md', 'datasets/my-username/my-dataset/data.json']

    >>> # Read/write files
    >>> with fs.open("my-username/my-model/pytorch_model.bin") as f:
    ...     data = f.read()
    >>> with fs.open("my-username/my-model/pytorch_model.bin", "wb") as f:
    ...     f.write(data)
    ```
    r/   hfNendpointtokenrE   rF   c                   s<   t  j|i | |ptj| _|| _t||d| _i | _d S )NrD   )	super__init__r   ENDPOINTrE   rF   r    _api_repo_and_revision_exists_cache)r7   rE   rF   argsstorage_options	__class__r9   r:   rH   f   s   zHfFileSystem.__init__r'   r(   r)   r.   c              
   C   s   |||f| j vrqz| jj|||tjd W nK ttfy= } zd|f| j |||f< d|f| j ||d f< W Y d }~n8d }~w ty` } zd|f| j |||f< d| j ||d f< W Y d }~nd }~ww d| j |||f< d| j ||d f< | j |||f S )N)r)   r'   timeoutF)TN)rK   rJ   	repo_infor   HF_HUB_ETAG_TIMEOUTr   r#   r   )r7   r'   r(   r)   er9   r9   r:   _repo_and_revision_existx   s"   

 z%HfFileSystem._repo_and_revision_existpathc                 C   sp  dt t dt t dt t fdd}| |}|std|dd d tj v r@d|vr2td|dd	\}}tj| }ntj	}|
ddkrd
|v r|d
d	\}}d|v rt|}|dury|d| fv rytd|d}| }n|dd	\}}nd}|t||}| |||\}	}
|	st||
 nd}d|ddd }d|ddd }|dd }d|dd	d }|}|}| |||\}	}
|	st|
ttfr|}|}| |||\}	}|	st||
 n1t||
 n+|}d}d
|v r|d
d	\}}|t||}nd}| |||\}	}|	s%td|dur,|ntj}t|||||dS )a  
        Resolve a Hugging Face file system path into its components.

        Args:
            path (`str`):
                Path to resolve.
            revision (`str`, *optional*):
                The revision of the repo to resolve. Defaults to the revision specified in the path.

        Returns:
            [`HfFileSystemResolvedPath`]: Resolved path information containing `repo_type`, `repo_id`, `revision` and `path_in_repo`.

        Raises:
            `ValueError`:
                If path contains conflicting revision information.
            `NotImplementedError`:
                If trying to list repositories.
        revision_in_pathr)   r.   c                 S   s:   |d ur| d ur| |krt d|  d| d|S | }|S )NzRevision specified in path ("z ") and in `revision` argument ("z") are not the same.)
ValueError)rV   r)   r9   r9   r:   %_align_revision_in_path_with_revision   s   zHHfFileSystem.resolve_path.<locals>._align_revision_in_path_with_revisionz0Access to repositories lists is not implemented.r1   r   r   r0   Nr/      )r-   )r   r@   _strip_protocolNotImplementedErrorsplitr   r2   valuesREPO_TYPES_MAPPINGREPO_TYPE_MODELcountSPECIAL_REFS_REVISION_REGEXsearchgroupsublstripr   rT   _raise_file_not_foundjoin
isinstancer   r#   r5   r&   )r7   rU   r)   rX   r'   r(   rV   matchr*   repo_and_revision_existerrrepo_id_with_namespacepath_in_repo_with_namespacerepo_id_without_namespacepath_in_repo_without_namespace_r9   r9   r:   resolve_path   sx   







zHfFileSystem.resolve_pathc                 C   s   |s| j   | j  dS | |}| }|r'| j |d | |}|s|jsE| j|j|j	dfd | j|j|j	|j
fd dS dS )ac  
        Clear the cache for a given path.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.invalidate_cache).

        Args:
            path (`str`, *optional*):
                Path to clear from cache. If not provided, clear the entire cache.

        N)dircacheclearrK   rq   r;   pop_parentr*   r'   r(   r)   )r7   rU   resolved_pathr9   r9   r:   invalidate_cache   s   


zHfFileSystem.invalidate_cacherbmode
block_sizeHfFileSystemFilec                 K   sL   d|v rt d|dkrt| |f|||d|S t| |f|||d|S )Naz/Appending to remote files is not yet supported.r   )ry   r)   rz   )r[   HfFileSystemStreamFiler{   )r7   rU   ry   r)   rz   kwargsr9   r9   r:   _open  s
   zHfFileSystem._openc              
   K   sR   | j ||d}| jj|j|j| j|j|j|d|dd | j	|
 d d S )Nr)   commit_messagecommit_description)r*   r(   rF   r'   r)   r   r   rU   )rq   rJ   delete_filer*   r(   rF   r'   r)   r3   rw   r;   )r7   rU   r)   r~   rv   r9   r9   r:   _rm  s   	zHfFileSystem._rmF	recursivemaxdepthc              
      s    j ||d} j||||d} fdd|D }dd |D }	d| d}
|
|r+dnd	7 }
|
|d
ur9d| dnd	7 }
 jj|j|j j|	|j|d|
|dd  j	|
 d d
S )a  
        Delete files from a repository.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.rm).

        <Tip warning={true}>

            Note: When possible, use `HfApi.delete_file()` for better performance.

        </Tip>

        Args:
            path (`str`):
                Path to delete.
            recursive (`bool`, *optional*):
                If True, delete directory and all its contents. Defaults to False.
            maxdepth (`int`, *optional*):
                Maximum number of subdirectories to visit when deleting recursively.
            revision (`str`, *optional*):
                The git revision to delete from.

        r   )r   r   r)   c                    s"   g | ]}  |s |jqS r9   )isdirrq   r*   ).0rU   r7   r9   r:   
<listcomp>B     " z#HfFileSystem.rm.<locals>.<listcomp>c                 S   s   g | ]}t |d qS ))r*   )r   )r   r*   r9   r9   r:   r   C      zDelete  zrecursively r/   Nzup to depth r   r   )r(   r'   rF   
operationsr)   r   r   r   )rq   expand_pathrJ   create_commitr(   r'   rF   r)   r3   rw   r;   )r7   rU   r   r   r)   r~   rv   pathspaths_in_repor   r   r9   r   r:   rm"  s"   
	zHfFileSystem.rmTdetailrefreshc                    s   | j  |d}|  d|i|}z| j f||d|}W n4 tyS   |js.t d | j|  f||d|} fdd|D }t|dkrQt d Y nw |rX|S dd |D S )	a  
        List the contents of a directory.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.ls).

        <Tip warning={true}>

            Note: When possible, use `HfApi.list_repo_tree()` for better performance.

        </Tip>

        Args:
            path (`str`):
                Path to the directory.
            detail (`bool`, *optional*):
                If True, returns a list of dictionaries containing file information. If False,
                returns a list of file paths. Defaults to True.
            refresh (`bool`, *optional*):
                If True, bypass the cache and fetch the latest data. Defaults to False.
            revision (`str`, *optional*):
                The git revision to list from.

        Returns:
            `List[Union[str, Dict[str, Any]]]`: List of file paths (if detail=False) or list of file information
            dictionaries (if detail=True).
        r   expand_info)r   r)   Nc                       g | ]
}|d   kr|qS namer9   r   or   r9   r:   r   z      z#HfFileSystem.ls.<locals>.<listcomp>r   c                 S   s   g | ]}|d  qS r   r9   r   r9   r9   r:   r   }  s    )rq   r;   _ls_treer   r*   rf   ru   len)r7   rU   r   r   r)   r~   rv   outr9   r   r:   lsS  s   

zHfFileSystem.lsr   c              	      sN  j ||d}| }t|j|j|jd|jd }g }|jv r|sшj| }	||	 g }
|rgt	dd |	D }|rg|
 }|d jvrO|
|d  nj|d  }	||	 |dd |	D  |s<g }|rtfdd|D }|rx|
s||r|rtj|
| }|d	s||ks|t|
|v r|d	n|  fd
d|D }jD ]}| d	 rj|d  qj d  |j |d||d |S jj|j|j|||j|jd}|D ]A}t|tr|d	 |j |jd|j|j|j|jd}n|d	 |j dd|j |jd}|d }j!|g | || q|S )Nr   r/   r*   r-   c                 S      g | ]
}|d  dkr|qS type	directoryr9   r   	path_infor9   r9   r:   r     r   z)HfFileSystem._ls_tree.<locals>.<listcomp>r   c                 S   r   r   r9   r   r9   r9   r:   r     r   c                    s&   g | ]}|d  du r  |d qS )last_commitNr   )ru   r   r   r9   r:   r     s   & r1   c                    s"   g | ]}|d    d s|qS )r   r1   )
startswithr   )common_pathr9   r:   r     r   T)r   r   r)   r   )r   expandr)   r'   filer   sizer   blob_idlfsr   securityr   r   r   r   r   tree_idr   )"rq   r;   r&   r'   r(   r)   r-   rr   extendr   popleftappendosrU   commonprefixendswithr   r4   ru   r   rt   r   rJ   list_repo_treer*   rh   r"   r   r   r   r   r   r   
setdefault)r7   rU   r   r   r)   r   rv   	root_pathr   cached_path_infosdirs_not_in_dircachedirs_to_visitdir_infodirs_not_expandedcommon_prefixcached_pathtreer   cache_path_infoparent_pathr9   )r   r7   r:   r     s   




(
zHfFileSystem._ls_treec                 /   sR    d| ddi|}| j|| dd }t j|g|R i |E dH  dS )a  
        Return all files below the given path.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.walk).

        Args:
            path (`str`):
                Root path to list files from.

        Returns:
            `Iterator[Tuple[str, List[str], List[str]]]`: An iterator of (path, list of directory names, list of file names) tuples.
        r   r   Fr)   r   N)r3   rq   r;   rG   walk)r7   rU   rL   r~   rN   r9   r:   r     s   $zHfFileSystem.walkc                    s@   d| ddi|}| j|| dd }t j|fi |S )ah  
        Find files by glob-matching.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.glob).

        Args:
            path (`str`):
                Path pattern to match.

        Returns:
            `List[str]`: List of paths matching the pattern.
        r   r   Fr)   r   )r3   rq   r;   rG   globr7   rU   r~   rN   r9   r:   r     s   zHfFileSystem.globwithdirsc                    s  |rt  j|f|||||d|S | j||d}| }d|i|}z| j|fd||jd| W n tyR   | j|fd|i|d dkrN|i i ni  Y n+w |s]d	d
  D  n| j|fd|ji|}	|	d dkrt|	g  n  dd  D  t }
|s|
S  fdd|
D S )a  
        List all files below path.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.find).

        Args:
            path (`str`):
                Root path to list files from.
            maxdepth (`int`, *optional*):
                Maximum depth to descend into subdirectories.
            withdirs (`bool`, *optional*):
                Include directory paths in the output. Defaults to False.
            detail (`bool`, *optional*):
                If True, returns a dict mapping paths to file information. Defaults to False.
            refresh (`bool`, *optional*):
                If True, bypass the cache and fetch the latest data. Defaults to False.
            revision (`str`, *optional*):
                The git revision to list from.

        Returns:
            `Union[List[str], Dict[str, Dict[str, Any]]]`: List of paths or dict of file information.
        )r   r   r   r   r)   r   r   T)r   r   r)   r)   r   r   c                 S   s   g | ]
}|d  dkr|qS r   r9   r   r9   r9   r:   r   <  r   z%HfFileSystem.find.<locals>.<listcomp>r   c                 S   s   i | ]}|d  |qS r   r9   r   r9   r9   r:   
<dictcomp>A  r   z%HfFileSystem.find.<locals>.<dictcomp>c                       i | ]}| | qS r9   r9   )r   r   r   r9   r:   r   F  r   )	rG   findrq   r;   r   r)   r   infosorted)r7   rU   r   r   r   r   r)   r~   rv   r   namesrN   r   r:   r     s8    
 
zHfFileSystem.findpath1path2c                 K   s,  | j ||d}| j ||d}|j|jko|j|jk}|rEd| d| }| jj|j|j|j|d||ddt|j|j|jdgd n?| j	|d	|jd}	|	
 }
W d
   n1 s]w   Y  d| d| }| jj|
|j|j| j|j|j|d||dd | j| d | j| d d
S )a  
        Copy a file within or between repositories.

        <Tip warning={true}>

            Note: When possible, use `HfApi.upload_file()` for better performance.

        </Tip>

        Args:
            path1 (`str`):
                Source path to copy from.
            path2 (`str`):
                Destination path to copy to.
            revision (`str`, *optional*):
                The git revision to copy from.

        r   zCopy z to r   r   r/   )src_path_in_repor*   src_revision)r(   r'   r)   r   r   r   rx   Npath_or_fileobjr*   r(   rF   r'   r)   r   r   r   )rq   r'   r(   rJ   r   r)   r3   r   r*   openreadupload_filerF   rw   r;   )r7   r   r   r)   r~   resolved_path1resolved_path2	same_repor   fcontentr9   r9   r:   cp_fileH  sF   




zHfFileSystem.cp_filec                 K   s   | j |fi |}|d d S )ai  
        Get the last modified time of a file.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.modified).

        Args:
            path (`str`):
                Path to the file.

        Returns:
            `datetime`: Last commit date of the file.
        r   dater   )r7   rU   r~   r   r9   r9   r:   modified  s   zHfFileSystem.modifiedc                    s  | j |d}| |dd}|js<ddd |r;| jj|j|j|jdd }i  d	t	|j
|j|jd
d nd	 | }|sQ|| jvrQ| j|dd || jv rmfdd| j| D }	|	sitd	 |	d  |s} d	u s}|rڈ rڈ d d	u r| jj|j|j||j|jd}
|
std	 |
d }t|j|j|jd|jd }t|tr|d |j |jd|j|j|j|jd n|d |j dd|j|jd |sڇ fdddD   d	usJ  S )a  
        Get information about a file or directory.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.info).

        <Tip warning={true}>

            Note: When possible, use `HfApi.get_paths_info()` or `HfApi.repo_info()`  for better performance.

        </Tip>

        Args:
            path (`str`):
                Path to get info for.
            refresh (`bool`, *optional*):
                If True, bypass the cache and fetch the latest data. Defaults to False.
            revision (`str`, *optional*):
                The git revision to get info from.

        Returns:
            `Dict[str, Any]`: Dictionary containing file information (type, size, commit info, etc.).

        r   r   Tr   r   )r   r   r   )r'   r)   N)oidtitler   )r   r   Fr   c                    r   r   r9   r   r   r9   r:   r     r   z%HfFileSystem.info.<locals>.<listcomp>r   )r   r)   r'   r/   r   r1   r   r   r   c                    r   r9   r9   )r   kr   r9   r:   r     r   z%HfFileSystem.info.<locals>.<dictcomp>)rq   r;   r3   r*   rJ   list_repo_commitsr(   r'   r)   r!   	commit_idr   
created_atru   rr   r   rf   get_paths_infor&   r-   rh   r"   rU   r   r   r   r   r   r   )r7   rU   r   r)   r~   rv   r   r   r   out1
paths_infor   r   r9   )r   rU   r:   r     s   


 

zHfFileSystem.infoc                 K   sH   z| ddr| | | j|fi i |ddi W dS    Y dS )a  
        Check if a file exists.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.exists).

        <Tip warning={true}>

            Note: When possible, use `HfApi.file_exists()` for better performance.

        </Tip>

        Args:
            path (`str`):
                Path to check.

        Returns:
            `bool`: True if file exists, False otherwise.
        r   Fr   T)r3   rw   r   r   r9   r9   r:   exists  s   
zHfFileSystem.existsc                 C   s.   z| j |ddd dkW S  ty   Y dS w )ai  
        Check if a path is a directory.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.isdir).

        Args:
            path (`str`):
                Path to check.

        Returns:
            `bool`: True if path is a directory, False otherwise.
        Fr   r   r   )r   OSErrorr7   rU   r9   r9   r:   r     s
   zHfFileSystem.isdirc                 C   s&   z| j |ddd dkW S    Y dS )a`  
        Check if a path is a file.

        For more details, refer to [fsspec documentation](https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem.isfile).

        Args:
            path (`str`):
                Path to check.

        Returns:
            `bool`: True if path is a file, False otherwise.
        Fr   r   r   r   r   r9   r9   r:   isfile%  s   zHfFileSystem.isfilec                 C   sB   |  |}t|j|j|j|j| jd}| |r|ddd}|S )z
        Get the HTTP URL of the given path.

        Args:
            path (`str`):
                Path to get URL for.

        Returns:
            `str`: HTTP URL to access the file or directory on the Hub.
        )r'   r)   rE   z	/resolve/z/tree/r   )	rq   r   r(   r*   r'   r)   rE   r   replace)r7   rU   rv   urlr9   r9   r:   r   7  s   

zHfFileSystem.urlc              
      sb  | d}t| dh }t|ttfrt|dkr)t j||f||d|S t	|r0|}n| 
|r>tj|dd dS t|ttfrPtjtj|dd d}|du r]t|d}d}| }	| j||d	}
| j||d	d
 }|| z2tt|
j|
j|
j|
j| jd|||d| j t|tr|jndd | |	 W |r|!  dS dS |r|!  w w )a^  
        Copy single remote file to local.

        <Tip warning={true}>

            Note: When possible, use `HfApi.hf_hub_download()` for better performance.

        </Tip>

        Args:
            rpath (`str`):
                Remote path to download from.
            lpath (`str`):
                Local path to download to.
            callback (`Callback`, *optional*):
                Optional callback to track download progress. Defaults to no callback.
            outfile (`IO`, *optional*):
                Optional file-like object to write to. If provided, `lpath` is ignored.

        r)   r   )callbackoutfileT)exist_okNFwbr   r   r(   r)   filenamer'   rE   )r   	temp_filedisplayed_filenameexpected_sizeresume_sizeheaders	_tqdm_bar)"r3   setkeysrh   r   r   r   rG   get_filer   r   r   makedirsr@   r   rU   dirnamer   tellrq   r   set_sizer   r   r(   r)   r*   r'   rE   rJ   _build_hf_headerstqdmseekclose)r7   rpathlpathr   r   r~   r)   unhandled_kwargs
close_fileinitial_posresolve_remote_pathr   rN   r9   r:   r  N  sR   




zHfFileSystem.get_filec                 C      t d)zA context within which files are committed together upon exit

        Requires the file class to implement `.commit()` and `.discard()`
        for the normal and exception cases.
        (Transactional commits are not supported.r[   r   r9   r9   r:   transaction  s   	zHfFileSystem.transactionc                 C   r  )z@Begin write transaction for deferring files, non-context versionr  r  r   r9   r9   r:   start_transaction  s   zHfFileSystem.start_transactionN)rx   NN)FNN)TFN)FFNT)NFFFN)FNr.   N),r<   r=   r>   r?   root_markerprotocolr   r@   r   boolrH   r   	ExceptionrT   r&   rq   rw   intr   r   r   r   r	   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r  propertyr  r  __classcell__r9   r9   rN   r:   rB   :   s    (
_

2
/
,h	 =;(dG

rB   c                       s   e Zd Zddededee f fddZ fddZd	ed
ede	fddZ
dddZddeddfddZd fdd	ZdefddZ  ZS )r{   NfsrU   r)   c              
      s   z
|j ||d| _W n ty' } zd|ddv r"t| d| d }~ww |dddkr;|j| j dd| _t j|| j fi | |  d S )	Nr   wry   r/   B.
Make sure the repository and revision exist before writing data.rx   Fr   )	rq   rv   FileNotFoundErrorr3   r   r;   detailsrG   rH   )r7   r!  rU   r)   r~   rS   rN   r9   r:   rH     s    zHfFileSystemFile.__init__c                       t | dsd S t  S Nrv   hasattrrG   __del__r   rN   r9   r:   r*       

zHfFileSystemFile.__del__startendr.   c                 C   sl   dd| d|d  i| j j }t| jj| jj| jj| jj| j j	d}t
d||dtjd}t| |jS )	Nrangezbytes=-r   r   GETi  i  i  i  )r   retry_on_status_codesrP   )r!  rJ   r	  r   rv   r(   r)   r*   r'   rE   r%   r   HF_HUB_DOWNLOAD_TIMEOUTr$   r   )r7   r,  r-  r   r   rr9   r9   r:   _fetch_range  s(   
zHfFileSystemFile._fetch_rangec                 C   s   t jddd| _d S )Nzhffs-F)prefixdelete)tempfileNamedTemporaryFiler   r   r9   r9   r:   _initiate_upload  s   z!HfFileSystemFile._initiate_uploadFfinalc                 C   s   | j d | j  }| j| |rN| j  | jjj| jj	| j
j| j
j| jj| j
j| j
j| jd| jdd t| jj	 | jj| j
 d d S d S )Nr   r   r   r   r   )bufferr  r   r   writer  r!  rJ   r   r   rv   r*   r(   rF   r'   r)   r~   r3   r   removerw   r;   )r7   r;  blockr9   r9   r:   _upload_chunk  s(   





zHfFileSystemFile._upload_chunkr   c                    sl   | j dkr0|du s|dkr0| jdkr0| jj| jddd}| W  d   S 1 s+w   Y  t |S )a)  Read remote file.

        If `length` is not provided or is -1, the entire file is downloaded and read. On POSIX systems and if
        `hf_transfer` is not enabled, the file is loaded in memory directly. Otherwise, the file is downloaded to a
        temporary file and read from there.
        rx   Nr   r   )rz   )ry   locr!  r   rU   r   rG   )r7   lengthr   rN   r9   r:   r     s
   $ zHfFileSystemFile.readc                 C      | j | jS r  r!  r   rU   r   r9   r9   r:   r        zHfFileSystemFile.urlr  r  )Fr   )r<   r=   r>   rB   r@   r   rH   r*  r  bytesr5  r:  r  r@  r   r   r   r9   r9   rN   r:   r{     s     
r{   c                       s   e Zd Z				ddedededee d	ed
ef fddZddedefddZddefddZ	defddZ
 fddZdd Z  ZS )r}   rx   Nr   noner!  rU   ry   r)   rz   
cache_typec           	   
      s   |dkrt d| |dkrt d| d|v r"t d| dz
|j||d| _W n" tyN } zd|d	d
v rDt| d|W Y d }~nd }~ww | j d d| _t j|| j f|||d| d | _	|  d S )Nr   z:HfFileSystemStreamFile only supports block_size=0 but got rH  z?HfFileSystemStreamFile only supports cache_type='none' but got r"  z;HfFileSystemStreamFile only supports reading but got mode=''r   ry   r/   r#  )r   r   ry   rz   rI  )
rW   rq   rv   r$  r3   r;   r%  rG   rH   response)	r7   r!  rU   ry   r)   rz   rI  r~   rS   rN   r9   r:   rH     s8   

zHfFileSystemStreamFile.__init__rA  whencec                 C   s2   |dkr
|dkr
d S || j kr|dkrd S td)Nr   r   zCannot seek streaming HF file)rA  rW   )r7   rA  rM  r9   r9   r:   r    s
   zHfFileSystemStreamFile.seekr   rB  c                 C   sJ  |dkr|fnd}| j d u s| j j r<t| jj| jj| jj| jj| j	j
d}td|| j	j ddtjd| _ t| j  z	| j jj| }W nT ty   | j   t| jj| jj| jj| jj| j	j
d}td|dd	| j i| j	j ddtjd| _ t| j  z	| j jj| }W n ty   | j    w Y nw |  jt|7  _|S )
Nr   r9   r   r0  r1  T)r   r2  streamrP   Rangez	bytes=%d-)rL  rawisclosedr   rv   r(   r)   r*   r'   r!  rE   r%   rJ   r	  r   r3  r$   r   r  r  rA  r   )r7   rB  	read_argsr   r   r9   r9   r:   r   "  s`   




zHfFileSystemStreamFile.readr.   c                 C   rC  r  rD  r   r9   r9   r:   r   S  rE  zHfFileSystemStreamFile.urlc                    r&  r'  r(  r   rN   r9   r:   r*  V  r+  zHfFileSystemStreamFile.__del__c                 C   s   t | j| j| j| j| jjffS r  )reopenr!  rU   ry   	blocksizecacher   r   r9   r9   r:   
__reduce__\  s   z!HfFileSystemStreamFile.__reduce__)rx   Nr   rH  )r   rF  )r<   r=   r>   rB   r@   r   r  rH   r  r   r   r*  rV  r   r9   r9   rN   r:   r}     s.    1r}   r)   r.   c                 C   s   t | r| S t| S r  )ra   ri   
safe_quoter   r9   r9   r:   r6   `  s   r6   sc                 C   s   t | ddS )Nr/   )safe)r   )rX  r9   r9   r:   rW  d  s   rW  rU   rk   c                 C   sN   | }t |tr|  d}nt |tr|  d}n
t |tr"|  d}t||)Nz (repository not found)z (revision not found)z (invalid repository id))rh   r   r   r#   r$  )rU   rk   msgr9   r9   r:   rf   h  s   




rf   r!  ry   rz   rI  c                 C   s   | j ||||dS )NrK  )r   )r!  rU   ry   rz   rI  r9   r9   r:   rS  s  s   rS  )Gr   rer8  collectionsr   dataclassesr   r   r   	itertoolsr   pathlibr   typingr   r	   r
   r   r   r   r   r   urllib.parser   r   fsspecfsspec.callbacksr   r   r   fsspec.utilsr   requestsr   r/   r   _commit_apir   r   errorsr   r   r   file_downloadr   r   hf_apir    r!   r"   utilsr#   r$   r%   compileVERBOSEra   r&   AbstractFileSystemrB   specAbstractBufferedFiler{   r}   r@   r6   rW  r  rf   r  rS  r9   r9   r9   r:   <module>   sN    (
      sTe"