config.py 1.26 KB
Newer Older
Sugon_ldc's avatar
Sugon_ldc committed
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
43
44
45
46
47
48
import yaml
import os.path as osp
import os

from eiseg import pjpath


def parse_configs(path):
    if not path or not osp.exists(path):
        return
    with open(path, "r", encoding="utf-8") as f:
        return yaml.load(f.read(), Loader=yaml.FullLoader)


def save_configs(path=None, config=None, actions=None):
    if not path:
        path = osp.join(pjpath, "config/config.yaml")
    if not osp.exists(path):
        # os.makedirs(osp.basename(path))
        # windows无法使用mknod
        f = open(path, "w+")
        f.close()
    if not config:
        config = {}
    if actions:
        config["shortcut"] = {}
        for action in actions:
            config["shortcut"][action.data()] = action.shortcut().toString()
    with open(path, "w", encoding="utf-8") as f:
        yaml.dump(config, f)


class cfgData(object):
    def __init__(self, yaml_file):
        with open(yaml_file, "r", encoding="utf-8") as f:
            fig_data = f.read()
            self.dicts = yaml.load(fig_data)

    def get(self, key):
        if key in self.dicts.keys():
            return self.dicts[key]
        else:
            raise ValueError("Not find this keyword.")


if __name__ == "__main__":
    cfg = cfgData("EISeg/train/train_config.yaml")
    print(cfg.get("use_vdl"))