
    iP@              
       r   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 d	 ej                                        D             Zeeef         ed
<    ej        d          Z ee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#dS ) 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_idc                     i | ]\  }}||	S  r   ).0kvs      _/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/huggingface_hub/utils/_hf_uris.py
<dictcomp>r   0   s    "["["[DAq1a"["["[    _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dS )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         pd}| j        t          vr/t          |d| j         dt	          t                     d          | j        r| j                            d          dk    rt          |d| j         d	          | j        d
k    rG	 t          | j                   n1# t          $ r$}t          |t          |                    |d }~ww xY w| j
        | j
        st          |d          | j        d
k    r| j
        t          |d          | j        r=| j                            d          s	d| j        v rt          |d| j         d          d S d S )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      r   __post_init__zHfUri.__post_init__S   s   io2 9,,,*s49*s*sX^_oXpXp*s*s*stttt w 	\$'--,,11*Ztw*Z*Z*Z[[[[9  = ))))$ = = = Sc!ff5551<= =$T]$*QRRRR9  T]%>*RSSSS  	t ++C00 tDD<M4M4M S.r\`\m.r.r.rssss	t 	t4M4Ms   B' '
C1CCc                     | j         dk    S )z$True if this URI points at a bucket.r+   r   r0   s    r   	is_bucketzHfUri.is_bucketn        yH$$r   c                     | j         dk    S )zJTrue if this URI points at a repository (model, dataset, space or kernel).r+   r4   r5   s    r   is_repozHfUri.is_repos   r7   r   c                 r   t           j        t          | j                 d| j        g}| j        S| j        }d|v r0t                              |          |                    dd          }|	                    d|            | j
        r|	                    d| j
                    d                    |          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)   Nz%2F@r   )r   HF_PROTOCOLr   r   r   r   _SPECIAL_REFS_REVISION_REGEX	fullmatchreplaceappendr   join)r0   partsr   s      r   to_urizHfUri.to_urix   s    
 &1?493MsTXT[\=$ }Hh#?#I#I(#S#S#[#++C77LLX((( 	2LL0T.00111wwu~~r   r#   N)__name__
__module____qualname____doc__r   	HfUriType__annotations__r.   r   r   r   r"   r2   propertyboolr6   r9   rC   r   r   r   r   r   =   s          
GGGHcDjL#u%eUDQQQD#*QQQt t t t6 %4 % % % X% % % % % X%      r   r   c                   ~    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dS )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         pd}| j                            d          r| j        dk    rt          |d| j         d          d S )Nr   r)   zEMount path must be a non-empty absolute path starting with '/', got 'r*   r&   )r"   rP   r/   r   )r0   raws     r   r2   zHfMount.__post_init__   sf    io2))#.. 	$/S2H2Ho\`\kooo    3I2Hr   c                     | j                                         d| j        g}| j        |                    | j        rdnd           d                    |          S )zqRender the mount as a canonical 'hf://' string.

        Example: 'hf://models/my-org/my-model:/data:ro'
        :N:ro:rwr   )rO   rC   rP   rQ   r@   rA   )r0   rB   s     r   rC   zHfMount.to_uri   sU    
 ##%%sDO<>%LL$.;e<<<wwu~~r   rD   )rE   rF   rG   rH   r   rJ   r.   rQ   rL   r   r"   r2   rC   r   r   r   rN   rN      s          " MMMOOO!Itd{!!!u%eUDQQQD#*QQQ         r   rN   r'   r#   c                    |                      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*   rS   r+   )r/   r   r<   r   len_split_type_parse_bucket_body_parse_repo_body)r'   rS   bodytype_locations        r   parse_hf_urirc      s    > >>)/00 
