##// END OF EJS Templates
ini: move high level functionality and defaults to inifiles library
Mads Kiilerich -
r6819:94f6b23e default
parent child Browse files
Show More
@@ -22,6 +22,7 b' other custom values.'
22
22
23 import logging
23 import logging
24 import re
24 import re
25 import os
25
26
26 import mako.template
27 import mako.template
27
28
@@ -29,6 +30,19 b' import mako.template'
29 log = logging.getLogger(__name__)
30 log = logging.getLogger(__name__)
30
31
31
32
33 template_file = os.path.join(
34 os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
35 'kallithea/lib/paster_commands/template.ini.mako')
36
37 default_variables = {
38 'database_engine': 'sqlite',
39 'http_server': 'waitress',
40 'host': '127.0.0.1',
41 'port': '5000',
42 'uuid': lambda: 'VERY-SECRET',
43 }
44
45
32 def expand(template, mako_variable_values, settings):
46 def expand(template, mako_variable_values, settings):
33 """Expand mako template and tweak it.
47 """Expand mako template and tweak it.
34 Not entirely stable for random templates as input, but good enough for our
48 Not entirely stable for random templates as input, but good enough for our
@@ -81,9 +95,11 b' def expand(template, mako_variable_value'
81 third_extra = 3
95 third_extra = 3
82 <BLANKLINE>
96 <BLANKLINE>
83 """
97 """
98 mako_variables = dict(default_variables)
99 mako_variables.update(mako_variable_values or {})
84 settings = dict((k, dict(v)) for k, v in settings.items()) # deep copy before mutating
100 settings = dict((k, dict(v)) for k, v in settings.items()) # deep copy before mutating
85
101
86 ini_lines = mako.template.Template(template).render(**mako_variable_values)
102 ini_lines = mako.template.Template(template).render(**mako_variables)
87
103
88 def process_section(m):
104 def process_section(m):
89 """process a ini section, replacing values as necessary"""
105 """process a ini section, replacing values as necessary"""
@@ -131,3 +147,14 b' def expand(template, mako_variable_value'
131 if section_settings)
147 if section_settings)
132
148
133 return ini_lines
149 return ini_lines
150
151
152 def create(dest_file, mako_variable_values, settings):
153 """Create an ini file at dest_file"""
154 with open(template_file, 'rb') as f:
155 template = f.read().decode('utf-8')
156
157 ini_lines = expand(template, mako_variable_values, settings)
158
159 with open(dest_file, 'wb') as f:
160 f.write(ini_lines.encode('utf-8'))
@@ -9,17 +9,6 b' import re'
9
9
10 from kallithea.lib import inifile
10 from kallithea.lib import inifile
11
11
12 makofile = 'kallithea/lib/paster_commands/template.ini.mako'
13
14 # the mako variables used in all other ini files and templates
15 mako_variable_values = {
16 'database_engine': 'sqlite',
17 'http_server': 'waitress',
18 'host': '127.0.0.1',
19 'port': '5000',
20 'uuid': lambda: 'VERY-SECRET',
21 }
22
23 # files to be generated from the mako template
12 # files to be generated from the mako template
24 ini_files = [
13 ini_files = [
25 ('kallithea/tests/test.ini',
14 ('kallithea/tests/test.ini',
@@ -69,6 +58,7 b' ini_files = ['
69
58
70 def main():
59 def main():
71 # make sure all mako lines starting with '#' (the '##' comments) are marked up as <text>
60 # make sure all mako lines starting with '#' (the '##' comments) are marked up as <text>
61 makofile = inifile.template_file
72 print 'reading:', makofile
62 print 'reading:', makofile
73 mako_org = open(makofile).read()
63 mako_org = open(makofile).read()
74 mako_no_text_markup = re.sub(r'</?%text>', '', mako_org)
64 mako_no_text_markup = re.sub(r'</?%text>', '', mako_org)
@@ -80,8 +70,8 b' def main():'
80 # create ini files
70 # create ini files
81 for fn, settings in ini_files:
71 for fn, settings in ini_files:
82 print 'updating:', fn
72 print 'updating:', fn
83 ini_lines = inifile.expand(mako_marked_up, mako_variable_values, settings)
73 inifile.create(fn, None, settings)
84 open(fn, 'w').write(ini_lines)
74
85
75
86 if __name__ == '__main__':
76 if __name__ == '__main__':
87 main()
77 main()
General Comments 0
You need to be logged in to leave comments. Login now