o
    hf                      @   sP  U d Z ddlZddlZddlZddl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	lmZmZ d
ae Zdaee ed< eeZdee fddZdee fddZdee fddZdee fddZde
eef fddZde
eef ddfddZdedee fddZdededdfddZ dee dee fd d!Z!dS )"zWContains an helper to get the token from machine (env variable, secret or config file).    N)Path)Lock)DictOptional   )	constants   )is_colab_enterpriseis_google_colabF_GOOGLE_COLAB_SECRETreturnc                   C   s   t  pt pt S )aI  
    Get token if user is logged in.

    Note: in most cases, you should use [`huggingface_hub.utils.build_hf_headers`] instead. This method is only useful
          if you want to retrieve the token for other purposes than sending an HTTP request.

    Token is retrieved in priority from the `HF_TOKEN` environment variable. Otherwise, we read the token file located
    in the Hugging Face home folder. Returns None if user is not logged in. To log in, use [`login`] or
    `huggingface-cli login`.

    Returns:
        `str` or `None`: The token, `None` if it doesn't exist.
    )_get_token_from_google_colab_get_token_from_environment_get_token_from_file r   r   o/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/utils/_auth.py	get_token#   s   r   c                  C   s*  t  rt rdS t trtW  d   S zddlm}  ddlm} W n t	y4   Y W d   dS w z| 
d}t|aW n? | jyQ   td daY n/ | jya   td daY n |y } ztdt| d	 daW Y d}~nd}~ww d
atW  d   S 1 sw   Y  dS )zGet token from Google Colab secrets vault using `google.colab.userdata.get(...)`.

    Token is read from the vault only once per session and then stored in a global variable to avoid re-requesting
    access to the vault.
    Nr   )userdata)ErrorHF_TOKENz
Access to the secret `HF_TOKEN` has not been granted on this notebook.
You will not be requested again.
Please restart the session if you want to be prompted again.a  
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.z@
Error while fetching `HF_TOKEN` secret value from your vault: 'z'.
You are not authenticated with the Hugging Face Hub in this notebook.
If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).T)r
   r	   _GOOGLE_COLAB_SECRET_LOCK_IS_GOOGLE_COLAB_CHECKEDr   google.colabr   google.colab.errorsr   ImportErrorget_clean_tokenNotebookAccessErrorwarningswarnSecretNotFoundErrorstr)r   
ColabErrortokener   r   r   r   4   sH   


$r   c                   C   s   t tjdptjdS )Nr   HUGGING_FACE_HUB_TOKEN)r   osenvironr   r   r   r   r   r   t   s   r   c                   C   s*   z
t ttj W S  ty   Y d S w )N)r   r   r   HF_TOKEN_PATH	read_textFileNotFoundErrorr   r   r   r   r   y   s
   r   c               
      s   t tj} |  si }t  z |   fdd  D }W |S  tjy@ } zt	
d|  i }W Y d}~|S d}~ww )aA  
    Returns the parsed INI file containing the access tokens.
    The file is located at `HF_STORED_TOKENS_PATH`, defaulting to `~/.cache/huggingface/stored_tokens`.
    If the file does not exist, an empty dictionary is returned.

    Returns: `Dict[str, str]`
        Key is the token name and value is the token.
    c                    s   i | ]	}|  |d qS )hf_token)r   ).0
token_nameconfigr   r   
<dictcomp>   s    z%get_stored_tokens.<locals>.<dictcomp>z"Error parsing stored tokens file: N)r   r   HF_STORED_TOKENS_PATHexistsconfigparserConfigParserreadsectionsr   loggererror)tokens_pathstored_tokensr$   r   r.   r   get_stored_tokens   s   
	
r;   r:   c                 C   s   t tj}t }t|  D ]}|| ||d| |  q|j	j
ddd |d}|| W d   dS 1 s>w   Y  dS )z
    Saves the given configuration to the stored tokens file.

    Args:
        stored_tokens (`Dict[str, str]`):
            The stored tokens to save. Key is the token name and value is the token.
    r+   T)parentsexist_okwN)r   r   r1   r3   r4   sortedkeysadd_sectionsetparentmkdiropenwrite)r:   stored_tokens_pathr/   r-   config_filer   r   r   _save_stored_tokens   s   

"rI   r-   c                 C   s   t  }| |vr	dS t||  S )z
    Get the token by name.

    Args:
        token_name (`str`):
            The name of the token to get.

    Returns:
        `str` or `None`: The token, `None` if it doesn't exist.

    N)r;   r   )r-   r:   r   r   r   _get_token_by_name   s   rJ   r#   c                 C   s:   t tj}t }| ||< t| td| d|  dS )z
    Save the given token.

    If the stored tokens file does not exist, it will be created.
    Args:
        token (`str`):
            The token to save.
        token_name (`str`):
            The name of the token.
    zThe token `z` has been saved to N)r   r   r1   r;   rI   r7   info)r#   r-   r9   r:   r   r   r   _save_token   s
   
rL   c                 C   s(   | du rdS |  dd dd pdS )zuClean token by removing trailing and leading spaces and newlines.

    If token is an empty string, return None.
    N 
)replacestrip)r#   r   r   r   r      s   r   )"__doc__r3   loggingr&   r   pathlibr   	threadingr   typingr   r   rN   r   _runtimer	   r
   r   r   r   r!   __annotations__	getLogger__name__r7   r   r   r   r   r;   rI   rJ   rL   r   r   r   r   r   <module>   s.   
@