refactor: refresh 中异步保存文件

This commit is contained in:
amtoaer
2024-02-24 03:49:28 +08:00
parent a4c362d8ab
commit af8cd0d819
2 changed files with 13 additions and 2 deletions

View File

@@ -17,8 +17,7 @@ class PersistedCredential(Credential):
self.dedeuserid,
self.ac_time_value,
)
# 暂时使用同步调用
settings.save()
await settings.asave()
credential = PersistedCredential()

View File

@@ -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():