Skip to content
Myr

Change parsing format to use AST via RedBaron

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

Moved away from using manual parsing. Source is parsed using RedBaron to make the FST. Some import formats not yet implemented, and still some bugs to work out. However, the current script works well for the two cases in the file comments.

authored by Keyan Pishdadian · committed by Keyan Pishdadian · parent 0b2556f · Browse files · 3 files+51 added−27 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+51 added−27 removed

  • other:+51 added−27 removed

3 files+51 added−27 removed

deleted:scripts/fix_ext_import.py-20 removed
1from lib2to3.fixer_base import BaseFix2from lib2to3.fixer_util import Name, syms345class FixExtImport(BaseFix):67 PATTERN = "fixnode='oldname'"89 def transform(self, node, results):10 fixnode = results['fixnode']11 fixnode.replace(Name('newname', prefix=fixnode.prefix))1213 if node.type == syms.import_from and \14 getattr(results['imp'], 'value', None) == 'flask.ext':15 return 016 # TODO: Case 2171819# CASE 1 - from flask.ext.foo import bam --> from flask_foo import bam20# CASE 2 - from flask.ext import foo --> import flask_foo as foo
modified:scripts/flaskext_migrate.py+36 added-5 removed
3 unmodified lines
4from redbaron import RedBaron5import sys678def read_source(input_file):8with open("test.py", "r") as source_code:9 red = RedBaron(source_code.read())9 with open(input_file, "r") as source_code:10 red = RedBaron(source_code.read())11 return red1211print red.dumps()1314def write_source(red, input_file):13# with open("code.py", "w") as source_code:14# source_code.write(red.dumps())15 with open(input_file, "w") as source_code:16 source_code.write(red.dumps())171819def fix_imports(red):20 from_imports = red.find_all("FromImport")21 for x in range(len(from_imports)):22 values = from_imports[x].value23 if (values[0].value == 'flask') and (values[1].value == 'ext'):24 # Case 125 if len(from_imports[x].value) == 3:26 package = values[2].value27 modules = from_imports[x].modules()28 r = "{}," * len(modules)29 print modules30 from_imports[x].replace("from flask_%s import %s"31 % (package, r.format(*modules)[:-1]))32 # Case 233 else:34 module = from_imports[x].modules()[0]35 from_imports[x].replace("import flask_%s as %s"36 % (module, module))3738 return red394041if __name__ == "__main__":42 input_file = sys.argv[1]43 ast = read_source(input_file)44 new_ast = fix_imports(ast)45 write_source(new_ast, input_file)
modified:scripts/test.py+15 added-2 removed
1from flask.ext.foo import bam1from flask.ext.foo import \2 bam, \3 crackle45from flask.ext import foo67from flask.ext.foo import (bam,8 a,9 b10)111213from flask.ext import foo1415import sys1617def migrate(old_file):18 new_file = open("temp.py", "w")19 for line in old_file:20 if line[0, 15] is "from flask.ext":
6 unmodified lines
27 pass2829 new_file.write(line)303132if __name__ == "__main__":20 old_file = open(sys.arv[1])33 old_file = open(sys.arv[1])