234
This commit is contained in:
1 parent
881d0b8719
commit
bac04def8e
10 files changed
+400
-31
No files matched your search
@@ -2,10 +2,15 @@
|
||||
配置文件读取工具
|
||||
支持从 config.toml 文件中读取配置信息
|
||||
"""
|
||||
import toml
|
||||
import os
|
||||
import sys
|
||||
|
||||
# 优先使用 Python 3.11+ 内置的 tomllib,否则回退到 toml 包
|
||||
try:
|
||||
import tomllib # Python 3.11+
|
||||
except ImportError:
|
||||
import toml as tomllib # 回退到 toml 包
|
||||
|
||||
|
||||
def get_config_path():
|
||||
"""
|
||||
@@ -64,8 +69,15 @@ def cfg(key_name, default=None):
|
||||
print(f"警告: 配置文件不存在,路径: {config_path}")
|
||||
return default
|
||||
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
config = toml.load(f)
|
||||
# tomllib 需要二进制模式,toml 包需要文本模式
|
||||
if hasattr(tomllib, 'load'):
|
||||
# Python 3.11+ 的 tomllib
|
||||
with open(config_path, 'rb') as f:
|
||||
config = tomllib.load(f)
|
||||
else:
|
||||
# toml 包
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
config = tomllib.load(f)
|
||||
|
||||
# 处理嵌套键名,如 "redis.host"
|
||||
if "." in key_name:
|
||||
|
||||
Reference in new issue
Block a user