Skip to content

Table of Contents

fetch_cpy_src.adapter

Make adaption to downloaded files.

The adaption should be as small as possible. Typically only absolute import statements should be adapted (to relative import and thus be importable).

Although PEP-8 recommends that developer should use absolute imports as possible as one can, but the _cpython module of phy is purely internal used sub-module, which is much more sutible for relative imports.

FileAdapter Objects

class FileAdapter(ABC)

base class of file adapter

adapt

@abstractmethod
def adapt(src_file: Path,
          in_place: bool = True,
          dst_file: Path = None) -> Optional[Path]

apply adaption to file

DirAdapter Objects

class DirAdapter(ABC)

base class of directory adapter

adapt

@abstractmethod
def adapt(src_dir: Path,
          in_place: bool = True,
          dst_dir: Path = None) -> Optional[Path]

apply adaption to directory

ModAbsImportAdapter Objects

class ModAbsImportAdapter(FileAdapter, builtin_ast.NodeVisitor)

The module that copied to phy project should to adapted to relative imports, which ensure this module can be imported by phy components correctly.

Only flat structured module (non-nested one) is supported.

__init__

def __init__()

constructor

visit_ImportFrom

def visit_ImportFrom(node: builtin_ast.ImportFrom)

visit ImportFrom ast node

adapt

def adapt(src_file: Path,
          in_place: bool = True,
          dst_file: Path = None) -> Optional[Path]

Adapt source file.

If inplace=True, the source file would be overwritten; or saved to another path of dst file.

TopLevelScriptImportAdapter Objects

class TopLevelScriptImportAdapter(FileAdapter, builtin_ast.NodeVisitor)

Some ".py" file does not belong to module, and it imports same level file module or package via absolute import.

To adapt such script to be usable in phy project, firstly make the script folder a module by adding __init__.py file, and then change the absolute import statements to relative ones.

BE CAREFUL!!! This adapter should be applied after all sibling scripts downloaded, in order to get all importable symbols.

__init__

def __init__()

constructor

visit_Import

def visit_Import(node: builtin_ast.Import)

visit Import ast node

visit_ImportFrom

def visit_ImportFrom(node: builtin_ast.ImportFrom)

visit ImportFrom ast node

adapt

def adapt(src_file: Path,
          in_place: bool = True,
          dst_file: Path = None) -> Optional[Path]

Adapt source file.

If inplace=True, the source file would be overwritten; or saved to another path of dst file.

AddDunderInitAdapter Objects

class AddDunderInitAdapter(DirAdapter)

add an empty "init.py" to directory to make it a package