Coverage for bbconf/helpers.py: 84%
19 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 22:05 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 22:05 +0000
1import logging
2import os
4from yacman import select_config
6from bbconf.exceptions import BedBaseConnectionError
8_LOGGER = logging.getLogger(__name__)
11CFG_ENV_VARS = ["BEDBASE"]
14def get_bedbase_cfg(cfg: str = None) -> str:
15 """
16 Determine path to the bedbase configuration file.
18 The path can be either explicitly provided
19 or read from a $BEDBASE environment variable.
21 Args:
22 cfg: Path to the config file.
23 Optional, the $BEDBASE config env var will be used if not provided.
25 Returns:
26 Absolute configuration file path.
27 """
29 _LOGGER.info(f"Loading configuration file: {cfg}")
30 try:
31 selected_cfg = select_config(
32 config_filepath=cfg,
33 config_env_vars=CFG_ENV_VARS,
34 )
35 except OSError:
36 selected_cfg = None
38 if not selected_cfg:
39 raise BedBaseConnectionError(
40 f"You must provide a config file or set the "
41 f"{'or '.join(CFG_ENV_VARS)} environment variable"
42 )
43 return selected_cfg
46def get_absolute_path(path: str, base_path: str) -> str:
47 """
48 Get absolute path to the file.
50 Args:
51 path: Path to the file (abs or relative).
52 base_path: Base path to the file (will be added to the relative path).
54 Returns:
55 Absolute path to the file.
56 """
57 if not os.path.isabs(path) or not os.path.exists(path):
58 return os.path.join(base_path, path)
59 return path