Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Oct 15, 2024
1 parent 36fa296 commit 945e172
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
23 changes: 7 additions & 16 deletions ovos_gui/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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():
Expand Down Expand Up @@ -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']}")
Expand Down
9 changes: 5 additions & 4 deletions ovos_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
25 changes: 15 additions & 10 deletions test/unittests/test_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -94,27 +107,19 @@ 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"])

# 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
Expand Down

0 comments on commit 945e172

Please sign in to comment.