Show More
@@ -0,0 +1,85 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | # This program is free software: you can redistribute it and/or modify | |
|
3 | # it under the terms of the GNU General Public License as published by | |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
|
5 | # (at your option) any later version. | |
|
6 | # | |
|
7 | # This program is distributed in the hope that it will be useful, | |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
10 | # GNU General Public License for more details. | |
|
11 | # | |
|
12 | # You should have received a copy of the GNU General Public License | |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
|
14 | ||
|
15 | """ | |
|
16 | kallithea.lib.inifile | |
|
17 | ~~~~~~~~~~~~~~~~~~~~~ | |
|
18 | ||
|
19 | Handling of .ini files, mainly creating them from Mako templates and adding | |
|
20 | other custom values. | |
|
21 | """ | |
|
22 | ||
|
23 | import logging | |
|
24 | import re | |
|
25 | ||
|
26 | ||
|
27 | log = logging.getLogger(__name__) | |
|
28 | ||
|
29 | ||
|
30 | def expand(template, desc, selected_mako_conditionals, mako_variable_values, settings): | |
|
31 | """Expand mako template and tweak it. | |
|
32 | Not entirely stable for random templates as input, but good enough for our | |
|
33 | single template. | |
|
34 | """ | |
|
35 | # select the right mako conditionals for the other less sophisticated formats | |
|
36 | def sub_conditionals(m): | |
|
37 | """given a %if...%endif match, replace with just the selected | |
|
38 | conditional sections enabled and the rest as comments | |
|
39 | """ | |
|
40 | conditional_lines = m.group(1) | |
|
41 | def sub_conditional(m): | |
|
42 | """given a conditional and the corresponding lines, return them raw | |
|
43 | or commented out, based on whether conditional is selected | |
|
44 | """ | |
|
45 | criteria, lines = m.groups() | |
|
46 | if criteria not in selected_mako_conditionals: | |
|
47 | lines = '\n'.join((l if not l or l.startswith('#') else '#' + l) for l in lines.split('\n')) | |
|
48 | return lines | |
|
49 | conditional_lines = re.sub(r'^%(?:el)?if (.*):\n((?:^[^%\n].*\n|\n)*)', | |
|
50 | sub_conditional, conditional_lines, flags=re.MULTILINE) | |
|
51 | return conditional_lines | |
|
52 | mako_no_conditionals = re.sub(r'^(%if .*\n(?:[^%\n].*\n|%elif .*\n|\n)*)%endif\n', | |
|
53 | sub_conditionals, template, flags=re.MULTILINE) | |
|
54 | ||
|
55 | # expand mako variables | |
|
56 | def pyrepl(m): | |
|
57 | return mako_variable_values.get(m.group(1), m.group(0)) | |
|
58 | mako_no_variables = re.sub(r'\${([^}]*)}', pyrepl, mako_no_conditionals) | |
|
59 | ||
|
60 | # remove utf-8 coding header | |
|
61 | ini_lines = re.sub(r'^## -\*- coding: utf-8 -\*-\n', '', mako_no_variables) | |
|
62 | ||
|
63 | ini_lines = re.sub( | |
|
64 | '# Kallithea - config file generated with kallithea-config *#\n', | |
|
65 | ''.join('# %-77s#\n' % l.strip() for l in desc.strip().split('\n')), | |
|
66 | ini_lines) | |
|
67 | def process_section(m): | |
|
68 | """process a ini section, replacing values as necessary""" | |
|
69 | sectionname, lines = m.groups() | |
|
70 | if sectionname in settings: | |
|
71 | section_settings = settings[sectionname] | |
|
72 | def process_line(m): | |
|
73 | """process a section line and update value if necessary""" | |
|
74 | key, value = m.groups() | |
|
75 | line = m.group(0) | |
|
76 | if key in section_settings: | |
|
77 | line = '%s = %s' % (key, section_settings[key]) | |
|
78 | if '$' not in value: | |
|
79 | line = '#%s = %s\n%s' % (key, value, line) | |
|
80 | return line.rstrip() | |
|
81 | lines = re.sub(r'^([^#\n].*) = ?(.*)', process_line, lines, flags=re.MULTILINE) | |
|
82 | return sectionname + '\n' + lines | |
|
83 | ini_lines = re.sub(r'^(\[.*\])\n((?:(?:[^[\n].*)?\n)*)', process_section, ini_lines, flags=re.MULTILINE) | |
|
84 | ||
|
85 | return ini_lines |
@@ -7,6 +7,8 b' Based on kallithea/lib/paster_commands/t' | |||
|
7 | 7 | |
|
8 | 8 | import re |
|
9 | 9 | |
|
10 | from kallithea.lib import inifile | |
|
11 | ||
|
10 | 12 | makofile = 'kallithea/lib/paster_commands/template.ini.mako' |
|
11 | 13 | |
|
12 | 14 | # the mako conditionals used in all other ini files and templates |
@@ -94,58 +96,10 b' def main():' | |||
|
94 | 96 | print 'writing:', makofile |
|
95 | 97 | open(makofile, 'w').write(mako_marked_up) |
|
96 | 98 | |
|
97 | # select the right mako conditionals for the other less sophisticated formats | |
|
98 | def sub_conditionals(m): | |
|
99 | """given a %if...%endif match, replace with just the selected | |
|
100 | conditional sections enabled and the rest as comments | |
|
101 | """ | |
|
102 | conditional_lines = m.group(1) | |
|
103 | def sub_conditional(m): | |
|
104 | """given a conditional and the corresponding lines, return them raw | |
|
105 | or commented out, based on whether conditional is selected | |
|
106 | """ | |
|
107 | criteria, lines = m.groups() | |
|
108 | if criteria not in selected_mako_conditionals: | |
|
109 | lines = '\n'.join((l if not l or l.startswith('#') else '#' + l) for l in lines.split('\n')) | |
|
110 | return lines | |
|
111 | conditional_lines = re.sub(r'^%(?:el)?if (.*):\n((?:^[^%\n].*\n|\n)*)', | |
|
112 | sub_conditional, conditional_lines, flags=re.MULTILINE) | |
|
113 | return conditional_lines | |
|
114 | mako_no_conditionals = re.sub(r'^(%if .*\n(?:[^%\n].*\n|%elif .*\n|\n)*)%endif\n', | |
|
115 | sub_conditionals, mako_no_text_markup, flags=re.MULTILINE) | |
|
116 | ||
|
117 | # expand mako variables | |
|
118 | def pyrepl(m): | |
|
119 | return mako_variable_values.get(m.group(1), m.group(0)) | |
|
120 | mako_no_variables = re.sub(r'\${([^}]*)}', pyrepl, mako_no_conditionals) | |
|
121 | ||
|
122 | # remove utf-8 coding header | |
|
123 | base_ini = re.sub(r'^## -\*- coding: utf-8 -\*-\n', '', mako_no_variables) | |
|
124 | ||
|
125 | 99 | # create ini files |
|
126 | 100 | for fn, desc, settings in ini_files: |
|
127 | 101 | print 'updating:', fn |
|
128 | ini_lines = re.sub( | |
|
129 | '# Kallithea - config file generated with kallithea-config *#\n', | |
|
130 | ''.join('# %-77s#\n' % l.strip() for l in desc.strip().split('\n')), | |
|
131 | base_ini) | |
|
132 | def process_section(m): | |
|
133 | """process a ini section, replacing values as necessary""" | |
|
134 | sectionname, lines = m.groups() | |
|
135 | if sectionname in settings: | |
|
136 | section_settings = settings[sectionname] | |
|
137 | def process_line(m): | |
|
138 | """process a section line and update value if necessary""" | |
|
139 | setting, value = m.groups() | |
|
140 | line = m.group(0) | |
|
141 | if setting in section_settings: | |
|
142 | line = '%s = %s' % (setting, section_settings[setting]) | |
|
143 | if '$' not in value: | |
|
144 | line = '#%s = %s\n%s' % (setting, value, line) | |
|
145 | return line.rstrip() | |
|
146 | lines = re.sub(r'^([^#\n].*) = ?(.*)', process_line, lines, flags=re.MULTILINE) | |
|
147 | return sectionname + '\n' + lines | |
|
148 | ini_lines = re.sub(r'^(\[.*\])\n((?:(?:[^[\n].*)?\n)*)', process_section, ini_lines, flags=re.MULTILINE) | |
|
102 | ini_lines = inifile.expand(mako_no_text_markup, desc, selected_mako_conditionals, mako_variable_values, settings) | |
|
149 | 103 | open(fn, 'w').write(ini_lines) |
|
150 | 104 | |
|
151 | 105 | if __name__ == '__main__': |
General Comments 0
You need to be logged in to leave comments.
Login now