
    rOiP@              
       t   U d 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	m
Z
mZ ddlmZ ej                  j                         D  ci c]  \  } }|| 
 c}} Zeeef   ed	<    ej(                  d
      Z eej                  j/                               Zee   ed<    ed       G d d             Z ed       G d d             ZdedefdZdedefdZdededeeedz  edz  f   fdZdededeej@                  ef   fdZ!dedej@                  dedefdZ"dedej@                  dedefdZ#yc c}} w )a|  Centralized parser for Hugging Face Hub URIs ('hf://...') and mount specifications.

A HF URI is a URI-like string that identifies a location on the Hugging Face
Hub: a model/dataset/space/kernel repository, a bucket, optionally a revision,
and optionally a path inside the repo or bucket.

Canonical syntax:

```
hf://[<TYPE>/]<ID>[@<REVISION>][/<PATH>]
```

A HF mount wraps a HF URI with a local mount path and an optional ':ro'/':rw'
flag (used by Spaces and Jobs volumes):

```
hf://[<TYPE>/]<ID>[@<REVISION>][/<PATH>]:<MOUNT_PATH>[:ro|:rw]
```

See 'docs/source/en/package_reference/hf_uris.md' for the full grammar and examples.
    N)	dataclassfield)unquote)	constants)
HfUriErrorHFValidationError   )validate_repo_id_TYPE_TO_PREFIXz ^refs/(?:convert/[\w.-]+|pr/\d+)_VALID_URI_TYPEST)frozenc                       e Zd ZU dZej
                  ed<   eed<   dZedz  ed<   dZ	eed<    e
dddd	      Zedz  ed
<   ddZedefd       Zedefd       ZdefdZy)HfUria  Parsed representation of a Hugging Face Hub URI ('hf://...').

    Attributes:
        type (`str`):
            One of 'model', 'dataset', 'space', 'kernel' or 'bucket'.
        id (`str`):
            The repository id ('namespace/name', e.g. 'my-org/my-model') for repo URIs, or the bucket id ('namespace/name') for bucket URIs.
        revision (`str`, *optional*):
            The revision specified after '@' in the URI, URL-decoded. 'None' if no revision was specified, or for bucket URIs (which
            never carry a revision). Special refs like 'refs/pr/10' and 'refs/convert/parquet' are preserved as-is.
        path_in_repo (`str`):
            The path inside the repo or bucket. Empty string if the URI points at the root.
    typeidNrevision path_in_repoFreprhashcomparedefault_rawreturnc           	         | j                   xs d}| j                  t        vr+t        |d| j                   dt	        t               d      | j
                  r| j
                  j                  d      dk7  rt        |d| j
                   d	      | j                  d
k7  r	 t        | j
                         | j                  | j                  st        |d      | j                  d
