Skip to content

debug

Debugging utilities.

Environment dataclass ¤

Dataclass to store environment information.

Source code in src/pytkdocs/debug.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@dataclass
class Environment:
    """Dataclass to store environment information."""

    interpreter_name: str
    """Python interpreter name."""
    interpreter_version: str
    """Python interpreter version."""
    interpreter_path: str
    """Path to Python executable."""
    platform: str
    """Operating System."""
    packages: list[Package]
    """Installed packages."""
    variables: list[Variable]
    """Environment variables."""

interpreter_name instance-attribute ¤

Python interpreter name.

interpreter_path instance-attribute ¤

Path to Python executable.

interpreter_version instance-attribute ¤

Python interpreter version.

packages instance-attribute ¤

Installed packages.

platform instance-attribute ¤

Operating System.

variables instance-attribute ¤

Environment variables.

Package dataclass ¤

Dataclass describing a Python package.

Source code in src/pytkdocs/debug.py
22
23
24
25
26
27
28
29
@dataclass
class Package:
    """Dataclass describing a Python package."""

    name: str
    """Package name."""
    version: str
    """Package version."""

name instance-attribute ¤

Package name.

version instance-attribute ¤

Package version.

Variable dataclass ¤

Dataclass describing an environment variable.

Source code in src/pytkdocs/debug.py
12
13
14
15
16
17
18
19
@dataclass
class Variable:
    """Dataclass describing an environment variable."""

    name: str
    """Variable name."""
    value: str
    """Variable value."""

name instance-attribute ¤

Variable name.

value instance-attribute ¤

Variable value.

get_debug_info() ¤

Get debug/environment information.

Returns:

Type Description
Environment

Environment information.

Source code in src/pytkdocs/debug.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def get_debug_info() -> Environment:
    """Get debug/environment information.

    Returns:
        Environment information.
    """
    py_name, py_version = _interpreter_name_version()
    packages = ["pytkdocs"]
    variables = ["PYTHONPATH", *[var for var in os.environ if var.startswith("PYTKDOCS")]]
    return Environment(
        interpreter_name=py_name,
        interpreter_version=py_version,
        interpreter_path=sys.executable,
        platform=platform.platform(),
        variables=[Variable(var, val) for var in variables if (val := os.getenv(var))],
        packages=[Package(pkg, get_version(pkg)) for pkg in packages],
    )

get_version(dist='pytkdocs') ¤

Get version of the given distribution.

Parameters:

Name Type Description Default
dist str

A distribution name.

'pytkdocs'

Returns:

Type Description
str

A version number.

Source code in src/pytkdocs/debug.py
61
62
63
64
65
66
67
68
69
70
71
72
73
def get_version(dist: str = "pytkdocs") -> str:
    """Get version of the given distribution.

    Parameters:
        dist: A distribution name.

    Returns:
        A version number.
    """
    try:
        return metadata.version(dist)
    except metadata.PackageNotFoundError:
        return "0.0.0"

print_debug_info() ¤

Print debug/environment information.

Source code in src/pytkdocs/debug.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
def print_debug_info() -> None:
    """Print debug/environment information."""
    info = get_debug_info()
    print(f"- __System__: {info.platform}")
    print(f"- __Python__: {info.interpreter_name} {info.interpreter_version} ({info.interpreter_path})")
    print("- __Environment variables__:")
    for var in info.variables:
        print(f"  - `{var.name}`: `{var.value}`")
    print("- __Installed packages__:")
    for pkg in info.packages:
        print(f"  - `{pkg.name}` v{pkg.version}")