Coverage for bbconf/helpers.py: 53%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-17 04:01 +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 :param str cfg: path to the config file. 

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

23 :return str: absolute configuration file path 

24 """ 

25 selected_cfg = select_config(config_filepath=cfg, config_env_vars=CFG_ENV_VARS) 

26 if not selected_cfg: 

27 raise BedBaseConnectionError( 

28 f"You must provide a config file or set the " 

29 f"{'or '.join(CFG_ENV_VARS)} environment variable" 

30 ) 

31 return selected_cfg 

32 

33 

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

35 """ 

36 Get absolute path to the file and create it if it doesn't exist 

37 

38 :param path: path to the file (abs or relative) 

39 :param base_path: base path to the file (will be added to the relative path) 

40 

41 :return: absolute path to the file 

42 """ 

43 if not os.path.isabs(path) or not os.path.exists(path): 

44 return os.path.join(base_path, path) 

45 return path