changeset 6884:e3cce237d77c

ini: extract .ini handling from scripts/generate-ini.py to kallithea/lib/inifile.py No functional changes - just moving and a minimal amount of adjusting.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 14 Sep 2017 02:08:06 +0200
parents ab80515f3096
children d22eb3c1797b
files kallithea/lib/inifile.py scripts/generate-ini.py
diffstat 2 files changed, 88 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/lib/inifile.py	Thu Sep 14 02:08:06 2017 +0200
@@ -0,0 +1,85 @@
+# -*- coding: utf-8 -*-
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""
+kallithea.lib.inifile
+~~~~~~~~~~~~~~~~~~~~~
+
+Handling of .ini files, mainly creating them from Mako templates and adding
+other custom values.
+"""
+
+import logging
+import re
+
+
+log = logging.getLogger(__name__)
+
+
+def expand(template, desc, selected_mako_conditionals, mako_variable_values, settings):
+    """Expand mako template and tweak it.
+    Not entirely stable for random templates as input, but good enough for our
+    single template.
+    """
+    # 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
+        conditional sections enabled and the rest as comments
+        """
+        conditional_lines = m.group(1)
+        def sub_conditional(m):
+            """given a conditional and the corresponding lines, return them raw
+            or commented out, based on whether conditional is selected
+            """
+            criteria, lines = m.groups()
+            if criteria not in selected_mako_conditionals:
+                lines = '\n'.join((l if not l or l.startswith('#') else '#' + l) for l in lines.split('\n'))
+            return lines
+        conditional_lines = re.sub(r'^%(?:el)?if (.*):\n((?:^[^%\n].*\n|\n)*)',
+            sub_conditional, conditional_lines, flags=re.MULTILINE)
+        return conditional_lines
+    mako_no_conditionals = re.sub(r'^(%if .*\n(?:[^%\n].*\n|%elif .*\n|\n)*)%endif\n',
+        sub_conditionals, template, flags=re.MULTILINE)
+
+    # expand mako variables
+    def pyrepl(m):
+        return mako_variable_values.get(m.group(1), m.group(0))
+    mako_no_variables = re.sub(r'\${([^}]*)}', pyrepl, mako_no_conditionals)
+
+    # remove utf-8 coding header
+    ini_lines = re.sub(r'^## -\*- coding: utf-8 -\*-\n', '', mako_no_variables)
+
+    ini_lines = re.sub(
+        '# 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()
+                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)
+        return sectionname + '\n' + lines
+    ini_lines = re.sub(r'^(\[.*\])\n((?:(?:[^[\n].*)?\n)*)', process_section, ini_lines, flags=re.MULTILINE)
+
+    return ini_lines
--- a/scripts/generate-ini.py	Thu Sep 14 02:08:06 2017 +0200
+++ b/scripts/generate-ini.py	Thu Sep 14 02:08:06 2017 +0200
@@ -7,6 +7,8 @@
 
 import re
 
+from kallithea.lib import inifile
+
 makofile = 'kallithea/lib/paster_commands/template.ini.mako'
 
 # the mako conditionals used in all other ini files and templates
@@ -94,58 +96,10 @@
         print 'writing:', makofile
         open(makofile, 'w').write(mako_marked_up)
 
-    # 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
-        conditional sections enabled and the rest as comments
-        """
-        conditional_lines = m.group(1)
-        def sub_conditional(m):
-            """given a conditional and the corresponding lines, return them raw
-            or commented out, based on whether conditional is selected
-            """
-            criteria, lines = m.groups()
-            if criteria not in selected_mako_conditionals:
-                lines = '\n'.join((l if not l or l.startswith('#') else '#' + l) for l in lines.split('\n'))
-            return lines
-        conditional_lines = re.sub(r'^%(?:el)?if (.*):\n((?:^[^%\n].*\n|\n)*)',
-            sub_conditional, conditional_lines, flags=re.MULTILINE)
-        return conditional_lines
-    mako_no_conditionals = re.sub(r'^(%if .*\n(?:[^%\n].*\n|%elif .*\n|\n)*)%endif\n',
-        sub_conditionals, mako_no_text_markup, flags=re.MULTILINE)
-
-    # expand mako variables
-    def pyrepl(m):
-        return mako_variable_values.get(m.group(1), m.group(0))
-    mako_no_variables = re.sub(r'\${([^}]*)}', pyrepl, mako_no_conditionals)
-
-    # remove utf-8 coding header
-    base_ini = re.sub(r'^## -\*- coding: utf-8 -\*-\n', '', mako_no_variables)
-
     # create ini files
     for fn, desc, settings in ini_files:
         print 'updating:', fn
-        ini_lines = re.sub(
-            '# Kallithea - config file generated with kallithea-config *#\n',
-            ''.join('# %-77s#\n' % l.strip() for l in desc.strip().split('\n')),
-            base_ini)
-        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"""
-                    setting, value = m.groups()
-                    line = m.group(0)
-                    if setting in section_settings:
-                        line = '%s = %s' % (setting, section_settings[setting])
-                        if '$' not in value:
-                            line = '#%s = %s\n%s' % (setting, value, line)
-                    return line.rstrip()
-                lines = re.sub(r'^([^#\n].*) = ?(.*)', 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)
+        ini_lines = inifile.expand(mako_no_text_markup, desc, selected_mako_conditionals, mako_variable_values, settings)
         open(fn, 'w').write(ini_lines)
 
 if __name__ == '__main__':