File size: 1,245 Bytes
970a7a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 json
import os

class AdvancedConfig:

    def save(self, file):
        os.makedirs(os.path.split(file)[0], exist_ok=True)
        json.dump(self.config, open(file, 'w'), indent=4)

    def read_cfg(self, file):
        # Its a json file with comments
        new_lines = []
        for line in open(file).readlines():
            if line.find("#")!=-1:
                new_lines.append(line[:line.find("#")])
            else:
                new_lines.append(line)
        return json.loads('\n'.join(new_lines))
        

    def merge_config(self, cfg_dict, base_dict):
        for key in cfg_dict:  
            if key not in base_dict:
                # Strange, raise an error
                raise Exception(f'Key {key} not found in base config')
            if isinstance(cfg_dict[key], dict):
                base_dict[key] = self.merge_config(cfg_dict[key], base_dict[key])
            else:
                base_dict[key] = cfg_dict[key]
        return base_dict

    def __init__(self, file, base_file = 'configs/default.cfg') -> None:
        self.default_config = self.read_cfg(base_file)
        self.new_config = self.read_cfg(file)
        self.config = self.merge_config(self.new_config, self.default_config)