Skip to content

Commit

Permalink
Move IntentLayers class to ovos_workshop.decorators.layers with b…
Browse files Browse the repository at this point in the history
…ackwards-compat import to resolve circular import error (#32)

Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Oct 20, 2022
1 parent babfa7a commit dd03b1d
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 100 deletions.
2 changes: 1 addition & 1 deletion ovos_workshop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from ovos_workshop.decorators import *
from ovos_workshop.decorators.killable import killable_event, \
AbortEvent, AbortQuestion
from ovos_workshop.skills.layers import IntentLayers
from ovos_workshop.decorators.layers import IntentLayers
2 changes: 1 addition & 1 deletion ovos_workshop/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from ovos_workshop.decorators import *
from ovos_workshop.decorators.killable import killable_event, \
AbortEvent, AbortQuestion
from ovos_workshop.skills.layers import IntentLayers
from ovos_workshop.decorators.layers import IntentLayers
from ovos_workshop.resource_files import SkillResources, find_resource

# LF imports are only used in ask_selection, they are optional and provide
Expand Down
94 changes: 93 additions & 1 deletion ovos_workshop/decorators/layers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
from functools import wraps
from ovos_utils.log import LOG

from ovos_workshop.skills.layers import IntentLayers


def dig_for_skill(max_records: int = 10):
Expand Down Expand Up @@ -128,3 +128,95 @@ def real_decorator(func):
return func

return real_decorator


class IntentLayers:
def __init__(self):
self._skill = None
self._layers = {}
self._active_layers = []

def bind(self, skill):
if skill:
self._skill = skill
return self

@property
def skill(self):
return self._skill

@property
def bus(self):
return self._skill.bus if self._skill else None

@property
def skill_id(self):
return self._skill.skill_id if self._skill else "IntentLayers"

@property
def active_layers(self):
return self._active_layers

def disable(self):
LOG.info("Disabling layers")
# disable all layers
for layer_name, intents in self._layers.items():
self.deactivate_layer(layer_name)

def update_layer(self, layer_name, intent_list=None):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
intent_list = intent_list or []
if layer_name not in self._layers:
self._layers[layer_name] = []
self._layers[layer_name] += intent_list or []
LOG.info(f"Adding {intent_list} to {layer_name}")

def activate_layer(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
LOG.info("activating layer named: " + layer_name)
if layer_name not in self._active_layers:
self._active_layers.append(layer_name)
for intent in self._layers[layer_name]:
self.skill.enable_intent(intent)
else:
LOG.debug("no layer named: " + layer_name)

def deactivate_layer(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
LOG.info("deactivating layer named: " + layer_name)
if layer_name in self._active_layers:
self._active_layers.remove(layer_name)
for intent in self._layers[layer_name]:
self.skill.disable_intent(intent)
else:
LOG.debug("no layer named: " + layer_name)

def remove_layer(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
self.deactivate_layer(layer_name)
LOG.info("removing layer named: " + layer_name)
self._layers.pop(layer_name)
else:
LOG.debug("no layer named: " + layer_name)

def replace_layer(self, layer_name, intent_list=None):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
LOG.info("replacing layer named: " + layer_name)
self._layers[layer_name] = intent_list or []
else:
self.update_layer(layer_name, intent_list)

def is_active(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
return layer_name in self.active_layers

2 changes: 1 addition & 1 deletion ovos_workshop/decorators/ocp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import wraps
from ovos_workshop.skills.layers import IntentLayers
from ovos_workshop.decorators.layers import IntentLayers
from ovos_plugin_common_play.ocp import *
from ovos_plugin_common_play.ocp.status import *

Expand Down
2 changes: 1 addition & 1 deletion ovos_workshop/patches/base_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ovos_utils import ensure_mycroft_import
ensure_mycroft_import()

from mycroft.skills.mycroft_skill import MycroftSkill as _MycroftSkill
from mycroft.skills.mycroft_skill.mycroft_skill import MycroftSkill as _MycroftSkill
from mycroft.skills.fallback_skill import FallbackSkill as _FallbackSkill
from mycroft.skills.skill_data import read_vocab_file, load_vocabulary, \
load_regex
Expand Down
13 changes: 10 additions & 3 deletions ovos_workshop/skills/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
try:
from ovos_workshop.skills.ovos import MycroftSkill, OVOSSkill, OVOSFallbackSkill
from ovos_workshop.skills.idle_display_skill import IdleDisplaySkill
except ImportError:
pass
except ImportError as e:
import inspect
from pprint import pformat
from ovos_utils.log import LOG
for call in inspect.stack():
module = inspect.getmodule(call.frame)
name = module.__name__ if module else call.filename
LOG.info(f"{name}:{call.lineno}")

# if mycroft is not available do not export the skill class
# this is common in OvosAbstractApp implementations such as OCP

from ovos_workshop.skills.layers import IntentLayers
from ovos_workshop.decorators.layers import IntentLayers

93 changes: 1 addition & 92 deletions ovos_workshop/skills/layers.py
Original file line number Diff line number Diff line change
@@ -1,92 +1 @@
from ovos_utils.log import LOG


class IntentLayers:
def __init__(self):
self._skill = None
self._layers = {}
self._active_layers = []

def bind(self, skill):
if skill:
self._skill = skill
return self

@property
def skill(self):
return self._skill

@property
def bus(self):
return self._skill.bus if self._skill else None

@property
def skill_id(self):
return self._skill.skill_id if self._skill else "IntentLayers"

@property
def active_layers(self):
return self._active_layers

def disable(self):
LOG.info("Disabling layers")
# disable all layers
for layer_name, intents in self._layers.items():
self.deactivate_layer(layer_name)

def update_layer(self, layer_name, intent_list=None):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
intent_list = intent_list or []
if layer_name not in self._layers:
self._layers[layer_name] = []
self._layers[layer_name] += intent_list or []
LOG.info(f"Adding {intent_list} to {layer_name}")

def activate_layer(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
LOG.info("activating layer named: " + layer_name)
if layer_name not in self._active_layers:
self._active_layers.append(layer_name)
for intent in self._layers[layer_name]:
self.skill.enable_intent(intent)
else:
LOG.debug("no layer named: " + layer_name)

def deactivate_layer(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
LOG.info("deactivating layer named: " + layer_name)
if layer_name in self._active_layers:
self._active_layers.remove(layer_name)
for intent in self._layers[layer_name]:
self.skill.disable_intent(intent)
else:
LOG.debug("no layer named: " + layer_name)

def remove_layer(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
self.deactivate_layer(layer_name)
LOG.info("removing layer named: " + layer_name)
self._layers.pop(layer_name)
else:
LOG.debug("no layer named: " + layer_name)

def replace_layer(self, layer_name, intent_list=None):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
if layer_name in self._layers:
LOG.info("replacing layer named: " + layer_name)
self._layers[layer_name] = intent_list or []
else:
self.update_layer(layer_name, intent_list)

def is_active(self, layer_name):
if not layer_name.startswith(f"{self.skill_id}:"):
layer_name = f"{self.skill_id}:{layer_name}"
return layer_name in self.active_layers
from ovos_workshop.decorators.layers import IntentLayers
12 changes: 12 additions & 0 deletions test/unittests/test_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest


class TestImports(unittest.TestCase):
"""
These tests are only valid if `mycroft` package is available
"""
def test_skills(self):
import ovos_workshop.skills
self.assertIsNotNone(ovos_workshop.skills.MycroftSkill)
self.assertIsNotNone(ovos_workshop.skills.OVOSSkill)
self.assertIsNotNone(ovos_workshop.skills.OVOSFallbackSkill)

0 comments on commit dd03b1d

Please sign in to comment.