Skip to content
Myr

Support "import flask.ext.foo" format

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

New commit supports the above import format, however it does not fix function calls elsewhere in the source. Perhaps there should be an error or simply alert the user through stdout().

authored by Keyan Pishdadian · committed by Keyan Pishdadian · parent 61d5e6a · Browse files · 1 file+48 added−2 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+48 added−2 removed

  • other:+48 added−2 removed

1 file+48 added−2 removed

modified:scripts/flaskext_migrate.py+48 added-2 removed
1# CASE 1 - from flask.ext.foo import bam --> from flask_foo import bam2# CASE 2 - from flask.ext import foo --> import flask_foo as foo1# Script which modifies source code away from the deprecated "flask.ext"2# format. Does not yet fully support imports in the style:3#4# "import flask.ext.foo"5#6# these are converted to "import flask_foo" in the7# main import statement, but does not handle function calls in the source.8#9# Run in the terminal by typing: `python flaskext_migrate.py <source_file.py>`10#11# Author: Keyan Pishdadian 20151213from redbaron import RedBaron14import sys151617def read_source(input_file):18 """Parses the input_file into a RedBaron FST."""19 with open(input_file, "r") as source_code:20 red = RedBaron(source_code.read())21 return red222324def write_source(red, input_file):25 """Overwrites the input_file once the FST has been modified."""26 with open(input_file, "w") as source_code:27 source_code.write(red.dumps())282930def fix_imports(red):31 """Wrapper which fixes "from" style imports and then "import" style."""32 red = fix_standard_imports(red)33 red = fix_from_imports(red)34 return red353637def fix_from_imports(red):38 """39 Converts "from" style imports to not use "flask.ext".4041 Handles:42 Case 1: from flask.ext.foo import bam --> from flask_foo import bam43 Case 2: from flask.ext import foo --> import flask_foo as foo44 """45 from_imports = red.find_all("FromImport")46 for x in range(len(from_imports)):47 values = from_imports[x].value48 if (values[0].value == 'flask') and (values[1].value == 'ext'):
8 unmodified lines
57 else:58 module = from_imports[x].modules()[0]59 from_imports[x].replace("import flask_%s as %s"60 % (module, module))61 return red626364def fix_standard_imports(red):65 """66 Handles import modification in the form:67 import flask.ext.foo" --> import flask_foo6869 Does not modify function calls elsewhere in the source outside of the70 original import statement.71 """72 imports = red.find_all("ImportNode")73 for x in range(len(imports)):74 values = imports[x].value75 try:76 if (values[x].value[0].value == 'flask' and77 values[x].value[1].value == 'ext'):78 package = values[x].value[2].value79 imports[x].replace("import flask_%s" % package)80 except IndexError:81 pass8283 return red848586if __name__ == "__main__":
4 unmodified lines