Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # This Source Code Form is subject to the terms of the Mozilla Public | 1 # This Source Code Form is subject to the terms of the Mozilla Public |
2 # License, v. 2.0. If a copy of the MPL was not distributed with this | 2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 import os | 5 import os |
6 import io | 6 import io |
7 import ConfigParser | 7 import ConfigParser |
8 from StringIO import StringIO | 8 from StringIO import StringIO |
9 | 9 |
10 | 10 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 | 150 |
151 def option_source(self, section, option): | 151 def option_source(self, section, option): |
152 option = self.optionxform(option) | 152 option = self.optionxform(option) |
153 try: | 153 try: |
154 return self._origin[(section, option)] | 154 return self._origin[(section, option)] |
155 except KeyError: | 155 except KeyError: |
156 if not self.has_section(section): | 156 if not self.has_section(section): |
157 raise ConfigParser.NoSectionError(section) | 157 raise ConfigParser.NoSectionError(section) |
158 raise ConfigParser.NoOptionError(option, section) | 158 raise ConfigParser.NoOptionError(option, section) |
159 | 159 |
160 def section_as_dict(self, section, base): | 160 def serialize_section_if_present(self, section, base): |
Vasily Kuznetsov
2018/04/19 14:40:10
What this method does seems more like parsing than
Sebastian Noack
2018/04/19 15:02:59
In the end it gets serialized (as JSON). But you m
| |
161 """Parse a given section into a dictionary. | 161 """Serialize a given section as a dictionary into `base`. |
162 | 162 |
163 Parse arbitrary key/value pairs from 'section' of the current | 163 Parse arbitrary key/value pairs from 'section' of the current |
164 configuration into a dictionary and deep merge it into `base`. | 164 configuration into a dictionary and deep merge it into `base`. |
165 | 165 |
166 The following rules need to be considered: | 166 The following rules need to be considered: |
167 | 167 |
168 * An option's key may be declared as a series of nested dictionary keys, | 168 * An option's key may be declared as a series of nested dictionary keys, |
169 seperated by '.'. | 169 seperated by '.'. |
170 * Declaring an option's value in a new line (even if only one is given) | 170 * Declaring an option's value in a new line (even if only one is given) |
171 will define the option's value as an array. | 171 will define the option's value as a list. |
Sebastian Noack
2018/04/19 10:50:56
Nit: Talking about "arrays" made sense initially w
tlucas
2018/04/19 11:08:13
Done.
| |
172 * When an option's value is defined as an array, no other nested | 172 * When an option's value is defined as a list, no other nested |
173 objects may follow. | 173 objects may follow. |
174 * An array is expandable by the ConfigParser's '+=' token (Note: A | 174 * A list is expandable by the ConfigParser's '+=' token (Note: A |
175 previously declared string will be converted into an array). | 175 previously declared string will be converted into a list). |
176 * Values may be marked as `number` or `bool` by prefixing them | 176 * Values may be marked as `number` or `bool` by prefixing them |
177 accordingly (this also applies to values in an array): | 177 accordingly (this also applies to values in a list): |
178 * bool:<value> | 178 * bool:<value> |
179 * number:<value> | 179 * number:<value> |
180 | 180 |
181 Example: | 181 Example: |
182 { | 182 { |
183 foo = foo "foo": "foo", | 183 foo = foo "foo": "foo", |
184 asd = "asd": ["asd"], | 184 asd = "asd": ["asd"], |
185 asd "bar": { | 185 asd "bar": { |
Sebastian Noack
2018/04/19 10:50:57
Nit: For consistency, we might want to use two spa
tlucas
2018/04/19 11:08:14
Done.
| |
186 bar.baz = a "baz": ["a", "c", "d"] | 186 bar.baz = a "baz": ["a", "c", "d"] |
187 baz.foo = a }, | 187 baz.foo = a }, |
188 baz.z = "baz": { | 188 baz.z = "baz": { |
189 bar "foo": "a", | 189 bar "foo": "a", |
190 bool:true ===> "z": ["bar", true] | 190 bool:true ===> "z": ["bar", true] |
191 bar.baz += }, | 191 bar.baz += }, |
192 c "bad": true, | 192 c "bad": true, |
193 d "good": false, | 193 d "good": false, |
194 bad = bool:true "is": { | 194 bad = bool:true "is": { |
195 good = bool:false "integer": 1, | 195 good = bool:false "integer": 1, |
196 is.integer = number:1 "float": 1.4 | 196 is.integer = number:1 "float": 1.4 |
197 is.float = number:1.4 } | 197 is.float = number:1.4 } |
198 } | 198 } |
199 """ | 199 """ |
200 def parse_values(v): | 200 def parse_value(v): |
Sebastian Noack
2018/04/19 10:50:57
Nit; since this function is only called with a sin
tlucas
2018/04/19 11:08:13
Done.
| |
201 if isinstance(v, list): | |
202 return [parse_values(x) for x in v] | |
Sebastian Noack
2018/04/19 10:50:57
This code path can be removed now, since we no lon
tlucas
2018/04/19 11:08:14
Done.
| |
203 | |
204 if v.startswith('number:'): | 201 if v.startswith('number:'): |
205 v = v.split(':', 1)[1] | 202 v = v.split(':', 1)[1] |
206 try: | 203 try: |
207 return int(v) | 204 return int(v) |
208 except ValueError: | 205 except ValueError: |
209 return float(v) | 206 return float(v) |
210 if v == 'bool:true': | 207 if v == 'bool:true': |
211 return True | 208 return True |
212 if v == 'bool:false': | 209 if v == 'bool:false': |
213 return False | 210 return False |
214 return v | 211 return v |
215 | 212 |
216 data = self.items(section) | 213 if self.has_section(section): |
Sebastian Noack
2018/04/19 10:50:56
Nit: this variable is only used once, perhaps inli
tlucas
2018/04/19 11:08:14
Done.
One note: Not catching a NoSectionError her
tlucas
2018/04/19 11:45:15
Done.
| |
217 | 214 for k, v in self.items(section): |
218 for k, v in data: | 215 parents = k.split('.') |
219 | 216 tail = parents.pop() |
Sebastian Noack
2018/04/19 10:50:58
Nit: Is the blank line here intended? It looks wei
tlucas
2018/04/19 11:08:15
Done.
| |
220 parents = k.split('.') | 217 current = base |
221 tail = parents.pop() | 218 for name in parents: |
222 current = base | 219 current = base.setdefault(name, {}) |
223 for name in parents: | 220 |
224 current = base.setdefault(name, {}) | 221 if '\n' in v: |
225 | 222 current[tail] = [parse_value(x) for x in v.splitlines() if x ] |
226 if '\n' in v: | 223 else: |
227 current[tail] = [parse_values(x) for x in v.splitlines() if x] | 224 current[tail] = parse_value(v) |
228 else: | |
229 current[tail] = parse_values(v) | |
230 | |
231 return base | |
Sebastian Noack
2018/04/19 10:50:57
Nit: Returning the dictionary seems redundant. It'
tlucas
2018/04/19 11:08:14
I wouldn't mind either solution - but we also migh
Sebastian Noack
2018/04/19 11:12:41
That is exactly what I said. My suggestions above
tlucas
2018/04/19 11:45:15
Done.
| |
232 | 225 |
233 def readfp(self, fp, filename=None): | 226 def readfp(self, fp, filename=None): |
234 raise NotImplementedError | 227 raise NotImplementedError |
235 | 228 |
236 def set(self, section, option, value=None): | 229 def set(self, section, option, value=None): |
237 raise NotImplementedError | 230 raise NotImplementedError |
238 | 231 |
239 def add_section(self, section): | 232 def add_section(self, section): |
240 raise NotImplementedError | 233 raise NotImplementedError |
241 | 234 |
242 def remove_option(self, section, option): | 235 def remove_option(self, section, option): |
243 raise NotImplementedError | 236 raise NotImplementedError |
244 | 237 |
245 def remove_section(self, section): | 238 def remove_section(self, section): |
246 raise NotImplementedError | 239 raise NotImplementedError |
LEFT | RIGHT |