From af8cd0d8198d7ce31cd58940d469100c41d8769f Mon Sep 17 00:00:00 2001 From: amtoaer Date: Sat, 24 Feb 2024 03:49:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20refresh=20=E4=B8=AD=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E4=BF=9D=E5=AD=98=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- credential.py | 3 +-- settings.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/credential.py b/credential.py index 10b9950..b99008a 100644 --- a/credential.py +++ b/credential.py @@ -17,8 +17,7 @@ class PersistedCredential(Credential): self.dedeuserid, self.ac_time_value, ) - # 暂时使用同步调用 - settings.save() + await settings.asave() credential = PersistedCredential() diff --git a/settings.py b/settings.py index 694e0ef..ed45c7c 100644 --- a/settings.py +++ b/settings.py @@ -6,6 +6,7 @@ from pydantic_core import PydanticCustomError from typing_extensions import Annotated from constants import DEFAULT_CONFIG_PATH +from utils import amakedirs, aopen class SubtitleConfig(BaseModel): @@ -57,6 +58,17 @@ class Config(BaseModel): except Exception as e: raise RuntimeError(f"Failed to save config file: {path}") from e + async def asave(self, path: Path | None = None) -> "Config": + if not path: + path = DEFAULT_CONFIG_PATH + try: + await amakedirs(path.parent, exist_ok=True) + async with aopen(path, "w") as f: + await f.write(Config.model_dump_json(self, indent=4)) + return self + except Exception as e: + raise RuntimeError(f"Failed to save config file: {path}") from e + def init_settings() -> Config: if not DEFAULT_CONFIG_PATH.exists():