File size: 4,067 Bytes
c5db72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
from collections import OrderedDict
from configparser import ConfigParser

class Config:
    def __init__(self, main_conf_path):
        self.main_conf_path = main_conf_path        
        self.main_config = self.read_config(os.path.join(main_conf_path, 'main_config.cfg'))

    def read_config(self, conf_path):
        conf_dict = OrderedDict() 

        config = ConfigParser()
        config.read(conf_path)
        for section in config.sections():
            section_config = OrderedDict(config[section].items())
            conf_dict[section] = self.type_ensurance(section_config)
            self.__dict__.update((k, v) for k, v in conf_dict[section].items())

        return conf_dict


    def ensure_value_type(self, v):
        BOOLEAN = {'false': False, 'False': False,
                   'true': True, 'True': True}
        if isinstance(v, str):
            try:
                value = eval(v)
                if not isinstance(value, (str, int, float, list, tuple)):
                    value = v
            except:
                if v in BOOLEAN:
                    v = BOOLEAN[v]
                value = v
        else:
            value = v
        return value

    def type_ensurance(self, config):
        BOOLEAN = {'false': False, 'False': False,
                   'true': True, 'True': True}

        for k, v in config.items():
            try:
                value = eval(v)
                if not isinstance(value, (str, int, float, list, tuple)):
                    value = v
            except:
                if v in BOOLEAN:
                    v = BOOLEAN[v]
                value = v
            config[k] = value
        return config

    def get_param(self, section, param):
        if section in self.main_config:
            section = self.main_config[section]
        else:
            raise NameError("There are not the parameter named '%s'" % section)

        if param in section:
            value = section[param]
        else:
            raise NameError("There are not the parameter named '%s'" % param)

        return value

    def update_params(self, params):
        # for now, assume 'params' is dictionary

        for k, v in params.items():
            updated=False
            for section in self.main_config:
                if k in self.main_config[section]:
                    self.main_config[section][k] = self.ensure_value_type(v)
                    self.__dict__[k] = self.main_config[section][k]
                    updated = True

                    break

            if not updated:
                # raise ValueError
                print('Parameter not updated. \'%s\' not exists.' % k)


    def save(self, base_dir):
        def helper(section_k, section_v):
            sec_str = '[%s]\n' % section_k
            for k, v in section_v.items():
                sec_str += '%s=%s\n' % (str(k), str(v))
            sec_str += '\n'
            return sec_str
        
        # save main config
        main_conf_str =''
        for section in self.main_config:
            main_conf_str += helper(section, self.main_config[section])
        with open(os.path.join(base_dir, 'main_config.cfg'), 'wt') as f:
            f.write(main_conf_str)

        
        print('main config saved in %s' % base_dir)

    def __getitem__(self, item):
        if not isinstance(item, str):
            raise TypeError("index must be a str")

        if item in self.main_config:
            section = self.main_config[item]
        else:
            raise NameError("There are not the parameter named '%s'" % item)
        return section

    def __str__(self):
        config_str = '\n'

        config_str += '>>>>> Main Config\n'
        for section in self.main_config:
            config_str += '[%s]\n' % section
            config_str += '\n'.join(['{}: {}'.format(k, self.main_config[section][k]) for k in self.main_config[section]])
            config_str += '\n\n'

        return config_str

if __name__ == '__main__':
    param = Config('../main_config.cfg')

    print(param)