# HG changeset patch # User Mads Kiilerich # Date 1505347686 -7200 # Node ID 73934760f6014f3c836461639f3bd47a321e0e87 # Parent 952a83c9e478628de80aa9e62102a979925fb285 ini: use proper Mako for generating shipped ini files Get rid of some regexp hacking ... but we still need some hacking to substitute plain template variables that haven't been marked up. diff -r 952a83c9e478 -r 73934760f601 kallithea/lib/inifile.py --- 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 @@ -23,11 +23,13 @@ import logging import re +import mako.template + log = logging.getLogger(__name__) -def expand(template, desc, selected_mako_conditionals, mako_variable_values, settings): +def expand(template, desc, 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. @@ -49,21 +51,21 @@ ... %endif ... ''' >>> desc = 'Description\\nof this config file' - >>> selected_mako_conditionals = ["conditional_options == 'option-a'"] - >>> mako_variable_values = {'mako_variable': 'VALUE', 'mako_function()': 'FUNCTION RESULT'} + >>> selected_mako_conditionals = [] + >>> mako_variable_values = {'mako_variable': 'VALUE', 'mako_function': (lambda: 'FUNCTION RESULT'), + ... 'conditional_options': 'option-a'} >>> 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) + >>> print expand(template, desc, mako_variable_values, settings) [first-section] variable=VALUE #variable2 = value after tab variable2 = VAL2 - ## This section had some whitespace and stuff # FUNCTION RESULT @@ -72,33 +74,7 @@ # of this config file # """ - # 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 = '' - 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 = mako.template.Template(template).render(**mako_variable_values) ini_lines = re.sub( '# Kallithea - config file generated with kallithea-config *#\n', diff -r 952a83c9e478 -r 73934760f601 scripts/generate-ini.py --- 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 @@ -11,17 +11,13 @@ makofile = 'kallithea/lib/paster_commands/template.ini.mako' -# the mako conditionals used in all other ini files and templates -selected_mako_conditionals = set([ - "database_engine == 'sqlite'", - "http_server == 'waitress'", -]) - # the mako variables used in all other ini files and templates mako_variable_values = { + 'database_engine': 'sqlite', + 'http_server': 'waitress', 'host': '127.0.0.1', 'port': '5000', - 'uuid()': '${app_instance_uuid}', + 'uuid': lambda: '${app_instance_uuid}', } # files to be generated from the mako template @@ -97,7 +93,7 @@ # create ini files for fn, desc, settings in ini_files: print 'updating:', fn - ini_lines = inifile.expand(mako_no_text_markup, desc, selected_mako_conditionals, mako_variable_values, settings) + ini_lines = inifile.expand(mako_marked_up, desc, mako_variable_values, settings) open(fn, 'w').write(ini_lines) if __name__ == '__main__':