a
    
d~                     @   sJ   d Z ddlZddlZddlmZ ddlmZ ddlmZ G dd dZ	dS )	zAContain helper class to retrieve/store token from/to local cache.    N)Path)Optional   )	constantsc                   @   sh   e Zd ZeejZeejZe	e
ddddZe	ee
 dddZe	dddd	Ze	d
d ZdS )HfFolderN)tokenreturnc                 C   s"   | j jjddd | j | dS )a  
        Save token, creating folder as needed.

        Token is saved in the huggingface home folder. You can configure it by setting
        the `HF_HOME` environment variable.

        Args:
            token (`str`):
                The token to save to the [`HfFolder`]
        T)parentsexist_okN)
path_tokenparentmkdir
write_textclsr    r   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/huggingface_hub/utils/_hf_folder.py
save_token   s    zHfFolder.save_token)r   c                 C   s^   z|    W n ty   Y n0 tjd}|dur8|S z| j W S  tyX   Y dS 0 dS )a\  
        Get token or None if not existent.

        Note that a token can be also provided using the `HUGGING_FACE_HUB_TOKEN` environment variable.

        Token is saved in the huggingface home folder. You can configure it by setting
        the `HF_HOME` environment variable. Previous location was `~/.huggingface/token`.
        If token is found in old location but not in new location, it is copied there first.
        For more details, see https://github.com/huggingface/huggingface_hub/issues/1232.

        Returns:
            `str` or `None`: The token, `None` if it doesn't exist.
        ZHUGGING_FACE_HUB_TOKENN)_copy_to_new_path_and_warn	Exceptionosenvirongetr   	read_textFileNotFoundErrorr   r   r   r   	get_token,   s    zHfFolder.get_tokenc                 C   sH   z| j   W n ty    Y n0 z| j  W n tyB   Y n0 dS )zX
        Deletes the token from storage. Does not fail if token does not exist.
        N)r   unlinkr   _old_path_tokenr   r   r   r   delete_tokenL   s    zHfFolder.delete_tokenc                 C   sD   | j  r@| j s@| | j   td| j  d| j d d S )NzA token has been found in `zG`. This is the old path where tokens were stored. The new location is `z` which is configurable using `HF_HOME` environment variable. Your token has been copied to this new location. You can now safely delete the old token file manually or use `huggingface-cli logout`.)r   existsr   r   r   warningswarnr   r   r   r   r   [   s    
z#HfFolder._copy_to_new_path_and_warn)__name__
__module____qualname__r   r   ZHF_TOKEN_PATHr   Z_OLD_HF_TOKEN_PATHr   classmethodstrr   r   r   r   r   r   r   r   r   r      s   

r   )
__doc__r   r!   pathlibr   typingr    r   r   r   r   r   r   <module>   s   