Updated upgrade script to no longer change url_for that don't need to be changed
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 874079b · Browse files · 3 files·+59 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+59 added−18 removed
- other:+59 added−18 removed
Provenance
- Commit
85efa3f- Tree
290b80c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
874079b
3 files changed+59 added−18 removed
modified:scripts/testproj/templates/index.html+1 added
12{{ url_for('static', filename='test.css') }}{{ url_for('foo.static', filename='test.css') }}modified:scripts/testproj/test.py+10 added-1 removed
1-from flask import Flask, Module, render_template1+from flask import Flask, Module, render_template, url_for2 3 4 mod = Module(__name__)5 mod2 = Module(__name__, 'testmod2')5 unmodified lines11 g.db.close()12 return response13 14 15+@app.route('/')16+def index_foo():17+ x1 = url_for('somemod.index')18+ x2 = url_for('.index')19+ return render_template('test/index.html')20+21+22 @mod.route('/')23 def index():24+ x1 = url_for('somemod.index')25+ x2 = url_for('.index')26 return render_template('test/index.html')27 28 29 @mod2.route('/')17 unmodified linesmodified:scripts/flask-07-upgrade.py+48 added-17 removed
44 unmodified lines45 _module_constructor_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\s*=\s*Module'46 r'\(__name__\s*(?:,\s*(?:name\s*=\s*)?(%s))?' %47 _string_re_part)48 _error_handler_re = re.compile(r'%s\.error_handlers\[\s*(\d+)\s*\]' % _app_re_part)49-_mod_route_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route')49+_mod_route_re = re.compile(r'@([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route')50 _blueprint_related = [51 (re.compile(r'request\.module'), 'request.blueprint'),52 (re.compile(r'register_module'), 'register_blueprint'),53 (re.compile(r'%s\.modules' % _app_re_part), '\\1.blueprints')7 unmodified lines61 lineterm=''):62 print line63 64 65-def fix_url_for(contents):66- def handle_match(match):67- prefix = match.group(1)68- endpoint = ast.literal_eval(match.group(2))69- if endpoint.startswith('.'):70- endpoint = endpoint[1:]71- else:72- endpoint = '.' + endpoint73- return prefix + repr(endpoint)74- return _url_for_re.sub(handle_match, contents)75-76-65 def looks_like_teardown_function(node):66 returns = [x for x in ast.walk(node) if isinstance(x, ast.Return)]67 if len(returns) != 1:68 return12 unmodified lines81 82 return resp_name.id83 84 85+def fix_url_for(contents, module_declarations=None):86+ if module_declarations is None:87+ skip_module_test = True88+ else:89+ skip_module_test = False90+ mapping = dict(module_declarations)91+ annotated_lines = []92+93+ def make_line_annotations():94+ if not annotated_lines:95+ last_index = 096+ for line in contents.splitlines(True):97+ last_index += len(line)98+ annotated_lines.append((last_index, line))99+100+ def backtrack_module_name(call_start):101+ make_line_annotations()102+ for idx, (line_end, line) in enumerate(annotated_lines):103+ if line_end > call_start:104+ for _, line in reversed(annotated_lines[:idx]):105+ match = _mod_route_re.search(line)106+ if match is not None:107+ shortname = match.group(1)108+ return mapping.get(shortname)109+110+ def handle_match(match):111+ if not skip_module_test:112+ modname = backtrack_module_name(match.start())113+ if modname is None:114+ return match.group(0)115+ prefix = match.group(1)116+ endpoint = ast.literal_eval(match.group(2))117+ if endpoint.startswith('.'):118+ endpoint = endpoint[1:]119+ elif '.' not in endpoint:120+ endpoint = '.' + endpoint121+ else:122+ return match.group(0)123+ return prefix + repr(endpoint)124+ return _url_for_re.sub(handle_match, contents)125+126+127 def fix_teardown_funcs(contents):128 129 def is_return_line(line):130 args = line.strip().split()92 unmodified lines223 new_contents = rewrite_blueprint_imports(new_contents)224 225 for pattern, replacement in _blueprint_related:226 new_contents = pattern.sub(replacement, new_contents)197- return new_contents227+ return new_contents, dict(modules_declared)228 229 230 def upgrade_python_file(filename, contents, teardown):201- new_contents = fix_url_for(contents)231+ new_contents = contents232 if teardown:233 new_contents = fix_teardown_funcs(new_contents)204- new_contents = rewrite_for_blueprints(new_contents, filename)234+ new_contents, modules = rewrite_for_blueprints(new_contents, filename)235+ new_contents = fix_url_for(new_contents, modules)236 new_contents = _error_handler_re.sub('\\1.error_handler_spec[None][\\2]',237 new_contents)238 make_diff(filename, contents, new_contents)239 240 241 def upgrade_template_file(filename, contents):211- new_contents = fix_url_for(contents)242+ new_contents = fix_url_for(contents, None)243 make_diff(filename, contents, new_contents)244 245 246 def walk_path(path):53 unmodified lines