Ver Fonte

load model file for snowboy depending on tu given path

nico há 8 anos atrás
pai
commit
79736cced6

+ 2 - 1
kalliope/core/ConfigurationManager/BrainLoader.py

@@ -207,7 +207,8 @@ class BrainLoader(object):
             return brain_path
         raise IOError("Default brain.yml file not found")
 
-    def _get_brain_file_path(self):
+    @staticmethod
+    def _get_brain_file_path():
         """
         used to load the brain.yml file
         This function will try to load the file in this order:

+ 2 - 1
kalliope/core/ConfigurationManager/SettingLoader.py

@@ -505,7 +505,8 @@ class SettingLoader(object):
 
         return default_synapse
 
-    def _get_settings_file_path(self):
+    @staticmethod
+    def _get_settings_file_path():
         """
         used to load the settings.yml file
         This function will try to load the file in this order:

+ 47 - 0
kalliope/core/TriggerModule.py

@@ -0,0 +1,47 @@
+import os
+
+import logging
+
+from kalliope.core.ConfigurationManager import utils
+
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+
+class TriggerModule(object):
+    """
+    Mother class of a trigger object
+    """
+
+    def __init__(self):
+        super(TriggerModule, self).__init__()
+
+    @staticmethod
+    def get_file_from_path(file_path):
+        """
+        Trigger can be based on a model file, or other file.
+        If a file is precised in settings, the path can be relative or absolute.
+        If the path is absolute, there is no problem when can try to load it directly
+        If the path is relative, we need to test the get the full path of the file in the following order:
+            - from the current directory where kalliope has been called. Eg: /home/me/Documents/kalliope_config
+            - from /etc/kalliope
+            - from the root of the project. Eg: /usr/local/lib/python2.7/dist-packages/kalliope-version/kalliope/<file_path>
+
+        :return: absolute path
+        """
+        if not os.path.isabs(file_path):
+            path_order = {
+                1: os.getcwd() + os.sep + file_path,
+                2: "/etc/kalliope" + os.sep + file_path,
+                3: utils.get_root_kalliope_path() + os.sep + file_path
+            }
+
+            for key in sorted(path_order):
+                file_path_to_test = path_order[key]
+                logger.debug("Trigger: Try to load given file from %s: %s" % (key, file_path_to_test))
+                if os.path.isfile(file_path_to_test):
+                    logger.debug("Trigger: given path found in %s" % file_path_to_test)
+                    return file_path_to_test
+
+        logger.debug("Trigger file to load will be %s" % file_path)
+        return file_path

+ 10 - 23
kalliope/trigger/snowboy/snowboy.py

@@ -3,9 +3,14 @@ import logging
 import os
 import time
 
+from kalliope.core.TriggerModule import TriggerModule
 from kalliope.trigger.snowboy import snowboydecoder
 
 
+class SnowboyModelNotFounfd(Exception):
+    pass
+
+
 class MissingParameterException(Exception):
     pass
 
@@ -13,9 +18,10 @@ logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
 
-class Snowboy(object):
+class Snowboy(TriggerModule):
 
     def __init__(self, **kwargs):
+        super(Snowboy, self).__init__()
         # pause listening boolean
         self.interrupted = False
         self.kill_received = False
@@ -27,12 +33,12 @@ class Snowboy(object):
 
         # get the pmdl file to load
         self.pmdl = kwargs.get('pmdl_file', None)
-
         if self.pmdl is None:
             raise MissingParameterException("Pmdl file is required with snowboy")
 
-        # get the pmdl path from root of kalliope module
-        self.pmdl_path = self._get_root_pmdl_path(self.pmdl)
+        self.pmdl_path = self.get_file_from_path(self.pmdl)
+        if not os.path.isfile(self.pmdl_path):
+            raise SnowboyModelNotFounfd("The snowboy model file %s does not exist" % self.pmdl_path)
 
         self.detector = snowboydecoder.HotwordDetector(self.pmdl_path, sensitivity=0.5, detected_callback=self.callback,
                                                        interrupt_check=self.interrupt_callback,
@@ -78,22 +84,3 @@ class Snowboy(object):
         logger.debug("Unpausing snowboy process")
         self.detector.paused = False
 
-    @staticmethod
-    def _get_root_pmdl_path(pmdl_file):
-        """
-        Return the full path of the pmdl file
-        :Example:
-            pmdl_path = cls._get_root_pmdl_path(pmdl_file)
-        .. raises:: IOError
-        .. warnings:: Static method and Private
-        """
-
-        # get current script directory path. We are in /an/unknown/path/kalliope/trigger/snowboy
-        cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
-        # get parent dir. Now we are in /an/unknown/path/kalliope
-        parent_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir + os.sep + os.pardir)
-        pmdl_path = parent_dir + os.sep + pmdl_file
-        logger.debug("Real pmdl_file path: %s" % pmdl_path)
-        if os.path.isfile(pmdl_path):
-            return pmdl_path
-        raise IOError("Pmdl file not found: %s" % pmdl_path)