
    g              
          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jl                  dejn                        Z8e G d d             Z9 G d dejt                        Z; G d dejx                  jz                        Z> G d dejx                  jz                        Z?de@de@fdZAde@de@fd ZBd!e@d"eeC   defd#ZDd$e;d!e@d%e@d&eEd'e@f
d(ZFy))    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                   j    e Zd ZU dZeed<   eed<   eed<   eed<    edd      Zee   ed	<   d
efdZ	y)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                    t         j                  j                  | j                  d      | j                  z   }| j
                  r-| d| j
                   d| j                   j                  d      S | j                  t         j                  k7  r6| dt        | j                         d| j                   j                  d      S | d| j                   j                  d      S )N @/)r   REPO_TYPES_URL_PREFIXESgetr)   r*   r/   r,   rstripr+   DEFAULT_REVISIONsafe_revision)self	repo_paths     S/var/www/openai/venv/lib/python3.12/site-packages/huggingface_hub/hf_file_system.py	unresolvez"HfFileSystemResolvedPath.unresolve0   s    5599$.."MPTP\P\\	[$"4"4!5Qt7H7H6IJQQRUVV]]i888[-">!?qARAR@ST[[\_``[$"3"3!45<<SAA    )
__name__
__module____qualname____doc__str__annotations__r   r/   r   r=    r>   r<   r(   r(   $   s?    _NLM $)E#BM8C=BB3 Br>   r(   c                       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Z
d	ed
edee   dee	ee   f   fdZd.dedee   defdZd.dee   ddfdZ	 	 	 d/dededee   dee   ddf
dZd.dedee   ddfdZ	 	 	 d0dede	dee   dee   ddf
dZ	 d1dede	de	dee   deeeeeef   f      f
dZ	 	 	 	 d2dede	de	dee   de	f
dZdedeeeee   ee   f      f fdZdedee   f fdZ	 	 	 	 	 d3d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!Zd.d"ed#edee   ddfd$Zdedefd%Z d4dede	dee   deeef   fd&Z!d' Z"d( Z#d) Z$dedefd*Z%e&dfd5 fd+Z'e(d,        Z)d- Z* xZ+S )6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)
    ```
    r2   hfNendpointtokenrJ   rK   c                    t        |   |i | |xs t        j                  | _        || _        t        ||      | _        i | _        y )NrI   )	super__init__r   ENDPOINTrJ   rK   r!   _api_repo_and_revision_exists_cache)r:   rJ   rK   argsstorage_options	__class__s        r<   rN   zHfFileSystem.__init__f   sK     	$2/2 6I$6$6
859	  	,r>   r)   r*   r+   r0   c                    |||f| j                   vrR	 | j                  j                  |||t        j                         d| j                   |||f<   d| j                   ||d f<   | j                   |||f   S # t
        t        f$ r2}d|f| j                   |||f<   d|f| j                   ||d f<   Y d }~Nd }~wt        $ r0}d|f| j                   |||f<   d| j                   ||d f<   Y d }~d }~ww xY w)N)r+   r)   timeout)TNF)rQ   rP   	repo_infor   HF_HUB_ETAG_TIMEOUTr   r$   r   )r:   r)   r*   r+   es        r<   _repo_and_revision_existz%HfFileSystem._repo_and_revision_existx   s!    w)1U1UU^		##h)YMjMj $  Xb44i(5STS]44i$5OP33Y4RSS ,->? \W\^_W_44i(5STSXZ[S[44i$5OP( ^W\^_W_44i(5STS]44i$5OP^s#   -A6 6C.(B22C.>&C))C.pathc                    dt         t           dt         t           dt         t           fd}| j                  |      }|st        d      |j	                  d      d   dz   t
        j                  j                         v r8d|vrt        d      |j	                  dd      \  }}t
        j                  |   }nt
        j                  }|j                  d      dkD  rd	|v r|j	                  d	d      \  }}d|v rwt        j                  |      }|J|d
|j                         fv r6t        j                  d|      j                  d      }|j                         }n|j	                  dd      \  }}nd} |t!        |      |      }| j#                  |||      \  }	}
|	sJt%        ||
       n<d
}dj'                  |j	                  d      d
d       }dj'                  |j	                  d      dd
       }|j	                  d      d   }dj'                  |j	                  d      dd
       }|}|}| j#                  |||      \  }	}
|	st)        |
t*        t,        f      r)|}|}| j#                  |||      \  }	}|	sot%        ||
       nbt%        ||
       nU|}d}d	|v r(|j	                  d	d      \  }} |t!        |      |      }nd
}| j#                  |||      \  }	}|	st        d      ||nt
        j.                  }t1        |||||      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+   r0   c                 D    || | |k7  rt        d|  d| d      |S | }|S )NzRevision specified in path ("z ") and in `revision` argument ("z") are not the same.)
ValueError)r]   r+   s     r<   %_align_revision_in_path_with_revisionzHHfFileSystem.resolve_path.<locals>._align_revision_in_path_with_revision   sU     ##/4D4P$78H7IIijris t- -  O ,Or>   z0Access to repositories lists is not implemented.r4   r   r   r3   Nr2      )r/   )r   rC   _strip_protocolNotImplementedErrorsplitr   r5   valuesREPO_TYPES_MAPPINGREPO_TYPE_MODELcountSPECIAL_REFS_REVISION_REGEXsearchgroupsublstripr   rZ   _raise_file_not_foundjoin
isinstancer   r$   r8   r(   )r:   r[   r+   r`   r)   r*   r]   matchr,   repo_and_revision_existerrrepo_id_with_namespacepath_in_repo_with_namespacerepo_id_without_namespacepath_in_repo_without_namespace_s                   r<   resolve_pathzHfFileSystem.resolve_path   s   (	&sm	7?}	c]	 ##D)%&XYYZZ_Q#%)J)J)Q)Q)SS$)*\]]"jja0OIt!44Y?I!11I::c?Qd{,0JJsA,>))**7>>?OPE(X$9N-N'B'F'FrK['\'c'cdg'h+0;;=(9I9O9OPSUV9W6(,#%L@IYAZ\de/3/L/LYX_ai/j,'.)$4#' ),$**S/"12E)F&.1hhtzz#qr7J.K+,0JJsOA,>)14$**S/!":M1N.0:/3/L/LYX_ai/j,'.!#(?AR'ST";'E595R5RS\^ego5p2/61$<-dC8GLd{,0JJsA,>))@IYAZ\de#' )-)F)FyRY[c)d&#Q*)*\]]'389S9S'	7Hlbrssr>   c                    |s5| j                   j                          | j                  j                          y| j                  |      }|j	                         }|r0| j                   j                  |d       | j                  |      }|r0|j                  sq| j                  j                  |j                  |j                  dfd       | j                  j                  |j                  |j                  |j                  fd       yy)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clearrQ   ry   r=   pop_parentr,   r)   r*   r+   )r:   r[   resolved_paths      r<   invalidate_cachezHfFileSystem.invalidate_cache   s     MM!00668 --d3M **,D!!$-||D) 
 !--4488-:Q:QS`ShShjn9oquv4488",,m.C.C]E[E[\^b .r>   mode
block_sizeHfFileSystemFilec                 n    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   )r   r+   r   )rc   HfFileSystemStreamFiler   )r:   r[   r   r+   r   kwargss         r<   _openzHfFileSystem._open  sX     $;%&WXX?)$t4(_itmstt#D$nTHYcngmnnr>   c                 N   | j                  ||      }| j                  j                  |j                  |j                  | j
                  |j                  |j                  |j                  d      |j                  d             | j                  |j                                y )Nr+   commit_messagecommit_description)r,   r*   rK   r)   r+   r   r   r[   )ry   rP   delete_filer,   r*   rK   r)   r+   r6   r   r=   )r:   r[   r+   r   r   s        r<   _rmzHfFileSystem._rm  s    ))$)B		&33!))**#--"++!::&67%zz*>? 	 	
 	=#:#:#<=r>   	recursivemaxdepthc                 X   | j                  ||      }| j                  ||||      }|D cg c]/  }| j                  |      r| j                  |      j                  1 }}|D 	cg c]  }	t	        |	       }
}	d d}||rdndz  }||d	| dndz  }| j
                  j                  |j                  |j                  | j                  |
|j                  |j                  d
|      |j                  d             | j                  |j                                yc c}w c c}	w )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+   )r,   zDelete  zrecursively r2   Nzup to depth r   r   )r*   r)   rK   
operationsr+   r   r   r   )ry   expand_pathisdirr,   r   rP   create_commitr*   r)   rK   r+   r6   r   r=   )r:   r[   r   r   r+   r   r   pathspaths_in_repor,   r   r   s               r<   rmzHfFileSystem.rm"  s7   < ))$)B  XX` aJOh%$W[WaWabfWg**40==%h[hi[h<+F[h
i"4&*I.2=8LL
!4RTT		!))#--**!"++!::&6G%zz*>? 	  	
 	=#:#:#<= iis   D"D"'D'detailrefreshc                    | j                  ||      }|j                         }d|i|}	  | j                  |f||d|}|r|S |D cg c]  }|d   	 c}S # t        $ rx |j                  st        |d        | j                  | j                  |      f||d|}|D cg c]  }|d   |k(  s| nc c}w }}t        |      dk(  rt        |d       Y w xY wc c}w )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+   Nnamer   )ry   r=   _ls_treer   r,   rn   r~   len)	r:   r[   r   r   r+   r   r   outos	            r<   lszHfFileSystem.lsS  s    : ))$)B&&(262		2$--SgSFSC s:c":c1V9c":: " 	2 --%dD1$--T 2aGhaZ`aC!7cQvY$%61c7C73x1}%dD1	2 #;s*   A 	CA
C"B50B54"CCr   c           	         | j                  ||      }|j                         }t        |j                  |j                  |j
                  d|j                        j                         }g }|| j                  v r|s| j                  |   }	|j                  |	       g }
