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 
51  def __init__(self, currentLevel):
52  self._importDict_importDict={}
53  self._currentLevel_currentLevel=currentLevel
54 
55 
59  @property
60  def importDict(self):
61  return self._importDict_importDict
62 
63 
67  def visit_ImportFrom(self, node):
68  if node.module is not None:
69  module=node.module
70  if node.level==0:#若是绝对导入,需考虑层级
71  tempLst=module.split('.')
72  if len(tempLst)==1:
73  module=''
74  elif self._currentLevel_currentLevel in tempLst:
75  index=tempLst.index(self._currentLevel_currentLevel)
76  module='.'.join(tempLst[index+1:])
77 
78  lst=[{'name':name.name,'alias':name.asname} for name in node.names] #可能会import个多个,from A import a,b,c
79  for dic in lst: #lst中每个元素都是字典
80  key=module+'.'+dic['name'] #dic['name']可能是*
81  key=key.lstrip('.')
82  if dic['alias']:
83  self._importDict_importDict[key]=dic['alias']
84  else:
85  self._importDict_importDict[key]=dic['name']
86 
87 
88 
89 
94 class Def2format:
95 
97  def __init__(self):
98  self._prefix_prefix=''
99  self._relativePath_relativePath='' #记录包的相对路径,例如numpy/core/func.py
100 
101 
105  @property
106  def prefix(self):
107  return self._prefix_prefix
108 
109 
113  @property
114  def relativePath(self):
115  return self._relativePath_relativePath
116 
117 
121  def toFormat(self,filePath):
122  s=filePath.rsplit(f"site-packages{os.sep}", 1)[-1]
123  self._relativePath_relativePath=s
124  s=s.replace('\\','.').replace('/','.')
125  pos=s.rfind('.')
126  s=s[0:pos]
127  self._prefix_prefix=s.replace('\\','.').replace('/','.')
128 
129 
130 
131 
136 class AssignVisitor(ast.NodeVisitor):
137 
139  def __init__(self):
140  self._targetCall_targetCall={}
141 
142 
145  def get_target_call(self):
146  return self._targetCall_targetCall
147 
148 
152  def visit_Assign(self,node):
153  if isinstance(node.value,ast.Call):
154  targetName=ast.unparse(node.targets)
155  valueExpr=ast.unparse(node.value)
156  self._targetCall_targetCall[targetName]=valueExpr
Get all assign nodes from a lib source file 源码Assign节点遍历器
Definition: extractDef.py:136
def visit_Assign(self, node)
Visit an Assign node and record Call-type values 访问Assign节点,记录Call类型的值
Definition: extractDef.py:152
def get_target_call(self)
Return the assign target-to-value dictionary 返回赋值目标到值的字典
Definition: extractDef.py:145
def __init__(self)
Initialize the assign node container 初始化赋值节点容器
Definition: extractDef.py:139
Get prefix and relative path of a source file 获取源码文件路径前缀和相对路径
Definition: extractDef.py:94
def toFormat(self, filePath)
Convert a source file path to package prefix and relative path 将源码文件路径转换为包前缀和相对路径
Definition: extractDef.py:121
def relativePath(self)
Return the relative path of the source file 返回源文件的相对路径
Definition: extractDef.py:114
def prefix(self)
Return the fully qualified prefix of the source file 返回源文件的完整路径前缀
Definition: extractDef.py:106
def __init__(self)
Initialize the prefix and relative path containers 初始化前缀和相对路径容器
Definition: extractDef.py:97
From import statement node visitor From import语句节点遍历器
Definition: extractDef.py:47
def importDict(self)
Return the import dictionary 返回导入字典
Definition: extractDef.py:60
def __init__(self, currentLevel)
Initialize the from-import visitor for a given package level 初始化from-import遍历器,传入当前包层级
Definition: extractDef.py:51
def visit_ImportFrom(self, node)
Visit an ImportFrom node and resolve relative imports 访问ImportFrom节点,解析相对导入
Definition: extractDef.py:67
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