From 186df6d128a61839c29a8f921edffa8c7cfcc223 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Mon, 24 Mar 2014 15:01:46 +0200 Subject: [PATCH] conf: make it possible to add new sections Change the set_into_file() method so that a new section is added if the desired section is not found in the config. Change-Id: Ic4c513bd07b0b7ce8a82b921faba35ed36befef5 Signed-off-by: Markus Lehtonen --- gitbuildsys/conf.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/gitbuildsys/conf.py b/gitbuildsys/conf.py index 9095441..0bbaf0e 100644 --- a/gitbuildsys/conf.py +++ b/gitbuildsys/conf.py @@ -133,18 +133,19 @@ class BrainConfigParser(SafeConfigParser): if last_section_line is not None: self._flines.insert(last_section_line + 1, new_line) else: - raise NoSectionError(section) + self._flines.insert(lineno + 1, '\n') + self._flines.insert(lineno + 2, '[%s]\n' % section) + self._flines.insert(lineno + 3, new_line) def set_into_file(self, section, option, value, replace_opt=None): """When set new value, need to update the readin file lines, which can be saved back to file later. """ - try: - SafeConfigParser.set(self, section, option, value) - if replace_opt: - SafeConfigParser.remove_option(self, section, replace_opt) - except NoSectionError, err: - raise errors.ConfigError(str(err)) + if not self.has_section(section): + self.add_section(section) + SafeConfigParser.set(self, section, option, value) + if replace_opt: + SafeConfigParser.remove_option(self, section, replace_opt) # If the code reach here, it means the section and key are ok try: -- 2.7.4