From 2e27d2dffeb50a2486786a34d2df840d4d6c31ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Garc=C3=ADa=20Garc=C3=ADa?= Date: Mon, 8 Jul 2024 10:02:07 +0200 Subject: [PATCH 1/3] Added Device get_question_by_id, and always fetch template before data to ensure we do not get None --- src/pupil_labs/realtime_api/device.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/pupil_labs/realtime_api/device.py b/src/pupil_labs/realtime_api/device.py index e3a20a5..8f2cd04 100644 --- a/src/pupil_labs/realtime_api/device.py +++ b/src/pupil_labs/realtime_api/device.py @@ -4,12 +4,12 @@ import logging import types import typing as T +from uuid import UUID import aiohttp import numpy as np -import websockets - import pupil_labs # noqa: F401 +import websockets from .base import DeviceBase from .models import ( @@ -182,6 +182,8 @@ async def get_template_data(self, format: TemplateDataFormat = "simple"): format in TemplateDataFormat.__args__ ), f"format should be one of {TemplateDataFormat}" + self.template_definition = await self.get_template() + async with self.session.get(self.api_url(APIPath.TEMPLATE_DATA)) as response: confirmation = await response.json() if response.status != 200: @@ -193,8 +195,9 @@ async def get_template_data(self, format: TemplateDataFormat = "simple"): if format == "api": return result elif format == "simple": - template = await self.get_template() - return template.convert_from_api_to_simple_format(result) + return self.template_definition.convert_from_api_to_simple_format( + result + ) async def post_template_data( self, @@ -248,6 +251,14 @@ async def post_template_data( logger.debug(f"[{self}.get_template_data] Send data's template: {result}") return result + async def get_question_by_id(self, question_id: T.Union[str, UUID]): + self.template_definition = await self.get_template() + + for item in self.template_definition.items: + if str(item.id) == str(question_id): + return item + return None + async def close(self): await self.session.close() self.session = None From 60539d2400749c0932b236c2e3077b3a3d7aa4b1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 8 Jul 2024 08:03:45 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pupil_labs/realtime_api/device.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pupil_labs/realtime_api/device.py b/src/pupil_labs/realtime_api/device.py index 8f2cd04..0563568 100644 --- a/src/pupil_labs/realtime_api/device.py +++ b/src/pupil_labs/realtime_api/device.py @@ -8,9 +8,10 @@ import aiohttp import numpy as np -import pupil_labs # noqa: F401 import websockets +import pupil_labs # noqa: F401 + from .base import DeviceBase from .models import ( APIPath, From f04b8bb956b3fe9d0e1d98cfc7ca3f2b7fa3098a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Garc=C3=ADa=20Garc=C3=ADa?= Date: Mon, 8 Jul 2024 13:38:18 +0200 Subject: [PATCH 3/3] device.get_question fixes --- src/pupil_labs/realtime_api/device.py | 22 ++++++++++++++++---- src/pupil_labs/realtime_api/simple/device.py | 8 +++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/pupil_labs/realtime_api/device.py b/src/pupil_labs/realtime_api/device.py index 0563568..f4b7449 100644 --- a/src/pupil_labs/realtime_api/device.py +++ b/src/pupil_labs/realtime_api/device.py @@ -253,11 +253,25 @@ async def post_template_data( return result async def get_question_by_id(self, question_id: T.Union[str, UUID]): - self.template_definition = await self.get_template() + item = ( + self.template_definition.get_question_by_id(question_id) + if self.template_definition + else None + ) + + if item: + return item + + old_template = self.template_definition if self.template_definition else None + + await self.get_template() + + if ( + self.template_definition is not None + and self.template_definition is not old_template + ): + return self.template_definition.get_question_by_id(question_id) - for item in self.template_definition.items: - if str(item.id) == str(question_id): - return item return None async def close(self): diff --git a/src/pupil_labs/realtime_api/simple/device.py b/src/pupil_labs/realtime_api/simple/device.py index 9872eae..ca1434b 100644 --- a/src/pupil_labs/realtime_api/simple/device.py +++ b/src/pupil_labs/realtime_api/simple/device.py @@ -4,6 +4,7 @@ import threading import typing as T import weakref +from uuid import UUID try: from typing import Literal @@ -243,6 +244,13 @@ async def _post_template_data(): return asyncio.run(_post_template_data()) + def get_question_by_id(self, question_id: T.Union[str, UUID]): + async def _get_question_by_id(): + async with _DeviceAsync.convert_from(self) as control: + return await control.get_question_by_id(question_id) + + return asyncio.run(_get_question_by_id()) + def receive_scene_video_frame( self, timeout_seconds: T.Optional[float] = None ) -> T.Optional[SimpleVideoFrame]: