Browse Source

add some tests to be sure we've loaded a file

nico 8 năm trước cách đây
mục cha
commit
48d796ed26

+ 9 - 0
kalliope/core/ConfigurationManager/BrainLoader.py

@@ -18,6 +18,10 @@ logger = logging.getLogger("kalliope")
 FILE_NAME = "brain.yml"
 
 
+class BrainNotFound(Exception):
+    pass
+
+
 class BrainLoader(object):
     """
     This Class is used to get the brain YAML and the Brain as an object
@@ -28,6 +32,11 @@ class BrainLoader(object):
         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)
+        else:
+            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")
         self.yaml_config = self.get_yaml_config()
         self.brain = self.get_brain()
 

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

@@ -1,5 +1,4 @@
 import logging
-import os
 
 from YAMLLoader import YAMLLoader
 from kalliope.core.ConfigurationManager import utils
@@ -54,6 +53,9 @@ class SettingLoader(object):
         self.file_path = file_path
         if self.file_path is None:
             self.file_path = utils.get_real_file_path(FILE_NAME)
+        else:
+            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")
         self.yaml_config = self._get_yaml_config()

+ 6 - 2
kalliope/core/ConfigurationManager/utils.py

@@ -27,7 +27,7 @@ def get_real_file_path(file_path_to_test):
 
     :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
+    :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):
@@ -44,4 +44,8 @@ def get_real_file_path(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
+    else:
+        if os.path.isfile(file_path_to_test):
+            return file_path_to_test
+        else:
+            return None