File iniparse-insert-after-commented-option.patch of Package python-iniparse

Index: iniparse/ini.py
===================================================================
--- iniparse/ini.py	(revision 146)
+++ iniparse/ini.py	(working copy)
@@ -232,8 +232,11 @@
             if isinstance(d, list): self.extend(d)
             else: self.add(d)
 
-    def add(self, x):
-        self.contents.append(x)
+    def add(self, x, index=None):
+        if index:
+            self.contents.insert(index, x)
+        else:
+            self.contents.append(x)
 
     def extend(self, x):
         for i in x: self.add(i)
@@ -322,6 +325,7 @@
     _optionxformvalue = None
     _optionxformsource = None
     _compat_skip_empty_lines = set()
+    _commented_options = {}
     def __init__(self, lineobj, defaults = None,
                        optionxformvalue=None, optionxformsource=None):
         self._lines = [lineobj]
@@ -371,7 +375,10 @@
         if xkey not in self._options:
             # create a dummy object - value may have multiple lines
             obj = LineContainer(OptionLine(key, ''))
-            self._lines[-1].add(obj)
+            index = None
+            if xkey in self._commented_options:
+                index = self._commented_options[xkey]
+            self._lines[-1].add(obj, index)
             self._options[xkey] = obj
         # the set_value() function in LineContainer
         # automatically handles multi-line values
@@ -546,6 +553,7 @@
         except AttributeError:
             fname = '<???>'
         linecount = 0
+        sectionlinecount = 0
         exc = None
         line = None
 
@@ -565,6 +573,8 @@
                     raise MissingSectionHeaderError(fname, linecount, line)
                 else:
                     lineobj = make_comment(line)
+            if cur_section:
+                sectionlinecount += 1
 
             if lineobj is None:
                 if self._parse_exc:
@@ -611,6 +621,7 @@
                 pending_empty_lines = False
                 cur_section = LineContainer(lineobj)
                 self._data.add(cur_section)
+                sectionlinecount = 1
                 cur_option = None
                 cur_option_name = None
                 if cur_section.name == DEFAULTSECT:
@@ -630,6 +641,13 @@
 
             if isinstance(lineobj, (CommentLine, EmptyLine)):
                 pending_lines.append(lineobj)
+                if isinstance(lineobj, CommentLine) and cur_section_name and cur_section_name != 'DEFAULT':
+                    # check if there is a config line in the comment
+                    comment_lineobj = self._parse(lineobj.comment.strip())
+                    if isinstance(comment_lineobj, OptionLine):
+                        obj = LineContainer(comment_lineobj)
+                        self._sections[cur_section_name]._commented_options[obj.name] = sectionlinecount
+                        sectionlinecount += 1
                 if isinstance(lineobj, EmptyLine):
                     pending_empty_lines = True
 
Index: tests/test_ini.py
===================================================================
--- tests/test_ini.py	(revision 146)
+++ tests/test_ini.py	(working copy)
@@ -393,7 +393,41 @@
         ip.section.another = 'baz'
         self.assertEqual(str(ip), self.s6)
 
+    s7 = (
+"""
+[section]
+# Hello
+# option1 = foo
 
+#option2=bazz
+#option3=spam
+bumble=bee
+"""
+)
+
+    s8 = (
+"""
+[section]
+# Hello
+# option1 = foo
+option1 = bar
+
+#option2=bazz
+option2 = bazz
+#option3=spam
+option3 = spam
+bumble=bee
+"""
+)
+
+    def test_insert_after_commented_option(self):
+        ip = ini.INIConfig(StringIO(self.s7))
+        ip.section.option1 = 'bar'
+        ip.section.option2 = 'bazz'
+        ip.section.option3 = 'spam'
+        self.assertEqual(str(ip), self.s8)
+
+
 class suite(unittest.TestSuite):
     def __init__(self):
         unittest.TestSuite.__init__(self, [
openSUSE Build Service is sponsored by