Skip to content

Evaluation Structure Generation

eval_structure_generation

Contains all functionality relating to loading and parsing python source code and creating custom import objects representing the dependencies between the modules.

file_filter

FileFilter

Uses a regex pattern to determine whether a file or directory should be excluded.

Paths are matched in forward-slash form, so patterns work on all platforms.

is_excluded(obj: str | Path) -> bool

Returns True if the object matches one of the pre-configured exclusion patterns.

import_filter

ExternalImportFilter

Filters out imports of (some) external modules from the list of all imports. External modules are all modules that are neither the configured module itself nor one of its submodules.

Two possibilities: either all external modules are filtered out, or only those matching given regex patterns.

__init__(exclude_external_libraries: bool, internal_module_name: str, external_exclusions: tuple[str, ...]) -> None

Parameters:

Name Type Description Default
exclude_external_libraries bool

If True, external modules will be filtered out, otherwise this class implements a no-op.

required
internal_module_name str

name of the module that determines which modules are considered internal. A module is considered internal if it is this module itself or one of its submodules; all others are considered external.

required
external_exclusions tuple[str, ...]

regex pattern: all external modules that match it shall be filtered out.

required

filter(imports: Sequence[Import]) -> Sequence[Import]

According to the configuration, imports will be filtered.

Parameters:

Name Type Description Default
imports Sequence[Import]

list of imports to be filtered

required

Returns: filtered list of imports

import_types

AbsoluteImport

Bases: Import

Represents an absolute import.

NamedModule dataclass

Contains an ast module with its name.

Attributes:

Name Type Description
module AST

ast module object

name str

module name

RelativeImport

Bases: Import

Represents a relative import.

For 'from import ', the importee resolves to '<importer's level-th parent>..'. A RelativeImport with both module and name None represents the base package at that level alone, which Python imports in addition to the imported name; bare relative statements emit one such import plus one per alias.

importee_module_calculator

ImporteeModuleCalculator

Adds all parent modules of imported modules if they are not yet part of the modules list.

calculate_importee_modules(imports: Sequence[Import], all_modules: list[str]) -> list[str]

For all imported modules: Calculate parent modules and add them to the list of existing modules if they are not already part of this list. This mainly applies to external dependencies.

Parameters:

Name Type Description Default
imports Sequence[Import]

imports of the scanned source code

required
all_modules list[str]

all modules parsed in the scanned source code

required

Returns:

Type Description
list[str]

all modules extended by parent modules of imported modules

parser

Parser

Parses all files that match given criteria starting at a source path.

__init__(filter: FileFilter, source_root: Path) -> None

Parameters:

Name Type Description Default
filter FileFilter

filter for files and directories that shall not be parsed

required
source_root Path

directory the searched files are located in, as reached. It is used for module names, so a symlinked root keeps its link name, and it keeps the path form it was given in (relative or absolute), so exclusions match the form the caller used.

required

parse(path: Path) -> tuple[list[str], list[NamedModule]]

Reads all python files in the given path and returns list of ast modules with names.

Parameters:

Name Type Description Default
path Path

either to a file or to a directory

required

Returns: list of python modules, one per python file and module name