Skip to content

Commit

Permalink
refactor!:deprecate QML upload from bus
Browse files Browse the repository at this point in the history
never worked right, causes more issues than it helps
  • Loading branch information
JarbasAl committed Sep 18, 2024
1 parent 311ec46 commit 8d49159
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 78 deletions.
11 changes: 2 additions & 9 deletions ovos_gui/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,9 @@ def get_client_pages(self, namespace):
@return: list of page URIs for this GUI Client
"""
client_pages = []
server_url = self.ns_manager.gui_file_server.url if \
self.ns_manager.gui_file_server else \
self.ns_manager.gui_file_host_path
for page in namespace.pages:
uri = page.get_uri(self.framework, server_url)
uri = page.get_uri(self.framework)
client_pages.append(uri)

return client_pages

def synchronize(self):
Expand Down Expand Up @@ -260,16 +256,13 @@ def send_gui_pages(self, pages: List[GuiPage], namespace: str,
@param namespace: namespace to put GuiPages in
@param position: position to insert pages at
"""
server_url = self.ns_manager.gui_file_server.url if \
self.ns_manager.gui_file_server else \
self.ns_manager.gui_file_host_path
framework = self.framework

message = {
"type": "mycroft.gui.list.insert",
"namespace": namespace,
"position": position,
"data": [{"url": page.get_uri(framework, server_url)}
"data": [{"url": page.get_uri(framework)}
for page in pages]
}
LOG.debug(f"Showing pages: {message['data']}")
Expand Down
63 changes: 12 additions & 51 deletions ovos_gui/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def _validate_page_message(message: Message) -> bool:
@returns: True if request is valid, else False
"""
valid = (
"page" in message.data
"page_names" in message.data
and "__from" in message.data
and isinstance(message.data["page"], list)
and isinstance(message.data["page_names"], list)
)
if not valid:
if message.msg_type == "gui.page.show":
Expand Down Expand Up @@ -295,7 +295,7 @@ def load_pages(self, pages: List[GuiPage], show_index: int = 0):
target_page = pages[show_index]

for page in pages:
if page.id not in [p.id for p in self.pages]:
if page.name not in [p.name for p in self.pages]:
new_pages.append(page)

self.pages.extend(new_pages)
Expand Down Expand Up @@ -334,7 +334,7 @@ def focus_page(self, page):
# set the index of the page in the self.pages list
page_index = None
for i, p in enumerate(self.pages):
if p.id == page.id:
if p.name == page.name:
# save page index
page_index = i
break
Expand Down Expand Up @@ -535,8 +535,7 @@ def handle_delete_page(self, message: Message):
message_is_valid = _validate_page_message(message)
if message_is_valid:
namespace_name = message.data["__from"]
pages_to_remove = message.data.get("page_names") or \
message.data.get("page") # backwards compat
pages_to_remove = message.data.get("page_names")
LOG.debug(f"Got {namespace_name} request to delete: {pages_to_remove}")
with namespace_lock:
self._remove_pages(namespace_name, pages_to_remove)
Expand Down Expand Up @@ -579,24 +578,6 @@ def _parse_persistence(persistence: Optional[Union[int, bool]]) -> \
# Defines default behavior as displaying for 30 seconds
return False, 30

def _legacy_show_page(self, message: Message) -> List[GuiPage]:
"""
Backwards-compat method to handle messages without ui_directories and
page_names.
@param message: message requesting to display pages
@return: list of GuiPage objects
"""
pages_to_show = message.data["page"]
LOG.info(f"Handling legacy page show request. pages={pages_to_show}")

pages_to_load = list()
persist, duration = self._parse_persistence(message.data["__idle"])
for page in pages_to_show:
name = page.split('/')[-1]
# check if persistence is type of int or bool
pages_to_load.append(GuiPage(page, name, persist, duration))
return pages_to_load

def handle_show_page(self, message: Message):
"""
Handles a request to show one or more pages on the screen.
Expand All @@ -609,39 +590,19 @@ def handle_show_page(self, message: Message):

namespace_name = message.data["__from"]
page_ids_to_show = message.data.get('page_names')
page_resource_dirs = message.data.get('ui_directories')
persistence = message.data["__idle"]
show_index = message.data.get("index", None)
show_index = message.data.get("index", 0)

LOG.debug(f"Got {namespace_name} request to show: {page_ids_to_show} at index: {show_index}")

if not page_resource_dirs and page_ids_to_show and \
all((x.startswith("SYSTEM") for x in page_ids_to_show)):
page_resource_dirs = {"all": self._system_res_dir}

if not all((page_ids_to_show, page_resource_dirs)):
LOG.warning(f"GUI resources have not yet been uploaded for namespace: {namespace_name}")
pages = self._legacy_show_page(message)
else:
pages = list()
persist, duration = self._parse_persistence(message.data["__idle"])
for page in page_ids_to_show:
url = None
name = page
if isfile(page):
LOG.warning(f"Expected resource name but got file: {url}")
name = page.split('/')[-1]
url = f"file://{page}"
elif "://" in page:
LOG.warning(f"Expected resource name but got URI: {page}")
name = page.split('/')[-1]
url = page
pages.append(GuiPage(url, name, persist, duration,
page, namespace_name, page_resource_dirs))
pages = list()
persist, duration = self._parse_persistence(message.data["__idle"])
for page in page_ids_to_show:
pages.append(GuiPage(name=page, persistent=persist, duration=duration,
namespace=namespace_name))

if not pages:
LOG.error(f"Activated namespace '{namespace_name}' has no pages! "
f"Did you provide 'ui_directories' ?")
LOG.error(f"Activated namespace '{namespace_name}' has no pages!")
LOG.error(f"Can't show page, bad message: {message.data}")
return

Expand Down
20 changes: 2 additions & 18 deletions ovos_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,17 @@ class GuiPage:
A GuiPage represents a single GUI Display within a given namespace.
A Page can either be `persistent` or be removed after some `duration`.
Note that a page is generally framework-independent
@param url: URI (local or network path) of the GUI Page
@param name: Name of the page as shown in its namespace (could
@param persistent: If True, page is displayed indefinitely
@param duration: Number of seconds to display the page for
@param namespace: Skill/component identifier
@param page_id: Page identifier
(file path relative to gui_framework directory with no extension)
"""
url: Optional[str] # This param is left for backwards-compat.
name: str
persistent: bool
duration: Union[int, bool]
page_id: Optional[str] = None
namespace: Optional[str] = None
resource_dirs: Optional[dict] = None

@property
def id(self):
"""
Get a unique identifier for this page.
"""
return self.page_id or self.url

@staticmethod
def get_file_extension(framework: str) -> str:
"""
Expand All @@ -47,19 +35,15 @@ def get_file_extension(framework: str) -> str:

@property
def res_namespace(self):
return "system" if self.page_id.startswith("SYSTEM") else self.namespace
return "system" if self.name.startswith("SYSTEM") else self.namespace

def get_uri(self, framework: str = "qt5") -> str:
"""
Get a valid URI for this Page.
@param framework: String GUI framework to get resources for (currently only 'qt5')
@return: Absolute path to the requested resource
"""
if self.url:
LOG.warning(f"Static URI: {self.url}")
return self.url

res_filename = f"{self.page_id}.{self.get_file_extension(framework)}"
res_filename = f"{self.name}.{self.get_file_extension(framework)}"
path = f"{GUI_CACHE_PATH}/{self.res_namespace}/{framework}/{res_filename}"
LOG.debug(f"Resolved page URI: {path}")
if isfile(path):
Expand Down

0 comments on commit 8d49159

Please sign in to comment.