| 32 | |
| 33 | |
| 34 | def _import_via_path(path: Path, namespace: str | None = None) -> str: |
| 35 | if not namespace: |
| 36 | namespace = os.path.basename(path) |
| 37 | |
| 38 | if namespace == '__init__.py': |
| 39 | namespace = path.parent.name |
| 40 | |
| 41 | try: |
| 42 | spec = importlib.util.spec_from_file_location(namespace, path) |
| 43 | if spec and spec.loader: |
| 44 | imported = importlib.util.module_from_spec(spec) |
| 45 | sys.modules[namespace] = imported |
| 46 | spec.loader.exec_module(sys.modules[namespace]) |
| 47 | |
| 48 | return namespace |
| 49 | except Exception as err: |
| 50 | error( |
| 51 | f'Error: {err}', |
| 52 | f'The above error was detected when loading the plugin: {path}', |
| 53 | ) |
| 54 | |
| 55 | try: |
| 56 | del sys.modules[namespace] |
| 57 | except Exception: |
| 58 | pass |
| 59 | |
| 60 | return namespace |
| 61 | |
| 62 | |
| 63 | def load_plugin(path: Path) -> None: |