🧑‍💻添加更多实用开发配置项 (#555)

This commit is contained in:
呵呵です
2025-07-28 01:31:55 +08:00
committed by GitHub
parent 068c508f57
commit 7a5170936b
5 changed files with 28 additions and 15 deletions

View File

@@ -1,3 +1,5 @@
from pathlib import Path
from nonebot import get_driver, get_plugin_config
from nonebot_plugin_localstore import get_plugin_cache_dir, get_plugin_data_dir
from pydantic import BaseModel, Field
@@ -14,11 +16,17 @@ class Proxy(BaseModel):
top: str | None = None
class Dev(BaseModel):
enabled: bool = False
template_path: Path | None = None
enable_template_check: bool = True
class ScopedConfig(BaseModel):
request_timeout: float = 30.0
screenshot_quality: float = 2
proxy: Proxy = Field(default_factory=Proxy)
development: bool = False
dev: Dev = Field(default_factory=Dev)
class Config(BaseModel):

View File

@@ -137,7 +137,7 @@ async def get_tetra_league_data() -> None:
await session.commit()
if not config.tetris.development:
if not config.tetris.dev.enabled:
@driver.on_startup
async def _() -> None:

View File

@@ -78,7 +78,7 @@ class BrowserManager:
"""启动浏览器实例"""
playwright = await async_playwright().start()
cls._browser = await playwright.firefox.launch(
headless=not config.tetris.development,
headless=not config.tetris.dev.enabled,
firefox_user_prefs={
'network.http.max-persistent-connections-per-server': 64,
},

View File

@@ -45,7 +45,7 @@ class HostPage:
async def __aenter__(self) -> str:
return self.page_hash
if not config.tetris.development:
if not config.tetris.dev.enabled:
async def __aexit__(self, exc_type, exc, tb) -> None: # noqa: ANN001
self.pages.pop(self.page_hash, None)

View File

@@ -17,7 +17,8 @@ from ..config.config import CACHE_PATH, DATA_PATH, config
driver = get_driver()
TEMPLATES_DIR = DATA_PATH / 'templates'
TEMPLATES_DIR = config.tetris.dev.template_path or DATA_PATH / 'templates'
alc = on_alconna(Alconna('更新模板', Option('--revision', Args['revision', str], alias={'-R'})), permission=SUPERUSER)
@@ -111,16 +112,6 @@ async def check_tag(tag: str) -> bool:
).status_code != HTTPStatus.NOT_FOUND
@driver.on_startup
async def _():
if (path := (TEMPLATES_DIR / 'hash.sha256')).is_file() and await check_hash(path):
logger.success('模板验证成功')
return
if not await init_templates('latest'):
msg = '模板初始化失败'
raise RuntimeError(msg)
@alc.handle()
async def _(revision: str | None = None):
if revision is not None and not await check_tag(revision):
@@ -129,3 +120,17 @@ async def _(revision: str | None = None):
if await init_templates(revision or 'latest'):
await alc.finish('更新模板成功')
await alc.finish('更新模板失败')
if config.tetris.dev.enable_template_check:
# !https://github.com/python/mypy/issues/19516
# 只能放def后面了(
@driver.on_startup
async def _():
if (path := (TEMPLATES_DIR / 'hash.sha256')).is_file() and await check_hash(path):
logger.success('模板验证成功')
return
if not await init_templates('latest'):
msg = '模板初始化失败'
raise RuntimeError(msg)