k(  r| j                  t        |d      | j                  rE| j                  j                  d      sd| j                  v rt        |d| j                   d      y y # t        $ r}t        |t        |            |d }~ww xY w)Nr   zInvalid type 'z'. Must be one of .urimsg/r	   z"Id must be 'namespace/name', got ''.bucketz%Revision must not be an empty string.z&Bucket URIs do not support a revision.z//z+Path must not contain empty segments (got 'z').)r   r   r   r   sortedr   countr
   r   strr   r   
startswith)selfr   es      e/volume1/homes/robertsu/coba/app/.venv/lib/python3.12/site-packages/huggingface_hub/utils/_hf_uris.py__post_init__zHfUri.__post_init__S   s]   iio2 99,,N499+EWX^_oXpWqqr*stt ww$''--,1,NtwwiWY*Z[[99 = )
 ==$T]]*QRR99 T]]%>*RSS   ++C0DD<M<M4M S0[\`\m\m[nnq.rss 5N  % = Sc!f51<=s   #E 	E< E77E<c                      | j                   dk(  S )z$True if this URI points at a bucket.r#   r   r(   s    r*   	is_bucketzHfUri.is_bucketn        yyH$$    c                      | j                   dk7  S )zJTrue if this URI points at a repository (model, dataset, space or kernel).r#   r-   r.   s    r*   is_repozHfUri.is_repos   r0   r1   c                    t         j                  t        | j                     d| j                  g}| j
                  K| j
                  }d|v r't        j                  |      |j                  dd      }|j                  d|        | j                  r|j                  d| j                          dj                  |      S )zRender the URI as a canonical 'hf://' string.

        The type prefix is always written explicitly (e.g. 'hf://models/my-org/my-model').
        r!   z%2F@r   )r   HF_PROTOCOLr   r   r   r   _SPECIAL_REFS_REVISION_REGEX	fullmatchreplaceappendr   join)r(   partsr   s      r*   to_urizHfUri.to_urix   s    
 &11?4993MsTXT[T[\==$ }}Hh#?#I#I(#S#[#++C7LL1XJ(LL1T../01wwu~r1   r   N)__name__
__module____qualname____doc__r   	HfUriType__annotations__r&   r   r   r   r   r+   propertyboolr/   r3   r=    r1   r*   r   r   =   s     

GHcDjL#%eUDQD#*Qt6 %4 % % % % % r1   r   c                   v    e Zd ZU dZeed<   eed<   dZedz  ed<    e	dddd      Z
edz  ed<   dd
Zd	efdZy)HfMountaF  A HF URI paired with a local mount path and optional read-only flag.

    Used by Spaces and Jobs to describe volume mounts. The full syntax is:

    ```
    hf://[<TYPE>/]<ID>[@<REVISION>][/<PATH>]:<MOUNT_PATH>[:ro|:rw]
    ```

    Attributes:
        source ([`HfUri`]):
            The parsed HF URI identifying the Hub resource to mount.
        mount_path (`str`):
            The local mount path (always starts with '/').
        read_only (`bool`, *optional*):
            True if the mount ends with ':ro', False if it ends with ':rw', 'None' if no flag was provided.
    source
mount_pathN	read_onlyFr   r   r   c                     | j                   xs d}| j                  j                  d      r| j                  dk(  rt        |d| j                   d      y )Nr   r!   zEMount path must be a non-empty absolute path starting with '/', got 'r"   r   )r   rK   r'   r   )r(   raws     r*   r+   zHfMount.__post_init__   sW    iio2))#.$//S2H[\`\k\k[llno  3Ir1   c                     | j                   j                         d| j                  g}| j                  |j	                  | j                  rdnd       dj                  |      S )zqRender the mount as a canonical 'hf://' string.

        Example: 'hf://models/my-org/my-model:/data:ro'
        ::ro:rwr   )rJ   r=   rK   rL   r:   r;   )r(   r<   s     r*   r=   zHfMount.to_uri   sM    
 ##%sDOO<>>%LL$..e<wwu~r1   r>   )r?   r@   rA   rB   r   rD   r&   rL   rF   r   r   r+   r=   rG   r1   r*   rI   rI      sK    " MO!Itd{!%eUDQD#*Q r1   rI   r   r   c                 |   | j                  t        j                        s/t        | dt        j                   dt        j                   d      | }| t	        t        j                        d }|st        | dt        j                   d      t        ||      \  }}|dk(  rt        |||      S t        |||      S )	a  Parse a Hugging Face Hub URI ('hf://...').

    A HF URI is a URI-like string identifying a location on the Hugging Face Hub. The full grammar is:

    ```
    hf://[<TYPE>/]<ID>[@<REVISION>][/<PATH>]
    ```

    See 'docs/source/en/package_reference/hf_uris.md' for the full specification.

    Args:
        uri (`str`):
            The URI to parse. Must start with 'hf://'.

    Returns:
        [`HfUri`]: the parsed URI.

    Raises:
        [`HfUriError`]:
            If the URI is malformed (missing prefix, invalid type, missing id, etc.).

    Examples:
        ```py
        >>> from huggingface_hub.utils import parse_hf_uri
        >>> parse_hf_uri("hf://my-org/my-model")
        HfUri(type='model', id='my-org/my-model', revision=None, path_in_repo='')
        >>> parse_hf_uri("hf://datasets/my-org/my-dataset@refs/pr/3/train.json")
        HfUri(type='dataset', id='my-org/my-dataset', revision='refs/pr/3', path_in_repo='train.json')
        ```
    Must start with 'z'. Expected format: z#[<TYPE>/]<ID>[@<REVISION>][/<PATH>]NEmpty body after 'r"   rN   r#   )r'   r   r6   r   len_split_type_parse_bucket_body_parse_repo_body)r   rN   bodytype_locations        r*   parse_hf_urir^      s    > >>)//0	 5 56 7  ) 5 566Y[
 	
 Cs9(()+,D 293H3H2ILMM!$C0OE8!(Es;;He55r1   	mount_strc                    | j                  t        j                        st        | dt        j                   d      | }| t	        t        j                        d }|st        |dt        j                   d      t        ||      \  }}}|t        |d      t        j                  |z   }	 t        |      }t        ||||      S # t        $ r}t        ||j                        |d}~ww xY w)	a  Parse a HF mount specification ('hf://...:<MOUNT_PATH>[:ro|:rw]').

    A mount specification is a HF URI followed by a local mount path and an optional read-only/read-write flag.
    The full grammar is:

    ```
    hf://[<TYPE>/]<ID>[@<REVISION>][/<PATH>]:<MOUNT_PATH>[:ro|:rw]
    ```

    See 'docs/source/en/package_reference/hf_uris.md' for the full specification.

    Args:
        mount_str (`str`):
            The mount string to parse. Must start with 'hf://' and contain a ':<MOUNT_PATH>' segment.

    Returns:
        [`HfMount`]: the parsed mount.

    Raises:
        [`HfUriError`]:
            If the mount string is malformed (missing mount path, invalid URI, etc.).

    Examples:
        ```py
        >>> from huggingface_hub.utils import parse_hf_mount
        >>> parse_hf_mount("hf://my-org/my-model:/data:ro")
        HfMount(source=HfUri(type='model', id='my-org/my-model', revision=None, path_in_repo=''), mount_path='/data', read_only=True)
        >>> parse_hf_mount("hf://buckets/my-org/my-bucket/sub/dir:/mnt:rw")
        HfMount(source=HfUri(type='bucket', id='my-org/my-bucket', revision=None, path_in_repo='sub/dir'), mount_path='/mnt', read_only=False)
        ```
    rT   r"   r   NrU   rV   zKMissing mount path. Expected ':<MOUNT_PATH>' (e.g. 'hf://org/model:/data').)rJ   rK   rL   r   )	r'   r   r6   r   rW   _split_mountr^   r    rI   )	r_   rN   r[   r]   rK   rL   uri_strrJ   r)   s	            r*   parse_hf_mountrc      s    @ 	 5 56#I$9$9#:"=
 	

 CS../12DS(:9;P;P:QQS&TUU&24S&A#Hj)S&stt ##h.G4g& &Z9SVWW  4Saee,!34s   2C 	C2C--C2r[   rN   c                0   | j                  d      rd| j                  d      } }n'| j                  d      rd| j                  d      } }nd}| j                  d      }|dk(  r|t        |d	      | ddfS | d| }| |d
z   d }|st        |d	      |||fS )zSplit the ':<MOUNT_PATH>[:ro|:rw]' suffix from 'body'.

    Returns '(location, mount_path, read_only)' where 'mount_path' is 'None' if no mount segment is present.
    rQ   TrR   FNz:/zb':ro'/':rw' suffix is only valid when a mount path is provided (e.g. 'hf://...:/<MOUNT_PATH>:ro').r   r	   z#Missing location before mount path.)endswithremovesuffixrfindr   )r[   rN   rL   idxr]   rK   s         r*   ra   ra   "  s    
 }}U 1 1% 84		u	!2!25!94		 **T
C
by x  T4DSzHcAgiJS&KLLZ**r1   r]   c          	         | j                  d      }|dk(  rr| t        j                  v r$t        |d|  dt        j                   |  d      t
        j                  |       x}!t        |dt        j                   | d	      d
| fS | d| }| |dz   d }|t        j                  v rt        j                  |   |fS t
        j                  |      x}t        |d| d| d      d
| fS )zDetect the (optional) type prefix and return '(type, remaining_location)'.

    A missing type prefix defaults to 'model'. Singular forms ('model/', 'dataset/', etc.) are explicitly rejected with a helpful error.
    r!   re   zMissing identifier after 'z'. Expected 'z/<ID>'.r   Nz*Type prefix must be plural. Did you mean 'z/...'?modelr	   z!Type prefix must be plural, got 'z/'. Did you mean 'z/'?)findr   HF_URI_TYPE_PREFIXESr   r6   r   get)r]   rN   	slash_idxsingular_pluralfirstrests         r*   rX   rX   @  s/   
 c"IBy5550
-	H]H]G^_g^hhop   /228<<OI@AVAV@WXgWhhno    Zi EIMO$D	...--e4d::*..u55B<UGCUVeUffij
 	
 Hr1   r\   c                    d| v rt        |d      | j                  d      } | j                  dd      }t        |      dk  s
|d   r|d   st        |d|  d	      |d    d|d    }t        |      d
k\  r|d   nd}t	        ||d||      S )z8Parse the body of a bucket URI: 'namespace/name[/path]'.r5   z3Bucket URIs do not support a revision marker ('@').r   r!      r   r	   z)Bucket id must be 'namespace/name', got 'r"      r   Nr   r   r   r   r   )r   stripsplitrW   r   )r]   r\   rN   r<   	bucket_idpath_in_buckets         r*   rY   rY   _  s     hS&[\\~~c"HNN3"E
5zA~U1XU1XS(QRZQ[[]&^__8*AeAhZ(I!$UqU1XbN# r1   c                   | j                  d      } | st        |d      | j                  d      }|dk(  rVd}| j                  dd      }t	        |      dk  rt        |d|  d	      |d
    d|d    }t	        |      dkD  r|d   nd}n| d| }| |dz   d }|st        |d      |j                  d      dk7  rt        |d| d      t        j                  |      }	|	.|	j                         }|t	        |      d j                  d      }n(|j                  d      }
|
dk(  r|}d}n|d|
 }||
dz   d }t        |      }|st        |d      t        |||||      S )z@Parse the body of a repo URI: '<repo_id>[@<revision>][/<path>]'.r!   zMissing repository id.r   r5   re   Nrt   z-Repository id must be 'namespace/name', got 'z'. r   r	   r   z!Missing repository id before '@'.r"   zEmpty revision after '@'.rv   )rw   r   rl   rx   rW   r%   r7   matchgroupremoveprefixr   r   )r]   r\   rN   at_idxr   r<   repo_idr   rev_and_pathr|   ro   s              r*   rZ   rZ   w  s    ~~c"HS&>?? ]]3F|sA&u:>,YZbYccf*ghh1XJaaz*#&u:>uQxr7F#
-*MNN==",YZaYbbd*eff -22<@{{}H'H8EEcJL$))#.IB'!'
3+IMO<8$*EFF! r1   )$rB   redataclassesr   r   urllib.parser   huggingface_hubr   huggingface_hub.errorsr   r   _validatorsr
   rm   itemsr   dictr&   rD   compiler7   	frozensetvaluesr   r   rI   r^   rc   tuplerF   ra   rC   rX   rY   rZ   )kvs   00r*   <module>r      s  , 
 (   % @ )
 5>4R4R4X4X4Z"[4ZDAq1a44Z"[c3h [  *rzz*MN  $-Y-K-K-R-R-T#U )C. U $J J JZ $' ' 'T/6c /6e /6d7Xc 7Xg 7Xt+s +C +E#sTz4$;2N,O +<# s uY5H5H#5M/N > 
	
 0666 
	6
 6O
 #\s   D4