Ver Fonte

factorization of the get_real_path function

nico há 8 anos atrás
pai
commit
92544b1588

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

@@ -26,8 +26,8 @@ class BrainLoader(object):
 
     def __init__(self, file_path=None):
         self.file_path = file_path
-        if self.file_path is None:
-            self.file_path = self._get_brain_file_path()
+        if self.file_path is None:  # we don't provide a file path, so search for the default one
+            self.file_path = utils.get_real_file_path(FILE_NAME)
         self.yaml_config = self.get_yaml_config()
         self.brain = self.get_brain()
 
@@ -206,31 +206,3 @@ class BrainLoader(object):
         if os.path.isfile(brain_path):
             return brain_path
         raise IOError("Default brain.yml file not found")
-
-    @staticmethod
-    def _get_brain_file_path():
-        """
-        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
-

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

@@ -53,7 +53,7 @@ class SettingLoader(object):
     def __init__(self, file_path=None):
         self.file_path = file_path
         if self.file_path is None:
-            self.file_path = self._get_settings_file_path()
+            self.file_path = utils.get_real_file_path(FILE_NAME)
         if self.file_path is None:
             raise SettingNotFound("Settings.yml file not found")
         self.yaml_config = self._get_yaml_config()
@@ -505,29 +505,4 @@ class SettingLoader(object):
 
         return default_synapse
 
-    @staticmethod
-    def _get_settings_file_path():
-        """
-        used to load the settings.yml file
-        This function will try to load the file in this order:
-        - from the current directory where kalliope has been called. Eg: /home/me/Documents/kalliope_config
-        - from /etc/kalliope
-        - from the default settings.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 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)
-                return file_path_to_test
-
-        return None
 

+ 38 - 1
kalliope/core/ConfigurationManager/utils.py

@@ -1,10 +1,47 @@
 import inspect
+import logging
 import os
 
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
 
 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
+    return kalliope_root_path
+
+
+def get_real_file_path(file_path_to_test):
+    """
+    Try to return a full path from a given <file_path_to_test>
+    If the path is an absolute on, we return it directly.
+
+    If the path is relative, we try to get the full path in this order:
+    - from the current directory where kalliope has been called + the file_path_to_test.
+    Eg: /home/me/Documents/kalliope_config
+    - from /etc/kalliope + file_path_to_test
+    - from the default file passed as <file_name> at the root of the project
+
+    :param file_path_to_test file path to test
+    :type file_path_to_test: str
+    :return: absolute path to the file file_path_to_test
+    """
+
+    if not os.path.isabs(file_path_to_test):
+        path_order = {
+            1: os.getcwd() + os.sep + file_path_to_test,
+            2: "/etc/kalliope" + os.sep + file_path_to_test,
+            3: get_root_kalliope_path() + os.sep + file_path_to_test
+        }
+
+        for key in sorted(path_order):
+            new_file_path_to_test = path_order[key]
+            logger.debug("Try to load file from %s: %s" % (key, new_file_path_to_test))
+            if os.path.isfile(new_file_path_to_test):
+                logger.debug("File found in %s" % new_file_path_to_test)
+                return new_file_path_to_test
+
+    return file_path_to_test

+ 1 - 16
kalliope/core/TriggerModule.py

@@ -29,19 +29,4 @@ class TriggerModule(object):
 
         :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
+        return utils.get_real_file_path(file_path)