Преглед на файлове

update settings pojo. not anymore singleton

Singleton is now placed on the Settings loader class
nico преди 8 години
родител
ревизия
a21fcfd9e6

+ 53 - 58
core/ConfigurationManager/SettingLoader.py

@@ -2,6 +2,7 @@ import logging
 
 from YAMLLoader import YAMLLoader
 from core.FileManager import FileManager
+from core.Models import Singleton
 from core.Models.RestAPI import RestAPI
 from core.Models.Settings import Settings
 from core.Models.Stt import Stt
@@ -41,21 +42,25 @@ class SettingNotFound(Exception):
     pass
 
 
+@Singleton
 class SettingLoader(object):
     """
     This Class is used to get the Settings YAML and the Settings as an object
     """
 
-    def __init__(self):
-        pass
+    def __init__(self, file_path=None):
+        logger.debug("Loading settings with file path: %s" % file_path)
+        self.file_path = file_path
+        if self.file_path is None:
+            # use default file if not provided
+            self.file_path = FILE_NAME
+        self.yaml_config = self._get_yaml_config
+        self.settings = self._get_settings()
 
-    @classmethod
-    def get_yaml_config(cls, file_path=None):
+    def _get_yaml_config(self):
         """
         Class Methods which loads default or the provided YAML file and return it as a String
 
-        :param file_path: the setting file path to load if None takes default
-        :type file_path: str
         :return: The loaded settings YAML
         :rtype: dict
 
@@ -64,18 +69,12 @@ class SettingLoader(object):
 
         .. warnings:: Class Method
         """
+        return YAMLLoader.get_config(self.file_path)
 
-        if file_path is None:
-            file_path = FILE_NAME
-        return YAMLLoader.get_config(file_path)
-
-    @classmethod
-    def get_settings(cls, file_path=None):
+    def _get_settings(self):
         """
         Class Methods which loads default or the provided YAML file and return a Settings Object
 
-        :param file_path: the setting file path to load
-        :type file_path: str
         :return: The loaded Settings
         :rtype: Settings
 
@@ -88,36 +87,32 @@ class SettingLoader(object):
         """
 
         # create a new setting
-        setting_object = Settings.Instance()
-        logger.debug("Is Settings already loaded ? %r" % setting_object.is_loaded)
-        if setting_object.is_loaded is False:
-
-            # Get the setting parameters
-            settings = cls.get_yaml_config(file_path)
-            default_stt_name = cls._get_default_speech_to_text(settings)
-            default_tts_name = cls._get_default_text_to_speech(settings)
-            default_trigger_name = cls._get_default_trigger(settings)
-            stts = cls._get_stts(settings)
-            ttss = cls._get_ttss(settings)
-            triggers = cls._get_triggers(settings)
-            random_wake_up_answers = cls._get_random_wake_up_answers(settings)
-            random_wake_up_sounds = cls._get_random_wake_up_sounds(settings)
-            rest_api = cls._get_rest_api(settings)
-            cache_path = cls._get_cache_path(settings)
-
-            # Load the setting singleton with the parameters
-            setting_object.default_tts_name = default_tts_name
-            setting_object.default_stt_name = default_stt_name
-            setting_object.default_trigger_name = default_trigger_name
-            setting_object.stts = stts
-            setting_object.ttss = ttss
-            setting_object.triggers = triggers
-            setting_object.random_wake_up_answers = random_wake_up_answers
-            setting_object.random_wake_up_sounds = random_wake_up_sounds
-            setting_object.rest_api = rest_api
-            setting_object.cache_path = cache_path
-            # The Settings Singleton is loaded
-            setting_object.is_loaded = True
+        setting_object = Settings()
+
+        # Get the setting parameters
+        settings = self._get_yaml_config()
+        default_stt_name = self._get_default_speech_to_text(settings)
+        default_tts_name = self._get_default_text_to_speech(settings)
+        default_trigger_name = self._get_default_trigger(settings)
+        stts = self._get_stts(settings)
+        ttss = self._get_ttss(settings)
+        triggers = self._get_triggers(settings)
+        random_wake_up_answers = self._get_random_wake_up_answers(settings)
+        random_wake_up_sounds = self._get_random_wake_up_sounds(settings)
+        rest_api = self._get_rest_api(settings)
+        cache_path = self._get_cache_path(settings)
+
+        # Load the setting singleton with the parameters
+        setting_object.default_tts_name = default_tts_name
+        setting_object.default_stt_name = default_stt_name
+        setting_object.default_trigger_name = default_trigger_name
+        setting_object.stts = stts
+        setting_object.ttss = ttss
+        setting_object.triggers = triggers
+        setting_object.random_wake_up_answers = random_wake_up_answers
+        setting_object.random_wake_up_sounds = random_wake_up_sounds
+        setting_object.rest_api = rest_api
+        setting_object.cache_path = cache_path
 
         return setting_object
 
@@ -204,8 +199,8 @@ class SettingLoader(object):
         except KeyError, e:
             raise SettingNotFound("%s setting not found" % e)
 
-    @classmethod
-    def _get_stts(cls, settings):
+    @staticmethod
+    def _get_stts(settings):
         """
         Return a list of stt object
 
@@ -243,8 +238,8 @@ class SettingLoader(object):
                 stts.append(new_stt)
         return stts
 
-    @classmethod
-    def _get_ttss(cls, settings):
+    @staticmethod
+    def _get_ttss(settings):
         """
 
         Return a list of stt object
