diff --git a/src/configobj/__init__.py b/src/configobj/__init__.py index 3f6eac0..a5c0840 100644 --- a/src/configobj/__init__.py +++ b/src/configobj/__init__.py @@ -472,6 +472,7 @@ def _initialise(self): self.default_values = {} self.extra_values = [] self._created = False + self.source_map = {} def _interpolate(self, key, value): @@ -514,7 +515,7 @@ def _check(entry): return val - def __setitem__(self, key, value, unrepr=False): + def __setitem__(self, key, value, unrepr=False, line_range=None): """ Correctly set a value. @@ -571,7 +572,8 @@ def __setitem__(self, key, value, unrepr=False): else: raise TypeError('Value is not a string "%s".' % value) dict.__setitem__(self, key, value) - + if line_range: + self.source_map[key]=line_range def __delitem__(self, key): """Remove items from the sequence when deleting.""" @@ -582,6 +584,7 @@ def __delitem__(self, key): self.sections.remove(key) del self.comments[key] del self.inline_comments[key] + del self.source_map[key] def get(self, key, default=None): @@ -1031,6 +1034,14 @@ def _get_triple_quote(value): return tsquot if "'''" in value else tdquot +def _get_triple_quote(value): + """Helper for triple-quoting round-trips.""" + if ('"""' in value) and ("'''" in value): + raise ConfigObjError('Value cannot be safely quoted: {0!r}'.format(value)) + + return tsquot if "'''" in value else tdquot + + class ConfigObj(Section): """An object to read, create, and write config files.""" @@ -1599,6 +1610,7 @@ def _parse(self, infile): # it's not a section marker, # so it should be a valid ``key = value`` line mat = self._keyword.match(line) + start_line=cur_index if mat is None: self._handle_error( 'Invalid line ({0!r}) (matched as neither section nor keyword)'.format(line), @@ -1662,7 +1674,7 @@ def _parse(self, infile): # add the key. # we set unrepr because if we have got this far we will never # be creating a new section - this_section.__setitem__(key, value, unrepr=True) + this_section.__setitem__(key, value, unrepr=True, line_range=(start_line, cur_index)) this_section.inline_comments[key] = comment this_section.comments[key] = comment_list continue @@ -2448,3 +2460,4 @@ def get_extra_values(conf, _prepend=()): """*A programming language is a medium of expression.* - Paul Graham""" +ium of expression.* - Paul Graham"""