Skip to content

Commit

Permalink
fix/abstract app init (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Sep 19, 2023
1 parent e8540c0 commit e659917
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
12 changes: 6 additions & 6 deletions ovos_workshop/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ def __init__(self, skill_id: str, bus: Optional[MessageBusClient] = None,
@param enable_settings_manager: if True, enables a SettingsManager for
this application to manage default settings and backend sync
"""
super().__init__(skill_id=skill_id, bus=bus, gui=gui,
resources_dir=resources_dir,
enable_settings_manager=enable_settings_manager,
**kwargs)
self.skill_id = skill_id
self._dedicated_bus = False
if bus:
self._dedicated_bus = False
else:
self._dedicated_bus = True
bus = get_mycroft_bus()
self._startup(bus, skill_id)

super().__init__(skill_id=skill_id, bus=bus, gui=gui,
resources_dir=resources_dir,
enable_settings_manager=enable_settings_manager,
**kwargs)

if settings:
log_deprecation(f"Settings should be set in {self._settings_path}. "
f"Passing `settings` to __init__ is not supported.",
Expand Down
9 changes: 6 additions & 3 deletions ovos_workshop/skills/mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Optional

from ovos_bus_client import MessageBusClient, Message
from ovos_utils.log import LOG, log_deprecation
from ovos_utils.log import LOG, log_deprecation, deprecated
from ovos_workshop.skills.base import BaseSkill, is_classic_core


Expand Down Expand Up @@ -113,10 +113,12 @@ def __instancecheck__(self, instance):
if is_classic_core():
# instance imported from vanilla mycroft
from mycroft.skills import MycroftSkill as _CoreSkill
if issubclass(self.__class__, _CoreSkill):
if issubclass(instance.__class__, _CoreSkill):
return True

return super().__instancecheck__(instance)
from ovos_workshop.skills.ovos import OVOSSkill
return super().__instancecheck__(instance) or \
issubclass(instance.__class__, OVOSSkill)


class MycroftSkill(BaseSkill, metaclass=_SkillMetaclass):
Expand All @@ -126,6 +128,7 @@ 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):
"""
Expand Down
28 changes: 22 additions & 6 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from abc import ABCMeta
from threading import Event

from typing import List, Optional, Union

from ovos_bus_client import MessageBusClient
Expand All @@ -11,13 +11,29 @@
from ovos_utils.skills.audioservice import OCPInterface
from ovos_utils.skills.settings import PrivateSettings
from ovos_utils.sound import play_audio
from ovos_workshop.decorators.layers import IntentLayers
from ovos_workshop.resource_files import SkillResources
from ovos_workshop.skills.base import BaseSkill
from ovos_workshop.skills.mycroft_skill import is_classic_core, MycroftSkill

from ovos_workshop.decorators.layers import IntentLayers
from ovos_workshop.skills.mycroft_skill import MycroftSkill, is_classic_core

class _OVOSSkillMetaclass(ABCMeta):
"""
To override isinstance checks
"""

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

return super().__instancecheck__(instance) or \
issubclass(instance.__class__, MycroftSkill)


class OVOSSkill(MycroftSkill):
class OVOSSkill(BaseSkill, metaclass=_OVOSSkillMetaclass):
"""
New features:
- all patches for MycroftSkill class
Expand Down Expand Up @@ -140,7 +156,7 @@ def play_audio(self, filename: str):
core_supported = True # min version of ovos-core
except ImportError:
# skills don't require core anymore, running standalone
core_supported = True
core_supported = True

if core_supported:
message = dig_for_message() or Message("")
Expand Down Expand Up @@ -223,7 +239,7 @@ def _register_decorated(self):

def register_intent_layer(self, layer_name: str,
intent_list: List[Union[IntentBuilder, Intent,
str]]):
str]]):
"""
Register a named intent layer.
@param layer_name: Name of intent layer to add
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/skills/test_active.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ActiveSkillExample(ActiveSkill):

def make_active(self):
self.active()
ActiveSkill.make_active(self)
ActiveSkill.activate(self)


class TestActiveSkill(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/test_skill_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ def test_load_skill_module(self):
def test_get_skill_class(self):
from ovos_workshop.skill_launcher import get_skill_class, \
load_skill_module
from ovos_workshop.skills.mycroft_skill import _SkillMetaclass
from ovos_workshop.skills.ovos import _OVOSSkillMetaclass
test_path = join(dirname(__file__), "ovos_tskill_abort",
"__init__.py")
skill_id = "test_skill.test"
module = load_skill_module(test_path, skill_id)
skill = get_skill_class(module)
self.assertIsNotNone(skill)
self.assertEqual(skill.__class__, _SkillMetaclass, skill.__class__)
self.assertEqual(skill.__class__, _OVOSSkillMetaclass, skill.__class__)

# Test invalid request
with self.assertRaises(ValueError):
Expand Down

0 comments on commit e659917

Please sign in to comment.