kerykeion.settings.kerykeion_settings

This is part of Kerykeion (C) 2024 Giacomo Battaglia

 1# -*- coding: utf-8 -*-
 2"""
 3    This is part of Kerykeion (C) 2024 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
12
13
14def get_settings(new_settings_file: Union[Path, None] = None) -> KerykeionSettingsModel:
15    """
16    This function is used to get the settings dict from the settings file.
17    If no settings file is passed as argument, or the file is not found, it will fallback to:
18    - The system wide config file, located in ~/.config/kerykeion/kr.config.json
19    - The default config file, located in the package folder
20    
21    Args:
22        new_settings_file (Union[Path, None], optional): The path of the settings file. Defaults to None.
23        
24    Returns:
25        Dict: The settings dict
26    """
27
28    # Config path we passed as argument
29    if new_settings_file is not None:
30        settings_file = new_settings_file
31
32        if not settings_file.exists():
33            raise FileNotFoundError(f"File {settings_file} does not exist")
34
35    # System wide config path
36    else:
37        home_folder = Path.home()
38        settings_file = home_folder / ".config" / "kerykeion" / "kr.config.json"
39
40    # Fallback to the default config in the package
41    if not settings_file.exists():
42        settings_file = Path(__file__).parent / "kr.config.json"
43
44    logging.debug(f"Kerykeion config file path: {settings_file}")
45    with open(settings_file, "r", encoding="utf8") as f:
46        settings_dict = load(f)
47
48    return KerykeionSettingsModel(**settings_dict)
49
50
51def merge_settings(settings: KerykeionSettingsModel, new_settings: Dict) -> KerykeionSettingsModel:
52    """
53    This function is used to merge the settings file with the default settings,
54    it's useful to add new settings to the config file without breaking the old ones.
55    
56    Args:
57        settings (KerykeionSettingsModel): The default settings
58        new_settings (Dict): The new settings to add to the default ones
59        
60    Returns:
61        KerykeionSettingsModel: The new settings
62    """
63    new_settings_dict = settings.model_dump() | new_settings
64    return KerykeionSettingsModel(**new_settings_dict)
65
66
67if __name__ == "__main__":
68    from kerykeion.utilities import setup_logging
69    setup_logging(level="debug")
70
71    print(get_settings())
def get_settings( new_settings_file: Optional[pathlib.Path] = None) -> kerykeion.kr_types.settings_models.KerykeionSettingsModel:
15def get_settings(new_settings_file: Union[Path, None] = 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    # Config path we passed as argument
30    if new_settings_file is not None:
31        settings_file = new_settings_file
32
33        if not settings_file.exists():
34            raise FileNotFoundError(f"File {settings_file} does not exist")
35
36    # System wide config path
37    else:
38        home_folder = Path.home()
39        settings_file = home_folder / ".config" / "kerykeion" / "kr.config.json"
40
41    # Fallback to the default config in the package
42    if not settings_file.exists():
43        settings_file = Path(__file__).parent / "kr.config.json"
44
45    logging.debug(f"Kerykeion config file path: {settings_file}")
46    with open(settings_file, "r", encoding="utf8") as f:
47        settings_dict = load(f)
48
49    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

52def merge_settings(settings: KerykeionSettingsModel, new_settings: Dict) -> KerykeionSettingsModel:
53    """
54    This function is used to merge the settings file with the default settings,
55    it's useful to add new settings to the config file without breaking the old ones.
56    
57    Args:
58        settings (KerykeionSettingsModel): The default settings
59        new_settings (Dict): The new settings to add to the default ones
60        
61    Returns:
62        KerykeionSettingsModel: The new settings
63    """
64    new_settings_dict = settings.model_dump() | new_settings
65    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