changeset 6890:2b6459f9e91a

ini: improve structure and documentation of regexps
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 14 Sep 2017 02:08:06 +0200
parents 73934760f601
children 7292c5976752
files kallithea/lib/inifile.py
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/inifile.py	Thu Sep 14 02:08:06 2017 +0200
+++ b/kallithea/lib/inifile.py	Thu Sep 14 02:08:06 2017 +0200
@@ -80,11 +80,13 @@
         '# 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 @@
                     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