def __init__(self, snips=None, config=None): """Initialize the mixin by setting the :attr:`config`, :attr:`snips` and :attr:`assistant` attributes. To initialize the :attr:`assistant` attribute, the location of the assistant is read from the Snips configuration file. If the location is not specified there, a default :class:`.AssistantConfig` object is created. Args: snips (:class:`.SnipsConfig`, optional): a Snips configuration. If the argument is not specified, a default :class:`.SnipsConfig` object is created for a locally installed instance of Snips. config (:class:`.AppConfig`, optional): an app configuration. If the argument is not specified, the app has no configuration. .. versionadded:: 0.3.0 """ self.config = config if not snips: snips = SnipsConfig() self.snips = snips try: assistant_directory = snips['snips-common']['assistant'] assistant_file = Path(assistant_directory) / 'assistant.json' self.assistant = AssistantConfig(assistant_file) except KeyError: self.assistant = AssistantConfig()
def test_assistant_config_no_config_file(fs): """Test whether an `AssistantConfig` object raises `AssistantConfigNotFoundError` when there's no assistant configuration found in the search path. """ with pytest.raises(AssistantConfigNotFoundError): assistant_config = AssistantConfig()
def test_assistant_config_broken_json(fs): """Test whether an `AssistantConfig` object raises `JSONDecodeError` when a broken JSON file is read. """ assistant_file = '/usr/share/snips/assistant/assistant.json' fs.create_file(assistant_file, contents='{"language": "en", }') with pytest.raises(JSONDecodeError): assistant_config = AssistantConfig()
def test_assistant_config_default(fs): """Test whether a default `AssistantConfig` object is initialized correctly. """ assistant_file = '/usr/local/share/snips/assistant/assistant.json' fs.create_file(assistant_file, contents='{"language": "en"}') assistant_config = AssistantConfig() assert assistant_config.filename == assistant_file assert assistant_config['language'] == 'en'
def test_assistant_config_key_not_found(fs): """Test whether accessing a key that doesn't exist in an `AssistantConfig` object raises a `KeyError`. """ assistant_file = '/usr/local/share/snips/assistant/assistant.json' fs.create_file(assistant_file, contents='{"language": "en"}') assistant_config = AssistantConfig() with pytest.raises(KeyError): assistant_config['name']
def test_assistant_config_with_filename(fs): """Test whether an `AssistantConfig` object is initialized correctly with a filename argument. """ assistant_file = '/opt/assistant/assistant.json' fs.create_file(assistant_file, contents='{"language": "en"}') assistant_config = AssistantConfig(assistant_file) assert assistant_config.filename == assistant_file assert assistant_config['language'] == 'en'
def test_assistant_config_file_not_found(fs): """Test whether an `AssistantConfig` object raises `FileNotFoundError` when the specified assistant configuration file doesn't exist. """ with pytest.raises(FileNotFoundError): assistant_config = AssistantConfig('/opt/assistant/assistant.json')
#!/usr/bin/env python3 """ This module contains a Snips app that answers questions about your Snips assistant. """ import importlib from pathlib import Path from snipskit.config import AssistantConfig from snipskit.hermes.apps import HermesSnipsApp from snipskit.hermes.decorators import intent # Use the assistant's language. i18n = importlib.import_module('translations.' + AssistantConfig()['language']) class AssistantInformation(HermesSnipsApp): """ This app answers questions about your Snips assistant. """ @intent(i18n.INTENT_ASSISTANT_ID) def handle_assistant_id(self, hermes, intent_message): """Handle the intent AssistantID.""" # Spell out the individual letters of the ID prefix, suffix = self.assistant['id'].split('_') suffix = ' '.join(list(suffix)) project_id = prefix + '_' + suffix result_sentence = i18n.RESULT_ASSISTANT_ID.format(project_id)