o
    0jP@                  
   @   sr  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	d
 ej D Zeeef ed< edZeej Zee ed< eddG dd dZeddG dd dZdedefddZdedefddZdededeeedB edB f fddZdededeej ef fd d!Z!ded"ej dedefd#d$Z"ded"ej dedefd%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                 C   s   i | ]\}}||qS  r
   ).0kvr
   r
   _/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/huggingface_hub/utils/_hf_uris.py
<dictcomp>0   s    r   _TYPE_TO_PREFIXz ^refs/(?:convert/[\w.-]+|pr/\d+)_VALID_URI_TYPEST)frozenc                   @   s   e Zd ZU dZejed< eed< dZedB ed< dZ	eed< e
ddddd	ZedB ed
< dddZedefddZedefddZdefd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              
   C   s  | j pd}| jtvrt|d| j dtt dd| jr%| jddkr0t|d| j d	d| jd
krRzt| j W n tyQ } z	t|t	|d|d }~ww | j
d ur`| j
s`t|dd| jd
krp| j
d urpt|dd| jr| jds~d| jv rt|d| j d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"   er
   r
   r   __post_init__S   s*   

 
zHfUri.__post_init__c                 C   s
   | j dkS )z$True if this URI points at a bucket.r&   r   r+   r
   r
   r   	is_bucketn      
zHfUri.is_bucketc                 C   s
   | j dkS )zJTrue if this URI points at a repository (model, dataset, space or kernel).r&   r.   r/   r
   r
   r   is_repos   r1   zHfUri.is_repoc                 C   sz   t jt| j d| jg}| jdur,| j}d|v r$t|du r$|dd}|	d|  | j
r8|	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)r+   partsr   r
   r
   r   to_urix   s   

zHfUri.to_urir   N)__name__
__module____qualname____doc__r   	HfUriType__annotations__r)   r   r   r   r   r-   propertyboolr0   r2   r;   r
   r
   r
   r   r   =   s   
 

r   c                   @   sf   e Zd ZU dZeed< eed< dZedB ed< e	dddddZ
edB ed< dd
dZd	efd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                 C   s:   | j pd}| jdr| jdkrt|d| j ddd S )Nr   r$   zEMount path must be a non-empty absolute path starting with '/', got 'r%   r!   )r   rG   r*   r   )r+   rawr
   r
   r   r-      s   
zHfMount.__post_init__c                 C   s:   | j  d| jg}| jdur|| 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   )rF   r;   rG   rH   r8   r9   )r+   r:   r
   r
   r   r;      s   

zHfMount.to_urir<   )r=   r>   r?   r@   r   rB   r)   rH   rD   r   r   r-   r;   r
   r
   r
   r   rE      s   
 
rE   r"   r   c                 C   s   |  tjst| dtj dtj d| }| ttjd }|s+t| dtj dt||d\}}|dkr>t|||dS t|||d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%   rI   r&   )r*   r   r4   r   len_split_type_parse_bucket_body_parse_repo_body)r"   rI   bodytype_locationr
   r
   r   parse_hf_uri   s   
rW   	mount_strc           	   
   C   s   |  tjst| dtj dd| }| ttjd }|s)t|dtj ddt||d\}}}|du r<t|ddtj| }zt|}W n ty[ } zt||jd|d}~ww t||||d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)
        ```
    rM   r%   r!   NrN   rO   zKMissing mount path. Expected ':<MOUNT_PATH>' (e.g. 'hf://org/model:/data').)rF   rG   rH   r   )	r*   r   r4   r   rP   _split_mountrW   r#   rE   )	rX   rI   rT   rV   rG   rH   Zuri_strrF   r,   r
   r
   r   parse_hf_mount   s(    
rZ   rT   rI   c                C   s   |  drd| d}} n|  drd| d}} nd}| d}|dkr6|dur1t|dd	| ddfS | d| }| |d
 d }|sLt|d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.
    rK   TrL   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   )rT   rI   rH   idxrV   rG   r
   r
   r   rY   "  s$   




rY   rV   c                C   s   |  d}|dkr8| tjv rt|d|  dtj |  ddt|  }dur4t|dtj | d	dd
| fS | d| }| |d d }|tjv rRtj| |fS t| }durht|d| 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$   r[   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   r4   r   get)rV   rI   	slash_idxZsingular_pluralfirstrestr
   r
   r   rQ   @  s,   


rQ   rU   c                C   s   d| v r
t |dd| d} | dd}t|dk s#|d r#|d s-t |d|  d	d|d  d|d  }t|d
krB|d nd}t||d||dS )z8Parse the body of a bucket URI: 'namespace/name[/path]'.r3   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splitrP   r   )rV   rU   rI   r:   Z	bucket_idZpath_in_bucketr
   r
   r   rR   _  s   
rR   c                C   sn  |  d} | st|dd| d}|dkrFd}| dd}t|dk r.t|d|  d	d|d
  d|d  }t|dkrC|d nd}nh| d| }| |d d }|s\t|dd|ddkrmt|d| ddt|}	|	dur|	 }|t|d 	d}n|d}
|
dkr|}d}n|d|
 }||
d d }t
|}|st|ddt|||||dS )z@Parse the body of a repo URI: '<repo_id>[@<revision>][/<path>]'.r$   zMissing repository id.r!   r3   r[   Nrg   z-Repository id must be 'namespace/name', got 'z'. r   r   r   z!Missing repository id before '@'.r%   zEmpty revision after '@'.ri   )rj   r   ra   rk   rP   r(   r5   matchgroupremoveprefixr   r   )rV   rU   rI   Zat_idxr   r:   Zrepo_idr   Zrev_and_pathrl   rd   r
   r
   r   rS   w  sJ   



rS   )$r@   redataclassesr   r   urllib.parser   Zhuggingface_hubr   Zhuggingface_hub.errorsr   r   Z_validatorsr	   rb   itemsr   dictr)   rB   compiler5   	frozensetvaluesr   r   rE   rW   rZ   tuplerD   rY   rA   rQ   rR   rS   r
   r
   r
   r   <module>   sH   $
M*2(: 
