changeset 8550:6b01e99bdb2b

inifile: make it possible for expand() to comment out settings without assigning new value For completeness, when we already can create and update values, also make it possible to delete. This fits nicely into the implementation. Potentiallly "nice to have" but not used yet. The ini file is a text file and it only really makes sense to set string values. Intuitively, it makes sense that setting something to None means that the old value should be commented out but no new value should be set. If we want to set a value of "None", then specify "None" - not None.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 23 Apr 2020 11:05:09 +0200
parents d757635af3c2
children 1b598ea781b2
files kallithea/lib/inifile.py
diffstat 1 files changed, 7 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/inifile.py	Fri Apr 24 13:32:17 2020 +0200
+++ b/kallithea/lib/inifile.py	Thu Apr 23 11:05:09 2020 +0200
@@ -119,9 +119,6 @@
     #variable7 = 7.1
     #variable8 = 8.0
     <BLANKLINE>
-    variable8 = None
-    variable9 = None
-    <BLANKLINE>
     [fourth-section]
     fourth = "four"
     fourth_extra = 4
@@ -180,7 +177,7 @@
                 new_value = section_settings[key]
                 if new_value == line_value:
                     line = line.lstrip('#')
-                else:
+                elif new_value is not None:
                     line += '\n%s = %s' % (key, new_value)
                 section_settings.pop(key)
                 return line
@@ -189,8 +186,12 @@
 
             # 3rd pass:
             # settings that haven't been consumed yet at is appended to section
-            if section_settings:
-                lines += '\n' + ''.join('%s = %s\n' % (key, value) for key, value in sorted(section_settings.items()))
+            append_lines = ''.join(
+                '%s = %s\n' % (key, value)
+                for key, value in sorted(section_settings.items())
+                if value is not None)
+            if append_lines:
+                lines += '\n' + append_lines
 
         return sectionname + '\n' + re.sub('[ \t]+\n', '\n', lines)