bitbake: asyncrpc: Check websockets version

Checks that the minimum version of the websockets module is present, and
if not raises an ImportError. This allows the user to get earlier
feedback if using websockets is going to succeed

(Bitbake rev: 330ea6914aad65dc8b34c986c44779820c392f03)

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
joshua Watt
2024-05-02 12:35:16 -06:00
committed by Richard Purdie
parent aff8b07334
commit 23c5058707

View File

@@ -24,6 +24,9 @@ ADDR_TYPE_UNIX = 0
ADDR_TYPE_TCP = 1
ADDR_TYPE_WS = 2
WEBSOCKETS_MIN_VERSION = (9, 1)
def parse_address(addr):
if addr.startswith(UNIX_PREFIX):
return (ADDR_TYPE_UNIX, (addr[len(UNIX_PREFIX) :],))
@@ -39,6 +42,7 @@ def parse_address(addr):
return (ADDR_TYPE_TCP, (host, int(port)))
class AsyncClient(object):
def __init__(
self,
@@ -86,6 +90,24 @@ class AsyncClient(object):
async def connect_websocket(self, uri):
import websockets
try:
version = tuple(
int(v)
for v in websockets.__version__.split(".")[
0 : len(WEBSOCKETS_MIN_VERSION)
]
)
except ValueError:
raise ImportError(
f"Unable to parse websockets version '{websockets.__version__}'"
)
if version < WEBSOCKETS_MIN_VERSION:
min_ver_str = ".".join(str(v) for v in WEBSOCKETS_MIN_VERSION)
raise ImportError(
f"Websockets version {websockets.__version__} is less than minimum required version {min_ver_str}"
)
async def connect_sock():
websocket = await websockets.connect(uri, ping_interval=None)
return WebsocketConnection(websocket, self.timeout)