Skip to content
Myr

render_template calls no longer need changing for blueprints. They need new paths though :(

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 37fab78 · Browse files · 1 file+6 added−69 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+6 added−69 removed

  • other:+6 added−69 removed

1 file+6 added−69 removed

modified:scripts/flask-07-upgrade.py+6 added-69 removed
19 unmodified lines
20 :license: see LICENSE for more details.21"""22import re23import os24import sys24import inspect25import difflib26import posixpath27from optparse import OptionParser
147 unmodified lines
175 new_file.append(line)176 return ''.join(new_file)177178180def rewrite_render_template_calls(contents, module_declarations):181 mapping = dict(module_declarations)182 annotated_lines = []183184 def make_line_annotations():185 if not annotated_lines:186 last_index = 0187 for line in contents.splitlines(True):188 last_index += len(line)189 annotated_lines.append((last_index, line))190191 def backtrack_module_name(call_start):192 make_line_annotations()193 for idx, (line_end, line) in enumerate(annotated_lines):194 if line_end > call_start:195 for _, line in reversed(annotated_lines[:idx]):196 match = _mod_route_re.search(line)197 if match is not None:198 shortname = match.group(1)199 return mapping.get(shortname)200201 def handle_match(match):202 template_name = ast.literal_eval(match.group(2))203 modname = backtrack_module_name(match.start())204 if modname is not None and template_name.startswith(modname + '/'):205 template_name = modname + ':' + template_name[len(modname) + 1:]206 return match.group(1) + repr(template_name)207 return _render_template_re.sub(handle_match, contents)208209210def rewrite_for_blueprints(contents, filename, template_bundles=False):179def rewrite_for_blueprints(contents, filename):180 modules_declared = []181 def handle_match(match):182 target = match.group(1)183 name_param = match.group(2)
6 unmodified lines
190 new_contents = _module_constructor_re.sub(handle_match, contents)191192 if modules_declared:193 new_contents = rewrite_blueprint_imports(new_contents)225 if template_bundles:226 new_contents = rewrite_render_template_calls(new_contents,227 modules_declared)194195 for pattern, replacement in _blueprint_related:196 new_contents = pattern.sub(replacement, new_contents)197 return new_contents198199234def upgrade_python_file(filename, contents, teardown, template_bundles):200def upgrade_python_file(filename, contents, teardown):201 new_contents = fix_url_for(contents)202 if teardown:203 new_contents = fix_teardown_funcs(new_contents)238 new_contents = rewrite_for_blueprints(new_contents, filename,239 template_bundles)204 new_contents = rewrite_for_blueprints(new_contents, filename)205 new_contents = _error_handler_re.sub('\\1.error_handler_spec[None][\\2]',206 new_contents)207 make_diff(filename, contents, new_contents)208
21 unmodified lines
230 if '{% for' or '{% if' or '{{ url_for' in contents:231 yield filename, 'template'232233269def scan_path(path=None, teardown=True, template_bundles=True):234def scan_path(path=None, teardown=True):235 for filename, type in walk_path(path):236 with open(filename) as f:237 contents = f.read()238 if type == 'python':274 upgrade_python_file(filename, contents, teardown,275 template_bundles)239 upgrade_python_file(filename, contents, teardown)240 elif type == 'template':241 upgrade_template_file(filename, contents)242243280def autodetect_template_bundles(paths):281 folders_with_templates = set()282 for path in paths:283 for filename, type in walk_path(path):284 if type == 'template':285 fullpath = filename.replace(os.path.sep, '/')286 index = fullpath.find('/templates/')287 if index >= 0:288 folder = fullpath[:index]289 else:290 folder = posixpath.dirname(fullpath)291 folders_with_templates.add(folder)292 return len(folders_with_templates) > 1293294244def main():245 """Entrypoint"""246 parser = OptionParser(usage='%prog [options] [paths]')247 parser.add_option('-T', '--no-teardown-detection', dest='no_teardown',
2 unmodified lines
250 parser.add_option('-b', '--bundled-templates', dest='bundled_tmpl',251 action='store_true', help='Indicate to the system '252 'that templates are bundled with modules. Default '253 'is auto detect.')305 parser.add_option('-B', '--no-bundled-templates', dest='no_bundled_tmpl',306 action='store_true', help='Indicate to the system '307 'that templates are not bundled with modules. '308 'Default is auto detect')254 options, args = parser.parse_args()255 if not args:256 args = ['.']257
1 unmodified line
259 parser.error('Python 2.6 or later is required to run the upgrade script.\n'260 'The runtime requirements for Flask 0.7 however are still '261 'Python 2.5.')262318 if options.no_bundled_tmpl:319 template_bundles = False320 elif options.bundled_tmpl:321 template_bundles = True322 else:323 template_bundles = autodetect_template_bundles(args)324263 for path in args:326 scan_path(path, teardown=not options.no_teardown,327 template_bundles=template_bundles)264 scan_path(path, teardown=not options.no_teardown)265266267if __name__ == '__main__':268 main()