Skip to content

Commit

Permalink
MycroftSkill
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Sep 21, 2023
1 parent d97f353 commit e4dd6bc
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 65 deletions.
14 changes: 14 additions & 0 deletions ovos_workshop/skills/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,20 @@ def _real_wait_response(self, is_cancel, validator, on_fail, num_retries):
else:
self.bus.emit(msg)

@staticmethod
def acknowledge():
"""
Acknowledge a successful request.
This method plays a sound to acknowledge a request that does not
require a verbal response. This is intended to provide simple feedback
to the user that their request was handled successfully.
"""
# DEPRECATED - note that this is a staticmethod and uses the old endpoint
# the OVOSSkill class does things properly
from ovos_utils.sound import play_acknowledge_sound
return play_acknowledge_sound()

def ask_yesno(self, prompt: str,
data: Optional[dict] = None) -> Optional[str]:
"""
Expand Down
141 changes: 76 additions & 65 deletions ovos_workshop/skills/mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@
# limitations under the License.

import shutil

from abc import ABCMeta
from os.path import join, exists
from typing import Optional

from ovos_bus_client import MessageBusClient, Message
from ovos_utils.log import LOG, log_deprecation, deprecated
from ovos_workshop.decorators.compat import backwards_compat
from ovos_workshop.skills.base import BaseSkill
from ovos_utils.sound import play_acknowledge_sound


def is_classic_core(): # TODO - use decorator
return False


class _SkillMetaclass(ABCMeta):
Expand Down Expand Up @@ -114,13 +109,15 @@ def __call__(cls, *args, **kwargs):
skill._startup(bus, skill_id)
return skill

def __instancecheck__(self, instance):
if is_classic_core():
# instance imported from vanilla mycroft
from mycroft.skills import MycroftSkill as _CoreSkill
if issubclass(instance.__class__, _CoreSkill):
return True
def __instancecheck_classic__(self, instance):
# instance imported from vanilla mycroft
from mycroft.skills import MycroftSkill as _CoreSkill
if issubclass(instance.__class__, _CoreSkill):
return True
return issubclass(instance.__class__, MycroftSkill)

@backwards_compat(classic_core=__instancecheck_classic__)
def __instancecheck__(self, instance):
from ovos_workshop.skills.ovos import OVOSSkill
return super().__instancecheck__(instance) or \
issubclass(instance.__class__, OVOSSkill)
Expand All @@ -133,9 +130,8 @@ class MycroftSkill(BaseSkill, metaclass=_SkillMetaclass):
recommended to implement `OVOSSkill` to properly implement new methods.
"""

@deprecated("MycroftSkill class has been deprecated, please subclass from OVOSSkill", "0.1.0")
def __init__(self, name: str = None, bus: MessageBusClient = None,
use_settings: bool = True, *args, **kwargs):
def __classic_init__(self, name: str = None, bus: MessageBusClient = None,
use_settings: bool = True, *args, **kwargs):
"""
Create a MycroftSkill object.
@param name: DEPRECATED skill_name
Expand All @@ -153,49 +149,56 @@ def __init__(self, name: str = None, bus: MessageBusClient = None,
log_deprecation("use_settings has been deprecated! "
"skill settings are always enabled", "0.1.0")

if is_classic_core():
self.settings_write_path = self.root_dir
self.settings_write_path = self.root_dir

@deprecated("MycroftSkill class has been deprecated, please subclass from OVOSSkill", "0.1.0")
@backwards_compat(classic_core=__classic_init__)
def __init__(self, name: str = None, bus: MessageBusClient = None,
use_settings: bool = True, *args, **kwargs):
"""
Create a MycroftSkill object.
@param name: DEPRECATED skill_name
@param bus: MessageBusClient to bind to skill
@param use_settings: DEPRECATED option to disable settings sync
"""
super().__init__(name=name, bus=bus, *args, **kwargs)
self._initial_settings = {}
self.settings_write_path = None
self.settings_manager = None

def __init_settings_manager_classic(self):
super()._init_settings_manager()
from mycroft.skills.settings import SettingsMetaUploader
self._settings_meta = SettingsMetaUploader(self.root_dir,
self.skill_id)

def __init_settings_manager_standalone(self):
super()._init_settings_manager()

@backwards_compat(classic_core=__init_settings_manager_classic,
no_core=__init_settings_manager_standalone)
def _init_settings_manager(self):
super()._init_settings_manager()
# backwards compat - self.settings_meta has been deprecated
# in favor of settings manager
if is_classic_core():
from mycroft.skills.settings import SettingsMetaUploader
else:
try: # ovos-core compat layer
from mycroft.deprecated.skills.settings import \
SettingsMetaUploader
self._settings_meta = SettingsMetaUploader(self.root_dir,
self.skill_id)
except ImportError:
pass # standalone skill, skip backwards compat property

@staticmethod
def acknowledge():
"""
Acknowledge a successful request.
This method plays a sound to acknowledge a request that does not
require a verbal response. This is intended to provide simple feedback
to the user that their request was handled successfully.
"""
# DEPRECATED - note that this is a staticmethod and uses the old endpoint
# the OVOSSkill class does things properly
return play_acknowledge_sound()
from mycroft.deprecated.skills.settings import SettingsMetaUploader
self._settings_meta = SettingsMetaUploader(self.root_dir,
self.skill_id)

def __init_settings_classic(self):
# migrate settings if needed
if not exists(self._settings_path) and \
exists(self._old_settings_path):
LOG.warning("Found skill settings at pre-xdg location, "
"migrating!")
shutil.copy(self._old_settings_path, self._settings_path)
LOG.info(f"{self._old_settings_path} moved to "
f"{self._settings_path}")
super()._init_settings()

@backwards_compat(classic_core=__init_settings_classic)
def _init_settings(self):
"""Setup skill settings."""
if is_classic_core():
# migrate settings if needed
if not exists(self._settings_path) and \
exists(self._old_settings_path):
LOG.warning("Found skill settings at pre-xdg location, "
"migrating!")
shutil.copy(self._old_settings_path, self._settings_path)
LOG.info(f"{self._old_settings_path} moved to "
f"{self._settings_path}")

super()._init_settings()

# renamed in base class for naming consistency
Expand All @@ -220,14 +223,8 @@ def make_active(self):
self._activate()

# patched due to functional (internal) differences under mycroft-core
def _on_event_end(self, message: Message, handler_info: str,
skill_data: dict):
"""
Store settings and indicate that the skill handler has completed
"""
if not is_classic_core():
return super()._on_event_end(message, handler_info, skill_data)

def __on_end_classic(self, message: Message, handler_info: str,
skill_data: dict):
# mycroft-core style settings
if self.settings != self._initial_settings:
try:
Expand All @@ -241,6 +238,14 @@ def _on_event_end(self, message: Message, handler_info: str,
message.context["skill_id"] = self.skill_id
self.bus.emit(message.forward(msg_type, skill_data))

@backwards_compat(classic_core=__on_end_classic)
def _on_event_end(self, message: Message, handler_info: str,
skill_data: dict):
"""
Store settings and indicate that the skill handler has completed
"""
return super()._on_event_end(message, handler_info, skill_data)

# renamed in base class for naming consistency
# refactored to use new resource utils
def translate(self, text: str, data: Optional[dict] = None):
Expand Down Expand Up @@ -304,12 +309,18 @@ def _old_settings_path(self):
return join(old_dir, old_folder, self.skill_id, 'settings.json')

# patched due to functional (internal) differences under mycroft-core
def __get_settings_pclassic(self):
if self.settings_write_path and \
self.settings_write_path != self.root_dir:
log_deprecation("`self.settings_write_path` is no longer used",
"0.1.0")
return join(self.settings_write_path, 'settings.json')
return super()._settings_path

@backwards_compat(classic_core=__get_settings_pclassic)
def __get_settings_p(self):
return super()._settings_path

@property
def _settings_path(self):
if is_classic_core():
if self.settings_write_path and \
self.settings_write_path != self.root_dir:
log_deprecation("`self.settings_write_path` is no longer used",
"0.1.0")
return join(self.settings_write_path, 'settings.json')
return super()._settings_path
return self.__get_settings_p()
15 changes: 15 additions & 0 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ def deactivate(self):
"""
self._deactivate()

@staticmethod
def __acknowledge_classic():
"""
Acknowledge a successful request.
This method plays a sound to acknowledge a request that does not
require a verbal response. This is intended to provide simple feedback
to the user that their request was handled successfully.
"""
# DEPRECATED - note that this is a staticmethod and uses the old endpoint
# the OVOSSkill class does things properly
from ovos_utils.sound import play_acknowledge_sound
return play_acknowledge_sound()

@backwards_compat(classic_core=__acknowledge_classic)
def acknowledge(self):
"""
Acknowledge a successful request.
Expand Down

0 comments on commit e4dd6bc

Please sign in to comment.