|rt        |	D cg c]  }|d   dk(  s| c}      }|r|j                         }|d   | j                  vr|
j                  |d          nK| j                  |d      }	|j                  |	       |j                  |	D cg c]  }|d   dk(  s| c}       |rg }|r'|D cg c]  }|d   	| j                  |d          }}|r|
s|r|rt        j                  j                  |
|z         }|j!                  d      s||k(  s|t#        |
|      v r|j%                  d      n| j                  |      }|D cg c]  }|d   j'                  |dz         r| }}| j                  D ]3  }|j'                  |dz         s| j                  j)                  |d        5 | j                  j)                  |d        |j                  | j+                  ||d	||
             |S | j,                  j/                  |j                  |j0                  |||j
                  |j                        }|D ]  }t3        |t4              rM|dz   |j                  z   |j6                  d|j8                  |j:                  |j<                  |j>                  d}n,|dz   |j                  z   dd|j@                  |j<                  d}| j                  |d         }| j                  jC                  |g       j                  |       |j                  |        |S c c}w c c}w c c}w c c}w )Nr   r2   r,   r/   type	directoryr   last_commitr4   T)r   r   r+   r   )r   expandr+   r)   filer   sizer   blob_idlfsr   securityr   r   r   r   tree_idr   )"ry   r=   r(   r)   r*   r+   r/   r{   extendr   popleftappendr~   osr[   commonprefixendswithr   r7   
