PCART  v1.4
Automated Repair of Python API Parameter Compatibility Issues
extractDef.py
Go to the documentation of this file.
1 
10 
11 
12 
13 import ast
14 import os
15 
16 
17 
18 
22 class FunctionDefVisitor(ast.NodeVisitor):
23 
25  def __init__(self):
26  self._defNodes_defNodes=[]
27 
28 
31  def functionNodes(self):
32  return self._defNodes_defNodes
33 
34 
38  def visit_FunctionDef(self, node):
39  self._defNodes_defNodes.append(node)
40 
41 
42 
43 
47 class FromImport(ast.NodeVisitor):
48  #跳过函数和类作用域,避免将局部导入当作包级别名
49  def visit_FunctionDef(self, node):
50  pass
51 
52  def visit_AsyncFunctionDef(self, node):
53  pass
54 
55  def visit_ClassDef(self, node):
56  pass
57 
58 
61  def __init__(self, currentLevel):
62  self._importDict_importDict={}
63  self._currentLevel_currentLevel=currentLevel
64 
65 
69  @property
70  def importDict(self):
71  return self._importDict_importDict
72 
73 
77  def visit_ImportFrom(self, node):
78  if node.module is not None:
79  module=node.module
80  if node.level==0:#若是绝对导入,需考虑层级
81  tempLst=module.split('.')
82  if len(tempLst)==1:
83  module=''
84  elif self._currentLevel_currentLevel in tempLst:
85  index=tempLst.index(self._currentLevel_currentLevel)
86  module='.'.join(tempLst[index+1:])
87 
88  lst=[{'name':name.name,'alias':name.asname} for name in node.names] #可能会import个多个,from A import a,b,c
89  for dic in lst: #lst中每个元素都是字典
90  key=module+'.'+dic['name'] #dic['name']可能是*
91  key=key.lstrip('.')
92  if dic['alias']:
93  self._importDict_importDict[key]=dic['alias']
94  else:
95  self._importDict_importDict[key]=dic['name']
96 
97 
98 
99 
105 
107  def __init__(self):
108  self._prefix_prefix=''
109  self._relativePath_relativePath='' #记录包的相对路径,例如numpy/core/func.py
110 
111 
115  @property
116  def prefix(self):
117  return self._prefix_prefix
118 
119 
123  @property
124  def relativePath(self):
125  return self._relativePath_relativePath
126 
127 
131  def toFormat(self,filePath):
132  s=filePath.rsplit(f"site-packages{os.sep}", 1)[-1]
133  self._relativePath_relativePath=s
134  s=s.replace('\\','.').replace('/','.')
135  pos=s.rfind('.')
136  s=s[0:pos]
137  self._prefix_prefix=s.replace('\\','.').replace('/','.')
138 
139 
140 
141 
146 class AssignVisitor(ast.NodeVisitor):
147 
149  def __init__(self):
150  self._targetCall_targetCall={}
151 
152 
155  def get_target_call(self):
156  return self._targetCall_targetCall
157 
158 
162  def visit_Assign(self,node):
163  if isinstance(node.value,ast.Call):
164  targetName=ast.unparse(node.targets)
165  valueExpr=ast.unparse(node.value)
166  self._targetCall_targetCall[targetName]=valueExpr
Get all assign nodes from a lib source file 源码Assign节点遍历器
Definition: extractDef.py:146
def visit_Assign(self, node)
Visit an Assign node and record Call-type values 访问Assign节点,记录Call类型的值
Definition: extractDef.py:162
def get_target_call(self)
Return the assign target-to-value dictionary 返回赋值目标到值的字典
Definition: extractDef.py:155
def __init__(self)
Initialize the assign node container 初始化赋值节点容器
Definition: extractDef.py:149
Get prefix and relative path of a source file 获取源码文件路径前缀和相对路径
Definition: extractDef.py:104
def toFormat(self, filePath)
Convert a source file path to package prefix and relative path 将源码文件路径转换为包前缀和相对路径
Definition: extractDef.py:131
def relativePath(self)
Return the relative path of the source file 返回源文件的相对路径
Definition: extractDef.py:124
def prefix(self)
Return the fully qualified prefix of the source file 返回源文件的完整路径前缀
Definition: extractDef.py:116
def __init__(self)
Initialize the prefix and relative path containers 初始化前缀和相对路径容器
Definition: extractDef.py:107
From import statement node visitor From import语句节点遍历器
Definition: extractDef.py:47
def importDict(self)
Return the import dictionary 返回导入字典
Definition: extractDef.py:70
def __init__(self, currentLevel)
Initialize the from-import visitor for a given package level 初始化from-import遍历器,传入当前包层级
Definition: extractDef.py:61
def visit_FunctionDef(self, node)
Definition: extractDef.py:49
def visit_ClassDef(self, node)
Definition: extractDef.py:55
def visit_ImportFrom(self, node)
Visit an ImportFrom node and resolve relative imports 访问ImportFrom节点,解析相对导入
Definition: extractDef.py:77
def visit_AsyncFunctionDef(self, node)
Definition: extractDef.py:52
Function definition node visitor 函数定义节点遍历器
Definition: extractDef.py:22
def visit_FunctionDef(self, node)
Visit a FunctionDef node and record it 访问FunctionDef节点并记录
Definition: extractDef.py:38
def functionNodes(self)
Return collected function definition nodes 返回收集到的函数定义节点
Definition: extractDef.py:31
def __init__(self)
Initialize the function definition node container 初始化函数定义节点容器
Definition: extractDef.py:25