Browse Source

load brainfrom cur dir or /etc/brain.yml or default brain

nico 8 years ago
parent
commit
917937dcf5

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

@@ -3,6 +3,7 @@ import logging
 import os
 
 from YAMLLoader import YAMLLoader
+from kalliope.core.ConfigurationManager import utils
 from kalliope.core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker
 from kalliope.core.Models import Singleton
 from kalliope.core.Models.Brain import Brain
@@ -14,6 +15,8 @@ from kalliope.core.Models.Synapse import Synapse
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
+FILE_NAME = "brain.yml"
+
 
 class BrainLoader(object):
     """
@@ -22,8 +25,9 @@ class BrainLoader(object):
     __metaclass__ = Singleton
 
     def __init__(self, file_path=None):
-        logger.debug("Loading brain with file path: %s" % file_path)
         self.file_path = file_path
+        if self.file_path is None:
+            self.file_path = self._get_brain_file_path()
         self.yaml_config = self.get_yaml_config()
         self.brain = self.get_brain()
 
@@ -202,3 +206,30 @@ class BrainLoader(object):
         if os.path.isfile(brain_path):
             return brain_path
         raise IOError("Default brain.yml file not found")
+
+    def _get_brain_file_path(self):
+        """
+        used to load the brain.yml file
+        This function will try to load the file in this order:
+        - from the file given by the user to the function
+        - from the current directory where kalliope has been called. Eg: /home/me/Documents/kalliope_config
+        - from /etc/kalliope
+        - from the default brain.yml at the root of the project
+
+        :return: path to the settings.yml file
+        """
+        path_order = {
+            1: os.getcwd() + os.sep + FILE_NAME,
+            2: "/etc/kalliope" + os.sep + FILE_NAME,
+            3: utils.get_root_kalliope_path() + os.sep + FILE_NAME
+        }
+
+        for key in sorted(path_order):
+            file_path_to_test = path_order[key]
+            logger.debug("Try to load brain.yml file from %s: %s" % (key, file_path_to_test))
+            if os.path.isfile(file_path_to_test):
+                logger.debug("brain.yml file found in %s" % file_path_to_test)
+                return file_path_to_test
+
+        return None
+

+ 4 - 10
kalliope/core/ConfigurationManager/SettingLoader.py

@@ -3,6 +3,7 @@ import logging
 import os
 
 from YAMLLoader import YAMLLoader
+from kalliope.core.ConfigurationManager import utils
 from kalliope.core.Models import Singleton
 from kalliope.core.Models.RestAPI import RestAPI
 from kalliope.core.Models.Settings import Settings
@@ -517,22 +518,15 @@ class SettingLoader(object):
         path_order = {
             1: os.getcwd()+os.sep+FILE_NAME,
             2: "/etc/kalliope"+os.sep+FILE_NAME,
-            3: self.get_root_kalliope_path()+os.sep+FILE_NAME
+            3: utils.get_root_kalliope_path() + os.sep + FILE_NAME
         }
 
         for key in sorted(path_order):
             file_path_to_test = path_order[key]
-            logger.debug("Try to load settings file from %s: %s" % (key, file_path_to_test))
+            logger.debug("Try to load settings.yml file from %s: %s" % (key, file_path_to_test))
             if os.path.isfile(file_path_to_test):
-                logger.debug("Settings.yml file found in %s" % file_path_to_test)
+                logger.debug("settings.yml file found in %s" % file_path_to_test)
                 return file_path_to_test
 
         return None
 
-    @staticmethod
-    def get_root_kalliope_path():
-        # here we are in /an/unknown/path/kalliope/core/ConfigurationManager
-        current_script_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
-        # get parent dir. Now we are in /an/unknown/path/kalliope
-        kalliope_root_path = os.path.normpath(current_script_path + os.sep + os.pardir + os.sep + os.pardir)
-        return kalliope_root_path

+ 1 - 6
kalliope/core/ConfigurationManager/YAMLLoader.py

@@ -41,12 +41,7 @@ class YAMLLoader:
         .. warnings:: Class Method and Public
         """
 
-        current_dir = os.path.dirname(os.path.abspath(__file__))
-        logger.debug("Current dir: %s " % current_dir)
-        root_dir = os.path.join(current_dir, "../../")
-        root_dir = os.path.normpath(root_dir)
-        logger.debug("Root dir: %s " % root_dir)
-        cls.file_path_to_load = os.path.join(root_dir, yaml_file)
+        cls.file_path_to_load = yaml_file
         logger.debug("File path to load: %s " % cls.file_path_to_load)
         if os.path.isfile(cls.file_path_to_load):
             inc_import = IncludeImport(cls.file_path_to_load)

+ 10 - 0
kalliope/core/ConfigurationManager/utils.py

@@ -0,0 +1,10 @@
+import inspect
+import os
+
+
+def get_root_kalliope_path():
+    # here we are in /an/unknown/path/kalliope/core/ConfigurationManager
+    current_script_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
+    # get parent dir. Now we are in /an/unknown/path/kalliope
+    kalliope_root_path = os.path.normpath(current_script_path + os.sep + os.pardir + os.sep + os.pardir)
+    return kalliope_root_path