startswithr}   r   rP   list_repo_treer,   rp   r#   r   r   r   r   r   r   
setdefault)r:   r[   r   r   r+   r   r   	root_pathr   cached_path_infosdirs_not_in_dircache	path_infodirs_to_visitdir_infodirs_not_expandedr   common_prefixcommon_pathcached_pathtreecache_path_infoparent_paths                         r<   r   zHfFileSystem._ls_tree  s    ))$)B&&(,##!!""'55
 )+ 	 4==  $d 3JJ()#%  !&0Af0A9YvEVZeEeY0Af! $,446H't}}<,33HV4DE,0MM(6:J,K)

#45%,,8In8I9YW]M^bmMmY8In $ !#FI$fcQ}M]MeT\\!F)%<c!$f2HY !# 4 45IL]5] ^ %--c2$	1$.BDU(VV "((- m4  #&U#QQvY-A-A+PSBS-Tq#U#'==K"--kC.?@))+t< $1 !!+t4

MM#"+ $!)$/ " P 
= 99++%%**#"&//'11 , D "	i2 )C).. @ ) &#,#4#4(}}'0'<'<$-$6$6'O !*C).. @ ! +#,#4#4'0'<'<'O #ll?6+BC((b9@@Q

?++ ", 
W g o %g Vs0   +N>9N>.O<O
OO+OOc              /      K   d|j                  dd      i|}| j                  ||j                  d            j                         }t        |   |g|i |E d{    y7 w)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)r6   ry   r=   rM   walk)r:   r[   rR   r   rT   s       r<   r   zHfFileSystem.walk  sf       He!<GG  

:0F GQQS7<6t6v666s   AA( A&!A(c                     d|j                  dd      i|}| j                  ||j                  d            j                         }t        |   |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   )r6   ry   r=   rM   glob)r:   r[   r   rT   s      r<   r   zHfFileSystem.glob  sZ      He!<GG  

:0F GQQSw|D+F++r>   withdirsc           	      J   |rt        |   |f|||||d|S | j                  ||      }|j                         }d|i|}	  | j                  |fd||j
                  d|}	|s|	D 
