Left: | ||
Right: |
LEFT | RIGHT |
---|---|
(no file at all) | |
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 is_addition = option.endswith('+') | 86 is_addition = option.endswith('+') |
87 is_diff = is_addition or option.endswith('-') | 87 is_diff = is_addition or option.endswith('-') |
88 | 88 |
89 if is_diff: | 89 if is_diff: |
90 option = option[:-1].rstrip() | 90 option = option[:-1].rstrip() |
91 try: | 91 try: |
92 orig_value = self.get(section, option) | 92 orig_value = self.get(section, option) |
93 except ConfigParser.NoOptionError: | 93 except ConfigParser.NoOptionError: |
94 raise DiffForUnknownOptionError(option, section) | 94 raise DiffForUnknownOptionError(option, section) |
95 | 95 |
96 orig_values = orig_value.split() | 96 orig_values = orig_value.splitlines() |
97 diff_values = value.split() | 97 diff_values = value.splitlines() |
98 | 98 |
99 if is_addition: | 99 if is_addition: |
100 new_values = orig_values + [v for v in diff_values if v not in o rig_values] | 100 new_values = orig_values + [v for v in diff_values if v not in o rig_values] |
101 else: | 101 else: |
102 new_values = [v for v in orig_values if v not in diff_values] | 102 new_values = [v for v in orig_values if v not in diff_values] |
103 | 103 |
104 value = ' '.join(new_values) | 104 value = '\n'.join(new_values) |
105 | 105 |
106 return is_diff, option, value | 106 return is_diff, option, value |
107 | 107 |
108 def _process_parsers(self, parsers): | 108 def _process_parsers(self, parsers): |
109 for parser, filename in parsers: | 109 for parser, filename in parsers: |
110 for section in parser.sections(): | 110 for section in parser.sections(): |
111 if not self.has_section(section): | 111 if not self.has_section(section): |
112 try: | 112 try: |
113 ConfigParser.SafeConfigParser.add_section(self, section) | 113 ConfigParser.SafeConfigParser.add_section(self, section) |
114 except ValueError: | 114 except ValueError: |
(...skipping 35 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 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 """Serialize a given section as a dictionary into `base`. | |
162 | |
163 Parse arbitrary key/value pairs from 'section' of the current | |
164 configuration into a dictionary and deep merge it into `base`. | |
165 | |
166 The following rules need to be considered: | |
167 | |
168 * An option's key may be declared as a series of nested dictionary keys, | |
169 seperated by '.'. | |
170 * Declaring an option's value in a new line (even if only one is given) | |
171 will define the option's value as a list. | |
172 * When an option's value is defined as a list, no other nested | |
173 objects may follow. | |
174 * A list is expandable by the ConfigParser's '+=' token (Note: A | |
175 previously declared string will be converted into a list). | |
176 * Values may be marked as `number` or `bool` by prefixing them | |
177 accordingly (this also applies to values in a list): | |
178 * bool:<value> | |
179 * number:<value> | |
180 | |
181 Example: | |
182 { | |
183 foo = foo "foo": "foo", | |
184 asd = "asd": ["asd"], | |
185 asd "bar": { | |
186 bar.baz = a "baz": ["a", "c", "d"] | |
187 baz.foo = a }, | |
188 baz.z = "baz": { | |
189 bar "foo": "a", | |
190 bool:true ===> "z": ["bar", true] | |
191 bar.baz += }, | |
192 c "bad": true, | |
193 d "good": false, | |
194 bad = bool:true "is": { | |
195 good = bool:false "integer": 1, | |
196 is.integer = number:1 "float": 1.4 | |
197 is.float = number:1.4 } | |
198 } | |
199 """ | |
200 def parse_value(v): | |
201 if v.startswith('number:'): | |
202 v = v.split(':', 1)[1] | |
203 try: | |
204 return int(v) | |
205 except ValueError: | |
206 return float(v) | |
207 if v == 'bool:true': | |
208 return True | |
209 if v == 'bool:false': | |
210 return False | |
211 return v | |
212 | |
213 if self.has_section(section): | |
214 for k, v in self.items(section): | |
215 parents = k.split('.') | |
216 tail = parents.pop() | |
217 current = base | |
218 for name in parents: | |
219 current = base.setdefault(name, {}) | |
220 | |
221 if '\n' in v: | |
222 current[tail] = [parse_value(x) for x in v.splitlines() if x ] | |
223 else: | |
224 current[tail] = parse_value(v) | |
225 | |
160 def readfp(self, fp, filename=None): | 226 def readfp(self, fp, filename=None): |
161 raise NotImplementedError | 227 raise NotImplementedError |
162 | 228 |
163 def set(self, section, option, value=None): | 229 def set(self, section, option, value=None): |
164 raise NotImplementedError | 230 raise NotImplementedError |
165 | 231 |
166 def add_section(self, section): | 232 def add_section(self, section): |
167 raise NotImplementedError | 233 raise NotImplementedError |
168 | 234 |
169 def remove_option(self, section, option): | 235 def remove_option(self, section, option): |
170 raise NotImplementedError | 236 raise NotImplementedError |
171 | 237 |
172 def remove_section(self, section): | 238 def remove_section(self, section): |
173 raise NotImplementedError | 239 raise NotImplementedError |
LEFT | RIGHT |