1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from ConfigParser import RawConfigParser as ConfigParser
24 from collections import OrderedDict
25 import gobject
26
28 '''
29 Config module to read *.ini file.
30 '''
31
32 __gsignals__ = {
33 "config-changed" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
34 (gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING))
35 }
36
37 - def __init__(self,
38 config_file,
39 default_config=None):
40 '''
41 Init config module.
42
43 @param config_file: Config filepath.
44 @param default_config: Default config value use when config file is empty.
45 '''
46 gobject.GObject.__init__(self)
47 self.config_parser = ConfigParser()
48 self.remove_option = self.config_parser.remove_option
49 self.has_option = self.config_parser.has_option
50 self.add_section = self.config_parser.add_section
51 self.getboolean = self.config_parser.getboolean
52 self.getint = self.config_parser.getint
53 self.getfloat = self.config_parser.getfloat
54 self.options = self.config_parser.options
55 self.config_file = config_file
56 self.default_config = default_config
57
58
59 self.load_default()
60
62 '''
63 Load config items with default setting.
64 '''
65
66 if isinstance(self.default_config, list):
67 self.default_config = self.convert_from_list(self.default_config)
68
69 if self.default_config:
70 for section, items in self.default_config.iteritems():
71 self.add_section(section)
72 for key, value in items.iteritems():
73 self.config_parser.set(section, key, value)
74
76 '''
77 Load config items from the file.
78 '''
79 self.config_parser.read(self.config_file)
80
81 - def get(self, section, option, default=None):
82 '''
83 Get specified the section for read the option value.
84
85 @param section: Section to index item.
86 @param option: Option to index item.
87 @param default: Default value if item is not exist.
88 @return: Return item value with match in config file.
89 '''
90 try:
91 return self.config_parser.get(section, option)
92 except Exception, e:
93 print "config.get error: %s" % (e)
94 return default
95
96 - def set(self, section, option, value):
97 '''
98 Set item given value.
99
100 @param section: Section to setting.
101 @param option: Option to setting.
102 @param value: Item value to save.
103 '''
104 if not self.config_parser.has_section(section):
105 print "Section \"%s\" not exist. create..." % (section)
106 self.add_section(section)
107
108 self.config_parser.set(section, option, value)
109 self.emit("config-changed", section, option, value)
110
111 - def write(self, given_filepath=None):
112 '''
113 Save configure to file.
114
115 @param given_filepath: If given_filepath is None, save to default filepath, otherwise save to given filepath.
116 '''
117 if given_filepath:
118 f = file(given_filepath, "w")
119 else:
120 f = file(self.config_file, "w")
121 self.config_parser.write(f)
122 f.close()
123
125 '''
126 Get default config value.
127
128 @return: Return default config value.
129 '''
130 return self.default_config
131
133 '''
134 Set default config value and load it.
135
136 @param default_config: Default config value.
137 '''
138 self.default_config = default_config
139 self.load_default()
140
142 '''
143 Convert to dict from list format.
144
145 @param config_list: Config value as List format.
146 @return: Return config value as Dict format.
147 '''
148 config_dict = OrderedDict()
149 for (section, option_list) in config_list:
150 option_dict = OrderedDict()
151 for (option, value) in option_list:
152 option_dict[option] = value
153 config_dict[section] = option_dict
154
155 return config_dict
156
157 gobject.type_register(Config)
158