mirror of https://github.com/mitsuhiko/flask.git
Browse Source
Switching to using RedBaron instead of lib2to3, committing to save lib2to3 code in case using Baron is not successful.pull/1342/head
Keyan Pishdadian
10 years ago
4 changed files with 28 additions and 43 deletions
@ -0,0 +1,20 @@ |
|||||||
|
from lib2to3.fixer_base import BaseFix |
||||||
|
from lib2to3.fixer_util import Name, syms |
||||||
|
|
||||||
|
|
||||||
|
class FixExtImport(BaseFix): |
||||||
|
|
||||||
|
PATTERN = "fixnode='oldname'" |
||||||
|
|
||||||
|
def transform(self, node, results): |
||||||
|
fixnode = results['fixnode'] |
||||||
|
fixnode.replace(Name('newname', prefix=fixnode.prefix)) |
||||||
|
|
||||||
|
if node.type == syms.import_from and \ |
||||||
|
getattr(results['imp'], 'value', None) == 'flask.ext': |
||||||
|
return 0 |
||||||
|
# TODO: Case 2 |
||||||
|
|
||||||
|
|
||||||
|
# CASE 1 - from flask.ext.foo import bam --> from flask_foo import bam |
||||||
|
# CASE 2 - from flask.ext import foo --> import flask_foo as foo |
@ -1,30 +1,14 @@ |
|||||||
# from flask.ext import foo => import flask_foo as foo |
# CASE 1 - from flask.ext.foo import bam --> from flask_foo import bam |
||||||
# from flask.ext.foo import bam => from flask_foo import bam |
# CASE 2 - from flask.ext import foo --> import flask_foo as foo |
||||||
# import flask.ext.foo => import flask_foo |
|
||||||
|
|
||||||
|
from redbaron import RedBaron |
||||||
import sys |
import sys |
||||||
|
|
||||||
|
|
||||||
def migrate(old_file): |
with open("test.py", "r") as source_code: |
||||||
new_file = open("temp.py", "w") |
red = RedBaron(source_code.read()) |
||||||
for line in old_file: |
|
||||||
if line[0:14] == "from flask.ext": |
|
||||||
if line[14] == '.': |
|
||||||
import_statement = line[15::].split(' ') |
|
||||||
extension = import_statement[0] |
|
||||||
line = line.replace("flask.ext." + extension, |
|
||||||
"flask_" + extension) |
|
||||||
elif line[14] == " ": |
|
||||||
import_statement = line[15::].split(' ')[1] |
|
||||||
import_statement = import_statement.strip('\n') |
|
||||||
line = ("import flask_" + |
|
||||||
import_statement + |
|
||||||
" as " + |
|
||||||
import_statement) |
|
||||||
|
|
||||||
new_file.write(line) |
print red.dumps() |
||||||
new_file.close() |
|
||||||
|
|
||||||
if __name__ == "__main__": |
# with open("code.py", "w") as source_code: |
||||||
old_file = open(sys.argv[1]) |
# source_code.write(red.dumps()) |
||||||
migrate(old_file) |
|
||||||
|
@ -1,19 +0,0 @@ |
|||||||
from flask_foo import bam |
|
||||||
import flask_foo as foo |
|
||||||
|
|
||||||
def migrate(old_file): |
|
||||||
new_file = open("temp.py", "w") |
|
||||||
for line in old_file: |
|
||||||
if line[0, 15] is "from flask.ext": |
|
||||||
if line[15] == '.': |
|
||||||
import_statement = line[16::].split(' ') |
|
||||||
extension = import_statement[0] |
|
||||||
line = line. replace("flask.ext." + extension, |
|
||||||
"flask_" + extension) |
|
||||||
else: |
|
||||||
pass |
|
||||||
|
|
||||||
new_file.write(line) |
|
||||||
|
|
||||||
if __name__ == "__main__": |
|
||||||
old_file = open(sys.arv[1]) |
|
Loading…
Reference in new issue