PCART  v1.4
Automated Repair of Python API Parameter Compatibility Issues
Classes | Functions
getDef Namespace Reference

Extract API definitions from lib source code. More...

Classes

class  RegexMatch
 Regular expression match class 正则表达式匹配类 More...
 

Functions

def getApiPaths (apiPath, fileDict, importCache=None, exportMap=None)
 Get all available paths of a library API definition 获取库API定义的所有可用路径 More...
 
def getAssign (root_node, filePath=None)
 Extract all assign node from a .py file's AST 通过AST获取.py文件的Assign语句 More...
 
def getClass (lst, root, prefix, fileDict, pyiFlag=0, importCache=None, exportMap=None)
 Extract class method definitions from a give class. More...
 
def getDefFunction (args)
 Extract all lib API definitions from a specified version 抽取给定版本的库API定义 More...
 
def getExportMap (filePath)
 Build the re-export mapping of library API definitions 构建库API定义的重导出映射 More...
 
def getPublicAliasLine (line, sourceRoot, publicRoot)
 Return a public alias line for source-root API lines 为源码根路径API行生成公开路径别名 More...
 
def parseLibrarySource (source, filename='< unknown >')
 Parse library source with supported legacy syntax in the Python 3.9 runtime 在Python 3.9环境中解析含有受支持旧语法的库源码 More...
 
def shortenPath (lst, fileDict, importCache=None)
 Shorten the API path based on init.py and import alias 通过解析__init__.py和import别名,把源码中的部分API路径缩短 More...
 
def task (codeText, libApi, prefix, fileDict, pyiFlag=0, importCache=None, exportMap=None)
 Extract all lib API definitions from a source file 抽取库源码API定义任务 More...
 
def writeApiLine (fw, line, sourceRoot='', publicRoot='')
 Write one lib API line and its public alias if needed 写出一行库API,并按需写出公开路径别名 More...
 

Detailed Description

Extract API definitions from lib source code.

Extracts API definitions from library source files by parsing AST for class definitions, function definitions, and init.py assignment aliases. Handles .py/.pyi deduplication and API path shortening via init.py imports. 通过解析AST提取库源码中的类定义、函数定义和__init__.py赋值别名。处理.py/.pyi 去重,通过__init__.py导入缩短API路径。

Function Documentation

◆ getApiPaths()

def getDef.getApiPaths (   apiPath,
  fileDict,
  importCache = None,
  exportMap = None 
)

Get all available paths of a library API definition 获取库API定义的所有可用路径

Preserve the original path, reuse shortenPath for parent package aliases, and expand transitive package re-exports on demand. 保留原始路径,复用shortenPath处理父包别名,并按需展开多级包重导出路径。

Parameters
apiPathThe original fully qualified API path
fileDictThe absolute and relative paths of the source file
importCacheCache of init.py import mappings used by shortenPath
exportMapReverse mapping from definition paths to package export paths
Returns
A list containing the original path and all resolved alias paths

Definition at line 414 of file getDef.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAssign()

def getDef.getAssign (   root_node,
  filePath = None 
)

Extract all assign node from a .py file's AST 通过AST获取.py文件的Assign语句

Parameters
root_nodeThe ast node of the .py file
filePathSource filename for diagnostics

Definition at line 242 of file getDef.py.

Here is the caller graph for this function:

◆ getClass()

def getDef.getClass (   lst,
  root,
  prefix,
  fileDict,
  pyiFlag = 0,
  importCache = None,
  exportMap = None 
)

Extract class method definitions from a give class.

抽取类中方法定义。递归访问类中所有节点,主要解决嵌套类的问题

Parameters
lstList of lib API definitions extracted from a lib source file
rootThe class type ast node
prefixThe fully qualified name of a source file. For example, the prefix for lib/a/b/c.py is lib.a.b.c.
fileDictA file with a dictionary format {absolute path of the file: relative path of the file}. The relative path begins with the lib name, separated by "/", e.g., lib/a/b/c.py
pyiFlagA flag denotes whether the Python source file is .pyi file.
importCacheCache of init.py import mappings used to shorten API paths.
exportMapReverse mapping from definition paths to package re-export paths.

Definition at line 444 of file getDef.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getDefFunction()

def getDef.getDefFunction (   args)

Extract all lib API definitions from a specified version 抽取给定版本的库API定义

Parameters
argsThe arguments include the lib name, lib version, and the lib path

Definition at line 597 of file getDef.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getExportMap()

def getDef.getExportMap (   filePath)

Build the re-export mapping of library API definitions 构建库API定义的重导出映射

Scan module-level FromImport nodes in all init.py files and map source definition paths to their package export paths. 扫描所有__init__.py中的模块级FromImport节点,建立源码定义路径到包导出路径的映射。

Parameters
filePathAll Python source files under the library package
Returns
A dictionary mapping source definition paths to package export paths

Definition at line 354 of file getDef.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getPublicAliasLine()

def getDef.getPublicAliasLine (   line,
  sourceRoot,
  publicRoot 
)

Return a public alias line for source-root API lines 为源码根路径API行生成公开路径别名

Parameters
lineA lib API output line
sourceRootThe source package root name, e.g., tensorflow_core
publicRootThe public package root name, e.g., tensorflow

Definition at line 563 of file getDef.py.

Here is the caller graph for this function:

◆ parseLibrarySource()

def getDef.parseLibrarySource (   source,
  filename = '<unknown>' 
)

Parse library source with supported legacy syntax in the Python 3.9 runtime 在Python 3.9环境中解析含有受支持旧语法的库源码

Parameters
sourceLibrary source code
filenameSource filename for syntax errors
Returns
Parsed AST with the original names restored

Definition at line 28 of file getDef.py.

Here is the caller graph for this function:

◆ shortenPath()

def getDef.shortenPath (   lst,
  fileDict,
  importCache = None 
)

Shorten the API path based on init.py and import alias 通过解析__init__.py和import别名,把源码中的部分API路径缩短

缩短API路径可能会将不同文件中的API还原成相同的形式,比如A.b.f,A.c.f都还原成A.f

Parameters
lstAn API path. Initially, lst is the fully qualified API name.
fileDictA file with a dictionary format {absolute path of the file: relative path of the file}. The relative path begins with the lib name, separated by "/", e.g., lib/a/b/c.py
importCacheCache of init.py import mappings, keyed by (init path, current level).

Definition at line 289 of file getDef.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ task()

def getDef.task (   codeText,
  libApi,
  prefix,
  fileDict,
  pyiFlag = 0,
  importCache = None,
  exportMap = None 
)

Extract all lib API definitions from a source file 抽取库源码API定义任务

Parameters
codeTextThe code text of a source file, read by f.read()
libApiList of lib API definitions extracted from a lib source file
prefixThe fully qualified name of a source file. For example, the prefix for lib/a/b/c.py is lib.a.b.c.
fileDictA file with a dictionary format {absolute path of the file: relative path of the file}. The relative path begins with the lib name, separated by "/", e.g., lib/a/b/c.py
pyiFlagA flag denotes whether the Python source file is .pyi file.
importCacheCache of init.py import mappings used to shorten API paths.
exportMapReverse mapping from definition paths to package re-export paths.

Definition at line 528 of file getDef.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeApiLine()

def getDef.writeApiLine (   fw,
  line,
  sourceRoot = '',
  publicRoot = '' 
)

Write one lib API line and its public alias if needed 写出一行库API,并按需写出公开路径别名

Parameters
fwOutput file object
lineA lib API output line
sourceRootThe source package root name
publicRootThe public package root name

Definition at line 585 of file getDef.py.

Here is the call graph for this function:
Here is the caller graph for this function: