Skip to content

Commit

Permalink
feat/generalize runtime requirements (#49)
Browse files Browse the repository at this point in the history
* feat/network_reqs_to_utils

move shared utils from workshop to utils for usage across more projects

* renaming

* renaming

* add log

* bump ovos utils
  • Loading branch information
JarbasAl authored Feb 9, 2023
1 parent a43d887 commit 1871f4c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 46 deletions.
10 changes: 2 additions & 8 deletions ovos_workshop/decorators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from ovos_workshop.decorators.killable import \
killable_intent, killable_event
from ovos_workshop.decorators.killable import killable_intent, killable_event
from ovos_workshop.decorators.layers import enables_layer, \
disables_layer, layer_intent, removes_layer, resets_layers, replaces_layer
from ovos_workshop.decorators.converse import converse_handler
from ovos_workshop.decorators.fallback_handler import fallback_handler
from ovos_utils import classproperty
try:
from ovos_workshop.decorators.ocp import ocp_next, ocp_play, ocp_pause, ocp_resume, ocp_search, ocp_previous, ocp_featured_media
except ImportError:
Expand All @@ -25,9 +25,3 @@ def real_decorator(func):

return real_decorator


class classproperty(property):
"""Decorator for a Class-level property.
Credit to Denis Rhyzhkov on Stackoverflow: https://stackoverflow.com/a/13624858/1280629"""
def __get__(self, owner_self, owner_cls):
return self.fget(owner_cls)
40 changes: 14 additions & 26 deletions ovos_workshop/skills/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
from ovos_workshop.filesystem import FileSystemAccess
from ovos_workshop.resource_files import ResourceFile, \
CoreResources, SkillResources, find_resource
from ovos_utils.process_utils import RuntimeRequirements


# backwards compat alias
class SkillNetworkRequirements(RuntimeRequirements):
def __init__(self, *args, **kwargs):
LOG.warning("SkillNetworkRequirements has been renamed to RuntimeRequirements\n"
"from ovos_utils.process_utils import RuntimeRequirements")
super().__init__(*args, **kwargs)


def simple_trace(stack_trace):
Expand All @@ -73,27 +82,6 @@ def simple_trace(stack_trace):
return tb


@dataclass
class SkillNetworkRequirements:
# to ensure backwards compatibility the default values require internet before skill loading
# skills in the wild may assume this behaviour and require network on initialization
# any ovos aware skills should change these as appropriate

# xxx_before_load is used by skills service
network_before_load: bool = True
internet_before_load: bool = True

# requires_xxx is currently purely informative and not consumed by core
# this allows a skill to spec if it needs connectivity to handle utterances
requires_internet: bool = True
requires_network: bool = True

# xxx_fallback is currently purely informative and not consumed by core
# this allows a skill to spec if it has a fallback for temporary offline events, eg, by having a cache
no_internet_fallback: bool = False
no_network_fallback: bool = False


class BaseSkill:
"""Base class for mycroft skills providing common behaviour and parameters
to all Skill implementations. This base class does not require `mycroft` to be importable
Expand Down Expand Up @@ -161,14 +149,14 @@ def __init__(self, name=None, bus=None, resources_dir=None,

# classproperty not present in mycroft-core
@classproperty
def network_requirements(self):
def runtime_requirements(self):
""" skill developers should override this if they do not require connectivity
some examples:
IOT skill that controls skills via LAN could return:
scans_on_init = True
SkillNetworkRequirements(internet_before_load=False,
RuntimeRequirements(internet_before_load=False,
network_before_load=scans_on_init,
requires_internet=False,
requires_network=True,
Expand All @@ -177,22 +165,22 @@ def network_requirements(self):
online search skill with a local cache:
has_cache = False
SkillNetworkRequirements(internet_before_load=not has_cache,
RuntimeRequirements(internet_before_load=not has_cache,
network_before_load=not has_cache,
requires_internet=True,
requires_network=True,
no_internet_fallback=True,
no_network_fallback=True)
a fully offline skill:
SkillNetworkRequirements(internet_before_load=False,
RuntimeRequirements(internet_before_load=False,
network_before_load=False,
requires_internet=False,
requires_network=False,
no_internet_fallback=True,
no_network_fallback=True)
"""
return SkillNetworkRequirements()
return RuntimeRequirements()

# property not present in mycroft-core
@property
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ovos-utils~=0.0,>=0.0.27
ovos-utils~=0.0, >=0.0.28a4
ovos_config~=0.0,>=0.0.4
ovos-lingua-franca~=0.4,>=0.4.6

Expand Down
22 changes: 11 additions & 11 deletions test/unittests/test_skill_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from ovos_workshop import OVOSAbstractApplication
from ovos_workshop.decorators import classproperty
from ovos_workshop.skills.ovos import OVOSSkill
from ovos_workshop.skills.base import SkillNetworkRequirements
from ovos_utils.process_utils import RuntimeRequirements
from ovos_workshop.skills.mycroft_skill import is_classic_core


class OfflineSkill(OVOSSkill):
@classproperty
def network_requirements(self):
return SkillNetworkRequirements(internet_before_load=False,
def runtime_requirements(self):
return RuntimeRequirements(internet_before_load=False,
network_before_load=False,
requires_internet=False,
requires_network=False,
Expand All @@ -21,9 +21,9 @@ def network_requirements(self):

class LANSkill(OVOSSkill):
@classproperty
def network_requirements(self):
def runtime_requirements(self):
scans_on_init = True
return SkillNetworkRequirements(internet_before_load=False,
return RuntimeRequirements(internet_before_load=False,
network_before_load=scans_on_init,
requires_internet=False,
requires_network=True,
Expand Down Expand Up @@ -75,24 +75,24 @@ def test_bus_setter(self):
skill.bus = None

def test_class_property(self):
self.assertEqual(OfflineSkill.network_requirements,
SkillNetworkRequirements(internet_before_load=False,
self.assertEqual(OfflineSkill.runtime_requirements,
RuntimeRequirements(internet_before_load=False,
network_before_load=False,
requires_internet=False,
requires_network=False,
no_internet_fallback=True,
no_network_fallback=True)
)
self.assertEqual(LANSkill.network_requirements,
SkillNetworkRequirements(internet_before_load=False,
self.assertEqual(LANSkill.runtime_requirements,
RuntimeRequirements(internet_before_load=False,
network_before_load=True,
requires_internet=False,
requires_network=True,
no_internet_fallback=True,
no_network_fallback=False)
)
self.assertEqual(OVOSSkill.network_requirements,
SkillNetworkRequirements()
self.assertEqual(OVOSSkill.runtime_requirements,
RuntimeRequirements()
)

def test_class_inheritance(self):
Expand Down

0 comments on commit 1871f4c

Please sign in to comment.