Skip to content

Commit

Permalink
Move get_ui_directories method from workshop to gui module
Browse files Browse the repository at this point in the history
Add unit tests for `get_ui_directories`
Update default GUI path to support available `all` directory
  • Loading branch information
NeonDaniel committed Jul 6, 2023
1 parent fce76e7 commit 62e4cca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
28 changes: 26 additions & 2 deletions ovos_utils/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from collections import namedtuple
from enum import IntEnum
from os.path import join, splitext, isfile
from os.path import join, splitext, isfile, isdir

from ovos_utils import resolve_ovos_resource_file, resolve_resource_file
from ovos_utils.log import LOG, log_deprecation
Expand Down Expand Up @@ -79,6 +79,29 @@ def can_use_gui(bus=None,
return can_use_local_gui() or is_gui_connected(bus)


def get_ui_directories(root_dir: str) -> dict:
"""
Get a dict of available UI directories by GUI framework.
@param root_dir: base directory to inspect for available UI directories
@return: Dict of framework name to UI resource directory
"""
ui_directories = dict()
base_directory = root_dir
if isdir(join(base_directory, "gui")):
LOG.debug("Skill implements resources in `gui` directory")
ui_directories["all"] = join(base_directory, "gui")
return ui_directories
LOG.info("Checking for legacy UI directories")
if isdir(join(base_directory, "ui5")):
ui_directories["qt5"] = join(base_directory, "ui5")
if isdir(join(base_directory, "ui6")):
ui_directories["qt6"] = join(base_directory, "ui6")
if isdir(join(base_directory, "ui")) and "qt5" not in ui_directories:
LOG.debug("Handling `ui` directory as `qt5`")
ui_directories["qt5"] = join(base_directory, "ui")
return ui_directories


def extend_about_data(about_data: Union[list, dict],
bus=None):
"""
Expand Down Expand Up @@ -623,7 +646,8 @@ def upload_gui_pages(self, message: Message):
if not self.ui_directories:
LOG.debug("No UI resources to upload")
return
request_res_type = message.data.get("framework", "qt5")
request_res_type = message.data.get("framework") or "all" if "all" in \
self.ui_directories else "qt5"
# Note that ui_directory "all" is a special case that will upload all
# gui files, including all framework subdirectories
if request_res_type not in self.ui_directories:
Expand Down
13 changes: 13 additions & 0 deletions test/unittests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ def test_gui_dict(self):
from ovos_utils.gui import _GUIDict
# TODO

def test_get_ui_directories(self):
from ovos_utils.gui import get_ui_directories
test_dir = join(dirname(__file__), "test_ui")

# gui dir (best practice)
dirs = get_ui_directories(test_dir)
self.assertEqual(dirs, {"all": join(test_dir, "gui")})

# ui and uid dirs (legacy)
dirs = get_ui_directories(join(test_dir, "legacy"))
self.assertEqual(dirs, {"qt5": join(test_dir, "legacy", "ui"),
"qt6": join(test_dir, "legacy", "ui6")})


class TestGuiInterface(unittest.TestCase):
from ovos_utils.messagebus import FakeBus
Expand Down
1 change: 1 addition & 0 deletions test/unittests/test_ui/legacy/ui/test.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qt5
1 change: 1 addition & 0 deletions test/unittests/test_ui/legacy/ui6/test.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qt6

0 comments on commit 62e4cca

Please sign in to comment.