conf: make it possible to add new sections
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Mon, 24 Mar 2014 13:01:46 +0000 (15:01 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 25 Mar 2014 07:23:07 +0000 (09:23 +0200)
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 <markus.lehtonen@linux.intel.com>
gitbuildsys/conf.py

index 9095441..0bbaf0e 100644 (file)
@@ -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: