# HG changeset patch # User Mads Kiilerich # Date 1505347687 -7200 # Node ID 94f6b23e52d0151603cd560ff4731c84f3e63ab2 # Parent d06039dc4ca2cbdfe0dbcf8c60b6ab3d1cb31cd8 ini: move high level functionality and defaults to inifiles library diff -r d06039dc4ca2 -r 94f6b23e52d0 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:07 2017 +0200 @@ -22,6 +22,7 @@ import logging import re +import os import mako.template @@ -29,6 +30,19 @@ log = logging.getLogger(__name__) +template_file = os.path.join( + os.path.dirname(os.path.dirname(os.path.dirname(__file__))), + 'kallithea/lib/paster_commands/template.ini.mako') + +default_variables = { + 'database_engine': 'sqlite', + 'http_server': 'waitress', + 'host': '127.0.0.1', + 'port': '5000', + 'uuid': lambda: 'VERY-SECRET', +} + + def expand(template, mako_variable_values, settings): """Expand mako template and tweak it. Not entirely stable for random templates as input, but good enough for our @@ -81,9 +95,11 @@ third_extra = 3 """ + mako_variables = dict(default_variables) + mako_variables.update(mako_variable_values or {}) settings = dict((k, dict(v)) for k, v in settings.items()) # deep copy before mutating - ini_lines = mako.template.Template(template).render(**mako_variable_values) + ini_lines = mako.template.Template(template).render(**mako_variables) def process_section(m): """process a ini section, replacing values as necessary""" @@ -131,3 +147,14 @@ if section_settings) return ini_lines + + +def create(dest_file, mako_variable_values, settings): + """Create an ini file at dest_file""" + with open(template_file, 'rb') as f: + template = f.read().decode('utf-8') + + ini_lines = expand(template, mako_variable_values, settings) + + with open(dest_file, 'wb') as f: + f.write(ini_lines.encode('utf-8')) diff -r d06039dc4ca2 -r 94f6b23e52d0 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:07 2017 +0200 @@ -9,17 +9,6 @@ from kallithea.lib import inifile -makofile = 'kallithea/lib/paster_commands/template.ini.mako' - -# 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': lambda: 'VERY-SECRET', -} - # files to be generated from the mako template ini_files = [ ('kallithea/tests/test.ini', @@ -69,6 +58,7 @@ def main(): # make sure all mako lines starting with '#' (the '##' comments) are marked up as + makofile = inifile.template_file print 'reading:', makofile mako_org = open(makofile).read() mako_no_text_markup = re.sub(r'', '', mako_org) @@ -80,8 +70,8 @@ # create ini files for fn, settings in ini_files: print 'updating:', fn - ini_lines = inifile.expand(mako_marked_up, mako_variable_values, settings) - open(fn, 'w').write(ini_lines) + inifile.create(fn, None, settings) + if __name__ == '__main__': main()