[	 5 [ [ ) 5[ [ [
 
 	
 Cs9())++,D NL93HLLLMMM!$C000OE8!(Es;;;;He5555r   	mount_strc                    |                      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          |          }n)# t          $ r}t          ||j                  |d}~ww xY wt          ||||          S )	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)
        ```
    rY   r*   r&   NrZ   r[   zKMissing mount path. Expected ':<MOUNT_PATH>' (e.g. 'hf://org/model:/data').)rO   rP   rQ   r"   )	r/   r   r<   r   r\   _split_mountrc   r(   rN   )	rd   rS   r`   rb   rP   rQ   uri_strrO   r1   s	            r   parse_hf_mountrh      s<   @ 	 566 
=I$9===
 
 
 	

 CS.//112D VS&T9;P&T&T&TUUUU&24S&A&A&A#Hj)S&stttt #h.G4g&& 4 4 4Sae,,,!34 &Z9SVWWWWs   ;C 
C1C,,C1r`   rS   c                ~   |                      d          rd|                     d          } }n/|                      d          rd|                     d          } }nd}|                     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.
    rV   TrW   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`   rS   rQ   idxrb   rP   s         r   rf   rf   "  s    
 }}U  1 1% 8 84			u		 !2!25!9!94			 **T

C
byy x    T4DSDzHcAgiiJ MS&KLLLLZ**r   rb   c          	          |                      d          }|dk    rs| t          j        v r$t          |d|  dt          j         |  d          t
                              |           x}!t          |dt          j         | d	          d
| fS | d|         }| |dz   d         }|t          j        v rt          j        |         |fS t
                              |          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)   rj   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   r<   r   get)rb   rS   	slash_idxsingular_pluralfirstrests         r   r]   r]   @  sV   
 c""IBy555ppp	H]p_gppp     /228<<<OIoAVoXgooo      ZiZ EIMOO$D	...-e4d::*..u555BjUjjVejjj
 
 
 	
 Hr   ra   c                z   d| v rt          |d          |                     d          } |                     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]'.r;   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splitr\   r   )rb   ra   rS   rB   	bucket_idpath_in_buckets         r   r^   r^   _  s     hS&[\\\\~~c""HNN3""E
5zzA~~U1X~U1X~S&^RZ&^&^&^____8((eAh((I!$UqU1XXbN#   r   c                   |                      d          } | st          |d          |                     d          }|dk    rrd}|                     dd          }t	          |          dk     rt          |d|  d	          |d
          d|d          }t	          |          dk    r|d         nd}n| d|         }| |dz   d         }|st          |d          |                    d          dk    rt          |d| d          t                              |          }	|	?|	                                }|t	          |          d         	                    d          }n7|                    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&   r;   rj   Nry   z-Repository id must be 'namespace/name', got 'z'. r   r	   r   z!Missing repository id before '@'.r*   zEmpty revision after '@'.r{   )r|   r   rq   r}   r\   r-   r=   matchgroupremoveprefixr   r   )rb   ra   rS   at_idxr   rB   repo_idr   rev_and_pathr   rt   s              r   r_   r_   w  s%    ~~c""H @S&>???? ]]3F||sA&&u::>>*gZb*g*g*ghhhh1X**a**#&u::>>uQxxr7F7#
- 	O*MNNNN==""*eZa*e*e*effff -22<@@{{}}H'H8EEcJJLL$))#..IB'!'

3+IMOO<8$$ 	G*EFFFF!   r   )$rH   redataclassesr   r   urllib.parser   huggingface_hubr   huggingface_hub.errorsr   r   _validatorsr
   rr   itemsr   dictr.   rJ   compiler=   	frozensetvaluesr   r   rN   rc   rh   tuplerL   rf   rI   r]   r^   r_   r   r   r   <module>r      s    , 
			 ( ( ( ( ( ( ( (             % % % % % % @ @ @ @ @ @ @ @ ) ) ) ) ) )
 #\"[I4R4X4X4Z4Z"["["[c3h [ [ [  *rz*MNN  $-9Y-K-R-R-T-T#U#U )C. U U U $J J J J J J J JZ $' ' ' ' ' ' ' 'T/6c /6e /6 /6 /6 /6d7Xc 7Xg 7X 7X 7X 7Xt+s +C +E#sTz4$;2N,O + + + +<# s uY5H#5M/N    > 
	
    0666 
	6
 6 6 6 6 6 6r   