| 46 | |
| 47 | |
| 48 | def load_backend_module(backend_name, backend_dict): |
| 49 | if backend_name not in backend_dict: |
| 50 | raise ValueError(f"Backend '{backend_name}' not found.") |
| 51 | |
| 52 | module_path = backend_dict[backend_name] |
| 53 | |
| 54 | mod_dir = os.path.dirname(module_path) |
| 55 | |
| 56 | sys.path.insert(0, mod_dir) |
| 57 | |
| 58 | # If it's a package (directory), add its parent directory to sys.path |
| 59 | if os.path.isdir(module_path): |
| 60 | module_path = os.path.join(module_path, "__init__.py") |
| 61 | |
| 62 | spec = importlib.util.spec_from_file_location(backend_name, module_path) |
| 63 | module = importlib.util.module_from_spec(spec) |
| 64 | spec.loader.exec_module(module) |
| 65 | |
| 66 | if mod_dir in sys.path: |
| 67 | sys.path.remove(mod_dir) |
| 68 | |
| 69 | log.info(f"Loaded backend: {module}") |
| 70 | |
| 71 | return module |
| 72 | |
| 73 | |
| 74 | def start_backend(): |