|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Contain helper class to retrieve/store token from/to local cache.""" |
|
|
|
from pathlib import Path |
|
from typing import Optional |
|
|
|
from .. import constants |
|
from ._auth import get_token |
|
|
|
|
|
class HfFolder: |
|
|
|
|
|
@classmethod |
|
def save_token(cls, token: str) -> None: |
|
""" |
|
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`] |
|
""" |
|
path_token = Path(constants.HF_TOKEN_PATH) |
|
path_token.parent.mkdir(parents=True, exist_ok=True) |
|
path_token.write_text(token) |
|
|
|
|
|
|
|
@classmethod |
|
def get_token(cls) -> Optional[str]: |
|
""" |
|
Get token or None if not existent. |
|
|
|
This method is deprecated in favor of [`huggingface_hub.get_token`] but is kept for backward compatibility. |
|
Its behavior is the same as [`huggingface_hub.get_token`]. |
|
|
|
Returns: |
|
`str` or `None`: The token, `None` if it doesn't exist. |
|
""" |
|
return get_token() |
|
|
|
|
|
|
|
@classmethod |
|
def delete_token(cls) -> None: |
|
""" |
|
Deletes the token from storage. Does not fail if token does not exist. |
|
""" |
|
try: |
|
Path(constants.HF_TOKEN_PATH).unlink() |
|
except FileNotFoundError: |
|
pass |
|
|