-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_import.py
More file actions
88 lines (75 loc) · 2.57 KB
/
Copy pathdynamic_import.py
File metadata and controls
88 lines (75 loc) · 2.57 KB
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
"""
Dynamic import utilities for optional dependencies.
"""
import importlib
import logging
from typing import Dict, Any, Callable, Optional
logger = logging.getLogger(__name__)
class LazyModule:
"""Lazy module loader that only imports when accessed"""
def __init__(self, module_name: str, error_message: Optional[str] = None):
"""
Initialize lazy module loader
Args:
module_name: Name of the module to import
error_message: Custom error message on import failure
"""
self.module_name = module_name
self.error_message = error_message
self._module = None
def get_module(self) -> Any:
"""
Get the module, importing it if necessary
Returns:
The imported module
Raises:
ImportError: If the module cannot be imported
"""
if self._module is None:
try:
self._module = importlib.import_module(self.module_name)
except ImportError as e:
msg = self.error_message or f"Could not import {self.module_name}: {e}"
raise ImportError(msg) from e
return self._module
# Dictionary of lazy module loaders
modules: Dict[str, LazyModule] = {
"boto3": LazyModule(
"boto3",
"boto3 is required for S3 storage. Install with 'pip install boto3'"
),
"botocore": LazyModule(
"botocore",
"botocore is required for S3 storage. Install with 'pip install boto3'"
),
"google.cloud.storage": LazyModule(
"google.cloud.storage",
"google-cloud-storage is required for GCS storage. Install with 'pip install google-cloud-storage'"
),
"extract_zip": LazyModule(
"extract_zip",
"extract-zip is required for ZIP handling. Install with 'pip install extract-zip'"
),
"archiver": LazyModule(
"archiver",
"archiver is required for ZIP handling. Install with 'pip install archiver'"
),
"aioredis": LazyModule(
"redis.asyncio",
"Redis is required for Redis storage. Install with 'pip install redis'"
),
}
# Convenience accessors for modules
boto3 = modules["boto3"]
botocore = modules["botocore"]
google_cloud_storage = modules["google.cloud.storage"]
redis_module = modules["aioredis"]
def import_module(module_name: str) -> Any:
"""
Import a module dynamically
Args:
module_name: Name of the module to import
Returns:
The imported module
"""
return importlib.import_module(module_name)