|
5 | 5 | from fnmatch import fnmatchcase |
6 | 6 | from importlib import import_module |
7 | 7 |
|
| 8 | +from jumpstarter.common.exceptions import MissingDriverError |
| 9 | + |
8 | 10 | logger = logging.getLogger(__name__) |
9 | 11 |
|
10 | 12 |
|
| 13 | +def _format_missing_driver_message(class_path: str) -> str: |
| 14 | + """Format error message depending on whether the class path is a Jumpstarter driver.""" |
| 15 | + # Extract package name from class path (first component) |
| 16 | + package_name = class_path.split(".")[0] |
| 17 | + |
| 18 | + if class_path.startswith("jumpstarter_driver_"): |
| 19 | + return ( |
| 20 | + f"Driver '{class_path}' is not installed.\n\n" |
| 21 | + "This usually indicates a version mismatch between your client and the exporter.\n" |
| 22 | + "Please try to update your client to the latest version and ensure the exporter " |
| 23 | + "has the correct version installed.\n" |
| 24 | + ) |
| 25 | + else: |
| 26 | + return ( |
| 27 | + f"Driver '{class_path}' is not installed.\n\n" |
| 28 | + "Please install the missing module:\n" |
| 29 | + f" pip install {package_name}\n\n" |
| 30 | + "or if using uv:\n" |
| 31 | + f" uv pip install {package_name}" |
| 32 | + ) |
| 33 | + |
| 34 | + |
11 | 35 | def cached_import(module_path, class_name): |
12 | 36 | # Check whether module is loaded and fully initialized. |
13 | 37 | if not ( |
@@ -40,36 +64,9 @@ def import_class(class_path: str, allow: list[str], unsafe: bool): |
40 | 64 | try: |
41 | 65 | return cached_import(module_path, class_name) |
42 | 66 | except ModuleNotFoundError as e: |
43 | | - module_name = str(e).split("'")[1] if "'" in str(e) else str(e).split()[-1] |
44 | | - |
45 | | - is_jumpstarter_driver = unsafe or any(fnmatchcase(class_path, pattern) for pattern in allow) |
46 | | - |
47 | | - if is_jumpstarter_driver: |
48 | | - logger.error( |
49 | | - "Missing Jumpstarter driver module '%s' for class '%s'. " |
50 | | - "This usually indicates a version mismatch between your client and the exporter.", |
51 | | - module_name, |
52 | | - class_path, |
53 | | - ) |
54 | | - raise ConnectionError( |
55 | | - f"Missing Jumpstarter driver module '{module_name}'.\n\n" |
56 | | - "This usually indicates a version mismatch between your client and the exporter.\n" |
57 | | - "Please try to update your client to the latest version and ensure the exporter " |
58 | | - "has the correct version installed.\n" |
59 | | - ) from e |
60 | | - else: |
61 | | - logger.error( |
62 | | - "Missing Python module '%s' while importing '%s'. " |
63 | | - "This module needs to be installed in your environment.", |
64 | | - module_name, |
65 | | - class_path, |
66 | | - ) |
67 | | - raise ConnectionError( |
68 | | - f"Missing Python module '{module_name}'.\n\n" |
69 | | - "Please install the missing module:\n" |
70 | | - f" pip install {module_name}\n\n" |
71 | | - "or if using uv:\n" |
72 | | - f" uv pip install {module_name}" |
73 | | - ) from e |
| 67 | + raise MissingDriverError( |
| 68 | + message=_format_missing_driver_message(class_path), |
| 69 | + class_path=class_path, |
| 70 | + ) from e |
74 | 71 | except AttributeError as e: |
75 | 72 | raise ImportError(f"{module_path} doesn't have specified class {class_name}") from e |
0 commit comments