From f1b0679adf56a5f0ed56a1b1f089539904e19750 Mon Sep 17 00:00:00 2001 From: geoqiao <105639506+geoqiao@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:47:02 +0800 Subject: [PATCH] Update config_utils.py --- configs/config_utils.py | 51 ++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/configs/config_utils.py b/configs/config_utils.py index 5a58b65..60a1ac1 100644 --- a/configs/config_utils.py +++ b/configs/config_utils.py @@ -1,31 +1,30 @@ -import yaml - -CONFIG_FILE = "./configs/config.yaml" +from pathlib import Path - -def load_config(): - with open(CONFIG_FILE, "r") as file: - config = yaml.safe_load(file) - return config +import yaml -# def get_config_value(key, default=None): -# """ -# Get a configuration value by key, supporting nested keys. +class Config: + CONFIG_FILE = Path("./configs/config.yaml") -# Args: -# key (str): Configuration key, e.g. 'blog.title' or 'github.token'. -# default (any, optional): Default value if the key is not found. + def __init__(self): + config_loaded = Config.load_config() + self.blog_title = config_loaded["blog"]["title"] + self.github_name = config_loaded["github"]["name"] + self.meta_description = config_loaded["blog"]["description"] + self.theme_path = config_loaded["theme"]["path"] + self.google_search_verification = config_loaded["GoogleSearchConsole"][ + "content" + ] -# Returns: -# any: Configuration value or default value if the key is not found. -# """ -# config = load_config() -# keys = key.split(".") -# value = config -# for k in keys: -# if isinstance(value, dict) and k in value: -# value = value[k] -# else: -# return default -# return value + @classmethod + def load_config(cls) -> dict: + try: + with open(cls.CONFIG_FILE, "r") as file: + config = yaml.safe_load(file) + return config + except yaml.YAMLError as e: + print(f"Error reading YAML file: {e}") + raise + except FileNotFoundError: + print(f"Config file not found: {cls.CONFIG_FILE}") + raise