浏览代码

[Feature] Singleton Brain and Settings

monf 8 年之前
父节点
当前提交
c9435fb399

+ 8 - 5
core/ConfigurationManager/BrainLoader.py

@@ -3,6 +3,7 @@ import logging
 import os
 
 from YAMLLoader import YAMLLoader
+from core.ConfigurationManager import Singleton
 from core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker
 from core.Models.Brain import Brain
 from core.Models.Event import Event
@@ -13,14 +14,16 @@ from core.Models.Synapse import Synapse
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
-
+@Singleton
 class BrainLoader(object):
 
     def __init__(self):
-        pass
+        # Todo check how to provide the file_path
+        self.brain = self._get_brain()
+        self.yaml_config = self._get_yaml_config()
 
     @classmethod
-    def get_yaml_config(cls, file_path=None):
+    def _get_yaml_config(cls, file_path=None):
         if file_path is None:
             brain_file_path = cls._get_root_brain_path()
         else:
@@ -28,14 +31,14 @@ class BrainLoader(object):
         return YAMLLoader.get_config(brain_file_path)
 
     @classmethod
-    def get_brain(cls, file_path=None):
+    def _get_brain(cls, file_path=None):
         """
         return a brain object from YAML settings
         :return: Brain object
         :rtype: Brain
         """
         # get the brain with dict
-        dict_brain = cls.get_yaml_config(file_path)
+        dict_brain = cls._get_yaml_config(file_path)
         # create a new brain
         brain = Brain()
         brain.brain_yaml = dict_brain

+ 7 - 4
core/ConfigurationManager/SettingLoader.py

@@ -1,6 +1,7 @@
 from YAMLLoader import YAMLLoader
 import logging
 
+from core.ConfigurationManager import Singleton
 from core.FileManager import FileManager
 from core.Models.RestAPI import RestAPI
 from core.Models.Settings import Settings
@@ -25,20 +26,22 @@ class NullSettingException(Exception):
 class SettingNotFound(Exception):
     pass
 
-
+@Singleton
 class SettingLoader(object):
 
     def __init__(self):
-        pass
+        # Todo check how to provide the file_path
+        self.settings = self._get_settings()
+        self.yaml_config = self._get_yaml_config()
 
     @classmethod
-    def get_yaml_config(cls, file_path=None):
+    def _get_yaml_config(cls, file_path=None):
         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(cls, file_path=None):
         """
         Return a Settings object from settings.yml file
         :return:

+ 38 - 0
core/ConfigurationManager/Singleton.py

@@ -0,0 +1,38 @@
+class Singleton:
+    """
+    A non-thread-safe helper class to ease implementing singletons.
+    This should be used as a decorator -- not a metaclass -- to the
+    class that should be a singleton.
+
+    The decorated class can define one `__init__` function that
+    takes only the `self` argument. Other than that, there are
+    no restrictions that apply to the decorated class.
+
+    To get the singleton instance, use the `Instance` method. Trying
+    to use `__call__` will result in a `TypeError` being raised.
+
+    Limitations: The decorated class cannot be inherited from.
+
+    """
+
+    def __init__(self, decorated):
+        self._decorated = decorated
+
+    def Instance(self):
+        """
+        Returns the singleton instance. Upon its first call, it creates a
+        new instance of the decorated class and calls its `__init__` method.
+        On all subsequent calls, the already created instance is returned.
+
+        """
+        try:
+            return self._instance
+        except AttributeError:
+            self._instance = self._decorated()
+            return self._instance
+
+    def __call__(self):
+        raise TypeError('Singletons must be accessed through `Instance()`.')
+
+    def __instancecheck__(self, inst):
+        return isinstance(inst, self._decorated)

+ 1 - 0
core/ConfigurationManager/__init__.py

@@ -1,2 +1,3 @@
 from YAMLLoader import YAMLLoader
 from .SettingLoader import SettingLoader
+from Singleton import Singleton