PCART  v1.4
Automated Repair of Python API Parameter Compatibility Issues
getDef.py
Go to the documentation of this file.
1 
10 
11 
12 
13 import ast
14 import io
15 import os
16 import re
17 import tokenize
18 from Path.getPath import *
19 from Extract.extractDef import *
20 from Extract.extractCall import *
21 
22 
23 
28 def parseLibrarySource(source,filename='<unknown>'):
29  replacements={}
30  docstrings=[]
31  python2Tried=False
32  while True:
33  try:
34  try:
35  # Use the memory source for error positions instead of the disk file.
36  # 按内存源码定位语法错误,避免Python 3.9回读磁盘原文。
37  root=ast.parse(source,filename='<unknown>',mode='exec')
38  except SyntaxError:
39  # Use the old grammar to preserve async/await identifiers.
40  # 使用旧语法保留async/await名称,不替换真正的关键字。
41  root=ast.parse(source,filename='<unknown>',mode='exec',feature_version=(3,6))
42  break
43  except SyntaxError as error:
44  error.filename=filename
45  lines=io.StringIO(source).readlines()
46  try:
47  tokens=list(tokenize.generate_tokens(io.StringIO(source).readline))
48  except (tokenize.TokenError,IndentationError):
49  raise error
50 
51  if error.lineno and error.offset and error.lineno<=len(lines):
52  line=lines[error.lineno-1]
53  offset=error.offset-1
54  token=next((it for it in tokens if it.type==tokenize.NAME and
55  it.start==(error.lineno,offset)),None)
56  if token and token.string in ('True','False'):
57  placeholder=f'__pcart_keyword_{len(replacements)}__'
58  while placeholder in source:
59  placeholder+='_'
60  replacements[placeholder]=token.string
61  lines[error.lineno-1]=line[:offset]+placeholder+line[token.end[1]:]
62  source=''.join(lines)
63  continue
64 
65  if isinstance(error,TabError):
66  # Expand code indentation using Python 2's eight-column rule.
67  # 按Python 2的八列规则展开代码缩进,保留多行字符串内容。
68  stringRows={row for token in tokens if token.type==tokenize.STRING
69  for row in range(token.start[0]+1,token.end[0]+1)}
70  for row,line in enumerate(lines,1):
71  if row not in stringRows:
72  prefix=re.match(r'[ \t]*',line).group()
73  lines[row-1]=prefix.expandtabs(8)+line[len(prefix):]
74  converted=''.join(lines)
75  if converted!=source:
76  source=converted
77  continue
78 
79  if 'unicodeescape' in error.msg:
80  # Plain Python 2 docstrings leave Unicode-only escapes literal.
81  # 仅处理Python 2普通文档字符串中的Unicode专用转义。
82  statement=[]
83  for token in tokens:
84  if token.type in (tokenize.NL,tokenize.COMMENT,tokenize.INDENT,tokenize.DEDENT):
85  continue
86  if token.type==tokenize.NEWLINE or token.string==';':
87  if statement[:3]==['from','__future__','import'] and 'unicode_literals' in statement:
88  raise error
89  statement=[]
90  else:
91  statement.append(token.string)
92  for i,token in enumerate(tokens):
93  if (token.type!=tokenize.STRING or not token.string.startswith(('"',"'")) or
94  not token.start[0]<=error.lineno<=token.end[0]):
95  continue
96  previous=i-1
97  while previous>=0 and tokens[previous].type in (tokenize.NL,tokenize.COMMENT):
98  previous-=1
99  following=i+1
100  while following<len(tokens) and tokens[following].type in (tokenize.NL,tokenize.COMMENT):
101  following+=1
102  if ((previous>=0 and tokens[previous].type not in (tokenize.NEWLINE,tokenize.INDENT) and
103  tokens[previous].string!=':') or following>=len(tokens) or
104  tokens[following].type not in (tokenize.NEWLINE,tokenize.ENDMARKER)):
105  continue
106  converted=re.sub(r'(\\+)([uUN])',
107  lambda it: it[1]+('\\' if len(it[1])%2 else '')+it[2],token.string)
108  if converted==token.string:
109  continue
110  startRow,startColumn=token.start
111  endRow,endColumn=token.end
112  source=(''.join(lines[:startRow-1])+lines[startRow-1][:startColumn]+
113  converted+lines[endRow-1][endColumn:]+''.join(lines[endRow:]))
114  docstrings.append((startRow,converted,error))
115  break
116  else:
117  raise error
118  continue
119 
120  if error.msg=='Generator expression must be parenthesized' and error.lineno:
121  brackets=[]
122  for i,token in enumerate(tokens):
123  if token.type==tokenize.NAME and token.string=='for' and brackets:
124  brackets[-1][1]=True
125  if token.type!=tokenize.OP:
126  continue
127  if token.string in '([{':
128  brackets.append([token.string,False,token.start[0]])
129  elif token.string in ')]}':
130  if brackets:
131  brackets.pop()
132  elif (token.string==',' and brackets and brackets[-1][:2]==['(',True] and
133  brackets[-1][2]<=error.lineno<=token.start[0]):
134  nextToken=i+1
135  while nextToken<len(tokens) and tokens[nextToken].type in (tokenize.NL,tokenize.COMMENT):
136  nextToken+=1
137  if nextToken<len(tokens) and tokens[nextToken].string==')':
138  row,column=token.start
139  lines[row-1]=lines[row-1][:column]+lines[row-1][column+1:]
140  source=''.join(lines)
141  break
142  else:
143  raise error
144  continue
145 
146  # Only run the three syntax fixers needed by old library sources.
147  # 仅使用旧库源码所需的三种Python 2语法转换。
148  python2Pattern=r'(?m)^\s*(?:print\s+(?!\‍()|exec\s+(?!\‍()|except\s+[^:\n]+,\s*\w+\s*:)'
149  if not python2Tried and re.search(python2Pattern,source):
150  python2Tried=True
151  try:
152  from lib2to3.refactor import RefactoringTool
153  fixers=['lib2to3.fixes.fix_print','lib2to3.fixes.fix_except','lib2to3.fixes.fix_exec']
154  text=source if source.endswith('\n') else source+'\n'
155  converted=str(RefactoringTool(fixers).refactor_string(text,filename))
156  except Exception:
157  raise error
158  if converted!=source:
159  source=converted
160  continue
161  raise error
162 
163  # Check that every repaired string is a real module, class or function docstring.
164  # 确认修复的字符串确实是模块、类或函数的文档字符串。
165  positions=set()
166  for node in ast.walk(root) if docstrings else ():
167  if isinstance(node,(ast.Module,ast.ClassDef,ast.FunctionDef,ast.AsyncFunctionDef)) and node.body:
168  first=node.body[0]
169  if isinstance(first,ast.Expr) and isinstance(first.value,ast.Constant) and isinstance(first.value.value,str):
170  positions.add((first.value.lineno,ast.get_source_segment(source,first.value)))
171  for row,text,error in docstrings:
172  if (row,text) not in positions:
173  raise error
174 
175  if not replacements:
176  return root
177 
178  # Restore original names so extracted API paths and signatures are unchanged.
179  for node in ast.walk(root):
180  for field,value in ast.iter_fields(node):
181  if isinstance(value,str):
182  for placeholder,original in replacements.items():
183  value=value.replace(placeholder,original)
184  setattr(node,field,value)
185  elif isinstance(value,list):
186  for i,item in enumerate(value):
187  if isinstance(item,str):
188  for placeholder,original in replacements.items():
189  item=item.replace(placeholder,original)
190  value[i]=item
191  return root
192 
193 
194 
195 
200 
201 
205  def __init__(self,codeText,pattern):
206  self._codeText_codeText=codeText
207  self._pattern_pattern=pattern
208  self._result_result=[]
209 
210 
213  def get_result(self):
214  return self._result_result
215 
216 
219  def regex_match(self):
220  obj=re.compile(self._pattern_pattern,re.DOTALL)
221  lst=obj.findall(self._codeText_codeText)
222  if len(lst)>0:
223  #对找到的所有参数字符串进行处理
224  for index in range(0,len(lst)):
225  if lst[index].find('\n')!=-1: #若参数字符串含有换行符'\n'
226  lst[index]=lst[index].replace('\n','') #去掉所有的换行符
227  lst[index]=lst[index].replace(' ','') #去掉所有的空格
228 
229  self._result_result=lst
230  return 1
231  else:
232  self._result_result=[]
233  return 0
234 
235 
236 
237 
242 def getAssign(rootNode,filePath=None):
243  #找出树中所有的模块名
244  importVisitor=Import()
245  try:
246  # Preserve NodeVisitor's depth-first order without recursive calls.
247  nodes=[rootNode]
248  while nodes:
249  node=nodes.pop()
250  if isinstance(node,ast.expr):
251  continue
252  if isinstance(node,(ast.Import,ast.ImportFrom)):
253  importVisitor.visit(node)
254  nodes.extend(reversed(list(ast.iter_child_nodes(node))))
255  except Exception as e:
256  print(f"{filePath or '<unknown>'} import visit failed: {e}")
257  mdNames=importVisitor.get_md_name() #dict
258 
259  #找出所有的Assign节点
260  assignVisitor=AssignVisitor()
261  nodes=[rootNode]
262  while nodes:
263  node=nodes.pop()
264  if isinstance(node,ast.expr):
265  continue
266  if isinstance(node,ast.Assign):
267  assignVisitor.visit(node)
268  nodes.extend(reversed(list(ast.iter_child_nodes(node))))
269  targetCall=assignVisitor.get_target_call()
270 
271  for key,val in targetCall.items():
272  nameParts=val.split('.')
273  if nameParts[0] in targetCall:
274  targetCall[key]=targetCall[nameParts[0]]+'.'+'.'.join(nameParts[1:])
275 
276  for key,val in targetCall.items():
277  nameParts=val.split('.')
278  if nameParts[0] in mdNames:
279  targetCall[key]=(mdNames[nameParts[0]]+'.'+'.'.join(nameParts[1:])).rstrip('.')
280 
281  return targetCall
282 
283 
284 
285 
293 def shortenPath(lst,fileDict,importCache=None): #lst是传入传出参数,保存修正之后的API路径
294  absolutePath=[k for k in fileDict.keys()][0] #/home/zhang/pkg/file.py
295  relativePath=[v for v in fileDict.values()][0] #pkg/file.py
296  normRelative=relativePath.replace('\\','/')
297  normAbsolute=absolutePath.replace('\\','/')
298  pos1=normRelative.rfind('/')
299  if pos1==-1:
300  return
301  relativePath=relativePath[0:pos1] #更新relativatePath
302  pos2=normAbsolute.rfind('/')
303  absolutePath=absolutePath[0:pos2] #更新absolutePath,使其和relativatePath保持一致
304  initPath=f"{absolutePath}/__init__.py"
305  api=lst[0]
306  currentLevel=relativePath.replace('\\','/').split('/')[-1]
307  cacheKey=(initPath,currentLevel)
308  if (importCache is not None and cacheKey in importCache) or os.path.exists(initPath): #优先使用已缓存的导入映射
309  if importCache is not None and cacheKey in importCache:
310  importDict=importCache[cacheKey]
311  else:
312  try:
313  with tokenize.open(initPath) as f:
314  root=parseLibrarySource(f.read(),initPath)
315  except Exception as e:
316  print(f"shortenPath --> ast.parse failed: {e}")
317  return
318  obj=FromImport(currentLevel)
319  obj.visit(root)
320  importDict=obj.importDict
321  if importCache is not None:
322  importCache[cacheKey]=importDict
323  replaceKey1=''
324  replaceVal1=''
325  replaceKey2=''
326  replaceVal2=''
327  packagePrefix=relativePath.replace('\\','/').replace('/','.')+'.'
328  #按API路径前缀查找导入映射,避免遍历所有无关导入
329  explicitKeys=set()
330  wildcardKeys=set()
331  if api.startswith(packagePrefix):
332  candidatePath=api[len(packagePrefix):]
333  if '*' in importDict:
334  wildcardKeys.add('*')
335  while candidatePath:
336  if candidatePath in importDict:
337  explicitKeys.add(candidatePath)
338  candidatePath,separator,_=candidatePath.rpartition('.')
339  if not separator:
340  break
341  wildcardKey=candidatePath+'.*'
342  if wildcardKey in importDict:
343  wildcardKeys.add(wildcardKey)
344  matchingKeys=explicitKeys or wildcardKeys
345  if len(matchingKeys)>1:
346  #多个匹配时,保留导入字典中最后出现的匹配
347  matchingKeys={next(key for key in reversed(importDict) if key in matchingKeys)}
348  for key in matchingKeys:
349  value=importDict[key]
350  if key[-1]=='*':
351  key=packagePrefix+key[:-1]
352  if api.startswith(key):
353  replaceKey1=key
354  replaceVal1=packagePrefix
355  else:
356  key=packagePrefix+key
357  #只替换当前包下完整的导出路径,避免匹配同名前缀或其他模块
358  if api==key or api.startswith(key+'.'):
359  replaceKey2=key
360  replaceVal2=packagePrefix+value
361  if replaceKey2: #优先使用第二种替换方式
362  api=replaceVal2+api[len(replaceKey2):]
363  elif replaceKey1:
364  api=replaceVal1+api[len(replaceKey1):]
365  lst[0]=api
366  shortenPath(lst,{absolutePath:relativePath},importCache)
367 
368 
369 
370 
379 def getExportMap(filePath):
380  exportMap={}
381  for file in filePath:
382  #只处理包初始化文件,函数内部导入和普通源码文件不属于包重导出
383  if os.path.basename(file)!='__init__.py':
384  continue
385 
386  #根据__init__.py路径得到当前导出包名
387  def2format=Def2format()
388  def2format.toFormat(file)
389  package=def2format.prefix.rsplit('.__init__',1)[0]
390  packageParts=package.split('.')
391  try:
392  with tokenize.open(file) as f:
393  root=parseLibrarySource(f.read(),file)
394  except Exception as e:
395  print(f"getExportMap --> ast.parse failed: {e}")
396  continue
397 
398  #只访问模块顶层节点,避免把函数或类中的局部导入识别为包导出
399  for node in root.body:
400  if not isinstance(node,ast.ImportFrom):
401  continue
402 
403  #将绝对导入和相对导入统一还原为完整源码路径
404  if node.level==0:
405  module=node.module or ''
406  else:
407  parentCount=node.level-1
408  if parentCount>len(packageParts):
409  continue
410  moduleParts=packageParts[:len(packageParts)-parentCount]
411  if node.module:
412  moduleParts.extend(node.module.split('.'))
413  module='.'.join(moduleParts)
414 
415  for name in node.names:
416  #星号导入无法静态确定具体导出对象,暂不处理
417  if name.name=='*':
418  continue
419  sourcePath='.'.join(it for it in (module,name.name) if it)
420  publicPath=f"{package}.{name.asname or name.name}"
421  if sourcePath and sourcePath!=publicPath:
422  exportMap.setdefault(sourcePath,set()).add(publicPath)
423  return exportMap
424 
425 
426 
427 
439 def getApiPaths(apiPath,fileDict,importCache=None,exportMap=None):
440  paths=[apiPath]
441  #保留现有父目录__init__.py路径缩短结果
442  shortenedPath=[apiPath]
443  shortenPath(shortenedPath,fileDict,importCache)
444  if shortenedPath[0] not in paths:
445  paths.append(shortenedPath[0])
446 
447  #按需展开多级重导出,paths同时记录已访问路径以防止循环
448  index=0
449  while index<len(paths):
450  currentPath=paths[index]
451  for publicPath in sorted((exportMap or {}).get(currentPath,set())):
452  if publicPath not in paths:
453  paths.append(publicPath)
454  index+=1
455  return paths
456 
457 
458 
459 
469 def getClass(lst,root,prefix,fileDict, pyiFlag=0, importCache=None, exportMap=None): #lst是传入传出参数
470  className=root.name
471  classPath=f"{prefix}.{className}"
472  #类的导出路径同样适用于其构造方法和普通方法
473  classPaths=getApiPaths(classPath,fileDict,importCache,exportMap)
474  flagInit=0
475  flagNew=0
476  flagCall=0
477  #首先抽取当前类中的所有函数
478  for n in ast.iter_child_nodes(root):
479  #if isinstance(n,ast.FunctionDef):
480  # Add the support of extracting AsyncFunctionDef type node -- 2025/5/19
481  if isinstance(n,(ast.FunctionDef, ast.AsyncFunctionDef)):
482  if 'overload' in ast.unparse(n.decorator_list) and not pyiFlag: #非pyi文件中,遇到带有overload装饰器的就跳过
483  continue
484  funcName=n.name
485  arg=ast.unparse(n.args) #从args节点出解析出函数参数
486  arg=arg.replace(' ','') #去掉字符串中的空格
487  try:
488  ret='->'+ast.unparse(n.returns)
489  except:
490  ret=''
491  if funcName=='__init__': #一个class中也可能存在多个init,即class API的重载
492  init=arg
493  flagInit=1
494  elif funcName=='__new__':
495  new=arg
496  flagNew=1
497  elif funcName=='__call__':
498  call=arg
499  flagCall=1
500  else:
501  methodPath=f"{classPath}.{funcName}"
502  methodPaths=getApiPaths(methodPath,fileDict,importCache,exportMap)
503  for currentClassPath in classPaths:
504  publicMethodPath=f"{currentClassPath}.{funcName}"
505  if publicMethodPath not in methodPaths:
506  methodPaths.append(publicMethodPath)
507  for currentMethodPath in methodPaths:
508  lst.append(f"{currentMethodPath}({arg}){ret}")
509 
510  if flagInit==1:
511  para=f"({init})"
512  specialMethod='__init__'
513  elif flagNew==1:#若class中不含init,再看是否有new
514  para=f"({new})"
515  specialMethod='__new__'
516  elif flagCall==1:
517  para=f"({call})"
518  specialMethod='__call__'
519  else: #若类中不含init,new,call,就将类的继承作为类的参数
520  specialMethod=''
521  pattern=fr"class {re.escape(className)}(\‍(.*?):"
522  codeText=ast.unparse(root)
523  R=RegexMatch(codeText,pattern)
524  flag=R.regex_match()
525  if flag==1:
526  args=R.get_result()
527  else:
528  args=['']
529  para=args[0]
530  for currentClassPath in classPaths:
531  if specialMethod:
532  lst.append(f"{currentClassPath}.{specialMethod}{para}")
533  lst.append(f"{currentClassPath}{para}")
534 
535  #然后再判断当前类节点下是否还有嵌套类,有的话就往下递归
536  prefix+=f".{className}" #更新前缀
537  for n in ast.iter_child_nodes(root):
538  if isinstance(n,ast.ClassDef):
539  getClass(lst,n,prefix,fileDict,pyiFlag,importCache,exportMap)
540 
541 
542 
543 
554 def task(codeText,libApi,prefix,fileDict, pyiFlag=0, importCache=None, exportMap=None, rootNode=None): #这里的prefix只到文件名
555  if rootNode is None:
556  try:
557  rootNode=parseLibrarySource(codeText,list(fileDict.keys())[0])
558  except Exception as e:
559  file = list(fileDict.keys())[0]
560  print(f"{file} ast.parse falied: {e}")
561  return
562  for node in ast.iter_child_nodes(rootNode):
563  if isinstance(node, ast.ClassDef): #抽取类内API
564  getClass(libApi,node,prefix,fileDict,pyiFlag,importCache,exportMap)
565 
566  #if isinstance(node,ast.FunctionDef): #再抽取类外的API
567  # Add the support of extracting AsyncFunctionDef type node -- 2025/5/19
568  if isinstance(node,(ast.FunctionDef, ast.AsyncFunctionDef)):
569  if 'overload' in ast.unparse(node.decorator_list) and not pyiFlag: #遇到含overload装饰器的就跳过
570  continue
571  funcName=node.name
572  arg=ast.unparse(node.args)
573  arg=arg.replace(' ','')
574  try:
575  ret='->'+ast.unparse(node.returns)
576  except:
577  ret=''
578  apiPath=f"{prefix}.{funcName}"
579  for currentPath in getApiPaths(apiPath,fileDict,importCache,exportMap):
580  libApi.append(f"{currentPath}({arg}){ret}")
581 
582 
583 
584 
590 def getPublicAliasLine(line,sourceRoot,publicRoot):
591  if not sourceRoot or not publicRoot:
592  return None
593  sourcePrefix=sourceRoot+'.'
594  publicPrefix=publicRoot+'.'
595  assignSourcePrefix='A:'+sourcePrefix
596  assignPublicPrefix='A:'+publicPrefix
597  if line.startswith(sourcePrefix):
598  return publicPrefix+line[len(sourcePrefix):]
599  if line.startswith(assignSourcePrefix):
600  return assignPublicPrefix+line[len(assignSourcePrefix):]
601  return None
602 
603 
604 
605 
612 def writeApiLine(fw,line,sourceRoot='',publicRoot=''):
613  fw.write(f"{line}\n")
614  aliasLine=getPublicAliasLine(line,sourceRoot,publicRoot)
615  if aliasLine and aliasLine!=line:
616  fw.write(f"{aliasLine}\n")
617 
618 
619 
620 
624 def getDefFunction(args):
625  libName, version, libPath=args
626  fileObj=Path('DF')
627  fileObj.getPath(libPath)
628  #filePath是库下所有文件对应的路径
629  filePath=fileObj.path
630  if not os.path.exists(f"LibAPIExtraction/{libName}"):
631  try:
632  os.mkdir(f"LibAPIExtraction/{libName}") #多进程可能同时执行这句,所以这个结构需要修改
633  except:
634  pass
635  f=open(f'LibAPIExtraction/{libName}/{libName}{version}','w',encoding='UTF-8')
636 
637 
638  fileVisitLst=[]
639  importCache={}
640  #在逐文件抽取定义前统一构建重导出映射,避免对每个API重复扫描
641  exportMap=getExportMap(filePath)
642  publicAliasSource=''
643  publicAliasTarget=''
644  if libName=="tensorflow" and os.path.basename(os.path.normpath(libPath))=="tensorflow_core":
645  publicAliasSource="tensorflow_core"
646  publicAliasTarget="tensorflow"
647  for file in filePath:
648  pyLst=[] #保存每个.py文件中的API
649  pyiLst=[] #保存每个.pyi文件中的API
650  def2format=Def2format()
651  def2format.toFormat(file)
652  prefix=def2format.prefix #前缀,包名.文件名
653  relativePath=def2format.relativePath #相对路径,只从包名开始
654  fileDict={file:relativePath}
655  #对于每个file,首先判断一下他是.py文件还是.pyi文件
656  if file[-1]=='y' and file not in fileVisitLst:
657  fileVisitLst.append(file)
658  #对于每一个.py文件,首先看它有没有.pyi,若无,则直接以.py中的API定义为准
659  #若有,则再抽取.pyi中的API,最后保留.pyi和.py的差集
660  pyiFlag=0
661  if file+'i' not in fileVisitLst: #判断.pyi之前是否访问过
662  try:
663  with tokenize.open(file+'i') as fr:
664  codeText=fr.read()
665  task(codeText,pyiLst,prefix,fileDict, 1, importCache,exportMap) #抽取.pyi中的API
666  pyiFlag=1
667  fileVisitLst.append(file+'i')
668  except FileNotFoundError:
669  pass
670 
671  with tokenize.open(file) as fr:
672  try:
673  codeText=fr.read()
674  except Exception as e:
675  print(f"{file} read failed: {e}")
676  continue
677  try:
678  rootNode=parseLibrarySource(codeText,file)
679  except Exception as e:
680  print(f'{file} ast.parse failed: {e}')
681  continue
682  assignDict=getAssign(rootNode,file) #抽取.py中的所有Assign Node
683  f.write('\n'+'-' * 40 + f"{file}" + '-' * 40+'\n')
684  for key,val in assignDict.items():
685  writeApiLine(f,f'A:{prefix}.{key}->{val}',publicAliasSource,publicAliasTarget)
686  #抽取.py中的Definition Node
687  task(codeText,pyLst,prefix,fileDict,0,importCache,exportMap,rootNode=rootNode)
688  pyLst=sorted(set(pyLst)) #按完整定义去重,保留不同签名
689  for it in pyLst:
690  writeApiLine(f,it,publicAliasSource,publicAliasTarget)
691  f.write('\n')
692 
693  if pyiFlag:
694  removeLst=[]
695  for it1 in pyiLst:
696  for it2 in pyLst:
697  if it2.split('(')[0]==it1.split('(')[0]:
698  removeLst.append(it1)
699  break
700  for it in removeLst:
701  pyiLst.remove(it)
702 
703  #此时.pyi中保存的都是内置的API注释
704  pyiLst=sorted(set(pyiLst))
705  f.write('\n'+'-' * 40 + f"{file}"+'i' + '-' * 40+'\n')
706  for it in pyiLst:
707  writeApiLine(f,it,publicAliasSource,publicAliasTarget)
708  f.write('\n')
709 
710  elif file[-1]=='i' and file not in fileVisitLst:
711  fileVisitLst.append(file)
712  if file.rstrip('i') not in fileVisitLst:
713  try:
714  with tokenize.open(file.rstrip('i')) as fr:
715  codeText=fr.read()
716  rootNode=parseLibrarySource(codeText,file.rstrip('i'))
717  task(codeText,pyLst,prefix,fileDict,0,importCache,exportMap,rootNode=rootNode) #抽取.py中的API
718  fileVisitLst.append(file.rstrip('i'))
719  assignDict=getAssign(rootNode,file.rstrip('i'))
720  f.write('\n'+'-' * 40 + f"{file.rstrip('i')}" + '-' * 40+'\n')
721  for key,value in assignDict.items():
722  writeApiLine(f,f'A:{prefix}.{key}->{value}',publicAliasSource,publicAliasTarget)
723  pyLst=sorted(set(pyLst))
724  for it in pyLst:
725  writeApiLine(f,it,publicAliasSource,publicAliasTarget)
726  f.write('\n')
727  except FileNotFoundError:
728  pass
729 
730  with tokenize.open(file) as fr:
731  codeText=fr.read()
732  task(codeText,pyiLst,prefix,fileDict,1,importCache,exportMap) #抽取.pyi中的API
733  removeLst=[]
734  pyiLst=sorted(set(pyiLst))
735  for it1 in pyiLst:
736  for it2 in pyLst:
737  if it2.split('(')[0]==it1.split('(')[0]:
738  removeLst.append(it1)
739  break
740  for it in removeLst:
741  pyiLst.remove(it)
742 
743  f.write('\n'+'-' * 40 + f"{file}" + '-' * 40+'\n')
744  for it in pyiLst:
745  writeApiLine(f,it,publicAliasSource,publicAliasTarget)
746  f.write('\n')
747 
748  f.close()
Regular expression match class 正则表达式匹配类
Definition: getDef.py:199
def regex_match(self)
Perform the regular expression match 执行正则表达式匹配
Definition: getDef.py:219
def get_result(self)
Return the match result 返回匹配结果
Definition: getDef.py:213
def __init__(self, codeText, pattern)
The constructor 构造函数
Definition: getDef.py:205
def writeApiLine(fw, line, sourceRoot='', publicRoot='')
Write one lib API line and its public alias if needed 写出一行库API,并按需写出公开路径别名
Definition: getDef.py:612
def parseLibrarySource(source, filename='< unknown >')
Parse library source with supported legacy syntax in the Python 3.9 runtime 在Python 3....
Definition: getDef.py:28
def getExportMap(filePath)
Build the re-export mapping of library API definitions 构建库API定义的重导出映射
Definition: getDef.py:379
def getClass(lst, root, prefix, fileDict, pyiFlag=0, importCache=None, exportMap=None)
Extract class method definitions from a give class.
Definition: getDef.py:469
def getAssign(rootNode, filePath=None)
Extract all assign node from a .py file's AST 通过AST获取.py文件的Assign语句
Definition: getDef.py:242
def shortenPath(lst, fileDict, importCache=None)
Shorten the API path based on init.py and import alias 通过解析__init__.py和import别名,把源码中的部分API路径缩短
Definition: getDef.py:293
def task(codeText, libApi, prefix, fileDict, pyiFlag=0, importCache=None, exportMap=None, rootNode=None)
Extract all lib API definitions from a source file 抽取库源码API定义任务
Definition: getDef.py:554
def getApiPaths(apiPath, fileDict, importCache=None, exportMap=None)
Get all available paths of a library API definition 获取库API定义的所有可用路径
Definition: getDef.py:439
def getDefFunction(args)
Extract all lib API definitions from a specified version 抽取给定版本的库API定义
Definition: getDef.py:624
def getPublicAliasLine(line, sourceRoot, publicRoot)
Return a public alias line for source-root API lines 为源码根路径API行生成公开路径别名
Definition: getDef.py:590