Skip to content

Commit

Permalink
improve non-decorator usage callback handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Dec 18, 2024
1 parent 2d52f5c commit 96968e3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
49 changes: 31 additions & 18 deletions ovos_workshop/skills/common_play.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os
from inspect import signature
from threading import Event
from typing import List

from ovos_utils import camel_case_split
from ovos_utils.log import LOG
from typing import List, Callable, Optional

from ovos_bus_client import Message
from ovos_config.locations import get_xdg_cache_save_path
from ovos_workshop.skills.ovos import OVOSSkill

from ovos_utils import camel_case_split
from ovos_utils.log import LOG
# backwards compat imports, do not delete, skills import from here
from ovos_workshop.decorators.ocp import ocp_play, ocp_next, ocp_pause, ocp_resume, ocp_search, \
ocp_previous, ocp_featured_media
Expand Down Expand Up @@ -56,21 +53,27 @@ def ...
vocab for starting playback is needed.
"""

def __init__(self, supported_media: List[MediaType] = None,
def __init__(self, *args,
supported_media: List[MediaType] = None,
skill_icon: str = "",
skill_voc_filename: str = "",
*args, **kwargs):
playback_handler: Optional[Callable[[Optional[Message]], None]] = None,
pause_handler: Optional[Callable[[Optional[Message]], None]] = None,
next_handler: Optional[Callable[[Optional[Message]], None]] = None,
prev_handler: Optional[Callable[[Optional[Message]], None]] = None,
resume_handler: Optional[Callable[[Optional[Message]], None]] = None,
**kwargs):
self.supported_media = supported_media or [MediaType.GENERIC]
self.skill_aliases = []
self.skill_voc_filename = skill_voc_filename
self._search_handlers = [] # added via decorators
self._featured_handlers = [] # added via decorators
self._current_query = None
self.__playback_handler = None
self.__pause_handler = None
self.__next_handler = None
self.__prev_handler = None
self.__resume_handler = None
self.__playback_handler = playback_handler
self.__pause_handler = pause_handler
self.__next_handler = next_handler
self.__prev_handler = prev_handler
self.__resume_handler = resume_handler
self._stop_event = Event()
self._playing = Event()
self._paused = Event()
Expand Down Expand Up @@ -388,7 +391,9 @@ def play_media(self, media, disambiguation=None, playlist=None):
# @killable_event("ovos.common_play.stop", react_to_stop=True)
def __handle_ocp_play(self, message):
if self.__playback_handler:
self.__playback_handler(message)
params = signature(self.__playback_handler).parameters
kwargs = {"message": message} if "message" in params else {}
self.__playback_handler(**kwargs)
self.bus.emit(Message("ovos.common_play.player.state",
{"state": PlayerState.PLAYING}))
self._playing.set()
Expand All @@ -400,7 +405,9 @@ def __handle_ocp_play(self, message):
def __handle_ocp_pause(self, message):
self._paused.set()
if self.__pause_handler:
if self.__pause_handler(message):
params = signature(self.__playback_handler).parameters
kwargs = {"message": message} if "message" in params else {}
if self.__pause_handler(**kwargs):
self.bus.emit(Message("ovos.common_play.player.state",
{"state": PlayerState.PAUSED}))
else:
Expand All @@ -410,7 +417,9 @@ def __handle_ocp_pause(self, message):
def __handle_ocp_resume(self, message):
self._paused.clear()
if self.__resume_handler:
if self.__resume_handler(message):
params = signature(self.__playback_handler).parameters
kwargs = {"message": message} if "message" in params else {}
if self.__resume_handler(**kwargs):
self.bus.emit(Message("ovos.common_play.player.state",
{"state": PlayerState.PLAYING}))
else:
Expand All @@ -419,14 +428,18 @@ def __handle_ocp_resume(self, message):

def __handle_ocp_next(self, message):
if self.__next_handler:
self.__next_handler(message)
params = signature(self.__playback_handler).parameters
kwargs = {"message": message} if "message" in params else {}
self.__next_handler(**kwargs)
else:
LOG.error(f"Play Next requested but {self.skill_id} handler not "
"implemented")

def __handle_ocp_prev(self, message):
if self.__prev_handler:
self.__prev_handler(message)
params = signature(self.__playback_handler).parameters
kwargs = {"message": message} if "message" in params else {}
self.__prev_handler(**kwargs)
else:
LOG.error(f"Play Next requested but {self.skill_id} handler not "
"implemented")
Expand Down
15 changes: 7 additions & 8 deletions ovos_workshop/skills/game_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class OVOSGameSkill(OVOSCommonPlaybackSkill):
- 'ovos.common_play.{self.skill_id}.pause'
- 'ovos.common_play.{self.skill_id}.resume'
- 'ovos.common_play.{self.skill_id}.stop'
- 'ovos.common_play.{self.skill_id}.save' - TODO add intent to ocp_pipeline exclusive to MediaType.GAME
- 'ovos.common_play.{self.skill_id}.load' - TODO add intent to ocp_pipeline exclusive to MediaType.GAME
- 'ovos.common_play.{self.skill_id}.save'
- 'ovos.common_play.{self.skill_id}.load'
"""

Expand All @@ -39,12 +39,11 @@ def __init__(self, skill_voc_filename: str,
without that ocp_pipeline might not recognize the skill as a game"""
self.game_image = game_image
super().__init__(skill_icon=skill_icon, skill_voc_filename=skill_voc_filename,
supported_media=[MediaType.GAME], *args, **kwargs)
# in regular OCP skills these can be set via decorators
# here we make them mandatory implementations via abc.abstractmethod
self.__playback_handler = self.on_play_game
self.__pause_handler = self.on_pause_game
self.__resume_handler = self.on_resume_game
supported_media=[MediaType.GAME],
playback_handler=self.on_play_game,
pause_handler=self.on_pause_game,
resume_handler=self.on_resume_game,
*args, **kwargs)

@ocp_featured_media()
def _ocp_featured(self) -> Playlist:
Expand Down

0 comments on commit 96968e3

Please sign in to comment.