浏览代码

[Refacto] CLean up path management + add some tests

monf 8 年之前
父节点
当前提交
57c3186103

+ 36 - 0
Tests/test_configmanager_utils.py

@@ -0,0 +1,36 @@
+import unittest
+import os
+
+from kalliope.core.Utils.Utils import Utils
+
+
+class TestUtils(unittest.TestCase):
+    """
+    Class to test Utils methods
+    """
+
+    def setUp(self):
+        pass
+
+    def test_get_current_file_parent_parent_path(self):
+        """
+        Expect to get back the parent path file
+        """
+        path_to_test = "../kalliope/core/Utils"
+        expected_result = os.path.normpath("../kalliope/core")
+
+        self.assertEquals(Utils.get_current_file_parent_path(path_to_test),
+                          expected_result,
+                          "fail getting the parent parent path from the given path")
+
+    def test_get_current_file_parent_parent_path(self):
+        """
+        Expect to get back the parent parent path file
+        """
+        path_to_test = "../kalliope/core/Utils"
+        expected_result = os.path.normpath("../kalliope")
+
+        self.assertEquals(Utils.get_current_file_parent_parent_path(path_to_test),
+                          expected_result,
+                          "fail getting the parent parent path from the given path")
+

+ 3 - 3
kalliope/core/ConfigurationManager/BrainLoader.py

@@ -3,7 +3,7 @@ import logging
 import os
 
 from YAMLLoader import YAMLLoader
-from kalliope.core.ConfigurationManager import utils
+from kalliope.core.Utils import Utils
 from kalliope.core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker
 from kalliope.core.Models import Singleton
 from kalliope.core.Models.Brain import Brain
@@ -31,9 +31,9 @@ class BrainLoader(object):
     def __init__(self, file_path=None):
         self.file_path = 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.file_path = Utils.get_real_file_path(FILE_NAME)
         else:
-            self.file_path = utils.get_real_file_path(file_path)
+            self.file_path = Utils.get_real_file_path(file_path)
         # if the returned file path is none, the file doesn't exist
         if self.file_path is None:
             raise BrainNotFound("brain file not found")

+ 3 - 3
kalliope/core/ConfigurationManager/SettingLoader.py

@@ -1,7 +1,7 @@
 import logging
 
 from YAMLLoader import YAMLLoader
-from kalliope.core.ConfigurationManager import utils
+from kalliope.core.Utils import Utils
 from kalliope.core.Models import Singleton
 from kalliope.core.Models.RestAPI import RestAPI
 from kalliope.core.Models.Settings import Settings
@@ -52,9 +52,9 @@ class SettingLoader(object):
     def __init__(self, file_path=None):
         self.file_path = file_path
         if self.file_path is None:
-            self.file_path = utils.get_real_file_path(FILE_NAME)
+            self.file_path = Utils.get_real_file_path(FILE_NAME)
         else:
-            self.file_path = utils.get_real_file_path(file_path)
+            self.file_path = Utils.get_real_file_path(file_path)
         # if the returned file path is none, the file doesn't exist
         if self.file_path is None:
             raise SettingNotFound("Settings.yml file not found")

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

@@ -1,51 +0,0 @@
-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
-
-
-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 or None if is doen't exist
-    """
-
-    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
-
-    else:
-        if os.path.isfile(file_path_to_test):
-            return file_path_to_test
-        else:
-            return None

+ 2 - 4
kalliope/core/TriggerModule.py

@@ -1,8 +1,6 @@
-import os
-
 import logging
 
-from kalliope.core.ConfigurationManager import utils
+from kalliope.core.Utils import Utils
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
@@ -29,4 +27,4 @@ class TriggerModule(object):
 
         :return: absolute path
         """
-        return utils.get_real_file_path(file_path)
+        return Utils.get_real_file_path(file_path)

+ 75 - 8
kalliope/core/Utils/Utils.py

@@ -1,4 +1,6 @@
 import logging
+import os
+import inspect
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
@@ -26,6 +28,11 @@ class Utils(object):
         UNDERLINE='\033[4m'
     )
 
+    ##################
+    #
+    # Shell properly displayed
+    #
+    #########
     @classmethod
     def print_info(cls, text_to_print):
         print cls.color_list["BLUE"] + text_to_print + cls.color_list["ENDLINE"]
@@ -58,8 +65,23 @@ class Utils(object):
     def print_underline(cls, text_to_print):
         print cls.color_list["UNDERLINE"] + text_to_print + cls.color_list["ENDLINE"]
 
-    @classmethod
-    def get_dynamic_class_instantiation(cls, package_name, module_name, parameters=None):
+    @staticmethod
+    def print_yaml_nicely(to_print):
+        """
+        Used for debug
+        :param to_print: Dict to print nicely
+        :return:
+        """
+        import json
+        print json.dumps(to_print, indent=2)
+
+    ##################
+    #
+    # Dynamic loading
+    #
+    #########
+    @staticmethod
+    def get_dynamic_class_instantiation(package_name, module_name, parameters=None):
         """
         Load a python class dynamically
 
@@ -91,12 +113,57 @@ class Utils(object):
                 return klass(parameters)
         return None
 
+    ##################
+    #
+    # Paths management
+    #
+    #########
+    @staticmethod
+    def get_current_file_parent_parent_path(current_script_path):
+        parent_parent_path = os.path.normpath(current_script_path + os.sep + os.pardir + os.sep + os.pardir)
+        return parent_parent_path
+
+    @staticmethod
+    def get_current_file_parent_path(current_script_path):
+        parent_path = os.path.normpath(current_script_path + os.sep + os.pardir)
+        return parent_path
+
     @classmethod
-    def print_yaml_nicely(cls, to_print):
+    def get_real_file_path(cls, file_path_to_test):
         """
-        Used for debug
-        :param to_print: Dict to print nicely
-        :return:
+        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 or None if is doen't exist
         """
-        import json
-        print json.dumps(to_print, indent=2)
+
+        if not os.path.isabs(file_path_to_test):
+            current_script_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
+            path_order = {
+                1: os.getcwd() + os.sep + file_path_to_test,
+                2: "/etc/kalliope" + os.sep + file_path_to_test,
+                # In this case 'get_current_file_parent_parent_path' is corresponding to kalliope root path
+                # from /an/unknown/path/kalliope/core/Utils to /an/unknown/path/kalliope
+                3: cls.get_current_file_parent_path(current_script_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
+
+        else:
+            if os.path.isfile(file_path_to_test):
+                return file_path_to_test
+            else:
+                return None