blob: d810a13086ec5b2b36ff8b790ad2afc698dd7783 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
"""Utilities for Python JSON Logger"""
### IMPORTS
### ============================================================================
## Future
from __future__ import annotations
## Standard Library
import importlib.util
## Installed
## Application
from .exception import MissingPackageError
### FUNCTIONS
### ============================================================================
def package_is_available(
name: str, *, throw_error: bool = False, extras_name: str | None = None
) -> bool:
"""Determine if the given package is available for import.
Args:
name: Import name of the package to check.
throw_error: Throw an error if the package is unavailable.
extras_name: Extra dependency name to use in `throw_error`'s message.
Raises:
MissingPackageError: When `throw_error` is `True` and the return value would be `False`
Returns:
If the package is available for import.
"""
available = importlib.util.find_spec(name) is not None
if not available and throw_error:
raise MissingPackageError(name, extras_name)
return available
|