import logging
import os
from typing import List, Tuple, Optional

from amlb.utils import config_load, Namespace

log = logging.getLogger(__name__)


def _find_local_benchmark_definition(name: str, benchmark_definition_dirs: List[str]) -> str:
    # 'name' should be either a full path to the benchmark,
    # or a filename (without extension) in the benchmark directory.
    if os.path.exists(name):
        return name

    for bd in benchmark_definition_dirs:
        bf = os.path.join(bd, f"{name}.yaml")
        if os.path.exists(bf):
            # We don't account for duplicate definitions (yet).
            return bf

    # should we support s3 and check for s3 path before raising error?
    raise ValueError(f"Incorrect benchmark name or path `{name}`, name not available in {benchmark_definition_dirs}.")


def load_file_benchmark(name: str, benchmark_definition_dirs: List[str]) -> Tuple[str, Optional[str], List[Namespace]]:
    """ Loads benchmark from a local file. """
    benchmark_file = _find_local_benchmark_definition(name, benchmark_definition_dirs)
    log.info("Loading benchmark definitions from %s.", benchmark_file)
    tasks = config_load(benchmark_file)
    benchmark_name, _ = os.path.splitext(os.path.basename(benchmark_file))
    return benchmark_name, benchmark_file, tasks

[((7, 6, 7, 33), 'logging.getLogger', 'logging.getLogger', ({(7, 24, 7, 32): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((13, 7, 13, 27), 'os.path.exists', 'os.path.exists', ({(13, 22, 13, 26): 'name'}, {}), '(name)', False, 'import os\n'), ((30, 12, 30, 39), 'amlb.utils.config_load', 'config_load', ({(30, 24, 30, 38): 'benchmark_file'}, {}), '(benchmark_file)', False, 'from amlb.utils import config_load, Namespace\n'), ((17, 13, 17, 45), 'os.path.join', 'os.path.join', ({(17, 26, 17, 28): 'bd', (17, 30, 17, 44): 'f"""{name}.yaml"""'}, {}), "(bd, f'{name}.yaml')", False, 'import os\n'), ((18, 11, 18, 29), 'os.path.exists', 'os.path.exists', ({(18, 26, 18, 28): 'bf'}, {}), '(bf)', False, 'import os\n'), ((31, 41, 31, 73), 'os.path.basename', 'os.path.basename', ({(31, 58, 31, 72): 'benchmark_file'}, {}), '(benchmark_file)', False, 'import os\n')]