Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindinsight
提交
97ca18d2
M
mindinsight
项目概览
MindSpore
/
mindinsight
通知
8
Star
4
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindinsight
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
97ca18d2
编写于
6月 22, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
6月 22, 2020
浏览文件
操作
浏览文件
下载
差异文件
!328 The annotate maybe lost when delete the import statement.
Merge pull request !328 from ggpolar/br_wzk_dev
上级
92f425bb
5c5ea91a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
65 addition
and
31 deletion
+65
-31
mindinsight/mindconverter/ast_edits.py
mindinsight/mindconverter/ast_edits.py
+65
-31
未找到文件。
mindinsight/mindconverter/ast_edits.py
浏览文件 @
97ca18d2
...
@@ -20,7 +20,6 @@ import re
...
@@ -20,7 +20,6 @@ import re
from
enum
import
Enum
from
enum
import
Enum
import
pasta
import
pasta
from
pasta.augment
import
import_utils
from
mindinsight.mindconverter.code_analysis
import
CodeAnalyzer
from
mindinsight.mindconverter.code_analysis
import
CodeAnalyzer
from
mindinsight.mindconverter.code_analysis
import
APIAnalysisSpec
from
mindinsight.mindconverter.code_analysis
import
APIAnalysisSpec
...
@@ -290,58 +289,93 @@ class AstEditVisitor(ast.NodeVisitor):
...
@@ -290,58 +289,93 @@ class AstEditVisitor(ast.NodeVisitor):
for
convert_fun
,
args
in
tasks
:
for
convert_fun
,
args
in
tasks
:
convert_fun
(
*
args
)
convert_fun
(
*
args
)
def
_convert_external_reference
(
self
):
@
staticmethod
"""Convert import statements."""
def
_dump_without_prefix
(
node
):
name_replace
=
APIAnalysisSpec
.
import_name_mapping
"""Get the python source for an AST."""
replace_imports
=
list
(
name_replace
.
values
())
pos
=
0
source_prefix
=
pasta
.
base
.
formatting
.
get
(
node
,
'prefix'
)
if
source_prefix
:
pos
=
len
(
source_prefix
)
source_code
=
pasta
.
dump
(
node
)
return
source_code
[
pos
]
def
_replace_external_reference
(
self
):
"""
Replace external reference statements.
Returns:
dict, key is external name, value is the new replaced node.
"""
all_name_mappings
=
APIAnalysisSpec
.
import_name_mapping
names_replaced_with
=
dict
()
for
ref_info
in
self
.
_code_analyzer
.
external_references
.
values
():
for
ref_info
in
self
.
_code_analyzer
.
external_references
.
values
():
external_ref_info
=
ref_info
[
'external_ref_info'
]
external_ref_info
=
ref_info
[
'external_ref_info'
]
paren
t_node
=
ref_info
[
'parent_node'
]
impor
t_node
=
ref_info
[
'parent_node'
]
if
paren
t_node
is
None
:
if
impor
t_node
is
None
:
continue
continue
code
=
pasta
.
dump
(
parent_node
)
code
=
self
.
_dump_without_prefix
(
import_node
)
import_parent_node
=
self
.
_code_analyzer
.
root_scope
.
parent
(
import_node
)
# replace import with new name
if
external_ref_info
.
name
in
APIAnalysisSpec
.
get_convertible_external_names
():
if
external_ref_info
.
name
in
APIAnalysisSpec
.
get_convertible_external_names
():
external_ref_info
=
ref_info
[
'external_ref_info'
]
external_ref_info
=
ref_info
[
'external_ref_info'
]
if
external_ref_info
.
name
in
name_replace
.
keys
():
if
external_ref_info
.
name
in
all_name_mappings
.
keys
():
import_utils
.
remove_import_alias_node
(
self
.
_code_analyzer
.
root_scope
,
external_ref_info
.
node
)
replace_info
=
all_name_mappings
[
external_ref_info
.
name
]
replace_info
=
name_replace
[
external_ref_info
.
name
]
new_node
=
self
.
_make_import
(
name_to_import
=
replace_info
[
0
],
as_name
=
replace_info
[
1
])
new_ref_name
=
replace_info
[
1
]
new_code
=
pasta
.
dump
(
new_node
)
new_external_name
=
replace_info
[
0
]
pasta
.
ast_utils
.
replace_child
(
import_parent_node
,
import_node
,
new_node
)
if
new_ref_name
:
names_replaced_with
.
update
({
external_ref_info
.
name
:
new_node
})
new_code
=
f
'import
{
new_external_name
}
as
{
new_ref_name
}
'
self
.
_process_log
.
info
(
import_node
.
lineno
,
import_node
.
col_offset
,
LOG_FMT_CONVERT
%
else
:
new_code
=
f
'import
{
new_external_name
}
'
self
.
_process_log
.
info
(
parent_node
.
lineno
,
parent_node
.
col_offset
,
LOG_FMT_CONVERT
%
(
code
.
strip
(),
new_code
.
strip
()))
(
code
.
strip
(),
new_code
.
strip
()))
elif
external_ref_info
.
name
.
startswith
(
'torch.'
):
elif
external_ref_info
.
name
.
startswith
(
'torch.'
):
self
.
_process_log
.
warning
(
parent_node
.
lineno
,
paren
t_node
.
col_offset
,
LOG_FMT_NOT_CONVERT
%
self
.
_process_log
.
warning
(
import_node
.
lineno
,
impor
t_node
.
col_offset
,
LOG_FMT_NOT_CONVERT
%
(
code
.
strip
(),
LOG_SUGGESTION_MANUAL_CONVERT
))
(
code
.
strip
(),
LOG_SUGGESTION_MANUAL_CONVERT
))
else
:
else
:
pass
pass
return
names_replaced_with
# Insert import in reverse order, display in forward order.
def
_convert_external_reference
(
self
):
for
idx
in
range
(
len
(
replace_imports
)
-
1
,
-
1
,
-
1
):
"""Convert import statements."""
replace_import
=
replace_imports
[
idx
]
all_name_mappings
=
APIAnalysisSpec
.
import_name_mapping
if
replace_import
[
1
]:
self
.
_add_import
(
name_to_import
=
replace_import
[
0
],
as_name
=
replace_import
[
1
])
# Step1. Replace external reference first.
names_replaced_with
=
self
.
_replace_external_reference
()
new_import_node
=
dict
()
insert_pos
=
0
# Step2. Find out remaining mapping name which not found in script.
for
src_name
,
new_import_name
in
all_name_mappings
.
items
():
if
src_name
not
in
names_replaced_with
:
new_node
=
self
.
_make_import
(
name_to_import
=
new_import_name
[
0
],
as_name
=
new_import_name
[
1
])
new_import_node
.
update
({
insert_pos
:
new_node
})
insert_pos
+=
1
else
:
else
:
self
.
_add_import
(
name_to_import
=
replace_import
[
0
])
try
:
replaced_with_node
=
names_replaced_with
[
src_name
]
insert_pos
=
self
.
_tree
.
body
.
index
(
replaced_with_node
)
+
1
except
ValueError
:
pass
# Step3. Insert import reference in order.
insert_cnt
=
0
for
insert_pos
,
new_node
in
new_import_node
.
items
():
# Insert the node into the module
self
.
_tree
.
body
.
insert
(
insert_pos
+
insert_cnt
,
new_node
)
insert_cnt
+=
1
def
_add_import
(
self
,
name_to_import
,
as_name
=
None
):
@
staticmethod
def
_make_import
(
name_to_import
,
as_name
=
None
):
"""
"""
Adds
an import to the ast tree.
Create
an import to the ast tree.
Args:
Args:
name_to_import: (string) The absolute name to import.
name_to_import: (string) The absolute name to import.
as_name: (string) The alias for the import ("import name_to_import as asname")
as_name: (string) The alias for the import ("import name_to_import as asname")
Returns:
ast.Import, a new ast.Import node.
"""
"""
new_alias
=
ast
.
alias
(
name
=
name_to_import
,
asname
=
as_name
)
new_alias
=
ast
.
alias
(
name
=
name_to_import
,
asname
=
as_name
)
import_node
=
ast
.
Import
(
names
=
[
new_alias
])
import_node
=
ast
.
Import
(
names
=
[
new_alias
])
return
import_node
# Insert the node at the top of the module
self
.
_tree
.
body
.
insert
(
1
if
pasta
.
base
.
ast_utils
.
has_docstring
(
self
.
_tree
)
else
0
,
import_node
)
def
_convert_function
(
self
,
func_scope
,
is_forward
):
def
_convert_function
(
self
,
func_scope
,
is_forward
):
"""
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录