cg c]  }
|
d   dk7  s|
 }	}
n/ | j                  |fd|j
                  i|}|d   dk(  r|g|	z   n|	}	|	D 
ci c]  }
|
d	   |

 }	}
t        |	      }|s|S |D ci c]  }||	|   
 c}S c c}
w c c}
w # t        $ r%  | j                  |fd|i|d   d
k(  r|i i}	ni }	Y Zw xY wc c}w )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+   r   r   )	rM   findry   r=   r   r+   infor   sorted)r:   r[   r   r   r   r   r+   r   r   r   r   r   namesr   rT   s                 r<   r   zHfFileSystem.find  s   @ 7<'(6SZemqw  ))$)B&&(262	.$--qgP]PfPfqjpqC "%B#Q6k)Aq#B &DIIdV]5K5KVvV	+4V+<+KykC'QT),-A1V9a<C-sL056D#d)O66 C
 . " 	tyy;;F;FCvMRj	$ 7s*   !C/ +C%9C%3C*D /+DDpath1path2c                    | j                  ||      }| j                  ||      }|j                  |j                  k(  xr |j                  |j                  k(  }|rd| d| }| j                  j	                  |j                  |j                  |j
                  |j                  d|      |j                  dd      t        |j                  |j                  |j
                        g       n| j                  |d	|j
                        5 }	|	j                         }
d
d
d
       d| d| }| j                  j                  
|j                  |j                  | j                  |j                  |j
                  |j                  d|      |j                  d             | j                  |j                                | j                  |j                                y
# 1 sw Y   xY w)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   r2   )src_path_in_repor,   src_revision)r*   r)   r+   r   r   r   rbNpath_or_fileobjr,   r*   rK   r)   r+   r   r   r   )ry   r)   r*   rP   r   r+   r6   r   r,   openreadupload_filerK   r   r=   )r:   r   r   r+   r   resolved_path1resolved_path2	same_repor   fcontents              r<   cp_filezHfFileSystem.cp_fileH  s   & **58*D**58*D $$(@(@@u^E[E[_m_u_uEu 	 $UG4w7NII##&..(22'00%zz*:NK#)::.BB#G')7)D)D%3%@%@%3%<%< $  5$1H1HIQ&&( J$UG4w7NII!! '+88&..jj(22'00%zz*:NK#)::.B#C " 	 	>#;#;#=>>#;#;#=> JIs   G$$G-c                 8     | 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   )r:   r[   r   r   s       r<   modifiedzHfFileSystem.modified  s)     tyy((M"6**r>   c                 4   | j                  ||      }|j                         }|j                  dd      }|j                  s}|ddd}|r?| j                  j                  |j                  |j                  |j                        d   }i |d	t        |j                  |j                  |j                  
      d}nd	}| j                  |      }	|s!|	| j                  vr| j                  |	d       |	| j                  v r8| j                  |	   D 
cg c]  }
|
d   |k(  s|
 }}
|st!        |d	       |d   }|s||rH|rE|d   ?| j                  j#                  |j                  |j                  ||j                  |j                        }|st!        |d	       |d   }t%        |j                  |j                  |j                  d|j&                        j                         }t)        |t*              rM|dz   |j,                  z   |j.                  d|j0                  |j2                  |j4                  |j6                  d}n,|dz   |j,                  z   dd|j8                  |j4                  d}|sdD ci c]  }|||   
 }}|J |S c c}
