Coverage for bbconf/helpers.py: 84%

19 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-16 22:05 +0000

1import logging 

2import os 

3 

4from yacman import select_config 

5 

6from bbconf.exceptions import BedBaseConnectionError 

7 

8_LOGGER = logging.getLogger(__name__) 

9 

10 

11CFG_ENV_VARS = ["BEDBASE"] 

12 

13 

14def get_bedbase_cfg(cfg: str = None) -> str: 

15 """ 

16 Determine path to the bedbase configuration file. 

17 

18 The path can be either explicitly provided 

19 or read from a $BEDBASE environment variable. 

20 

21 Args: 

22 cfg: Path to the config file. 

23 Optional, the $BEDBASE config env var will be used if not provided. 

24 

25 Returns: 

26 Absolute configuration file path. 

27 """ 

28 

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 

37 

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 

44 

45 

46def get_absolute_path(path: str, base_path: str) -> str: 

47 """ 

48 Get absolute path to the file. 

49 

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). 

53 

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