Changeset - 201135e6548b
[Not reviewed]
stable
0 1 0
Mads Kiilerich - 6 years ago 2020-04-23 10:38:48
mads@kiilerich.com
Grafted from: 83ce7e84980a
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.
1 file changed with 12 insertions and 3 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/inifile.py
Show inline comments
 
@@ -74,18 +74,19 @@ def expand(template, mako_variable_value
 
    ... #variable5 = 5.0
 
    ... variable5 = 5.1
 
    ... #variable6 = 6.0
 
    ... #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"'},
 
    ... }
 
    >>> print(expand(template, mako_variable_values, settings))
 
    ERROR: http_server is 'nc' - it should be one of 'waitress', 'gearbox', 'gevent', 'gunicorn', 'uwsgi'
 
    <BLANKLINE>
 
@@ -93,12 +94,13 @@ def expand(template, mako_variable_value
 
    <BLANKLINE>
 
    variable=VALUE
 
    #variable2  =    value after tab
 
    variable2 = VAL2
 
    <BLANKLINE>
 
    first_extra = EXTRA
 
    spacey =  
 
    <BLANKLINE>
 
    <BLANKLINE>
 
    # FUNCTION RESULT
 
    [second-section]
 
    # option a was chosen
 
    <BLANKLINE>
 
@@ -111,12 +113,16 @@ def expand(template, mako_variable_value
 
    variable5 = 5.2
 
    #variable6 = 6.0
 
    #variable6 = 6.1
 
    variable6 = 6.2
 
    variable7 = 7.0
 
    #variable7 = 7.1
 
    #variable8 = 8.0
 
    <BLANKLINE>
 
    variable8 = None
 
    variable9 = None
 
    <BLANKLINE>
 
    [fourth-section]
 
    fourth = "four"
 
    fourth_extra = 4
 
    <BLANKLINE>
 
    [third-section]
 
@@ -157,12 +163,14 @@ def expand(template, mako_variable_value
 
                if comment:
 
                    return line
 
                return '#' + line
 

	
 
            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)
 
                key, line_value = m.groups()
 
                if key not in section_settings:
 
                    return line
 
@@ -175,13 +183,14 @@ def expand(template, mako_variable_value
 
                    line += '\n%s = %s' % (key, new_value)
 
                section_settings.pop(key)
 
                return line
 

	
 
            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()))
 

	
 
        return sectionname + '\n' + lines
 

	
 
    # process sections until comments before next section or end
0 comments (0 inline, 0 general)