Skip to content
Myr

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

1 file+65 added−1 removed

modified:scripts/flask-07-upgrade.py+65 added-1 removed
26 unmodified lines
27except ImportError:28 ast = None293031_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]434445def error(message):46 print >> sys.stderr, 'error:', message
84 unmodified lines
131132 return ''.join(content_lines)133134135def 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)140141142def 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 break154155 return ''.join(import_block).replace('Module', 'Blueprint')156157158def 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)170171172def 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)181182 if found_constructor:183 new_contents = rewrite_blueprint_imports(new_contents)184185 for pattern, replacement in _blueprint_related:186 new_contents = pattern.sub(replacement, new_contents)187 return new_contents188189190def 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)196197198def upgrade_template_file(filename, contents):
37 unmodified lines