Skip to content

config

@Time : 2023/5/1 11:59 @Author : adamzh0u @File : const.py

get_project_root()

Search upwards to find the project root directory.

Source code in src/config.py
def get_project_root():
    """Search upwards to find the project root directory."""
    current_path = Path.cwd()
    while True:
        if (
            (current_path / ".git").exists()
            or (current_path / ".project_root").exists()
            or (current_path / ".gitignore").exists()
        ):
            logger.info(f"PROJECT_ROOT set to {str(current_path)}")
            return current_path
        parent_path = current_path.parent
        if parent_path == current_path:
            # loop until top level and land cwd
            cwd = Path.cwd()
            logger.info(f"PROJECT_ROOT set to current working directory: {str(cwd)}")
            return cwd
        current_path = parent_path

load_config(CONFIG_FILES)

Load config files, prioritize environment-specific configs, inherit missing configs from base config :return: dict

Source code in src/config.py
def load_config(CONFIG_FILES):
    """
    Load config files, prioritize environment-specific configs, inherit missing configs from base config
    :return: dict
    """
    final_config = {}
    for file in CONFIG_FILES:
        with open(file, "rb") as f:
            config = tomllib.load(f)
        final_config.update(config)
    return final_config