wmaee.core.config
1import os 2import yaml 3from typing import Optional, Any, Dict 4 5 6def load_config(path: Optional[str] = None) -> Dict[str, Any]: 7 """ 8 Loads a YAML config file from the specified path. If the path is not specified, 9 the routine will try to load "~/.wmaee.conf.yaml" in case it exists. Otherwise, 10 it uses the value from the environment variable `WMAEE_CONFIG_FILE`. If all options fail, 11 a `FileNotFoundError` is raised. 12 13 Parameters 14 ---------- 15 path : Optional[str], optional 16 Path to the config file. 17 18 Returns 19 ------- 20 Dict[str, Any] 21 Configuration dictionary. 22 """ 23 if path is None: 24 default_path = os.path.join(os.path.expanduser("~"), ".wmaee.conf.yaml") 25 if os.path.exists(default_path): 26 path = default_path 27 else: 28 env_path = os.environ.get("WMAEE_CONFIG_FILE") 29 if env_path is None or not os.path.exists(env_path): 30 raise FileNotFoundError("No config file was specified") 31 else: 32 path = env_path 33 34 with open(path) as config_handle: 35 return dict(yaml.safe_load(config_handle)) 36 37 38class Config: 39 """ 40 Configuration class that loads settings from a YAML file. 41 42 Parameters 43 ---------- 44 path : Optional[str], optional 45 Path to the config file. 46 47 Attributes 48 ---------- 49 _config : Dict[str, Any] 50 Configuration dictionary. 51 """ 52 def __init__(self, path: Optional[str] = None): 53 self._config: Dict[str, Any] = load_config(path) 54 55 def get(self, items: str) -> Any: 56 """ 57 Retrieve a value from the configuration. 58 59 Parameters 60 ---------- 61 items : str 62 Configuration item key. 63 64 Returns 65 ------- 66 Any 67 Value associated with the specified key. 68 """ 69 return self._config.get(items)
def
load_config(path: Optional[str] = None) -> Dict[str, Any]:
7def load_config(path: Optional[str] = None) -> Dict[str, Any]: 8 """ 9 Loads a YAML config file from the specified path. If the path is not specified, 10 the routine will try to load "~/.wmaee.conf.yaml" in case it exists. Otherwise, 11 it uses the value from the environment variable `WMAEE_CONFIG_FILE`. If all options fail, 12 a `FileNotFoundError` is raised. 13 14 Parameters 15 ---------- 16 path : Optional[str], optional 17 Path to the config file. 18 19 Returns 20 ------- 21 Dict[str, Any] 22 Configuration dictionary. 23 """ 24 if path is None: 25 default_path = os.path.join(os.path.expanduser("~"), ".wmaee.conf.yaml") 26 if os.path.exists(default_path): 27 path = default_path 28 else: 29 env_path = os.environ.get("WMAEE_CONFIG_FILE") 30 if env_path is None or not os.path.exists(env_path): 31 raise FileNotFoundError("No config file was specified") 32 else: 33 path = env_path 34 35 with open(path) as config_handle: 36 return dict(yaml.safe_load(config_handle))
Loads a YAML config file from the specified path. If the path is not specified,
the routine will try to load "~/.wmaee.conf.yaml" in case it exists. Otherwise,
it uses the value from the environment variable WMAEE_CONFIG_FILE. If all options fail,
a FileNotFoundError is raised.
Parameters
- path (Optional[str], optional): Path to the config file.
Returns
- Dict[str, Any]: Configuration dictionary.
class
Config:
39class Config: 40 """ 41 Configuration class that loads settings from a YAML file. 42 43 Parameters 44 ---------- 45 path : Optional[str], optional 46 Path to the config file. 47 48 Attributes 49 ---------- 50 _config : Dict[str, Any] 51 Configuration dictionary. 52 """ 53 def __init__(self, path: Optional[str] = None): 54 self._config: Dict[str, Any] = load_config(path) 55 56 def get(self, items: str) -> Any: 57 """ 58 Retrieve a value from the configuration. 59 60 Parameters 61 ---------- 62 items : str 63 Configuration item key. 64 65 Returns 66 ------- 67 Any 68 Value associated with the specified key. 69 """ 70 return self._config.get(items)
Configuration class that loads settings from a YAML file.
Parameters
- path (Optional[str], optional): Path to the config file.
Attributes
- _config (Dict[str, Any]): Configuration dictionary.
def
get(self, items: str) -> Any:
56 def get(self, items: str) -> Any: 57 """ 58 Retrieve a value from the configuration. 59 60 Parameters 61 ---------- 62 items : str 63 Configuration item key. 64 65 Returns 66 ------- 67 Any 68 Value associated with the specified key. 69 """ 70 return self._config.get(items)
Retrieve a value from the configuration.
Parameters
- items (str): Configuration item key.
Returns
- Any: Value associated with the specified key.