import os BACKEND_ENV_VAR = "BLE_RETICULUM_SESSION_BACKEND" DEFAULT_BACKEND = "auto" VALID_BACKENDS = {"auto", "cpp", "python"} def _requested_backend(): value = os.environ.get(BACKEND_ENV_VAR, DEFAULT_BACKEND).strip().lower() if value not in VALID_BACKENDS: raise ValueError( f"Invalid {BACKEND_ENV_VAR}={value!r}; expected 'auto', 'cpp', or 'python'" ) return value def _load_cpp_backend(): try: from ble_protocol_core_cpp import ( BLEPeerSessionManager, ConnectionId, ConnectionSnapshot, InputDecision, LocalRole, SessionActionType, ) except ImportError as e: raise ImportError( "C++ BLE session backend is not available. Build or install " "ble_protocol_core_cpp, or set BLE_RETICULUM_SESSION_BACKEND=python." ) from e return ( "cpp", BLEPeerSessionManager, ConnectionId, ConnectionSnapshot, InputDecision, LocalRole, SessionActionType, ) def _load_python_backend(): return ("python", None, None, None, None, None, None) REQUESTED_BACKEND = _requested_backend() if REQUESTED_BACKEND == "python": ( BACKEND, BLEPeerSessionManager, ConnectionId, ConnectionSnapshot, InputDecision, LocalRole, SessionActionType, ) = _load_python_backend() elif REQUESTED_BACKEND == "cpp": ( BACKEND, BLEPeerSessionManager, ConnectionId, ConnectionSnapshot, InputDecision, LocalRole, SessionActionType, ) = _load_cpp_backend() else: try: ( BACKEND, BLEPeerSessionManager, ConnectionId, ConnectionSnapshot, InputDecision, LocalRole, SessionActionType, ) = _load_cpp_backend() except ImportError: ( BACKEND, BLEPeerSessionManager, ConnectionId, ConnectionSnapshot, InputDecision, LocalRole, SessionActionType, ) = _load_python_backend() __all__ = [ "BACKEND", "BACKEND_ENV_VAR", "REQUESTED_BACKEND", "BLEPeerSessionManager", "ConnectionId", "ConnectionSnapshot", "InputDecision", "LocalRole", "SessionActionType", ]