File size: 871 Bytes
d16b52d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import os.path as osp

from omegaconf import OmegaConf


config_suffix = [".yaml"]


def load_config(config: str) -> OmegaConf:
    config = OmegaConf.load(config)
    base_config = config.pop("base", None)

    if base_config:
        config = OmegaConf.merge(OmegaConf.load(base_config), config)

    return config


def dump_config(config: OmegaConf, save_path: str = None):
    from omegaconf import Container

    if isinstance(config, Container):
        if not save_path.endswith(".yaml"):
            save_dir = save_path
            save_path = osp.join(save_dir, "config.yaml")
        else:
            save_dir = osp.basename(config)
        os.makedirs(save_dir, exist_ok=True)
        OmegaConf.save(config, save_path)

    else:
        raise TypeError("Only support saving `Config` from `OmegaConf`.")

    print(f"Dump Config to {save_path}.")