Stolen approximately from django. Import a dotted module path and return the attribute/class designated by the last name in the path. Raise ImportError if the import fails.
(dotted_path: str)
| 120 | |
| 121 | |
| 122 | def import_string(dotted_path: str) -> Any: |
| 123 | """ |
| 124 | Stolen approximately from django. Import a dotted module path and return the attribute/class designated by the |
| 125 | last name in the path. Raise ImportError if the import fails. |
| 126 | """ |
| 127 | from importlib import import_module |
| 128 | |
| 129 | try: |
| 130 | module_path, class_name = dotted_path.strip(' ').rsplit('.', 1) |
| 131 | except ValueError as e: |
| 132 | raise ImportError(f'"{dotted_path}" doesn\'t look like a module path') from e |
| 133 | |
| 134 | module = import_module(module_path) |
| 135 | try: |
| 136 | return getattr(module, class_name) |
| 137 | except AttributeError as e: |
| 138 | raise ImportError(f'Module "{module_path}" does not define a "{class_name}" attribute') from e |
| 139 | |
| 140 | |
| 141 | def truncate(v: Union[str], *, max_len: int = 80) -> str: |