From 6df406b7c1c06b8c7c58cd74d028310008cea6a4 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 9 Feb 2012 15:45:18 +0100 Subject: Config.write_in_file() now works as expected (can add sections, add inexistant options, and edit in place) --- src/config.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/config.py b/src/config.py index af9c9fbe..2ee4abb1 100644 --- a/src/config.py +++ b/src/config.py @@ -113,16 +113,31 @@ class Config(RawConfigParser): df.close() result_lines = [] we_are_in_the_right_section = False + written = False + section_found = False for line in lines_before: if line.startswith('['): # check the section + if we_are_in_the_right_section and not written: + result_lines.append('%s= %s' % (option, value)) if line == '[%s]' % section: we_are_in_the_right_section = True + section_found = True else: we_are_in_the_right_section = False if (line.startswith('%s ' % (option,)) or - line.startswith('%s=' % (option,))) and we_are_in_the_right_section: - line = '%s = %s' % (option, value) + line.startswith('%s=' % (option,)) or + line.startswith('%s = ' % (option,))) and we_are_in_the_right_section: + line = '%s= %s' % (option, value) + written = True result_lines.append(line) + + if not section_found: + result_lines.append('[%s]' % section) + result_lines.append('%s= %s' % (option, value)) + elif not written: + result_lines.append('%s= %s' % (option, value)) + + df = open(self.file_name, 'w') for line in result_lines: df.write('%s\n' % line) @@ -133,11 +148,11 @@ class Config(RawConfigParser): set the value in the configuration then save it to the file """ - try: + if self.has_section(section): + RawConfigParser.set(self, section, option, value) + else: + self.add_section(section) RawConfigParser.set(self, section, option, value) - except NoSectionError: - # TODO, add this section if it didn't exist - return self.write_in_file(section, option, value) def set(self, option, value, section=DEFSECTION): -- cgit v1.2.3