From 2092245dadfeff8585a2b938b9bc407d4b5d0fca Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sat, 23 Oct 2021 21:07:07 +0100 Subject: [PATCH] adding skip header directory option --- panaetius/config.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/panaetius/config.py b/panaetius/config.py index ab14ae2..0c64d92 100644 --- a/panaetius/config.py +++ b/panaetius/config.py @@ -21,14 +21,24 @@ from panaetius.exceptions import KeyErrorTooDeepException, InvalidPythonExceptio class Config: """The configuration class to access variables.""" - def __init__(self, header_variable: str, config_path: str = "") -> None: + def __init__( + self, + header_variable: str, + config_path: str | None = None, + skip_header_init: bool = False, + ) -> None: """ Create a Config object to set and access variables. Args: header_variable (str): Your header variable name. config_path (str, optional): The path where the header directory is stored. - Defaults to `~/.config`. + Defaults to None on initialisation. + skip_header_init (bool, optional): If True will not use a header + subdirectory in the `config_path`. Defaults to False. + + Examples: + `config_path` defaults to None on initialisation but will be set to `~/.config`. Example: A header of `data_analysis` with a config_path of `~/myapps` will define @@ -37,9 +47,10 @@ class Config: self.header_variable = header_variable self.config_path = ( pathlib.Path(config_path).expanduser() - if config_path + if config_path is not None else pathlib.Path.home() / ".config" ) + self.skip_header_init = skip_header_init self._missing_config = self._check_config_file_exists() # default logging options @@ -55,7 +66,12 @@ class Config: Returns: dict: The contents of the `.yml` loaded as a python dictionary. """ - config_file_location = self.config_path / self.header_variable / "config.yml" + if self.skip_header_init: + config_file_location = self.config_path / "config.yml" + else: + config_file_location = ( + self.config_path / self.header_variable / "config.yml" + ) try: with open(config_file_location, "r", encoding="utf-8") as config_file: # return dict(toml.load(config_file)) @@ -103,7 +119,10 @@ class Config: return self._get_env_value(env_key, default) def _check_config_file_exists(self) -> bool: - config_file_location = self.config_path / self.header_variable / "config.yml" + if self.skip_header_init is False: + config_file_location = self.config_path / self.header_variable / "config.yml" + else: + config_file_location = self.config_path / "config.yml" try: with open(config_file_location, "r", encoding="utf-8"): return False