| 1 |
|
| 2 |
|
| 3 |
"""Code for dealing with optional dependencies |
| 4 |
|
| 5 |
The functionality in this file is largely based on similar functionality in the |
| 6 |
Pandas library. |
| 7 |
|
| 8 |
Author: G.J.J van den Burg |
| 9 |
Copyright: 2020, The Alan Turing Institute |
| 10 |
License: See LICENSE file. |
| 11 |
|
| 12 |
""" |
| 13 |
|
| 14 |
import importlib |
| 15 |
|
| 16 |
from types import ModuleType |
| 17 |
|
| 18 |
from typing import Dict |
| 19 |
from typing import List |
| 20 |
from typing import NamedTuple |
| 21 |
from typing import Optional |
| 22 |
|
| 23 |
from packaging.version import Version |
| 24 |
|
| 25 |
|
| 26 |
class OptionalDependency(NamedTuple): |
| 27 |
import_name: str |
| 28 |
package_name: str |
| 29 |
min_version: str |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
OPTIONAL_DEPENDENCIES: List[OptionalDependency] = [ |
| 34 |
OptionalDependency("tabview", "tabview", "1.4"), |
| 35 |
OptionalDependency("pandas", "pandas", "0.24.1"), |
| 36 |
OptionalDependency("cchardet", "faust-cchardet", "2.1.18"), |
| 37 |
OptionalDependency("wilderness", "wilderness", "0.1.5"), |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
def import_optional_dependency( |
| 42 |
name: str, raise_on_missing: bool = True |
| 43 |
) -> Optional[ModuleType]: |
| 44 |
""" |
| 45 |
Import an optional dependency. |
| 46 |
|
| 47 |
This function is modelled on a similar function in the Pandas library. |
| 48 |
|
| 49 |
Parameters |
| 50 |
---------- |
| 51 |
name : str |
| 52 |
Name of the module to import |
| 53 |
|
| 54 |
raise_on_missing : bool |
| 55 |
Whether to raise an error when the package is missing or to simply |
| 56 |
return None. |
| 57 |
|
| 58 |
Returns |
| 59 |
------- |
| 60 |
module : module |
| 61 |
The module if importing was successful, None if |
| 62 |
:attr:`raise_on_missing` is False. |
| 63 |
|
| 64 |
Raises |
| 65 |
------ |
| 66 |
ImportError |
| 67 |
When a module can't be imported and :attr:`raise_on_missing` is True. |
| 68 |
|
| 69 |
""" |
| 70 |
msg = ( |
| 71 |
f"\nOptional dependency '{name}' is missing. You can install it using " |
| 72 |
"pip or conda, or you can install CleverCSV with all of its optional " |
| 73 |
"dependencies by running: pip install clevercsv[full]" |
| 74 |
) |
| 75 |
try: |
| 76 |
module = importlib.import_module(name) |
| 77 |
except ImportError: |
| 78 |
if raise_on_missing: |
| 79 |
raise ImportError(msg) from None |
| 80 |
else: |
| 81 |
return None |
| 82 |
|
| 83 |
opt_dependencies: Dict[str, OptionalDependency] = { |
| 84 |
d.import_name: d for d in OPTIONAL_DEPENDENCIES |
| 85 |
} |
| 86 |
|
| 87 |
dependency = opt_dependencies.get(name) |
| 88 |
if dependency is None: |
| 89 |
raise ImportError(f"No known optional dependency with name: {name}") |
| 90 |
|
| 91 |
version = getattr(module, "__version__", None) |
| 92 |
if version is None: |
| 93 |
return module |
| 94 |
|
| 95 |
if Version(version) < Version(dependency.min_version): |
| 96 |
msg = ( |
| 97 |
f"CleverCSV requires version '{dependency.min_version}' or newer " |
| 98 |
f"for optional dependency '{dependency.package_name}'. Please " |
| 99 |
"update the package or install CleverCSV with all its optional " |
| 100 |
"dependencies using: pip install clevercsv[full]" |
| 101 |
) |
| 102 |
raise ImportError(msg) |
| 103 |
|
| 104 |
return module |