File size: 1,292 Bytes
6498fe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import yaml
import os

def load_config(config_file):
    with open(config_file, encoding='utf-8') as f:
        config = yaml.safe_load(f)

    return config


class Cfg(dict):
    def __init__(self, config_dict):
        super(Cfg, self).__init__(**config_dict)
        self.__dict__ = self

    @staticmethod
    def load_config_from_file(fname, base_file='Vocr/config/base.yml'):
        # --- FIX ĐƯỜNG DẪN TUYỆT ĐỐI ---
        # Lấy đường dẫn thư mục chứa file config.py hiện tại (Vocr/tool)
        current_dir = os.path.dirname(os.path.abspath(__file__))
        # Lùi lại 1 cấp để ra thư mục Vocr, sau đó vào thư mục config tìm base.yml
        vocr_dir = os.path.dirname(current_dir)
        absolute_base_file = os.path.join(vocr_dir, 'config', 'base.yml')
        # -------------------------------

        # Nạp base_config bằng đường dẫn tuyệt đối
        base_config = load_config(absolute_base_file)

        with open(fname, encoding='utf-8') as f:
            config = yaml.safe_load(f)
        base_config.update(config)

        return Cfg(base_config)

    def save(self, fname):
        with open(fname, 'w') as outfile:
            yaml.dump(dict(self), outfile, default_flow_style=False, allow_unicode=True)