@@ -283,8 +278,8 @@ class SettingLoader(object):
                 ttss.append(new_tts)
         return ttss
 
-    @classmethod
-    def _get_triggers(cls, settings):
+    @staticmethod
+    def _get_triggers(settings):
         """
         Return a list of Trigger object
 
@@ -322,8 +317,8 @@ class SettingLoader(object):
                 triggers.append(new_trigger)
         return triggers
 
-    @classmethod
-    def _get_random_wake_up_answers(cls, settings):
+    @staticmethod
+    def _get_random_wake_up_answers(settings):
         """
         Return a list of the wake up answers set up on the settings.yml file
 
@@ -353,8 +348,8 @@ class SettingLoader(object):
 
         return random_wake_up_answers_list
 
-    @classmethod
-    def _get_random_wake_up_sounds(cls, settings):
+    @staticmethod
+    def _get_random_wake_up_sounds(settings):
         """
         Return a list of the wake up sounds set up on the settings.yml file
 
@@ -384,8 +379,8 @@ class SettingLoader(object):
 
         return random_wake_up_sounds_list
 
-    @classmethod
-    def _get_rest_api(cls, settings):
+    @staticmethod
+    def _get_rest_api(settings):
         """
         Return the settings of the RestApi
 
@@ -446,8 +441,8 @@ class SettingLoader(object):
         else:
             raise NullSettingException("rest_api settings cannot be null")
 
-    @classmethod
-    def _get_cache_path(cls, settings):
+    @staticmethod
+    def _get_cache_path(settings):
         """
         Return the path where to store the cache
 

+ 2 - 1
core/MainController.py

@@ -24,7 +24,8 @@ class MainController:
     def __init__(self, brain=None):
         self.brain = brain
         # get global configuration
-        self.settings = SettingLoader.get_settings()
+        sl = SettingLoader.Instance()
+        self.settings = sl.settings
 
         # run the api if the user want it
         if self.settings.rest_api.active:

+ 0 - 4
core/Models/Settings.py

@@ -1,10 +1,7 @@
-from core.Models import Singleton
 import platform
 
 
-@Singleton
 class Settings(object):
-    # TODO review the Singleton, should be Instantiate at the BrainLoader level
     """
     This Class is a Singleton Representing the settings.yml file with synapse
 
@@ -33,5 +30,4 @@ class Settings(object):
         self.triggers = triggers
         self.rest_api = rest_api
         self.cache_path = cache_path
-        self.is_loaded = False
         self.machine = platform.machine()   # can be x86_64 or armv7l

+ 2 - 1
core/NeuronModule.py

@@ -65,7 +65,8 @@ class NeuronModule(object):
         child_name = self.__class__.__name__
         logger.debug("NeuronModule called from class %s with parameters: %s" % (child_name, str(kwargs)))
 
-        self.settings = SettingLoader.get_settings()
+        sl = SettingLoader.Instance()
+        self.settings = sl.settings
         brain_loader = BrainLoader.Instance()
         self.brain = brain_loader.brain
 

+ 2 - 1
core/OrderListener.py

@@ -39,7 +39,8 @@ class OrderListener(Thread):
         self._ignore_stderr()
         self.stt_module_name = stt
         self.callback = callback
-        self.settings = SettingLoader.get_settings()
+        sl = SettingLoader.Instance()
+        self.settings = sl.settings
 
     def run(self):
         """

+ 4 - 2
core/RestAPI/utils.py

@@ -8,7 +8,8 @@ def check_auth(username, password):
     """This function is called to check if a username /
     password combination is valid.
     """
-    settings = SettingLoader.get_settings()
+    sl = SettingLoader.Instance()
+    settings = sl.settings
     return username == settings.rest_api.login and password == settings.rest_api.password
 
 
@@ -23,7 +24,8 @@ def authenticate():
 def requires_auth(f):
     @wraps(f)
     def decorated(*args, **kwargs):
-        settings = SettingLoader.get_settings()
+        sl = SettingLoader.Instance()
+        settings = sl.settings
         if settings.rest_api.password_protected:
             auth = request.authorization
             if not auth or not check_auth(auth.username, auth.password):

+ 2 - 1
core/ShellGui.py

@@ -43,7 +43,8 @@ class ShellGui:
         self.brain = brain
 
         # get settings
-        self.settings = SettingLoader.get_settings()
+        sl = SettingLoader.Instance()
+        self.settings = sl.settings
         locale.setlocale(locale.LC_ALL, '')
 
         self.d = Dialog(dialog="dialog")

+ 2 - 1
core/TTS/TTSModule.py

@@ -56,7 +56,8 @@ class TTSModule(object):
         self.base_cache_path = None
 
         # load settings
-        self.settings = SettingLoader.get_settings()
+        sl = SettingLoader.Instance()
+        self.settings = sl.settings
 
         # create the path in the tmp folder
         base_path = os.path.join(self.settings.cache_path, self.tts_caller_name, self.language, self.voice)

+ 2 - 1
trigger/snowboy/snowboydetect.py

@@ -6,7 +6,8 @@
 from core.ConfigurationManager import SettingLoader
 from sys import version_info
 
-settings = SettingLoader.get_settings()
+sl = SettingLoader.Instance()
+settings = sl.settings
 module_file_path = "%s/_snowboydetect" % settings.machine