From 948be5efb536c626778787e60f67b36d892b04ec Mon Sep 17 00:00:00 2001 From: Mose Mueller Date: Wed, 2 Aug 2023 10:14:58 +0200 Subject: [PATCH] docs: updating documentation of config.py --- icon_service_base/database/config.py | 38 +++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/icon_service_base/database/config.py b/icon_service_base/database/config.py index c675444..b9b3623 100644 --- a/icon_service_base/database/config.py +++ b/icon_service_base/database/config.py @@ -5,8 +5,22 @@ from confz import BaseConfig, EnvSource, FileSource from loguru import logger from pydantic import AnyUrl, SecretStr +# Retrieve the name of the current package +package_name = Path(__file__).resolve().parent.parent.name + def find_dir_upwards(start_dir: Path, targets: list[str]) -> Path | None: + """ + Traverse directory path upwards until finding a directory named as one of the + targets. + + Args: + start_dir: The starting point directory path. + targets: List of target directory names to be searched for. + + Returns: + The directory path if found, None otherwise. + """ for parent in start_dir.parents: for target in targets: if (parent / target).is_dir(): @@ -14,16 +28,28 @@ def find_dir_upwards(start_dir: Path, targets: list[str]) -> Path | None: return None -# we expect the database_config directory in the root directory of any module installing -# this package. +# Look for ".venv" or "venv" directories starting from the current file's directory. VENV_DIR = find_dir_upwards(Path(__file__).resolve(), [".venv", "venv"]) +# Look for "deps" directory starting from the current file's directory. +DEPS_DIR = find_dir_upwards(Path(__file__).resolve(), ["deps"]) + CONFIG_DIR: Optional[Path] = None -if VENV_DIR is not None: + +# If a ".venv" or "venv" directory was found and its parent's name is not the current +# package name, check for the "database_config" directory inside the parent directory of +# the found directory. +if VENV_DIR is not None and VENV_DIR.parent.name != package_name: + logger.debug(VENV_DIR.parent.name) CONFIG_DIR = VENV_DIR.parent / "database_config" - if not VENV_DIR.exists(): + if not CONFIG_DIR.exists(): + CONFIG_DIR = None +# If no ".venv" or "venv" directory was found or its parent's name is the current +# package name, but a "deps" directory was found, check for the "database_config" +# directory inside the parent directory of the found "deps" directory. +elif DEPS_DIR is not None: + CONFIG_DIR = DEPS_DIR.parent / "database_config" + if not CONFIG_DIR.exists(): CONFIG_DIR = None - else: - logger.debug(CONFIG_DIR) class OperationMode(BaseConfig): # type: ignore