Add support for function call fixing, add tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Addresses #1135, some code cleanup and refactoring. Changes wrapper function which handles testing, further modularized code, added test to cover function call fixing, and fixed duplicate test function name.
authored by Keyan Pishdadian · committed by Keyan Pishdadian · parent 9cbe83e · Browse files · 2 files·+56 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+56 added−21 removed
- other:+56 added−21 removed
Provenance
- Commit
4cb311b- Tree
5021057- Author
Keyan Pishdadian <kpishdadian@gmail.com>- Committer
Keyan Pishdadian <kpishdadian@gmail.com>- Parent
9cbe83e
2 files changed+56 added−21 removed
modified:scripts/flaskext_migrate.py+35 added-12 removed
68 unmodified lines69 def fix_standard_imports(red):70 """71 Handles import modification in the form:72 import flask.ext.foo" --> import flask_foo73-74- Does not modify function calls elsewhere in the source outside of the75- original import statement.73 """74 imports = red.find_all("ImportNode")75 for x, node in enumerate(imports):76 try:80- if (node.value[0].value == 'flask' and81- node.value[1].value == 'ext'):82- package = node.value[2].value83- name = node.names()[0]84- imports[x].replace("import flask_%s as %s" % (package, name))77+ if (node.value[0].value[0].value == 'flask' and78+ node.value[0].value[1].value == 'ext'):79+ package = node.value[0].value[2]80+ name = node.names()[0].split('.')[-1]81+ node.replace("import flask_%s as %s" % (package, name))82 except IndexError:83 pass84 85 return red86 87 88 def _get_modules(module):89 """93- Takes a list of modules and converts into a string90+ Takes a list of modules and converts into a string.91 92 The module list can include parens, this function checks each element in93 the list, if there is a paren then it does not add a comma before the next94 element. Otherwise a comma and space is added. This is to preserve module6 unmodified lines101 102 return ''.join(modules_string)103 104 105+def fix_function_calls(red):106+ """107+ Modifies function calls in the source to reflect import changes.108+109+ Searches the AST for AtomtrailerNodes and replaces them.110+ """111+ atoms = red.find_all("Atomtrailers")112+ for x, node in enumerate(atoms):113+ try:114+ if (node.value[0].value == 'flask' and115+ node.value[1].value == 'ext'):116+ node.replace("flask_foo%s" % node.value[3])117+ except IndexError:118+ pass119+120+ return red121+122+123 def check_user_input():124 """Exits and gives error message if no argument is passed in the shell."""125 if len(sys.argv) < 2:126 sys.exit("No filename was included, please try again.")127 128 114-def fix(ast):129+def fix_tester(ast):130 """Wrapper which allows for testing when not running from shell."""116- return fix_imports(ast).dumps()131+ ast = fix_imports(ast)132+ ast = fix_function_calls(ast)133+ return ast.dumps()134 135 119-if __name__ == "__main__":136+def fix():137+ """Wrapper for user argument checking and import fixing."""138 check_user_input()139 input_file = sys.argv[1]140 ast = read_source(input_file)141 ast = fix_imports(ast)142+ ast = fix_function_calls(ast)143 write_source(ast, input_file)144+145+146+if __name__ == "__main__":147+ fix()modified:scripts/test_import_migration.py+21 added-9 removed
6 unmodified lines7 8 9 def test_simple_from_import():10 red = RedBaron("from flask.ext import foo")11- output = migrate.fix(red)11+ output = migrate.fix_tester(red)12 assert output == "import flask_foo as foo"13 14 15 def test_from_to_from_import():16 red = RedBaron("from flask.ext.foo import bar")17- output = migrate.fix(red)17+ output = migrate.fix_tester(red)18 assert output == "from flask_foo import bar as bar"19 20 21 def test_multiple_import():22 red = RedBaron("from flask.ext.foo import bar, foobar, something")23- output = migrate.fix(red)23+ output = migrate.fix_tester(red)24 assert output == "from flask_foo import bar, foobar, something"25 26 27 def test_multiline_import():28 red = RedBaron("from flask.ext.foo import \29 bar,\30 foobar,\31 something")32- output = migrate.fix(red)32+ output = migrate.fix_tester(red)33 assert output == "from flask_foo import bar, foobar, something"34 35 36 def test_module_import():37 red = RedBaron("import flask.ext.foo")38- output = migrate.fix(red)39- assert output == "import flask_foo"38+ output = migrate.fix_tester(red)39+ assert output == "import flask_foo as foo"40 41 42-def test_module_import():42+def test_named_module_import():43+ red = RedBaron("import flask.ext.foo as foobar")44+ output = migrate.fix_tester(red)45+ assert output == "import flask_foo as foobar"46+47+48+def test__named_from_import():49 red = RedBaron("from flask.ext.foo import bar as baz")44- output = migrate.fix(red)50+ output = migrate.fix_tester(red)51 assert output == "from flask_foo import bar as baz"52 53 54 def test_parens_import():55 red = RedBaron("from flask.ext.foo import (bar, foo, foobar)")50- output = migrate.fix(red)56+ output = migrate.fix_tester(red)57 assert output == "from flask_foo import (bar, foo, foobar)"58+59+60+def test_function_call_migration():61+ red = RedBaron("flask.ext.foo(var)")62+ output = migrate.fix_tester(red)63+ assert output == "flask_foo(var)"