Coverage for mlos_bench/mlos_bench/tests/config/__init__.py: 100%
18 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-06 00:35 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-06 00:35 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Helper functions for config example loading tests.
7"""
9from typing import Callable, List, Optional
11import os
12import sys
14from mlos_bench.util import path_join
16if sys.version_info < (3, 10):
17 from importlib_resources import files
18else:
19 from importlib.resources import files
22BUILTIN_TEST_CONFIG_PATH = str(files("mlos_bench.tests.config").joinpath("")).replace("\\", "/")
25def locate_config_examples(root_dir: str,
26 config_examples_dir: str,
27 examples_filter: Optional[Callable[[List[str]], List[str]]] = None) -> List[str]:
28 """Locates all config examples in the given directory.
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.
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)