w c c}w )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   r   r   )r   r+   r)   r2   r   r4   r   r   r   )ry   r=   r6   r,   rP   list_repo_commitsr*   r)   r+   r"   	commit_idr   
created_atr~   r{   r   rn   get_paths_infor(   r/   rp   r#   r[   r   r   r   r   r   r   )r:   r[   r   r+   r   r   r   r   r   r   r   out1
paths_infor   r   ks                   r<   r   zHfFileSystem.info  s   0 ))$)B&&(jj4
 )) #C
 "ii99!))]5L5LWdWmWm : ##1'119J9JQ\QgQg$ C,,t,K;dmm#C7dmm+#'==#=S#=a6dAR#=S)$51g#++##mBTB\!YY55!))!..&*33+55 6 
 ")$5&qM	4!++!))!**!#"/"="= )+  i2 )C).. @ ) &#,#4#4(}}'0'<'<$-$6$6C !*C).. @ ! +#,#4#4'0'<'<C #.FG.F1c!f9.FCG
W TR Hs   J"J;Jc                     	 |j                  dd      r| j                  |        | j                  |fi i |ddi y#  Y yxY w)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)r6   r   r   )r:   r[   r   s      r<   existszHfFileSystem.exists  sP    &	zz)U+%%d+DIId?>>>?	s	   <? Ac                 T    	 | j                  |d      d   dk(  S # t        $ r Y yw xY 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r:   r[   s     r<   r   zHfFileSystem.isdir  s6    	99Tu95f=LL 		s    	''c                 D    	 | j                  |d      d   dk(  S #  Y yxY w)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   s     r<   isfilezHfFileSystem.isfile%  s.    	99Tu95f=GG	s    c                     | j                  |      }t        |j                  |j                  |j                  |j
                  | j                        }| j                  |      r|j                  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+   rJ   z	/resolve/z/tree/r   )	ry   r   r*   r,   r)   r+   rJ   r   replace)r:   r[   r   urls       r<   r   zHfFileSystem.url7  sn     ))$/!!&&#--"++]]
 ::d++k8Q7C
r>   c                    |j                  d      }t        |j                               dhz
  }t        |t        t
        f      rt        |      dkD  rt        |    ||f||d|S t        |      r|}n)| j                  |      rt        j                  |d       yt        |t        t        f      r4t        j                  t        j                  j!                  |      d       d}|t#        |d      }d}|j%                         }	| j'                  ||	      }
| j)                  ||	      d
   }|j+                  |       	 t-        t/        |
j0                  |
j2                  |
j4                  |
j6                  | j8                        |||d| j:                  j=                         t        |t
              r|j>                  nd       |jA                  |	       |r|jC                          yy# |r|jC                          w w xY 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)   rJ   )r   	temp_filedisplayed_filenameexpected_sizeresume_sizeheaders	_tqdm_bar)"r6   setkeysrp   r   r   r   rM   get_filer   r   r   makedirsrC   r   r[   dirnamer   tellry   r   set_sizer    r   r*   r+   r,   r)   rJ   rP   _build_hf_headerstqdmseekclose)r:   rpathlpathr   r   r   r+   unhandled_kwargs
close_fileinitial_posresolve_remote_pathr  rT   s               r<   r  zHfFileSystem.get_fileN  s   * ::j)v{{}-<(\<$@ASIYEZ]^E^ 7#E5_8W_X^__ eGZZKK-ec4[)KK.> 
?5$'GJlln #///I		%(	;FC-(	 /770990==1;;!]] "#(+		335+5h+M(--SW LL%  z s   BG1 1H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.rc   r:   s    r<   transactionzHfFileSystem.transaction  s     ""LMMr>   c                     t        d      )z@Begin write transaction for deferring files, non-context versionr  r  r  s    r<   start_transactionzHfFileSystem.start_transaction  s     ""LMMr>   N)r   NN)FNN)TFN)FFNT)NFFFN)FNr0   N),r?   r@   rA   rB   root_markerprotocolr   rC   r   boolrN   r   	ExceptionrZ   r(   ry   r   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__rT   s   @r<   rG   rG   :   s   &P KH
 #'(,	 3- T3_%	$TT'*T6>smT	tXi((	)T&]t ]t ]tIa ]t~Xc] d > "&$(oo o 3-	o
 SMo 
o> >x} >$ >   "&"&/>/> /> 3-	/>
 3-/> 
