Skip to content

Commit

Permalink
fix/fallback_some_more (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Apr 26, 2023
1 parent 695f449 commit 65b95b2
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions ovos_workshop/skills/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ovos_utils.messagebus import get_handler_name, Message
from ovos_utils.metrics import Stopwatch
from ovos_utils.skills import get_non_properties

from ovos_config import Configuration
from ovos_workshop.permissions import FallbackMode
from ovos_workshop.skills.ovos import OVOSSkill, is_classic_core

Expand All @@ -30,13 +30,22 @@ class _MutableFallback(type(OVOSSkill)):
""" To override isinstance checks we need to use a metaclass """

def __instancecheck__(self, instance):
if isinstance(instance, (FallbackSkillV1, FallbackSkillV2)):
if isinstance(instance, _MetaFB):
return True
return super().__instancecheck__(instance)


class FallbackSkill(OVOSSkill, metaclass=_MutableFallback):
class _MetaFB(OVOSSkill):
pass


class FallbackSkill(_MetaFB, metaclass=_MutableFallback):
def __new__(cls, *args, **kwargs):
if cls is FallbackSkill:
# direct instantiation of class, dynamic wizardry or unittests going on...
# return V2 as expected, V1 will eventually be dropped
return FallbackSkillV2(*args, **kwargs)

is_old = is_classic_core()
if not is_old:
try:
Expand All @@ -49,26 +58,19 @@ def __new__(cls, *args, **kwargs):
except ImportError:
pass
if is_old:
return FallbackSkillV1(*args, **kwargs)
# core supports fallback V2
return FallbackSkillV2(*args, **kwargs)
cls.__bases__ = (FallbackSkillV1, _MetaFB)
else:
cls.__bases__ = (FallbackSkillV2, _MetaFB)
return super().__new__(cls, *args, **kwargs)

@classmethod
def make_intent_failure_handler(cls, bus):
"""backwards compat, old version of ovos-core call this method to bind the bus to old class"""
return FallbackSkillV1.make_intent_failure_handler(bus)


class _MutableFallback1(type(OVOSSkill)):
""" To override isinstance checks we need to use a metaclass """

def __instancecheck__(self, instance):
if isinstance(instance, (FallbackSkillV2, FallbackSkill)):
return True
return super().__instancecheck__(instance)


class FallbackSkillV1(OVOSSkill, metaclass=_MutableFallback1):
class FallbackSkillV1(_MetaFB, metaclass=_MutableFallback):
"""Fallbacks come into play when no skill matches an Adapt or closely with
a Padatious intent. All Fallback skills work together to give them a
view of the user's utterance. Fallback handlers are called in an order
Expand Down Expand Up @@ -293,16 +295,7 @@ def _register_decorated(self):
self.register_fallback(method, method.fallback_priority)


class _MutableFallback2(type(OVOSSkill)):
""" To override isinstance checks we need to use a metaclass """

def __instancecheck__(self, instance):
if isinstance(instance, (FallbackSkillV1, FallbackSkill)):
return True
return super().__instancecheck__(instance)


class FallbackSkillV2(OVOSSkill, metaclass=_MutableFallback2):
class FallbackSkillV2(_MetaFB, metaclass=_MutableFallback):
"""
Fallbacks come into play when no skill matches an intent.
Expand Down Expand Up @@ -335,16 +328,17 @@ class FallbackSkillV2(OVOSSkill, metaclass=_MutableFallback2):
that core does not execute it's fallback handlers
"""

# "skill_id": priority (int) overrides
fallback_config = Configuration().get("skills", {}).get("fallbacks", {})

@classmethod
def make_intent_failure_handler(cls, bus):
"""backwards compat, old version of ovos-core call this method to bind the bus to old class"""
return FallbackSkillV1.make_intent_failure_handler(bus)

def __init__(self, bus=None, skill_id=""):
super().__init__(bus=bus, skill_id=skill_id)
# "skill_id": priority (int) overrides
self.fallback_config = self.config_core["skills"].get("fallbacks", {})
self._fallback_handlers = []
super().__init__(bus=bus, skill_id=skill_id)

@property
def priority(self):
Expand Down Expand Up @@ -373,7 +367,7 @@ def _register_system_event_handlers(self):
"""
super()._register_system_event_handlers()
self.add_event('ovos.skills.fallback.ping', self._handle_fallback_ack, speak_errors=False)
self.add_event("ovos.skills.fallback.request", self._handle_fallback_request, speak_errors=False)
self.add_event(f"ovos.skills.fallback.{self.skill_id}.request", self._handle_fallback_request, speak_errors=False)
self.bus.emit(Message("ovos.skills.fallback.register",
{"skill_id": self.skill_id, "priority": self.priority}))

Expand Down Expand Up @@ -416,6 +410,8 @@ def register_fallback(self, handler, priority):
list of handlers registered by this instance
"""

LOG.info(f"registering fallback handler -> ovos.skills.fallback.{self.skill_id}")

def wrapper(*args, **kwargs):
if handler(*args, **kwargs):
self.activate()
Expand Down

0 comments on commit 65b95b2

Please sign in to comment.