import{_ as a,c as n,a7 as i,j as t,o as l}from"./chunks/framework.v7PlT0Wt.js";const E=JSON.parse('{"title":"liteyuki.config","description":"","frontmatter":{"title":"liteyuki.config"},"headers":[],"relativePath":"en/dev/api/config.md","filePath":"en/dev/api/config.md","lastUpdated":1725101868000}'),h={name:"en/dev/api/config.md"};function e(k,s,p,r,d,o){return l(),n("div",null,s[0]||(s[0]=[i('
liteyuki.config
该模块用于常用配置文件的加载 多配置文件编写原则:
flat_config(config: dict[str, Any]) -> dict[str, Any]
Description: 扁平化配置文件
',6),t("p",{"a.b.c:":"",1:""},"{a:{b:{c:1}}} ->",-1),i(`Arguments:
- config: 配置项目
Return: 扁平化后的配置文件,但也包含原有的键值对
def flat_config(config: dict[str, Any]) -> dict[str, Any]:
new_config = copy.deepcopy(config)
for key, value in config.items():
if isinstance(value, dict):
for k, v in flat_config(value).items():
new_config[f'{key}.{k}'] = v
return new_config
load_from_yaml(file_: str) -> dict[str, Any]
Description: Load config from yaml file
def load_from_yaml(file_: str) -> dict[str, Any]:
logger.debug(f'Loading YAML config from {file_}')
config = yaml.safe_load(open(file_, 'r', encoding='utf-8'))
return flat_config(config if config is not None else {})
load_from_json(file_: str) -> dict[str, Any]
Description: Load config from json file
def load_from_json(file_: str) -> dict[str, Any]:
logger.debug(f'Loading JSON config from {file_}')
config = json.load(open(file_, 'r', encoding='utf-8'))
return flat_config(config if config is not None else {})
load_from_toml(file_: str) -> dict[str, Any]
Description: Load config from toml file
def load_from_toml(file_: str) -> dict[str, Any]:
logger.debug(f'Loading TOML config from {file_}')
config = toml.load(open(file_, 'r', encoding='utf-8'))
return flat_config(config if config is not None else {})
load_from_files(*files: str, *, no_warning: bool = False) -> dict[str, Any]
Description: 从指定文件加载配置项,会自动识别文件格式 默认执行扁平化选项
def load_from_files(*files: str, no_warning: bool=False) -> dict[str, Any]:
config = {}
for file in files:
if os.path.exists(file):
if file.endswith(('.yaml', 'yml')):
config.update(load_from_yaml(file))
elif file.endswith('.json'):
config.update(load_from_json(file))
elif file.endswith('.toml'):
config.update(load_from_toml(file))
elif not no_warning:
logger.warning(f'Unsupported config file format: {file}')
elif not no_warning:
logger.warning(f'Config file not found: {file}')
return config
load_configs_from_dirs(*directories: str, *, no_waring: bool = False) -> dict[str, Any]
Description: 从目录下加载配置文件,不递归 按照读取文件的优先级反向覆盖 默认执行扁平化选项
def load_configs_from_dirs(*directories: str, no_waring: bool=False) -> dict[str, Any]:
config = {}
for directory in directories:
if not os.path.exists(directory):
if not no_waring:
logger.warning(f'Directory not found: {directory}')
continue
for file in os.listdir(directory):
if file.endswith(_SUPPORTED_CONFIG_FORMATS):
config.update(load_from_files(os.path.join(directory, file), no_warning=no_waring))
return config
load_config_in_default(no_waring: bool = False) -> dict[str, Any]
Description: 从一个标准的轻雪项目加载配置文件 项目目录下的config.*和config目录下的所有配置文件 项目目录下的配置文件优先
Arguments:
- no_waring: 是否关闭警告
def load_config_in_default(no_waring: bool=False) -> dict[str, Any]:
config = load_configs_from_dirs('config', no_waring=no_waring)
config.update(load_from_files('config.yaml', 'config.toml', 'config.json', 'config.yml', no_warning=no_waring))
return config
Loader
__init__(self)
def __init__(self):
self.config = {}
load_from_yaml(self, fp: str) -> Loader
Description: 从yaml文件加载配置
Arguments:
- fp:
def load_from_yaml(self, fp: str) -> 'Loader':
with open(fp, 'r') as file:
self.config.update(yaml.safe_load(file))
return self
load_from_toml(self, fp: str) -> Loader
Description: 从toml文件加载配置
def load_from_toml(self, fp: str) -> 'Loader':
with open(fp, 'r') as file:
self.config.update(toml.load(file))
return self
load_from_json(self, fp: str) -> Loader
Description: 从json文件加载配置
def load_from_json(self, fp: str) -> 'Loader':
with open(fp, 'r') as file:
self.config.update(json.load(file))
return self
load_from_env(self, prefix: str = '') -> Loader
Description: 从环境变量加载配置
def load_from_env(self, prefix: str='') -> 'Loader':
for key, value in os.environ.items():
if key.startswith(prefix):
self.config[key[len(prefix):]] = value
return self
merge(self, loader: Loader) -> Loader
Description: 合并两个Loader键值对树
def merge(self, loader: 'Loader') -> 'Loader':
self.config.update(loader.config)
return self
get(self, key: str, default: Any = None) -> Any
Description: 获取配置值
def get(self, key: str, default: Any=None) -> Any:
return self.config.get(key, default)