/>d `d*;*;!%*;7;*;OWX[}*;	eCc3h'(	)*;^  "& ff f 	f
 3-f fP7 7(5d3iQUVYQZAZ;[2\ 7$, ,49 ,* #'"&;7;7 3-;7 	;7
 ;7 ;7 3-;7 
tCy$sDcN233	4;7z9?S 9? 9? 9?[_ 9?v+S +x + b bt bx} bbfgjlogobp bH8$$  . /@ E N N NNr>   rG   c                        e Zd Zddededee   f fdZ fdZdeded	e	fd
Z
ddZdded	dfdZd fd	Zd	efdZ xZS )r   Nfsr[   r+   c                    	 |j                  ||      | _        |j                  dd      dk(  r0|j	                  | j                  j                         d      | _        t        |    || j                  j                         fi | |  y # t        $ r)}d|j                  dd      v rt        | d      | d }~ww xY w)	Nr   wr   r2   B.
Make sure the repository and revision exist before writing data.r   Fr   )	ry   r   FileNotFoundErrorr6   r   r=   detailsrM   rN   )r:   r.  r[   r+   r   rY   rT   s         r<   rN   zHfFileSystemFile.__init__  s    	!#!ID ::fd#t+774#5#5#?#?#Au7UDLT//99;FvF ! 	fjj,,'c\] 	s   B 	B>$B99B>c                 :    t        | d      sy t        | 	         S Nr   hasattrrM   __del__r:   rT   s    r<   r8  zHfFileSystemFile.__del__      t_-w  r>   startendr0   c                    dd| d|dz
   i| j                   j                  j                         }t        | j                  j
                  | j                  j                  | j                  j                  | j                  j                  | j                   j                        }t        d||dt        j                        }t        |       |j                  S )	Nrangezbytes=-r   r  GETi  i  i  i  )r	  retry_on_status_codesrV   )r.  rP   r  r   r   r*   r+   r,   r)   rJ   r&   r   HF_HUB_DOWNLOAD_TIMEOUTr%   r   )r:   r;  r<  r	  r   rs         r<   _fetch_rangezHfFileSystemFile._fetch_range  s    veWAcAgY/
ggll,,.
 &&..''00''44((22WW%%
 "655
 	Ayyr>   c                 <    t        j                  dd      | _        y )Nzhffs-F)prefixdelete)tempfileNamedTemporaryFiler  r  s    r<   _initiate_uploadz!HfFileSystemFile._initiate_upload  s    !44GERr>   finalc                 H   | j                   j                  d       | j                   j                         }| j                  j	                  |       |rO| j                  j                          | j                  j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                   j#                  d      | j                   j#                  d             t%        j&                  | j                  j                         | j                  j)                  | j                  j+                                y y )Nr   r   r   r   r   )bufferr  r   r  writer  r.  rP   r   r   r   r,   r*   rK   r)   r+   r   r6   r   remover   r=   )r:   rL  blocks      r<   _upload_chunkzHfFileSystemFile._upload_chunk  s"     "U#NN  "GGLL$$ $ 3 3!//<<**22ggmm,,66++44#{{/?@#';;??3G#H % 	 IIdnn))*GG$$''113 %  r>   c                    | j                   dk(  rX||dk(  rQ| j                  dk(  rB| j                  j                  | j                  dd      5 }|j                         cddd       S t        |   |      S # 1 sw Y   xY w)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.
        r   Nr   r   )r   )r   locr.  r   r[   r   rM   )r:   lengthr   rT   s      r<   r   zHfFileSystemFile.read  sk     99&.FbLdhhRSmdii!<vvx =<w|F## =<s   A88Bc                 L    | j                   j                  | j                        S r#  r.  r   r[   r  s    r<   r   zHfFileSystemFile.url      ww{{499%%r>   r#  r$  )Fr   )r?   r@   rA   rG   rC   r   rN   r8  r)  bytesrE  rK  r'  rR  r   r   r+  r,  s   @r<   r   r     sj    < s hsm !# C E ,S4 D *
$&S &r>   r   c                        e Zd Z	 	 	 	 ddedededee   dedef fdZdded	efd
ZddefdZ	defdZ
 fdZd Z xZS )r   r.  r[   r   r+   r   
cache_typec                    |dk7  rt        d|       |dk7  rt        d|       d|v rt        d| d      	 |j                  ||      | _        | j                  j                         d d| _        t        	|    || j                  j                         f|||d| d | _	        |  y # t        $ r-}d|j	                  d	d
      v rt        | d      |Y d }~d }~ww xY w)Nr   z:HfFileSystemStreamFile only supports block_size=0 but got nonez?HfFileSystemStreamFile only supports cache_type='none' but got r0  z;HfFileSystemStreamFile only supports reading but got mode=''r   r   r2   r1  )r   r   r   r   r\  )
r_   ry   r   r2  r6   r=   r3  rM   rN   response)
r:   r.  r[   r   r+   r   r\  r   rY   rT   s
            r<   rN   zHfFileSystemStreamFile.__init__  s    ?YZdYefgg^_i^jkll$;Z[_Z``abcc	!#!ID !% 2 2 < < >M"",,.	
59j]g	
kq	
 -1 ! 	fjj,,'c\] -	s   B- -	C#6#CC#rT  whencec                 X    |dk(  r|dk(  ry || j                   k(  r|dk(  ry t        d      )Nr   r   zCannot seek streaming HF file)rT  r_   )r:   rT  rb  s      r<   r  zHfFileSystemStreamFile.seek  s2    !8!$((?v{899r>   rU  c           	      L   |dk\  r|fnd}| j                   $| j                   j                  j                         rt        | j                  j
                  | j                  j                  | j                  j                  | j                  j                  | j                  j                        }t        d|| j                  j                  j                         ddt        j                        | _         t!        | j                          	  | j                   j                  j"                  | }| xj(                  t+        |      z  c_        |S # t$        $ rK | j                   j'                          t        | j                  j
                  | j                  j                  | j                  j                  | j                  j                  | j                  j                        }t        d|dd	| j(                  z  i| j                  j                  j                         ddt        j                        | _         t!        | j                          	  | j                   j                  j"                  | }n(# t$        $ r | j                   j'                           w xY wY tw xY w)
Nr   rE   r  r@  rA  T)r	  rB  streamrV   Rangez	bytes=%d-)ra  rawisclosedr   r   r*   r+   r,   r)   r.  rJ   r&   rP   r  r   rC  r%   r   r(  r  rT  r   )r:   rU  	read_argsr   r   s        r<   r   zHfFileSystemStreamFile.read"  s   !'1VI"	== DMM$5$5$>$>$@**22++44++88,,66))C )668&:!99DM  .	($--##(()4C4 	CH
5  	MM! **22++44++88,,66))C ) +"8]DGGLL<Z<Z<\]&:!99DM  .,dmm'',,i8 ##% +	s+   #E DJ##I76J#7%JJ#"J#r0   c                 L    | j                   j                  | j                        S r#  rW  r  s    r<   r   zHfFileSystemStreamFile.urlS  rX  r>   c                 :    t        | d      sy t        | 	         S r5  r6  r9  s    r<   r8  zHfFileSystemStreamFile.__del__V  r:  r>   c                     t         | j                  | j                  | j                  | j                  | j
                  j                  ffS r#  )reopenr.  r[   r   	blocksizecacher   r  s    r<   
__reduce__z!HfFileSystemStreamFile.__reduce__\  s0    DIIt~~tzzWWWr>   )r   Nr   r^  )r   rY  )r?   r@   rA   rG   rC   r   r)  rN   r  r   r   r8  rp  r+  r,  s   @r<   r   r     s    
 "&   	
 3-  >: :S :/3 /b&S &!Xr>   r   r+   r0   c                 F    t         j                  |       r| S t        |       S r#  )ri   rq   
safe_quoter   s    r<   r9   r9   `  s     288B8\
S[H\\r>   sc                     t        | d      S )Nr2   )safe)r   )rs  s    r<   rr  rr  d  s    r>   r[   rs   c                     | }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))rp   r   r   r$   r2  )r[   rs   msgs      r<   rn   rn   h  s\    
C#./-.	C.	/+,	C*	+./
C
 c)r>   r.  r   r   r\  c                 ,    | j                  ||||      S )Nr`  )r   )r.  r[   r   r   r\  s        r<   rm  rm  s  s    774dzj7QQr>   )Gr   rerI  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   r2   r   _commit_apir   r   errorsr   r   r   file_downloadr   r    hf_apir!   r"   r#   utilsr$   r%   r&   compileVERBOSEri   r(   AbstractFileSystemrG   specAbstractBufferedFiler   r   rC   r9   rr  r(  rn   r)  rm  rE   r>   r<   <module>r     sR   	 	   (    N N N '  J J #   C V V / 3 3 G G )bjj
 JJ  B B B*jN6,, jNZQ&v{{77 Q&hbXV[[== bXJ]C ]C ]# # * *(9*= *( *R| R3 Rc Rs RPS Rr>   