kerykeion.settings.kerykeion_settings
This is part of Kerykeion (C) 2025 Giacomo Battaglia
1# -*- coding: utf-8 -*- 2""" 3 This is part of Kerykeion (C) 2025 Giacomo Battaglia 4""" 5 6 7from json import load 8import logging 9from pathlib import Path 10from typing import Dict, Union 11from kerykeion.kr_types import KerykeionSettingsModel 12import functools 13 14 15def get_settings(new_settings_file: Union[Path, None, KerykeionSettingsModel, dict] = None) -> KerykeionSettingsModel: 16 """ 17 This function is used to get the settings dict from the settings file. 18 If no settings file is passed as argument, or the file is not found, it will fallback to: 19 - The system wide config file, located in ~/.config/kerykeion/kr.config.json 20 - The default config file, located in the package folder 21 22 Args: 23 new_settings_file (Union[Path, None], optional): The path of the settings file. Defaults to None. 24 25 Returns: 26 Dict: The settings dict 27 """ 28 29 if isinstance(new_settings_file, dict): 30 return KerykeionSettingsModel(**new_settings_file) 31 elif isinstance(new_settings_file, KerykeionSettingsModel): 32 return new_settings_file 33 34 # Config path we passed as argument 35 if new_settings_file is not None: 36 settings_file = new_settings_file 37 38 if not settings_file.exists(): 39 raise FileNotFoundError(f"File {settings_file} does not exist") 40 41 # System wide config path 42 else: 43 home_folder = Path.home() 44 settings_file = home_folder / ".config" / "kerykeion" / "kr.config.json" 45 46 # Fallback to the default config in the package 47 if not settings_file.exists(): 48 settings_file = Path(__file__).parent / "kr.config.json" 49 50 logging.debug(f"Kerykeion config file path: {settings_file}") 51 settings_dict = load_settings_file(settings_file) 52 53 return KerykeionSettingsModel(**settings_dict) 54 55 56def merge_settings(settings: KerykeionSettingsModel, new_settings: Dict) -> KerykeionSettingsModel: 57 """ 58 This function is used to merge the settings file with the default settings, 59 it's useful to add new settings to the config file without breaking the old ones. 60 61 Args: 62 settings (KerykeionSettingsModel): The default settings 63 new_settings (Dict): The new settings to add to the default ones 64 65 Returns: 66 KerykeionSettingsModel: The new settings 67 """ 68 new_settings_dict = settings.model_dump() | new_settings 69 return KerykeionSettingsModel(**new_settings_dict) 70 71 72@functools.lru_cache 73def load_settings_file(settings_file_path: str) -> dict: 74 """ 75 This function is used to load the settings file from a path. 76 77 Args: 78 settings_file (Path): The path of the settings file 79 80 Returns: 81 dict: The settings dict 82 """ 83 with open(settings_file_path, "r", encoding="utf8") as f: 84 settings_dict = load(f) 85 86 return settings_dict 87 88 89if __name__ == "__main__": 90 from kerykeion.utilities import setup_logging 91 setup_logging(level="debug") 92 93 print(get_settings())
16def get_settings(new_settings_file: Union[Path, None, KerykeionSettingsModel, dict] = None) -> KerykeionSettingsModel: 17 """ 18 This function is used to get the settings dict from the settings file. 19 If no settings file is passed as argument, or the file is not found, it will fallback to: 20 - The system wide config file, located in ~/.config/kerykeion/kr.config.json 21 - The default config file, located in the package folder 22 23 Args: 24 new_settings_file (Union[Path, None], optional): The path of the settings file. Defaults to None. 25 26 Returns: 27 Dict: The settings dict 28 """ 29 30 if isinstance(new_settings_file, dict): 31 return KerykeionSettingsModel(**new_settings_file) 32 elif isinstance(new_settings_file, KerykeionSettingsModel): 33 return new_settings_file 34 35 # Config path we passed as argument 36 if new_settings_file is not None: 37 settings_file = new_settings_file 38 39 if not settings_file.exists(): 40 raise FileNotFoundError(f"File {settings_file} does not exist") 41 42 # System wide config path 43 else: 44 home_folder = Path.home() 45 settings_file = home_folder / ".config" / "kerykeion" / "kr.config.json" 46 47 # Fallback to the default config in the package 48 if not settings_file.exists(): 49 settings_file = Path(__file__).parent / "kr.config.json" 50 51 logging.debug(f"Kerykeion config file path: {settings_file}") 52 settings_dict = load_settings_file(settings_file) 53 54 return KerykeionSettingsModel(**settings_dict)
This function is used to get the settings dict from the settings file. If no settings file is passed as argument, or the file is not found, it will fallback to:
- The system wide config file, located in ~/.config/kerykeion/kr.config.json
- The default config file, located in the package folder
Args: new_settings_file (Union[Path, None], optional): The path of the settings file. Defaults to None.
Returns: Dict: The settings dict
57def merge_settings(settings: KerykeionSettingsModel, new_settings: Dict) -> KerykeionSettingsModel: 58 """ 59 This function is used to merge the settings file with the default settings, 60 it's useful to add new settings to the config file without breaking the old ones. 61 62 Args: 63 settings (KerykeionSettingsModel): The default settings 64 new_settings (Dict): The new settings to add to the default ones 65 66 Returns: 67 KerykeionSettingsModel: The new settings 68 """ 69 new_settings_dict = settings.model_dump() | new_settings 70 return KerykeionSettingsModel(**new_settings_dict)
This function is used to merge the settings file with the default settings, it's useful to add new settings to the config file without breaking the old ones.
Args: settings (KerykeionSettingsModel): The default settings new_settings (Dict): The new settings to add to the default ones
Returns: KerykeionSettingsModel: The new settings
73@functools.lru_cache 74def load_settings_file(settings_file_path: str) -> dict: 75 """ 76 This function is used to load the settings file from a path. 77 78 Args: 79 settings_file (Path): The path of the settings file 80 81 Returns: 82 dict: The settings dict 83 """ 84 with open(settings_file_path, "r", encoding="utf8") as f: 85 settings_dict = load(f) 86 87 return settings_dict
This function is used to load the settings file from a path.
Args: settings_file (Path): The path of the settings file
Returns: dict: The settings dict