Coverage for mlos_bench/mlos_bench/tests/config/__init__.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 01:52 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Helper functions for config example loading tests.""" 

6 

7import os 

8import sys 

9from typing import Callable, List, Optional 

10 

11from mlos_bench.util import path_join 

12 

13if sys.version_info < (3, 10): 

14 from importlib_resources import files 

15else: 

16 from importlib.resources import files 

17 

18 

19BUILTIN_TEST_CONFIG_PATH = str(files("mlos_bench.tests.config").joinpath("")).replace("\\", "/") 

20 

21 

22def locate_config_examples( 

23 root_dir: str, 

24 config_examples_dir: str, 

25 examples_filter: Optional[Callable[[List[str]], List[str]]] = None, 

26) -> List[str]: 

27 """ 

28 Locates all config examples in the given directory. 

29 

30 Parameters 

31 ---------- 

32 root_dir : str 

33 Root dir of the config_examples_dir. 

34 config_examples_dir: str 

35 Name to the directory containing config examples. 

36 examples_filter : callable 

37 Optional filter to provide on the returned results. 

38 

39 Returns 

40 ------- 

41 config_examples: List[str] 

42 List of paths to config examples. 

43 """ 

44 if examples_filter is None: 

45 examples_filter = list 

46 config_examples_path = path_join(root_dir, config_examples_dir) 

47 assert os.path.isdir(config_examples_path) 

48 config_examples = [] 

49 for root, _, dir_files in os.walk(config_examples_path): 

50 for file in dir_files: 

51 if file.endswith(".json") or file.endswith(".jsonc"): 

52 config_examples.append(path_join(root, file)) 

53 return examples_filter(config_examples)