-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy path_optional.py
104 lines (79 loc) · 2.76 KB
/
_optional.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
"""Code for dealing with optional dependencies
The functionality in this file is largely based on similar functionality in the
Pandas library.
Author: G.J.J van den Burg
Copyright: 2020, The Alan Turing Institute
License: See LICENSE file.
"""
import importlib
from types import ModuleType
from typing import Dict
from typing import List
from typing import NamedTuple
from typing import Optional
from packaging.version import Version
class OptionalDependency(NamedTuple):
import_name: str
package_name: str
min_version: str
# update this when changing setup.py
OPTIONAL_DEPENDENCIES: List[OptionalDependency] = [
OptionalDependency("tabview", "tabview", "1.4"),
OptionalDependency("pandas", "pandas", "0.24.1"),
OptionalDependency("cchardet", "faust-cchardet", "2.1.18"),
OptionalDependency("wilderness", "wilderness", "0.1.5"),
]
def import_optional_dependency(
name: str, raise_on_missing: bool = True
) -> Optional[ModuleType]:
"""
Import an optional dependency.
This function is modelled on a similar function in the Pandas library.
Parameters
----------
name : str
Name of the module to import
raise_on_missing : bool
Whether to raise an error when the package is missing or to simply
return None.
Returns
-------
module : module
The module if importing was successful, None if
:attr:`raise_on_missing` is False.
Raises
------
ImportError
When a module can't be imported and :attr:`raise_on_missing` is True.
"""
msg = (
f"\nOptional dependency '{name}' is missing. You can install it using "
"pip or conda, or you can install CleverCSV with all of its optional "
"dependencies by running: pip install clevercsv[full]"
)
try:
module = importlib.import_module(name)
except ImportError:
if raise_on_missing:
raise ImportError(msg) from None
else:
return None
opt_dependencies: Dict[str, OptionalDependency] = {
d.import_name: d for d in OPTIONAL_DEPENDENCIES
}
dependency = opt_dependencies.get(name)
if dependency is None:
raise ImportError(f"No known optional dependency with name: {name}")
version = getattr(module, "__version__", None)
if version is None:
return module
if Version(version) < Version(dependency.min_version):
msg = (
f"CleverCSV requires version '{dependency.min_version}' or newer "
f"for optional dependency '{dependency.package_name}'. Please "
"update the package or install CleverCSV with all its optional "
"dependencies using: pip install clevercsv[full]"
)
raise ImportError(msg)
return module