| OLD | NEW |
| 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 |
| 11 class Item(tuple): | 11 class Item(tuple): |
| 12 def __new__(cls, name, value, source): | 12 def __new__(cls, name, value, source): |
| 13 result = super(Item, cls).__new__(cls, (name, value)) | 13 result = super(Item, cls).__new__(cls, (name, value)) |
| 14 result.source = source | 14 result.source = source |
| 15 return result | 15 return result |
| 16 | 16 |
| 17 | 17 |
| 18 class DiffForUnknownOptionError(ConfigParser.Error): | 18 class DiffForUnknownOptionError(ConfigParser.Error): |
| 19 def __init__(self, option, section): | 19 def __init__(self, option, section): |
| 20 ConfigParser.Error.__init__(self, 'Failed to apply diff for unknown opti
on ' | 20 ConfigParser.Error.__init__(self, 'Failed to apply diff for unknown opti
on ' |
| 21 '%r in section %r' % (option, section)
) | 21 '%r in section %r' % (option, section)
) |
| 22 self.option = option | 22 self.option = option |
| 23 self.section = section | 23 self.section = section |
| 24 self.args = (option, section) | 24 self.args = (option, section) |
| 25 | 25 |
| 26 | 26 |
| 27 class ChainedConfigParser(ConfigParser.SafeConfigParser): | 27 class ChainedConfigParser(ConfigParser.SafeConfigParser): |
| 28 ''' | 28 """ |
| 29 This class provides essentially the same interfaces as SafeConfigParser bu
t | 29 This class provides essentially the same interfaces as SafeConfigParser bu
t |
| 30 allows chaining configuration files so that one config file provides the | 30 allows chaining configuration files so that one config file provides the |
| 31 default values for the other. To specify the config file to inherit from | 31 default values for the other. To specify the config file to inherit from |
| 32 a config file needs to contain the following option: | 32 a config file needs to contain the following option: |
| 33 | 33 |
| 34 [default] | 34 [default] |
| 35 inherit = foo/bar.config | 35 inherit = foo/bar.config |
| 36 | 36 |
| 37 It is also possible to add values to or remove values from | 37 It is also possible to add values to or remove values from |
| 38 whitespace-separated lists given by an inherited option: | 38 whitespace-separated lists given by an inherited option: |
| 39 | 39 |
| 40 [section] | 40 [section] |
| 41 opt1 += foo | 41 opt1 += foo |
| 42 opt2 -= bar | 42 opt2 -= bar |
| 43 | 43 |
| 44 The value of the inherit option has to be a relative path with forward | 44 The value of the inherit option has to be a relative path with forward |
| 45 slashes as delimiters. Up to 5 configuration files can be chained this way
, | 45 slashes as delimiters. Up to 5 configuration files can be chained this way
, |
| 46 longer chains are disallowed to deal with circular references. | 46 longer chains are disallowed to deal with circular references. |
| 47 | 47 |
| 48 As opposed to SafeConfigParser, files are decoded as UTF-8 while | 48 As opposed to SafeConfigParser, files are decoded as UTF-8 while |
| 49 reading. Also, ChainedConfigParser data is read-only. An additional | 49 reading. Also, ChainedConfigParser data is read-only. An additional |
| 50 option_source(section, option) method is provided to get the path | 50 option_source(section, option) method is provided to get the path |
| 51 of the configuration file defining this option (for relative paths). | 51 of the configuration file defining this option (for relative paths). |
| 52 Items returned by the items() function also have a source attribute | 52 Items returned by the items() function also have a source attribute |
| 53 serving the same purpose. | 53 serving the same purpose. |
| 54 ''' | 54 """ |
| 55 | 55 |
| 56 def __init__(self): | 56 def __init__(self): |
| 57 ConfigParser.SafeConfigParser.__init__(self) | 57 ConfigParser.SafeConfigParser.__init__(self) |
| 58 self._origin = {} | 58 self._origin = {} |
| 59 | 59 |
| 60 def _make_parser(self, filename): | 60 def _make_parser(self, filename): |
| 61 parser = ConfigParser.SafeConfigParser() | 61 parser = ConfigParser.SafeConfigParser() |
| 62 parser.optionxform = lambda option: option | 62 parser.optionxform = lambda option: option |
| 63 | 63 |
| 64 with io.open(filename, encoding='utf-8') as file: | 64 with io.open(filename, encoding='utf-8') as file: |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 raise NotImplementedError | 164 raise NotImplementedError |
| 165 | 165 |
| 166 def add_section(self, section): | 166 def add_section(self, section): |
| 167 raise NotImplementedError | 167 raise NotImplementedError |
| 168 | 168 |
| 169 def remove_option(self, section, option): | 169 def remove_option(self, section, option): |
| 170 raise NotImplementedError | 170 raise NotImplementedError |
| 171 | 171 |
| 172 def remove_section(self, section): | 172 def remove_section(self, section): |
| 173 raise NotImplementedError | 173 raise NotImplementedError |
| OLD | NEW |