diff --git a/qnexus/client/utils.py b/qnexus/client/utils.py index f0a382d..9bc3997 100644 --- a/qnexus/client/utils.py +++ b/qnexus/client/utils.py @@ -5,7 +5,6 @@ import os import warnings from functools import wraps -from pathlib import Path from typing import Any, Callable, Literal, ParamSpec, TypeVar from httpx import Response @@ -45,7 +44,7 @@ def remove_token(token_type: TokenTypes) -> None: # Don't try to delete refresh token in Jupyterhub if is_jupyterhub_environment() and token_type == "refresh_token": return - token_file_path = Path.home() / CONFIG.token_path / token_file_from_type[token_type] + token_file_path = CONFIG.resolved_token_path / token_file_from_type[token_type] if token_file_path.exists(): token_file_path.unlink() @@ -77,7 +76,7 @@ class AccessToken(BaseModel): def read_token(token_type: TokenTypes) -> str: """Read a token from a file.""" - token_file_path = Path.home() / CONFIG.token_path + token_file_path = CONFIG.resolved_token_path with (token_file_path / token_file_from_type[token_type]).open( encoding="UTF-8" ) as file: @@ -94,7 +93,7 @@ def write_token(token_type: TokenTypes, token: str) -> None: if is_jupyterhub_environment() and token_type == "refresh_token": return - token_file_path = Path.home() / CONFIG.token_path + token_file_path = CONFIG.resolved_token_path token_file_path.mkdir(parents=True, exist_ok=True) with (token_file_path / token_file_from_type[token_type]).open( encoding="UTF-8", mode="w" diff --git a/qnexus/config.py b/qnexus/config.py index 3261e13..21d6606 100644 --- a/qnexus/config.py +++ b/qnexus/config.py @@ -1,5 +1,6 @@ """Quantinuum Nexus API client configuration via pydantic-settings.""" +from pathlib import Path from pydantic import AliasChoices, Field from pydantic_settings import BaseSettings, SettingsConfigDict @@ -24,6 +25,14 @@ class Config(BaseSettings): # auth store_tokens: bool = True # Not implemented token_path: str = ".qnx/auth" + absolute_token_path: str | None = None + + @property + def resolved_token_path(self) -> Path: + """Resolve token path from token_path and absolute_token_path.""" + if self.absolute_token_path is not None: + return Path(self.absolute_token_path) + return Path(Path.home(), self.token_path) # testing qa_user_email: str = ""