files: # Copyright 2017 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.
  #
  import imp
  import operator
  import sys
  import time
  import csv
  import inspect
  from inspect import signature
  from datetime import datetime, timedelta
  
  import abc
  import re
  from adapt.intent import Intent, IntentBuilder
  from os.path import join, abspath, dirname, basename, exists
  from threading import Event
  
  from mycroft import dialog
  from mycroft.api import DeviceApi
  from mycroft.audio import wait_while_speaking
  from mycroft.client.enclosure.api import EnclosureAPI
  from mycroft.configuration import Configuration
  from mycroft.dialog import DialogLoader
  from mycroft.filesystem import FileSystemAccess
  from mycroft.messagebus.message import Message
  from mycroft.metrics import report_metric, report_timing, Stopwatch
  from mycroft.skills.settings import SkillSettings
  from mycroft.skills.skill_data import (load_vocabulary, load_regex, to_alnum,
                                         munge_regex, munge_intent_parser)
  from mycroft.util import resolve_resource_file
  from mycroft.util.log import LOG
  
  MainModule = '__init__'
  
  
  def dig_for_message():
      """
          Dig Through the stack for message.
      """
      stack = inspect.stack()
      # Limit search to 10 frames back
      stack = stack if len(stack) < 10 else stack[:10]
      local_vars = [frame[0].f_locals for frame in stack]
      for l in local_vars:
          if 'message' in l and isinstance(l['message'], Message):
              return l['message']
  
  
  def unmunge_message(message, skill_id):
      """Restore message keywords by removing the Letterified skill ID.
  
      Args:
          message (Message): Intent result message
          skill_id (str): skill identifier
  
      Returns:
          Message without clear keywords
      """
      if isinstance(message, Message) and isinstance(message.data, dict):
          skill_id = to_alnum(skill_id)
          for key in message.data:
              if key[:len(skill_id)] == skill_id:
                  new_key = key[len(skill_id):]
                  message.data[new_key] = message.data.pop(key)
  
      return message
  
  
  def open_intent_envelope(message):
      """ Convert dictionary received over messagebus to Intent. """
      intent_dict = message.data
      return Intent(intent_dict.get('name'),
                    intent_dict.get('requires'),
                    intent_dict.get('at_least_one'),
                    intent_dict.get('optional'))
  
  
  def load_skill(skill_descriptor, emitter, skill_id, BLACKLISTED_SKILLS=None):
      """
          load skill from skill descriptor.
  
          Args:
              skill_descriptor: descriptor of skill to load
              emitter:          messagebus emitter
              skill_id:         id number for skill
          Returns:
              MycroftSkill: the loaded skill or None on failure
      """
      BLACKLISTED_SKILLS = BLACKLISTED_SKILLS or []
      path = skill_descriptor["path"]
      name = basename(path)
      LOG.info("ATTEMPTING TO LOAD SKILL: {} with ID {}".format(
          name, skill_id
  
  [... the rest is omitted ...]
files: mycroft/skills/core.py
files: # Copyright 2017 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.
  #
  import imp
  import operator
  import sys
  import time
  import csv
  import inspect
  from inspect import signature
  from datetime import datetime, timedelta
  
  import abc
  import re
  from adapt.intent import Intent, IntentBuilder
  from os.path import join, abspath, dirname, basename, exists
  from threading import Event
  
  from mycroft import dialog
  from mycroft.api import DeviceApi
  from mycroft.audio import wait_while_speaking
  from mycroft.client.enclosure.api import EnclosureAPI
  from mycroft.configuration import Configuration
  from mycroft.dialog import DialogLoader
  from mycroft.filesystem import FileSystemAccess
  from mycroft.messagebus.message import Message
  from mycroft.metrics import report_metric, report_timing, Stopwatch
  from mycroft.skills.settings import SkillSettings
  from mycroft.skills.skill_data import (load_vocabulary, load_regex, to_letters,
                                         munge_regex, munge_intent_parser)
  from mycroft.util import resolve_resource_file
  from mycroft.util.log import LOG
  
  MainModule = '__init__'
  
  
  def dig_for_message():
      """
          Dig Through the stack for message.
      """
      stack = inspect.stack()
      # Limit search to 10 frames back
      stack = stack if len(stack) < 10 else stack[:10]
      local_vars = [frame[0].f_locals for frame in stack]
      for l in local_vars:
          if 'message' in l and isinstance(l['message'], Message):
              return l['message']
  
  
  def unmunge_message(message, skill_id):
      """Restore message keywords by removing the Letterified skill ID.
  
      Args:
          message (Message): Intent result message
          skill_id (int): skill identifier
  
      Returns:
          Message without clear keywords
      """
      if isinstance(message, Message) and isinstance(message.data, dict):
          skill_id = to_letters(skill_id)
          for key in message.data:
              if key[:len(skill_id)] == skill_id:
                  new_key = key[len(skill_id):]
                  message.data[new_key] = message.data.pop(key)
  
      return message
  
  
  def open_intent_envelope(message):
      """ Convert dictionary received over messagebus to Intent. """
      intent_dict = message.data
      return Intent(intent_dict.get('name'),
                    intent_dict.get('requires'),
                    intent_dict.get('at_least_one'),
                    intent_dict.get('optional'))
  
  
  def load_skill(skill_descriptor, emitter, skill_id, BLACKLISTED_SKILLS=None):
      """
          load skill from skill descriptor.
  
          Args:
              skill_descriptor: descriptor of skill to load
              emitter:          messagebus emitter
              skill_id:         id number for skill
          Returns:
              MycroftSkill: the loaded skill or None on failure
      """
      BLACKLISTED_SKILLS = BLACKLISTED_SKILLS or []
      path = skill_descriptor["path"]
      name = basename(path)
      LOG.info("ATTEMPTING TO LOAD SKILL: {} with ID {}".format(
          name, skill_id
  
  [... the rest is omitted ...]
files: mycroft/skills/core.py
files: # Copyright 2017 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.
  #
  import time
  from adapt.context import ContextManagerFrame
  from adapt.engine import IntentDeterminationEngine
  
  from mycroft.configuration import Configuration
  from mycroft.messagebus.message import Message
  from mycroft.skills.core import open_intent_envelope
  from mycroft.util.log import LOG
  from mycroft.util.parse import normalize
  from mycroft.metrics import report_timing, Stopwatch
  
  
  class ContextManager(object):
      """
      ContextManager
      Use to track context throughout the course of a conversational session.
      How to manage a session's lifecycle is not captured here.
      """
  
      def __init__(self, timeout):
          self.frame_stack = []
          self.timeout = timeout * 60  # minutes to seconds
  
      def clear_context(self):
          self.frame_stack = []
  
      def remove_context(self, context_id):
          self.frame_stack = [(f, t) for (f, t) in self.frame_stack
                              if context_id in f.entities[0].get('data', [])]
  
      def inject_context(self, entity, metadata=None):
          """
          Args:
              entity(object): Format example...
                                 {'data': 'Entity tag as <str>',
                                  'key': 'entity proper name as <str>',
                                  'confidence': <float>'
                                 }
              metadata(object): dict, arbitrary metadata about entity injected
          """
          metadata = metadata or {}
          try:
              if len(self.frame_stack) > 0:
                  top_frame = self.frame_stack[0]
              else:
                  top_frame = None
              if top_frame and top_frame[0].metadata_matches(metadata):
                  top_frame[0].merge_context(entity, metadata)
              else:
                  frame = ContextManagerFrame(entities=[entity],
                                              metadata=metadata.copy())
                  self.frame_stack.insert(0, (frame, time.time()))
          except (IndexError, KeyError):
              pass
  
      def get_context(self, max_frames=None, missing_entities=None):
          """ Constructs a list of entities from the context.
  
          Args:
              max_frames(int): maximum number of frames to look back
              missing_entities(list of str): a list or set of tag names,
              as strings
  
          Returns:
              list: a list of entities
          """
          missing_entities = missing_entities or []
  
          relevant_frames = [frame[0] for frame in self.frame_stack if
                             time.time() - frame[1] < self.timeout]
          if not max_frames or max_frames > len(relevant_frames):
              max_frames = len(relevant_frames)
  
          missing_entities = list(missing_entities)
          context = []
          for i in range(max_frames):
              frame_entities = [entity.copy() for entity in
                                relevant_frames[i].entities]
              for entity in frame_entities:
                  entity['confidence'] = entity.get('confidence', 1.0) \
                      / (2.0 + i)
              context += frame_entities
  
          result = []
          if len(missing_entities) > 0:
              for entity in context:
                  if entity.get('data') in missing_entities:
                      result.append(entity)
  
  
  [... the rest is omitted ...]
files: mycroft/skills/intent_service.py
files: # Copyright 2017 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.
  #
  import time
  from adapt.context import ContextManagerFrame
  from adapt.engine import IntentDeterminationEngine
  
  from mycroft.configuration import Configuration
  from mycroft.messagebus.message import Message
  from mycroft.skills.core import open_intent_envelope
  from mycroft.util.log import LOG
  from mycroft.util.parse import normalize
  from mycroft.metrics import report_timing, Stopwatch
  
  
  class ContextManager(object):
      """
      ContextManager
      Use to track context throughout the course of a conversational session.
      How to manage a session's lifecycle is not captured here.
      """
  
      def __init__(self, timeout):
          self.frame_stack = []
          self.timeout = timeout * 60  # minutes to seconds
  
      def clear_context(self):
          self.frame_stack = []
  
      def remove_context(self, context_id):
          self.frame_stack = [(f, t) for (f, t) in self.frame_stack
                              if context_id in f.entities[0].get('data', [])]
  
      def inject_context(self, entity, metadata=None):
          """
          Args:
              entity(object): Format example...
                                 {'data': 'Entity tag as <str>',
                                  'key': 'entity proper name as <str>',
                                  'confidence': <float>'
                                 }
              metadata(object): dict, arbitrary metadata about entity injected
          """
          metadata = metadata or {}
          try:
              if len(self.frame_stack) > 0:
                  top_frame = self.frame_stack[0]
              else:
                  top_frame = None
              if top_frame and top_frame[0].metadata_matches(metadata):
                  top_frame[0].merge_context(entity, metadata)
              else:
                  frame = ContextManagerFrame(entities=[entity],
                                              metadata=metadata.copy())
                  self.frame_stack.insert(0, (frame, time.time()))
          except (IndexError, KeyError):
              pass
  
      def get_context(self, max_frames=None, missing_entities=None):
          """ Constructs a list of entities from the context.
  
          Args:
              max_frames(int): maximum number of frames to look back
              missing_entities(list of str): a list or set of tag names,
              as strings
  
          Returns:
              list: a list of entities
          """
          missing_entities = missing_entities or []
  
          relevant_frames = [frame[0] for frame in self.frame_stack if
                             time.time() - frame[1] < self.timeout]
          if not max_frames or max_frames > len(relevant_frames):
              max_frames = len(relevant_frames)
  
          missing_entities = list(missing_entities)
          context = []
          for i in range(max_frames):
              frame_entities = [entity.copy() for entity in
                                relevant_frames[i].entities]
              for entity in frame_entities:
                  entity['confidence'] = entity.get('confidence', 1.0) \
                      / (2.0 + i)
              context += frame_entities
  
          result = []
          if len(missing_entities) > 0:
              for entity in context:
                  if entity.get('data') in missing_entities:
                      result.append(entity)
  
  
  [... the rest is omitted ...]
files: mycroft/skills/intent_service.py
files: # Copyright 2017 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.
  #
  import gc
  import os
  import sys
  import time
  from itertools import chain
  from threading import Timer, Thread, Event
  
  import monotonic
  from glob import glob
  from os.path import exists, join, basename, dirname, expanduser, isfile
  from msm import MycroftSkillsManager, SkillRepo
  
  import mycroft.lock
  from mycroft import dialog
  from mycroft.api import is_paired, BackendDown
  from mycroft.client.enclosure.api import EnclosureAPI
  from mycroft.configuration import Configuration
  from mycroft.messagebus.client.ws import WebsocketClient
  from mycroft.messagebus.message import Message
  from mycroft.skills.core import load_skill, create_skill_descriptor, \
      MainModule, FallbackSkill
  from mycroft.skills.event_scheduler import EventScheduler
  from mycroft.skills.intent_service import IntentService
  from mycroft.skills.padatious_service import PadatiousService
  from mycroft.util import (
      connected, wait_while_speaking, reset_sigint_handler,
      create_echo_function, create_daemon, wait_for_exit_signal
  )
  from mycroft.util.log import LOG
  
  ws = None
  event_scheduler = None
  skill_manager = None
  
  # Remember "now" at startup.  Used to detect clock changes.
  start_ticks = monotonic.monotonic()
  start_clock = time.time()
  
  DEBUG = Configuration.get().get("debug", False)
  skills_config = Configuration.get().get("skills")
  BLACKLISTED_SKILLS = skills_config.get("blacklisted_skills", [])
  PRIORITY_SKILLS = skills_config.get("priority_skills", [])
  SKILLS_DIR = '/opt/mycroft/skills'
  
  installer_config = Configuration.get().get("SkillInstallerSkill")
  
  MINUTES = 60  # number of seconds in a minute (syntatic sugar)
  
  
  def connect():
      global ws
      ws.run_forever()
  
  
  def _starting_up():
      """
          Start loading skills.
  
          Starts
          - SkillManager to load/reloading of skills when needed
          - a timer to check for internet connection
          - adapt intent service
          - padatious intent service
      """
      global ws, skill_manager, event_scheduler
  
      ws.on('intent_failure', FallbackSkill.make_intent_failure_handler(ws))
  
      # Create the Intent manager, which converts utterances to intents
      # This is the heart of the voice invoked skill system
  
      service = IntentService(ws)
      PadatiousService(ws, service)
      event_scheduler = EventScheduler(ws)
  
      # Create a thread that monitors the loaded skills, looking for updates
      skill_manager = SkillManager(ws)
      skill_manager.daemon = True
      # Wait until skills have been loaded once before starting to check
      # network connection
      skill_manager.load_priority()
      skill_manager.start()
      check_connection()
  
  
  def check_connection():
      """
          Check for network connection. If not paired trigger pairing.
          Runs as a Timer every second until connection is detected.
      """
  
  
  [... the rest is omitted ...]
files: mycroft/skills/main.py
files: # Copyright 2017 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.
  #
  import gc
  import os
  import sys
  import time
  from itertools import chain
  from threading import Timer, Thread, Event
  
  import monotonic
  from glob import glob
  from os.path import exists, join, basename, dirname, expanduser, isfile
  from msm import MycroftSkillsManager, SkillRepo
  
  import mycroft.lock
  from mycroft import dialog
  from mycroft.api import is_paired, BackendDown
  from mycroft.client.enclosure.api import EnclosureAPI
  from mycroft.configuration import Configuration
  from mycroft.messagebus.client.ws import WebsocketClient
  from mycroft.messagebus.message import Message
  from mycroft.skills.core import load_skill, create_skill_descriptor, \
      MainModule, FallbackSkill
  from mycroft.skills.event_scheduler import EventScheduler
  from mycroft.skills.intent_service import IntentService
  from mycroft.skills.padatious_service import PadatiousService
  from mycroft.util import (
      connected, wait_while_speaking, reset_sigint_handler,
      create_echo_function, create_daemon, wait_for_exit_signal
  )
  from mycroft.util.log import LOG
  
  ws = None
  event_scheduler = None
  skill_manager = None
  
  # Remember "now" at startup.  Used to detect clock changes.
  start_ticks = monotonic.monotonic()
  start_clock = time.time()
  
  DEBUG = Configuration.get().get("debug", False)
  skills_config = Configuration.get().get("skills")
  BLACKLISTED_SKILLS = skills_config.get("blacklisted_skills", [])
  PRIORITY_SKILLS = skills_config.get("priority_skills", [])
  SKILLS_DIR = '/opt/mycroft/skills'
  
  installer_config = Configuration.get().get("SkillInstallerSkill")
  
  MINUTES = 60  # number of seconds in a minute (syntatic sugar)
  
  
  def connect():
      global ws
      ws.run_forever()
  
  
  def _starting_up():
      """
          Start loading skills.
  
          Starts
          - SkillManager to load/reloading of skills when needed
          - a timer to check for internet connection
          - adapt intent service
          - padatious intent service
      """
      global ws, skill_manager, event_scheduler
  
      ws.on('intent_failure', FallbackSkill.make_intent_failure_handler(ws))
  
      # Create the Intent manager, which converts utterances to intents
      # This is the heart of the voice invoked skill system
  
      service = IntentService(ws)
      PadatiousService(ws, service)
      event_scheduler = EventScheduler(ws)
  
      # Create a thread that monitors the loaded skills, looking for updates
      skill_manager = SkillManager(ws)
      skill_manager.daemon = True
      # Wait until skills have been loaded once before starting to check
      # network connection
      skill_manager.load_priority()
      skill_manager.start()
      check_connection()
  
  
  def check_connection():
      """
          Check for network connection. If not paired trigger pairing.
          Runs as a Timer every second until connection is detected.
      """
  
  
  [... the rest is omitted ...]
files: mycroft/skills/main.py
files: # Copyright 2018 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.
  #
  
  """Module containing methods needed to load skill
  data such as dialogs, intents and regular expressions.
  """
  
  from os import listdir
  from os.path import splitext, join
  import re
  
  from mycroft.messagebus.message import Message
  
  
  def load_vocab_from_file(path, vocab_type, emitter):
      """Load Mycroft vocabulary from file
      The vocab is sent to the intent handler using the message bus
  
      Args:
          path:           path to vocabulary file (*.voc)
          vocab_type:     keyword name
          emitter:        emitter to access the message bus
          skill_id(str):  skill id
      """
      if path.endswith('.voc'):
          with open(path, 'r') as voc_file:
              for line in voc_file.readlines():
                  if line.startswith("#"):
                      continue
                  parts = line.strip().split("|")
                  entity = parts[0]
                  emitter.emit(Message("register_vocab", {
                      'start': entity, 'end': vocab_type
                  }))
                  for alias in parts[1:]:
                      emitter.emit(Message("register_vocab", {
                          'start': alias, 'end': vocab_type, 'alias_of': entity
                      }))
  
  
  def load_regex_from_file(path, emitter, skill_id):
      """Load regex from file
      The regex is sent to the intent handler using the message bus
  
      Args:
          path:       path to vocabulary file (*.voc)
          emitter:    emitter to access the message bus
      """
      if path.endswith('.rx'):
          with open(path, 'r') as reg_file:
              for line in reg_file.readlines():
                  if line.startswith("#"):
                      continue
                  re.compile(munge_regex(line.strip(), skill_id))
                  emitter.emit(
                      Message("register_vocab",
                              {'regex': munge_regex(line.strip(), skill_id)}))
  
  
  def load_vocabulary(basedir, emitter, skill_id):
      """Load vocabulary from all files in the specified directory.
  
      Args:
          basedir (str): path of directory to load from
          emitter (messagebus emitter): websocket used to send the vocab to
                                        the intent service
          skill_id: skill the data belongs to
      """
      for vocab_file in listdir(basedir):
          if vocab_file.endswith(".voc"):
              vocab_type = to_alnum(skill_id) + splitext(vocab_file)[0]
              load_vocab_from_file(
                  join(basedir, vocab_file), vocab_type, emitter)
  
  
  def load_regex(basedir, emitter, skill_id):
      """Load regex from all files in the specified directory.
  
      Args:
          basedir (str): path of directory to load from
          emitter (messagebus emitter): websocket used to send the vocab to
                                        the intent service
          skill_id (str): skill identifier
      """
      for regex_type in listdir(basedir):
          if regex_type.endswith(".rx"):
              load_regex_from_file(join(basedir, regex_type), emitter, skill_id)
  
  
  def to_alnum(skill_id):
      """Convert a skill id to only
  
  [... the rest is omitted ...]
files: mycroft/skills/skill_data.py
files: # Copyright 2018 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.
  #
  
  """Module containing methods needed to load skill
  data such as dialogs, intents and regular expressions.
  """
  
  from os import listdir
  from os.path import splitext, join
  import re
  
  from mycroft.messagebus.message import Message
  
  
  def load_vocab_from_file(path, vocab_type, emitter):
      """Load Mycroft vocabulary from file
      The vocab is sent to the intent handler using the message bus
  
      Args:
          path:           path to vocabulary file (*.voc)
          vocab_type:     keyword name
          emitter:        emitter to access the message bus
          skill_id(str):  skill id
      """
      if path.endswith('.voc'):
          with open(path, 'r') as voc_file:
              for line in voc_file.readlines():
                  if line.startswith("#"):
                      continue
                  parts = line.strip().split("|")
                  entity = parts[0]
                  emitter.emit(Message("register_vocab", {
                      'start': entity, 'end': vocab_type
                  }))
                  for alias in parts[1:]:
                      emitter.emit(Message("register_vocab", {
                          'start': alias, 'end': vocab_type, 'alias_of': entity
                      }))
  
  
  def load_regex_from_file(path, emitter, skill_id):
      """Load regex from file
      The regex is sent to the intent handler using the message bus
  
      Args:
          path:       path to vocabulary file (*.voc)
          emitter:    emitter to access the message bus
      """
      if path.endswith('.rx'):
          with open(path, 'r') as reg_file:
              for line in reg_file.readlines():
                  if line.startswith("#"):
                      continue
                  re.compile(munge_regex(line.strip(), skill_id))
                  emitter.emit(
                      Message("register_vocab",
                              {'regex': munge_regex(line.strip(), skill_id)}))
  
  
  def load_vocabulary(basedir, emitter, skill_id):
      """Load vocabulary from all files in the specified directory.
  
      Args:
          basedir (str): path of directory to load from
          emitter (messagebus emitter): websocket used to send the vocab to
                                        the intent service
          skill_id: skill the data belongs to
      """
      for vocab_file in listdir(basedir):
          if vocab_file.endswith(".voc"):
              vocab_type = to_letters(skill_id) + splitext(vocab_file)[0]
              load_vocab_from_file(
                  join(basedir, vocab_file), vocab_type, emitter)
  
  
  def load_regex(basedir, emitter, skill_id):
      """Load regex from all files in the specified directory.
  
      Args:
          basedir (str): path of directory to load from
          emitter (messagebus emitter): websocket used to send the vocab to
                                        the intent service
          skill_id (int): skill identifier
      """
      for regex_type in listdir(basedir):
          if regex_type.endswith(".rx"):
              load_regex_from_file(
                  join(basedir, regex_type), emitter, skill_id)
  
  
  def to_letters(number):
      """Convert number to string of letters.
  
  
  
  [... the rest is omitted ...]
files: mycroft/skills/skill_data.py
files: # -*- coding: utf-8 -*-
  # Copyright 2017 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.
  #
  import sys
  import unittest
  
  import mock
  from adapt.intent import IntentBuilder
  from os.path import join, dirname, abspath
  from re import error
  from datetime import datetime
  
  from mycroft.configuration import Configuration
  from mycroft.messagebus.message import Message
  from mycroft.skills.skill_data import load_regex_from_file, load_regex, \
      load_vocab_from_file, load_vocabulary
  from mycroft.skills.core import MycroftSkill, load_skill, \
      create_skill_descriptor, open_intent_envelope
  
  from mycroft.configuration.config import LocalConf, DEFAULT_CONFIG
  
  BASE_CONF = LocalConf(DEFAULT_CONFIG)
  
  
  class MockEmitter(object):
      def __init__(self):
          self.reset()
  
      def emit(self, message):
          self.types.append(message.type)
          self.results.append(message.data)
  
      def get_types(self):
          return self.types
  
      def get_results(self):
          return self.results
  
      def on(self, event, f):
          pass
  
      def reset(self):
          self.types = []
          self.results = []
  
  
  class MycroftSkillTest(unittest.TestCase):
      emitter = MockEmitter()
      regex_path = abspath(join(dirname(__file__), '../regex_test'))
      vocab_path = abspath(join(dirname(__file__), '../vocab_test'))
  
      def setUp(self):
          self.emitter.reset()
  
      def check_vocab_from_file(self, filename, vocab_type=None,
                                result_list=None):
          result_list = result_list or []
          load_vocab_from_file(join(self.vocab_path, filename), vocab_type,
                               self.emitter)
          self.check_emitter(result_list)
  
      def check_regex_from_file(self, filename, result_list=None):
          result_list = result_list or []
          regex_file = join(self.regex_path, filename)
          load_regex_from_file(regex_file, self.emitter, 'A')
          self.check_emitter(result_list)
  
      def check_vocab(self, path, result_list=None):
          result_list = result_list or []
          load_vocabulary(path, self.emitter, 'A')
          self.check_emitter(result_list)
  
      def check_regex(self, path, result_list=None):
          result_list = result_list or []
          load_regex(path, self.emitter, 'A')
          self.check_emitter(result_list)
  
      def check_emitter(self, result_list):
          for type in self.emitter.get_types():
              self.assertEquals(type, 'register_vocab')
          self.assertEquals(sorted(self.emitter.get_results(),
                                   key=lambda d: sorted(d.items())),
                            sorted(result_list, key=lambda d: sorted(d.items())))
          self.emitter.reset()
  
      def test_load_regex_from_file_single(self):
          self.check_regex_from_file('valid/single.rx',
                                     [{'regex': '(?P<ASingleTest>.*)'}])
  
      def test_load_regex_from_file_multiple(self):
          self.check_regex_from_file('valid/multiple.rx',
                                     [{'regex
  
  [... the rest is omitted ...]
files: test/unittests/skills/core.py
files: # -*- coding: utf-8 -*-
  # Copyright 2017 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.
  #
  import sys
  import unittest
  
  import mock
  from adapt.intent import IntentBuilder
  from os.path import join, dirname, abspath
  from re import error
  from datetime import datetime
  
  from mycroft.configuration import Configuration
  from mycroft.messagebus.message import Message
  from mycroft.skills.skill_data import load_regex_from_file, load_regex, \
      load_vocab_from_file, load_vocabulary
  from mycroft.skills.core import MycroftSkill, load_skill, \
      create_skill_descriptor, open_intent_envelope
  
  from mycroft.configuration.config import LocalConf, DEFAULT_CONFIG
  
  BASE_CONF = LocalConf(DEFAULT_CONFIG)
  
  
  class MockEmitter(object):
      def __init__(self):
          self.reset()
  
      def emit(self, message):
          self.types.append(message.type)
          self.results.append(message.data)
  
      def get_types(self):
          return self.types
  
      def get_results(self):
          return self.results
  
      def on(self, event, f):
          pass
  
      def reset(self):
          self.types = []
          self.results = []
  
  
  class MycroftSkillTest(unittest.TestCase):
      emitter = MockEmitter()
      regex_path = abspath(join(dirname(__file__), '../regex_test'))
      vocab_path = abspath(join(dirname(__file__), '../vocab_test'))
  
      def setUp(self):
          self.emitter.reset()
  
      def check_vocab_from_file(self, filename, vocab_type=None,
                                result_list=None):
          result_list = result_list or []
          load_vocab_from_file(join(self.vocab_path, filename), vocab_type,
                               self.emitter)
          self.check_emitter(result_list)
  
      def check_regex_from_file(self, filename, result_list=None):
          result_list = result_list or []
          load_regex_from_file(join(self.regex_path, filename), self.emitter, 0)
          self.check_emitter(result_list)
  
      def check_vocab(self, path, result_list=None):
          result_list = result_list or []
          load_vocabulary(path, self.emitter, 0)
          self.check_emitter(result_list)
  
      def check_regex(self, path, result_list=None):
          result_list = result_list or []
          load_regex(path, self.emitter, 0)
          self.check_emitter(result_list)
  
      def check_emitter(self, result_list):
          for type in self.emitter.get_types():
              self.assertEquals(type, 'register_vocab')
          self.assertEquals(sorted(self.emitter.get_results(),
                                   key=lambda d: sorted(d.items())),
                            sorted(result_list, key=lambda d: sorted(d.items())))
          self.emitter.reset()
  
      def test_load_regex_from_file_single(self):
          self.check_regex_from_file('valid/single.rx',
                                     [{'regex': '(?P<ASingleTest>.*)'}])
  
      def test_load_regex_from_file_multiple(self):
          self.check_regex_from_file('valid/multiple.rx',
                                     [{'regex': '(?P<AMultiple
  
  [... the rest is omitted ...]
files: test/unittests/skills/core.py