file_handler.py 1.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Utilities for file."""

import yaml
from pathlib import Path
from datetime import datetime


def new_output_dir():
    """Generate a new output directory.

    Generate a new output directory name based on current time and create it on filesystem.

    Returns:
        str: Output directory name.
    """
    output_name = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    output_path = Path('.', 'outputs', output_name)
    output_path.mkdir(mode=0o755, parents=True, exist_ok=True)
    return str(output_path)


def get_config(config_file):
    """Read SuperBench config yaml.

    Read config file, use default config if None is provided.

    Args:
        config_file (str): config file path.

    Returns:
        dict: Config object, None if file does not exist.
    """
    here = Path(__file__).parent.resolve()
    p = Path(config_file) if config_file else here / '../../config/default.yaml'
    if not p.is_file():
        return None
    with p.open() as f:
        config = yaml.safe_load(f)
    return config