import{_ as s,o as a,c as t,b as n,d as o,e}from"./app-SlpTmndF.js";const i={},p=n("h3",{id:"def-flat-config-config-dict-str-any-dict-str-any",tabindex:"-1"},[n("a",{class:"header-anchor",href:"#def-flat-config-config-dict-str-any-dict-str-any"},[n("span",null,[n("em",null,[n("strong",null,"def")]),o(),n("code",null,"flat_config(config: dict[str, Any]) -> dict[str, Any]")])])],-1),c=n("p",null,"扁平化配置文件",-1),l=n("p",{"a.b.c:1":""},"{a:{b:{c:1}}} ->",-1),r=e(`

Args:

config: 配置项目

Returns:

扁平化后的配置文件,但也包含原有的键值对
源代码
def flat_config(config: dict[str, Any]) -> dict[str, Any]:
    """
    扁平化配置文件

    {a:{b:{c:1}}} -> {"a.b.c": 1}
    Args:
        config: 配置项目

    Returns:
        扁平化后的配置文件,但也包含原有的键值对
    """
    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

def load_from_yaml(file_: str) -> dict[str, Any]

Load config from yaml file

源代码
def load_from_yaml(file_: str) -> dict[str, Any]:
    """
    Load config from yaml file

    """
    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 {})

def load_from_json(file_: str) -> dict[str, Any]

Load config from json file

源代码
def load_from_json(file_: str) -> dict[str, Any]:
    """
    Load config from json file
    """
    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 {})

def load_from_toml(file_: str) -> dict[str, Any]

Load config from toml file

源代码
def load_from_toml(file_: str) -> dict[str, Any]:
    """
    Load config from toml file
    """
    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 {})

def load_from_files() -> dict[str, Any]

从指定文件加载配置项,会自动识别文件格式

默认执行扁平化选项

源代码
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

def load_configs_from_dirs() -> dict[str, Any]

从目录下加载配置文件,不递归

按照读取文件的优先级反向覆盖

默认执行扁平化选项

源代码
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

def load_config_in_default(no_waring: bool) -> dict[str, Any]

从一个标准的轻雪项目加载配置文件

项目目录下的config.*和config目录下的所有配置文件

项目目录下的配置文件优先

源代码
def load_config_in_default(no_waring: bool=False) -> dict[str, Any]:
    """
    从一个标准的轻雪项目加载配置文件
    项目目录下的config.*和config目录下的所有配置文件
    项目目录下的配置文件优先
    """
    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

class SatoriNodeConfig(BaseModel)

class SatoriConfig(BaseModel)

class BasicConfig(BaseModel)

var new_config = copy.deepcopy(config)

var config = yaml.safe_load(open(file_, 'r', encoding='utf-8'))

var config = json.load(open(file_, 'r', encoding='utf-8'))

var config = toml.load(open(file_, 'r', encoding='utf-8'))

var config = {}

var config = {}

var config = load_configs_from_dirs('config', no_waring=no_waring)

`,38),u=[p,c,l,r];function d(k,f){return a(),t("div",null,u)}const m=s(i,[["render",d],["__file","config.html.vue"]]),v=JSON.parse(`{"path":"/dev/api/config.html","title":"liteyuki.config","lang":"zh-CN","frontmatter":{"title":"liteyuki.config","order":1,"icon":"laptop-code","category":"API","description":"def flat_config(config: dict[str, Any]) -> dict[str, Any] 扁平化配置文件 {a:{b:{c:1}}} -> Args: Returns: 源代码 def load_from_yaml(file_: str) -> dict[str, Any] Load config from yaml file...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/config.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/config.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"liteyuki.config"}],["meta",{"property":"og:description","content":"def flat_config(config: dict[str, Any]) -> dict[str, Any] 扁平化配置文件 {a:{b:{c:1}}} -> Args: Returns: 源代码 def load_from_yaml(file_: str) -> dict[str, Any] Load config from yaml file..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-21T09:59:21.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-21T09:59:21.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.config\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-21T09:59:21.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"def flat_config(config: dict[str, Any]) -> dict[str, Any]","slug":"def-flat-config-config-dict-str-any-dict-str-any","link":"#def-flat-config-config-dict-str-any-dict-str-any","children":[]},{"level":3,"title":"def load_from_yaml(file_: str) -> dict[str, Any]","slug":"def-load-from-yaml-file-str-dict-str-any","link":"#def-load-from-yaml-file-str-dict-str-any","children":[]},{"level":3,"title":"def load_from_json(file_: str) -> dict[str, Any]","slug":"def-load-from-json-file-str-dict-str-any","link":"#def-load-from-json-file-str-dict-str-any","children":[]},{"level":3,"title":"def load_from_toml(file_: str) -> dict[str, Any]","slug":"def-load-from-toml-file-str-dict-str-any","link":"#def-load-from-toml-file-str-dict-str-any","children":[]},{"level":3,"title":"def load_from_files() -> dict[str, Any]","slug":"def-load-from-files-dict-str-any","link":"#def-load-from-files-dict-str-any","children":[]},{"level":3,"title":"def load_configs_from_dirs() -> dict[str, Any]","slug":"def-load-configs-from-dirs-dict-str-any","link":"#def-load-configs-from-dirs-dict-str-any","children":[]},{"level":3,"title":"def load_config_in_default(no_waring: bool) -> dict[str, Any]","slug":"def-load-config-in-default-no-waring-bool-dict-str-any","link":"#def-load-config-in-default-no-waring-bool-dict-str-any","children":[]},{"level":3,"title":"class SatoriNodeConfig(BaseModel)","slug":"class-satorinodeconfig-basemodel","link":"#class-satorinodeconfig-basemodel","children":[]},{"level":3,"title":"class SatoriConfig(BaseModel)","slug":"class-satoriconfig-basemodel","link":"#class-satoriconfig-basemodel","children":[]},{"level":3,"title":"class BasicConfig(BaseModel)","slug":"class-basicconfig-basemodel","link":"#class-basicconfig-basemodel","children":[]},{"level":3,"title":"var new_config = copy.deepcopy(config)","slug":"var-new-config-copy-deepcopy-config","link":"#var-new-config-copy-deepcopy-config","children":[]},{"level":3,"title":"var config = yaml.safe_load(open(file_, 'r', encoding='utf-8'))","slug":"var-config-yaml-safe-load-open-file-r-encoding-utf-8","link":"#var-config-yaml-safe-load-open-file-r-encoding-utf-8","children":[]},{"level":3,"title":"var config = json.load(open(file_, 'r', encoding='utf-8'))","slug":"var-config-json-load-open-file-r-encoding-utf-8","link":"#var-config-json-load-open-file-r-encoding-utf-8","children":[]},{"level":3,"title":"var config = toml.load(open(file_, 'r', encoding='utf-8'))","slug":"var-config-toml-load-open-file-r-encoding-utf-8","link":"#var-config-toml-load-open-file-r-encoding-utf-8","children":[]},{"level":3,"title":"var config = {}","slug":"var-config","link":"#var-config","children":[]},{"level":3,"title":"var config = {}","slug":"var-config-1","link":"#var-config-1","children":[]},{"level":3,"title":"var config = load_configs_from_dirs('config', no_waring=no_waring)","slug":"var-config-load-configs-from-dirs-config-no-waring-no-waring","link":"#var-config-load-configs-from-dirs-config-no-waring-no-waring","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724234361000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":4}]},"readingTime":{"minutes":2.5,"words":749},"filePathRelative":"dev/api/config.md","localizedDate":"2024年8月19日","autoDesc":true}`);export{m as comp,v as data};