From 945e1725de46b43675f300d08e8f1d0cda51a258 Mon Sep 17 00:00:00 2001 From: miro Date: Tue, 15 Oct 2024 20:25:31 +0100 Subject: [PATCH] . --- ovos_gui/bus.py | 23 +++++++---------------- ovos_gui/page.py | 9 +++++---- test/unittests/test_bus.py | 25 +++++++++++++++---------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/ovos_gui/bus.py b/ovos_gui/bus.py index de38995..3d5b78b 100644 --- a/ovos_gui/bus.py +++ b/ovos_gui/bus.py @@ -124,18 +124,6 @@ def on_close(self): LOG.info('Closing {}'.format(id(self))) GUIWebsocketHandler.clients.remove(self) - def get_client_pages(self, namespace): - """ - Get a list of client page URLs for the given namespace - @param namespace: Namespace to get pages for - @return: list of page URIs for this GUI Client - """ - client_pages = [] - for page in namespace.pages: - uri = page.get_uri(self.framework) - client_pages.append(uri) - return client_pages - def synchronize(self): """ Upload namespaces, pages and data to the last connected client. @@ -151,11 +139,13 @@ def synchronize(self): "data": [{"skill_id": namespace.skill_id}] }) # Insert pages + # if uri (path) can not be resolved, it might exist client side + # if path doesn't exist in client side, client is responsible for resolving page by namespace/name self.send({"type": "mycroft.gui.list.insert", "namespace": namespace.skill_id, "position": 0, - "data": [{"url": url} for url in - self.get_client_pages(namespace)] + "data": [{"url": page.get_uri(self.framework), "page": page.name} + for page in namespace.pages] }) # Insert data for key, value in namespace.data.items(): @@ -257,12 +247,13 @@ def send_gui_pages(self, pages: List[GuiPage], namespace: str, @param position: position to insert pages at """ framework = self.framework - + # if uri (path) can not be resolved, it might exist client side + # if path doesn't exist in client side, client is responsible for resolving page by namespace/name message = { "type": "mycroft.gui.list.insert", "namespace": namespace, "position": position, - "data": [{"url": page.get_uri(framework)} + "data": [{"url": page.get_uri(framework), "page": page.name} for page in pages] } LOG.debug(f"Showing pages: {message['data']}") diff --git a/ovos_gui/page.py b/ovos_gui/page.py index 5085aec..a9e940a 100644 --- a/ovos_gui/page.py +++ b/ovos_gui/page.py @@ -37,7 +37,7 @@ def get_file_extension(framework: str) -> str: def res_namespace(self): return "system" if self.name.startswith("SYSTEM") else self.namespace - def get_uri(self, framework: str = "qt5") -> str: + def get_uri(self, framework: str = "qt5") -> Optional[str]: """ Get a valid URI for this Page. @param framework: String GUI framework to get resources for (currently only 'qt5') @@ -48,6 +48,7 @@ def get_uri(self, framework: str = "qt5") -> str: LOG.debug(f"Resolved page URI: {path}") if isfile(path): return path - raise FileNotFoundError(f"Unable to resolve resource file for " - f"resource {res_filename} for framework " - f"{framework}") + LOG.warning(f"Unable to resolve resource file for " + f"resource {res_filename} for framework " + f"{framework}") + return None diff --git a/test/unittests/test_bus.py b/test/unittests/test_bus.py index c6ecd41..9b9df37 100644 --- a/test/unittests/test_bus.py +++ b/test/unittests/test_bus.py @@ -80,6 +80,19 @@ def test_on_close(self): # TODO pass + def _get_client_pages(self, namespace) -> List[str]: + """ + Get a list of client page URLs for the given namespace + @param namespace: Namespace to get pages for + @return: list of page URIs for this GUI Client + """ + client_pages = [] + for page in namespace.pages: + # NOTE: in here page is resolved to a full URI (path) + uri = page.get_uri("qt5") + client_pages.append(uri) + return client_pages + def test_get_client_pages(self): from ovos_gui.namespace import Namespace test_namespace = Namespace("test") @@ -94,7 +107,7 @@ def test_get_client_pages(self): # Test no server_url self.handler.ns_manager.gui_file_server = None - pages = self.handler.get_client_pages(test_namespace) + pages = self._get_client_pages(test_namespace) page_1.get_uri.assert_called_once_with(self.handler.framework, None) page_2.get_uri.assert_called_once_with(self.handler.framework, None) self.assertEqual(pages, ["page_1_uri", "page_2_uri"]) @@ -102,19 +115,11 @@ def test_get_client_pages(self): # Test host path mapping test_path = "/test/ovos-gui-file-server" self.handler.ns_manager.gui_file_host_path = test_path - pages = self.handler.get_client_pages(test_namespace) + pages = self._get_client_pages(test_namespace) page_1.get_uri.assert_called_with(self.handler.framework, test_path) page_2.get_uri.assert_called_with(self.handler.framework, test_path) self.assertEqual(pages, ["page_1_uri", "page_2_uri"]) - # Test with server_url - self.handler.ns_manager.gui_file_server = Mock() - self.handler.ns_manager.gui_file_server.url = "server_url" - pages = self.handler.get_client_pages(test_namespace) - page_1.get_uri.assert_called_with(self.handler.framework, "server_url") - page_2.get_uri.assert_called_with(self.handler.framework, "server_url") - self.assertEqual(pages, ["page_1_uri", "page_2_uri"]) - def test_synchronize(self): # TODO pass