创建工程
This commit is contained in:
40
src/config.py
Normal file
40
src/config.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Configuration loader."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
CONFIG_PATH = Path(__file__).parent.parent / "config.json"
|
||||
|
||||
DEFAULT_CONFIG: dict[str, str] = {
|
||||
"language": "Chinese",
|
||||
"raw_dir": "raw",
|
||||
"wiki_dir": "wiki",
|
||||
}
|
||||
|
||||
|
||||
def load_config() -> dict[str, str]:
|
||||
"""
|
||||
加载项目配置。
|
||||
|
||||
返回
|
||||
-------
|
||||
dict[str, str]
|
||||
配置字典,包含 language、raw_dir、wiki_dir 等键。
|
||||
"""
|
||||
if CONFIG_PATH.is_file():
|
||||
with CONFIG_PATH.open(encoding="utf-8") as f:
|
||||
user_config = json.load(f)
|
||||
config = {**DEFAULT_CONFIG, **user_config}
|
||||
else:
|
||||
config = DEFAULT_CONFIG.copy()
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def get_language() -> str | None:
|
||||
"""获取配置的语言设置,"auto" 返回 None 以启用自动检测。"""
|
||||
config = load_config()
|
||||
language = config.get("language", "auto")
|
||||
if language.lower() == "auto":
|
||||
return None
|
||||
return language
|
||||
Reference in New Issue
Block a user