Skip to content

Sources

Base source

base

BaseSource

Bases: ABC

Base class for data sources.

get_path abstractmethod

get_path() -> Path | None

Returns the path to the directory to be analyzed.

Source code in docstr_health/sources/base.py
 8
 9
10
11
12
13
@abstractmethod
def get_path(self) -> Path | None:
    """
    Returns the path to the directory to be analyzed.
    """
    ...

get_scan_paths

get_scan_paths() -> list[Path] | None

Return specific paths to scan, or None to scan the whole source path.

Source code in docstr_health/sources/base.py
15
16
17
def get_scan_paths(self) -> list[Path] | None:
    """Return specific paths to scan, or None to scan the whole source path."""
    return None

cleanup abstractmethod

cleanup() -> None

Cleans up temporary files (if any).

Source code in docstr_health/sources/base.py
19
20
21
22
@abstractmethod
def cleanup(self) -> None:
    """Cleans up temporary files (if any)."""
    ...

Local source

local

LocalSource

LocalSource(path: Path)

Bases: BaseSource

Local directory.

Source code in docstr_health/sources/local.py
10
11
def __init__(self, path: Path) -> None:
    self._path = path.resolve()

get_path

get_path() -> Path
Source code in docstr_health/sources/local.py
13
14
15
16
17
18
def get_path(self) -> Path:
    if not self._path.exists():
        raise DirectoryNotFoundError(self._path)
    if not self._path.is_dir():
        raise NotADirectoryPathError(self._path)
    return self._path

cleanup

cleanup() -> None
Source code in docstr_health/sources/local.py
20
21
def cleanup(self) -> None:
    pass

Git repository source

git_repo

GitRepositorySource

GitRepositorySource(repo_url: str)

Bases: BaseSource

Remote git repository.

Source code in docstr_health/sources/git_repo.py
14
15
16
def __init__(self, repo_url: str) -> None:
    self._repo_url = repo_url
    self.check_git_installed()

cache_path property

cache_path: Path

check_git_installed staticmethod

check_git_installed()
Source code in docstr_health/sources/git_repo.py
18
19
20
21
@staticmethod
def check_git_installed():
    if shutil.which("git") is None:
        raise GitNotInstalledError()

get_from_cache

get_from_cache() -> Path | None
Source code in docstr_health/sources/git_repo.py
28
29
30
31
def get_from_cache(self) -> Path | None:
    if self.cache_path.is_dir() and any(self.cache_path.iterdir()):
        return self.cache_path
    return None

get_from_repo

get_from_repo()
Source code in docstr_health/sources/git_repo.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def get_from_repo(self):
    subprocess.run(
        [
            "git",
            "clone",
            "--depth",
            "1",
            self._repo_url,
            str(self.cache_path),
        ],
        check=True,
        capture_output=True,
        text=True,
    )
    return self.cache_path

get_path

get_path() -> Path | None
Source code in docstr_health/sources/git_repo.py
49
50
51
52
53
def get_path(self) -> Path | None:
    cached = self.get_from_cache()
    if cached is not None:
        return cached
    return self.get_from_repo()

cleanup

cleanup() -> None
Source code in docstr_health/sources/git_repo.py
55
56
57
58
59
60
61
62
63
64
def cleanup(self) -> None:
    base = config.get_cache_dir()
    try:
        self.cache_path.relative_to(base)
    except ValueError as e:
        logger.warning(e)
        return

    if not self.cache_path.is_dir():
        return

PyPI package source

pypi_package

PyPiPackageSource

PyPiPackageSource(package_url: str)

Bases: BaseSource

Remote PyPi package.

Source code in docstr_health/sources/pypi_package.py
14
15
def __init__(self, package_url: str) -> None:
    self._package_url = package_url

cache_path property

cache_path: Path

get_download_method

get_download_method() -> list[str]
Source code in docstr_health/sources/pypi_package.py
21
22
23
24
25
26
27
28
def get_download_method(self) -> list[str]:
    if config.parameters["use_uv"] and shutil.which("uv"):
        return ["uv", "pip", "install"]

    if config.parameters["use_uv"]:
        logger.warning("uv not found, falling back to pip")

    return ["pip", "install"]

get_from_cache

get_from_cache() -> Path | None
Source code in docstr_health/sources/pypi_package.py
34
35
36
37
def get_from_cache(self) -> Path | None:
    if self.cache_path.is_dir() and any(self.cache_path.iterdir()):
        return self.cache_path
    return None

get_from_package

get_from_package()
Source code in docstr_health/sources/pypi_package.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def get_from_package(self):
    download_method = self.get_download_method()
    subprocess.run(
        [
            *download_method,
            self._package_url,
            "--target",
            str(self.cache_path),
            "--no-deps",
        ],
        check=True,
        capture_output=True,
        text=True,
    )
    return self.cache_path

get_path

get_path() -> Path | None
Source code in docstr_health/sources/pypi_package.py
55
56
57
58
59
def get_path(self) -> Path | None:
    cached = self.get_from_cache()
    if cached is not None:
        return cached
    return self.get_from_package()

get_scan_paths

get_scan_paths() -> list[Path] | None
Source code in docstr_health/sources/pypi_package.py
61
62
63
64
65
66
67
68
69
def get_scan_paths(self) -> list[Path] | None:
    package_path = self.get_path()
    if package_path is None or not package_path.is_dir():
        return None

    top_levels = self._get_top_level_paths(package_path)
    if top_levels:
        return top_levels
    return None

cleanup

cleanup() -> None
Source code in docstr_health/sources/pypi_package.py
 98
 99
100
101
102
103
104
105
106
107
108
109
def cleanup(self) -> None:
    base = config.get_cache_dir()
    try:
        self.cache_path.relative_to(base)
    except ValueError as e:
        logger.warning(e)
        return

    if not self.cache_path.is_dir():
        return

    shutil.rmtree(self.cache_path)

Factory

sources

get_repository_source

get_repository_source(settings, args) -> BaseSource
Source code in docstr_health/sources/__init__.py
 7
 8
 9
10
11
12
def get_repository_source(settings, args) -> BaseSource:
    if settings.repo_url:
        return GitRepositorySource(settings.repo_url)
    if settings.pypi_package:
        return PyPiPackageSource(settings.pypi_package)
    return LocalSource(args.project_path)