Changeset - 94f6b23e52d0
[Not reviewed]
default
0 2 0
Mads Kiilerich - 8 years ago 2017-09-14 02:08:07
mads@kiilerich.com
ini: move high level functionality and defaults to inifiles library
2 files changed with 31 insertions and 14 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/inifile.py
Show inline comments
 
@@ -22,6 +22,7 @@ other custom values.
 

	
 
import logging
 
import re
 
import os
 

	
 
import mako.template
 

	
 
@@ -29,6 +30,19 @@ import mako.template
 
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 @@ def expand(template, mako_variable_value
 
    third_extra =  3
 
    <BLANKLINE>
 
    """
 
    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 @@ def expand(template, mako_variable_value
 
            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'))
scripts/generate-ini.py
Show inline comments
 
@@ -9,17 +9,6 @@ import re
 

	
 
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 @@ ini_files = [
 

	
 
def main():
 
    # make sure all mako lines starting with '#' (the '##' comments) are marked up as <text>
 
    makofile = inifile.template_file
 
    print 'reading:', makofile
 
    mako_org = open(makofile).read()
 
    mako_no_text_markup = re.sub(r'</?%text>', '', mako_org)
 
@@ -80,8 +70,8 @@ def main():
 
    # 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()
0 comments (0 inline, 0 general)