changeset 8380:201135e6548b stable

inifile: add doctest coverage of comment corner cases for trailing space and value of None Shows room for improvement - doesn't show any bugs. The value of None is undefined behaviour.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 23 Apr 2020 10:38:48 +0200
parents a893d2ce599c
children 3c53e19719cd
files kallithea/lib/inifile.py
diffstat 1 files changed, 12 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/inifile.py	Thu Apr 30 15:35:51 2020 +0200
+++ b/kallithea/lib/inifile.py	Thu Apr 23 10:38:48 2020 +0200
@@ -77,12 +77,13 @@
     ... #variable6 = 6.1
     ... #variable7 = 7.0
     ... variable7 = 7.1
+    ... variable8 = 8.0
     ... '''
     >>> mako_variable_values = {'mako_variable': 'VALUE', 'mako_function': (lambda: 'FUNCTION RESULT'),
     ...                         'conditional_options': 'option-a', 'http_server': 'nc'}
     >>> settings = { # only partially used
-    ...     '[first-section]': {'variable2': 'VAL2', 'first_extra': 'EXTRA'},
-    ...     '[comment-section]': {'variable3': '3.0', 'variable4': '4.1', 'variable5': '5.2', 'variable6': '6.2', 'variable7': '7.0'},
+    ...     '[first-section]': {'variable2': 'VAL2', 'first_extra': 'EXTRA', 'spacey': ' '},
+    ...     '[comment-section]': {'variable3': '3.0', 'variable4': '4.1', 'variable5': '5.2', 'variable6': '6.2', 'variable7': '7.0', 'variable8': None, 'variable9': None},
     ...     '[third-section]': {'third_extra': ' 3'},
     ...     '[fourth-section]': {'fourth_extra': '4', 'fourth': '"four"'},
     ... }
@@ -96,6 +97,7 @@
     variable2 = VAL2
     <BLANKLINE>
     first_extra = EXTRA
+    spacey =  
     <BLANKLINE>
     <BLANKLINE>
     # FUNCTION RESULT
@@ -114,6 +116,10 @@
     variable6 = 6.2
     variable7 = 7.0
     #variable7 = 7.1
+    #variable8 = 8.0
+    <BLANKLINE>
+    variable8 = None
+    variable9 = None
     <BLANKLINE>
     [fourth-section]
     fourth = "four"
@@ -160,6 +166,8 @@
 
             lines = re.sub(r'^(#)?([^#\n\s]*)[ \t]*=[ \t]*(.*)$', comment_out, lines, flags=re.MULTILINE)
 
+            # 2nd pass:
+            # find the best comment line and un-comment or add after
             def add_after_comment(m):
                 """process a section comment line and add new value"""
                 line = m.group(0)
@@ -178,7 +186,8 @@
 
             lines = re.sub(r'^#([^#\n\s]*)[ \t]*=[ \t]*(.*)$', add_after_comment, lines, flags=re.MULTILINE)
 
-            # add unused section settings
+            # 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()))