Skip to content

Commit

Permalink
refactor/move_intent_decorators (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Apr 21, 2023
1 parent a95bd03 commit 103a064
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions ovos_workshop/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,88 @@
from ovos_workshop.decorators.converse import converse_handler
from ovos_workshop.decorators.fallback_handler import fallback_handler
from ovos_utils import classproperty
from functools import wraps
try:
from ovos_workshop.decorators.ocp import ocp_next, ocp_play, ocp_pause, ocp_resume, ocp_search, ocp_previous, ocp_featured_media
except ImportError:
pass # these imports are only available if extra requirements are installed

"""
Decorators for use with MycroftSkill methods
Helper decorators for handling context from skills.
"""


def adds_context(context, words=''):
"""Decorator adding context to the Adapt context manager.
Args:
context (str): context Keyword to insert
words (str): optional string content of Keyword
"""

def context_add_decorator(func):
@wraps(func)
def func_wrapper(*args, **kwargs):
ret = func(*args, **kwargs)
args[0].set_context(context, words)
return ret

return func_wrapper

return context_add_decorator


def removes_context(context):
"""Decorator removing context from the Adapt context manager.
Args:
context (str): Context keyword to remove
"""

def context_removes_decorator(func):
@wraps(func)
def func_wrapper(*args, **kwargs):
ret = func(*args, **kwargs)
args[0].remove_context(context)
return ret

return func_wrapper

return context_removes_decorator


def intent_handler(intent_parser):
"""Decorator for adding a method as an intent handler."""

def real_decorator(func):
# Store the intent_parser inside the function
# This will be used later to call register_intent
if not hasattr(func, 'intents'):
func.intents = []
func.intents.append(intent_parser)
return func

return real_decorator


def intent_file_handler(intent_file):
"""Decorator for adding a method as an intent file handler.
This decorator is deprecated, use intent_handler for the same effect.
"""

def real_decorator(func):
# Store the intent_file inside the function
# This will be used later to call register_intent_file
if not hasattr(func, 'intent_files'):
func.intent_files = []
func.intent_files.append(intent_file)
return func

return real_decorator


def resting_screen_handler(name):
"""Decorator for adding a method as an resting screen handler.
Expand All @@ -25,3 +102,14 @@ def real_decorator(func):

return real_decorator


def skill_api_method(func):
"""Decorator for adding a method to the skill's public api.
Methods with this decorator will be registered on the message bus
and an api object can be created for interaction with the skill.
"""
# tag the method by adding an api_method member to it
if not hasattr(func, 'api_method') and hasattr(func, '__name__'):
func.api_method = True
return func

0 comments on commit 103a064

Please sign in to comment.