Changeset - 952a83c9e478
[Not reviewed]
default
0 1 0
Mads Kiilerich - 8 years ago 2017-09-14 02:08:06
mads@kiilerich.com
ini: be less strict about amount of whitespace around '=' in templates

Allow any amount of space/tab around '=' ... but no other whitespace on the
left hand side.
1 file changed with 3 insertions and 2 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/inifile.py
Show inline comments
 
@@ -52,25 +52,26 @@ def expand(template, desc, selected_mako
 
    >>> selected_mako_conditionals = ["conditional_options == 'option-a'"]
 
    >>> mako_variable_values = {'mako_variable': 'VALUE', 'mako_function()': 'FUNCTION RESULT'}
 
    >>> settings = { # only partially used
 
    ...     '[first-section]': {'variable2': 'VAL2', 'first_extra': 'EXTRA'},
 
    ...     '[third-section]': {'third_extra': ' 3'},
 
    ...     '[fourth-section]': {'fourth_extra': '4', 'fourth': '"four"'},
 
    ... }
 
    >>> print expand(template, desc, selected_mako_conditionals, mako_variable_values, settings)
 
    <BLANKLINE>
 
    [first-section]
 
    <BLANKLINE>
 
    variable=VALUE
 
    variable2  =    value after tab
 
    #variable2 = value after tab
 
    variable2 = VAL2
 
    ## This section had some whitespace and stuff
 
    <BLANKLINE>
 
    <BLANKLINE>
 
    # FUNCTION RESULT
 
    [second-section]
 
    # Description                                                                  #
 
    # of this config file                                                          #
 
    <BLANKLINE>
 
    """
 
    # select the right mako conditionals for the other less sophisticated formats
 
    def sub_conditionals(m):
 
        """given a %if...%endif match, replace with just the selected
 
@@ -108,17 +109,17 @@ def expand(template, desc, selected_mako
 
        sectionname, lines = m.groups()
 
        if sectionname in settings:
 
            section_settings = settings[sectionname]
 
            def process_line(m):
 
                """process a section line and update value if necessary"""
 
                key, value = m.groups()
 
                line = m.group(0)
 
                if key in section_settings:
 
                    line = '%s = %s' % (key, section_settings[key])
 
                    if '$' not in value:
 
                        line = '#%s = %s\n%s' % (key, value, line)
 
                return line.rstrip()
 
            lines = re.sub(r'^([^#\n].*) = ?(.*)', process_line, lines, flags=re.MULTILINE)
 
            lines = re.sub(r'^([^#\n\s]*)[ \t]*=[ \t]*(.*)$', process_line, lines, flags=re.MULTILINE)
 
        return sectionname + '\n' + lines
 
    ini_lines = re.sub(r'^(\[.*\])\n((?:(?:[^[\n].*)?\n)*)', process_section, ini_lines, flags=re.MULTILINE)
 

	
 
    return ini_lines
0 comments (0 inline, 0 general)