| 1 | """Central place for install-checker and guards for 'extras' dependencies. |
| 2 | |
| 3 | The ``*_INSTALLED`` constansts reveal wether the dependency is installed with a supported version. |
| 4 | The :py:func:`install_guard` guard function is intended for use inside functions which need specific |
| 5 | extra packages installed. |
| 6 | |
| 7 | Example usage: |
| 8 | |
| 9 | .. code-block:: python |
| 10 | |
| 11 | from rstcheck_core import _extras |
| 12 | |
| 13 | if _extras.SPHINX_INSTALLED: |
| 14 | import sphinx |
| 15 | |
| 16 | |
| 17 | def print_sphinx_version(): |
| 18 | _extras.install_guard("sphinx") |
| 19 | print(sphinx.version_info) |
| 20 | """ |
| 21 | |
| 22 | from __future__ import annotations |
| 23 | |
| 24 | import importlib |
| 25 | import importlib.metadata |
| 26 | import logging |
| 27 | import typing as t |
| 28 | |
| 29 | logger = logging.getLogger(__name__) |
| 30 | |
| 31 | |
| 32 | ExtraDependencies = t.Literal["sphinx", "tomli"] |
| 33 | """List of all dependencies installable through extras.""" |
| 34 | |
| 35 | |
| 36 | class DependencyInfos(t.TypedDict): |
| 37 | """Information about a dependency.""" |
| 38 | |
| 39 | min_version: tuple[int, ...] |
| 40 | extra: str |
| 41 | |
| 42 | |
| 43 | ExtraDependenciesInfos: dict[ExtraDependencies, DependencyInfos] = { |
| 44 | "sphinx": DependencyInfos(min_version=(5, 0), extra="sphinx"), |
| 45 | "tomli": DependencyInfos(min_version=(2, 0), extra="toml"), |
| 46 | } |
| 47 | """Dependency map with their min. supported version and extra by which they can be installed.""" |
| 48 | |
| 49 | |
| 50 | def is_installed_with_supported_version(package: ExtraDependencies) -> bool: |
| 51 | """Check if the package is installed and has the minimum required version. |
| 52 | |
| 53 | :param package: Name of packge to check |
| 54 | :return: Bool if package is installed with supported version |
| 55 | """ |
| 56 | logger.debug( |
| 57 | "Check if package is installed with supported version: '%s'.", |
| 58 | package, |
| 59 | ) |
| 60 | try: |
| 61 | importlib.import_module(package) |
| 62 | except ImportError: |
| 63 | return False |
| 64 | |
| 65 | version: str = importlib.metadata.version(package) |
| 66 | version_tuple = tuple(int(v) for v in version.split(".")[:3]) |
| 67 | |
| 68 | return version_tuple >= ExtraDependenciesInfos[package]["min_version"] |
| 69 | |
| 70 | |
| 71 | SPHINX_INSTALLED = is_installed_with_supported_version("sphinx") |
| 72 | TOMLI_INSTALLED = is_installed_with_supported_version("tomli") |
| 73 | |
| 74 | |
| 75 | ExtraDependenciesInstalled: dict[ExtraDependencies, bool] = { |
| 76 | "sphinx": SPHINX_INSTALLED, |
| 77 | "tomli": TOMLI_INSTALLED, |
| 78 | } |
| 79 | |
| 80 | |
| 81 | def install_guard(package: ExtraDependencies) -> None: |
| 82 | """Guard code that needs the ``package`` installed and throw :py:exc:`ModuleNotFoundError`. |
| 83 | |
| 84 | See example in module docstring. |
| 85 | |
| 86 | :param package: Name of packge to check |
| 87 | :raises ModuleNotFoundError: When the package is not installed. |
| 88 | """ |
| 89 | if ExtraDependenciesInstalled[package] is True: |
| 90 | return |
| 91 | |
| 92 | extra = ExtraDependenciesInfos[package] |
| 93 | |
| 94 | msg = ( |
| 95 | f"No supported version of {package} installed. Install rstcheck with " |
| 96 | f"{extra} extra (rstcheck[{extra}]) or install a supported version of {package} yourself." |
| 97 | ) |
| 98 | raise ModuleNotFoundError(msg) |
| 99 | |
| 100 | |
| 101 | def install_guard_tomli(*, tomllib_imported: bool) -> None: |
| 102 | """Specific version of :py:func:`install_guard` for ``tomli``. |
| 103 | |
| 104 | :param tomllib_imported: If tomllib is imported |
| 105 | :raises ModuleNotFoundError: When ``tomli`` is not installed. |
| 106 | """ |
| 107 | if tomllib_imported or ExtraDependenciesInstalled["tomli"] is True: |
| 108 | return |
| 109 | |
| 110 | extra = ExtraDependenciesInfos["tomli"] |
| 111 | |
| 112 | msg = ( |
| 113 | f"tomllib could not be imported and no supported version of tomli installed. " |
| 114 | f"Install rstcheck with {extra} extra (rstcheck[{extra}]) or install a " |
| 115 | "supported version of tomli yourself." |
| 116 | ) |
| 117 | raise ModuleNotFoundError(msg) |