Changeset - 2b6459f9e91a
[Not reviewed]
default
0 1 0
Mads Kiilerich - 8 years ago 2017-09-14 02:08:06
mads@kiilerich.com
ini: improve structure and documentation of regexps
1 file changed with 18 insertions and 1 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/inifile.py
Show inline comments
 
@@ -80,11 +80,13 @@ def expand(template, desc, mako_variable
 
        '# Kallithea - config file generated with kallithea-config *#\n',
 
        ''.join('# %-77s#\n' % l.strip() for l in desc.strip().split('\n')),
 
        ini_lines)
 

	
 
    def process_section(m):
 
        """process a ini section, replacing values as necessary"""
 
        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()
 
@@ -94,8 +96,23 @@ def expand(template, desc, mako_variable
 
                    if '$' not in value:
 
                        line = '#%s = %s\n%s' % (key, value, line)
 
                return line.rstrip()
 

	
 
            # process lines that not are comments or empty and look like name=value
 
            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)
 

	
 
    # process sections until next section start or end
 
    ini_lines = re.sub(r'''^
 
        (\[.*\])\n
 
        # after the section name, a number of chunks with:
 
        (
 
            (?:
 
                # a number of empty or non-section-start lines
 
                (?:[^\n[].*)?\n
 
            )*
 
        )
 
        ''',
 
        process_section, ini_lines, flags=re.MULTILINE|re.VERBOSE)
 

	
 
    return ini_lines
0 comments (0 inline, 0 general)