diff --git a/ovos_utils/waiting_for_mycroft/base_skill.py b/ovos_utils/waiting_for_mycroft/base_skill.py index 5a76ff7..5490371 100644 --- a/ovos_utils/waiting_for_mycroft/base_skill.py +++ b/ovos_utils/waiting_for_mycroft/base_skill.py @@ -7,7 +7,6 @@ from ovos_utils.lang import get_language_dir from ovos_utils.intents import ConverseTracker from ovos_utils.waiting_for_mycroft.skill_gui import SkillGUI -from ovos_utils.waiting_for_mycroft.skill_api import SkillApi from ovos_utils.log import LOG from ovos_utils import camel_case_split, get_handler_name, \ create_killable_daemon, ensure_mycroft_import @@ -393,7 +392,6 @@ def handle_skill_deactivated(self, message=None): """ pass - # https://github.com/MycroftAI/mycroft-core/pull/1822 # https://github.com/MycroftAI/mycroft-core/pull/1468 def bind(self, bus): if bus and not isinstance(self.gui, SkillGUI): @@ -404,64 +402,10 @@ def bind(self, bus): self.gui = SkillGUI(self) # pull/2683 super().bind(bus) if bus: - SkillApi.connect_bus(self.bus) # pull/1822 - self._register_public_api() ConverseTracker.connect_bus(self.bus) # pull/1468 self.add_event("converse.skill.deactivated", self._deactivate_skill) - def _send_public_api(self, message): - """Respond with the skill's public api.""" - self.bus.emit(message.response(data=self.public_api)) - - def _register_public_api(self): - """ Find and register api methods. - Api methods has been tagged with the api_method member, for each - method where this is found the method a message bus handler is - registered. - Finally create a handler for fetching the api info from any requesting - skill. - """ - - def wrap_method(func): - """Boiler plate for returning the response to the sender.""" - - def wrapper(message): - result = func(*message.data['args'], **message.data['kwargs']) - self.bus.emit(message.response(data={'result': result})) - - return wrapper - - methods = [attr_name for attr_name in get_non_properties(self) - if hasattr(getattr(self, attr_name), '__name__')] - - for attr_name in methods: - method = getattr(self, attr_name) - - if hasattr(method, 'api_method'): - doc = method.__doc__ or '' - name = method.__name__ - self.public_api[name] = { - 'help': doc, - 'type': '{}.{}'.format(self.skill_id, name), - 'func': method - } - for key in self.public_api: - if ('type' in self.public_api[key] and - 'func' in self.public_api[key]): - LOG.debug('Adding api method: ' - '{}'.format(self.public_api[key]['type'])) - - # remove the function member since it shouldn't be - # reused and can't be sent over the messagebus - func = self.public_api[key].pop('func') - self.add_event(self.public_api[key]['type'], - wrap_method(func)) - - if self.public_api: - self.add_event('{}.public_api'.format(self.skill_id), - self._send_public_api) - # https://github.com/MycroftAI/mycroft-core/pull/2675 def voc_match(self, utt, voc_filename, lang=None, exact=False): """Determine if the given utterance contains the vocabulary provided. @@ -490,8 +434,7 @@ def voc_match(self, utt, voc_filename, lang=None, exact=False): voc_filename + '.voc')) if not voc or not exists(voc): - raise FileNotFoundError( - 'Could not find {}.voc file'.format(voc_filename)) + return False # load vocab and flatten into a simple list vocab = read_vocab_file(voc) self.voc_match_cache[cache_key] = list(chain(*vocab)) diff --git a/ovos_utils/waiting_for_mycroft/skill_api.py b/ovos_utils/waiting_for_mycroft/skill_api.py deleted file mode 100644 index 6bdd257..0000000 --- a/ovos_utils/waiting_for_mycroft/skill_api.py +++ /dev/null @@ -1,99 +0,0 @@ -# https://github.com/MycroftAI/mycroft-core/pull/1822 - -# Copyright 2020 Mycroft AI Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Skill Api - -The skill api allows skills interact with eachother over the message bus -just like interacting with any other object. -""" - -try: - from mycroft.messagebus.message import Message -except ImportError: - import sys - from ovos_utils.log import LOG - from ovos_utils import get_mycroft_root - MYCROFT_ROOT_PATH = get_mycroft_root() - if MYCROFT_ROOT_PATH is not None: - sys.path.append(MYCROFT_ROOT_PATH) - try: - from mycroft.messagebus.message import Message - except ImportError: - LOG.error("Could not find mycroft root path") - from ovos_utils.messagebus import Message - - -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 - - -class SkillApi: - """SkillApi providing a simple interface to exported methods from skills - - Methods are built from a method_dict provided when initializing the skill. - """ - bus = None - - @classmethod - def connect_bus(cls, mycroft_bus): - """Registers the bus object to use.""" - # PATCH - in mycroft-core this would be called only once - # in here it is done in MycroftSkill.bind so i added this - # conditional check - if cls.bus is None and mycroft_bus is not None: - cls.bus = mycroft_bus - - def __init__(self, method_dict): - self.method_dict = method_dict - for key in method_dict: - def get_method(k): - def method(*args, **kwargs): - m = self.method_dict[k] - data = {'args': args, 'kwargs': kwargs} - method_msg = Message(m['type'], data) - response = SkillApi.bus.wait_for_response(method_msg) - if (response and response.data and - 'result' in response.data): - return response.data['result'] - else: - return None - - return method - - self.__setattr__(key, get_method(key)) - - @staticmethod - def get(skill): - """Generate api object from skill id. - Arguments: - skill (str): skill id for target skill - - Returns: - SkillApi - """ - public_api_msg = '{}.public_api'.format(skill) - api = SkillApi.bus.wait_for_response(Message(public_api_msg)) - if api: - return SkillApi(api.data) - else: - return None -