Import and some usage rewriting.
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 6dae36f · Browse files · 1 file·+65 added−1 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+65 added−1 removed
- other:+65 added−1 removed
Provenance
- Commit
5e91b0c- Tree
aad06b9- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
6dae36f
1 file changed+65 added−1 removed
modified:scripts/flask-07-upgrade.py+65 added-1 removed
26 unmodified lines27 except ImportError:28 ast = None29 30 31+_from_import_re = re.compile(r'^\s*from flask import\s+')32+_direct_module_usage_re = re.compile(r'flask\.Module')33 _string_re_part = r"('([^'\\]*(?:\\.[^'\\]*)*)'" \34 r'|"([^"\\]*(?:\\.[^"\\]*)*)")'35 _url_for_re = re.compile(r'\b(url_for\()(%s)' % _string_re_part)36 _after_request_re = re.compile(r'((?:@\S+\.(?:app_)?))(after_request)(\b\s*$)(?m)')37+_module_constructor_re = re.compile(r'Module\(__name__\s*(?:,\s*(%s))?' %38+ _string_re_part)39+_blueprint_related = [40+ (re.compile(r'request\.module'), 'request.blueprint'),41+ (re.compile(r'register_module'), 'register_blueprint')42+]43 44 45 def error(message):46 print >> sys.stderr, 'error:', message84 unmodified lines131 132 return ''.join(content_lines)133 134 135+def get_module_autoname(filename):136+ directory, filename = os.path.split(filename)137+ if filename != '__init__.py':138+ return os.path.splitext(filename)[0]139+ return os.path.basename(directory)140+141+142+def rewrite_from_imports(prefix, fromlist, lineiter):143+ import_block = [prefix, fromlist]144+ if fromlist[0] == '(' and fromlist[-1] != ')':145+ for line in lineiter:146+ import_block.append(line)147+ if line.rstrip().endswith(')'):148+ break149+ elif fromlist[-1] == '\\':150+ for line in lineiter:151+ import_block.append(line)152+ if line.rstrip().endswith('\\'):153+ break154+155+ return ''.join(import_block).replace('Module', 'Blueprint')156+157+158+def rewrite_blueprint_imports(contents):159+ new_file = []160+ lineiter = iter(contents.splitlines(True))161+ for line in lineiter:162+ match = _from_import_re.search(line)163+ if match is not None:164+ new_file.extend(rewrite_from_imports(match.group(),165+ line[match.end():],166+ lineiter))167+ continue168+ new_file.append(_direct_module_usage_re.sub('flask.Blueprint', line))169+ return ''.join(new_file)170+171+172+def rewrite_for_blueprints(contents, filename):173+ found_constructor = []174+ def handle_match(match):175+ found_constructor[:] = [True]176+ name_param = match.group(1)177+ if name_param is None:178+ return 'Blueprint(%r, __name__' % get_module_autoname(filename)179+ return 'Blueprint(%s, __name__' % name_param180+ new_contents = _module_constructor_re.sub(handle_match, contents)181+182+ if found_constructor:183+ new_contents = rewrite_blueprint_imports(new_contents)184+185+ for pattern, replacement in _blueprint_related:186+ new_contents = pattern.sub(replacement, new_contents)187+ return new_contents188+189+190 def upgrade_python_file(filename, contents, teardown):191 new_contents = fix_url_for(contents)192 if teardown:130- new_contents = fix_teardown_funcs(contents)193+ new_contents = fix_teardown_funcs(new_contents)194+ new_contents = rewrite_for_blueprints(new_contents, filename)195 make_diff(filename, contents, new_contents)196 197 198 def upgrade_template_file(filename, contents):37 unmodified lines