瀏覽代碼

fix yaml loader with include statement

nico 8 年之前
父節點
當前提交
014fe38dad
共有 1 個文件被更改,包括 21 次插入33 次删除
  1. 21 33
      core/ConfigurationManager/YAMLLoader.py

+ 21 - 33
core/ConfigurationManager/YAMLLoader.py

@@ -1,8 +1,7 @@
 import logging
 import os
-import yaml
 
-from core.Utils import Utils
+import yaml
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
@@ -28,36 +27,14 @@ class YAMLLoader:
         root_dir = os.path.join(current_dir, "../../")
         root_dir = os.path.normpath(root_dir)
         logger.debug("Root dir: %s " % root_dir)
-        file_path_to_load = os.path.join(root_dir, yaml_file)
-        logger.debug("File path to load: %s " % file_path_to_load)
-        if os.path.isfile(yaml_file):
-            # data = IncludeLoader(open(file_path_to_load, 'r')).get_data()
-            # print Utils.print_yaml_nicely(data)
-            data = IncludeImport(file_path_to_load).get_data()
+        cls.file_path_to_load = os.path.join(root_dir, 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)
+            data = inc_import.get_data()
             return data
         else:
-            raise YAMLFileNotFound("File %s not found" % file_path_to_load)
-
-
-class IncludeLoader(yaml.Loader):
-
-    def __init__(self, *args, **kwargs):
-        super(IncludeLoader, self).__init__(*args, **kwargs)
-        self.add_constructor('#include', self._include)
-        if 'root' in kwargs:
-            self.root = kwargs['root']
-        elif isinstance(self.stream, file):
-            self.root = os.path.dirname(self.stream.name)
-        else:
-            self.root = os.path.curdir
-
-    def _include(self, loader, node):
-        oldRoot = self.root
-        filename = os.path.join(self.root, loader.construct_scalar(node))
-        self.root = os.path.dirname(filename)
-        data = yaml.load(open(filename, 'r'))
-        self.root = oldRoot
-        return data
+            raise YAMLFileNotFound("File %s not found" % cls.file_path_to_load)
 
 
 class IncludeImport(object):
@@ -67,12 +44,22 @@ class IncludeImport(object):
         Load yaml file, with includes statement
         :param file_path: path to the yaml file to load
         """
+        # get the parent dir. will be used in case of relative path
+        parent_dir = os.path.normpath(file_path + os.sep + os.pardir)
+
+        # load the yaml file
         self.data = yaml.load(open(file_path, 'r'))
-        # print "content: %s" % self.data
+
+        # add included brain
         if isinstance(self.data, list):
             for el in self.data:
                 if "includes" in el:
                     for inc in el["includes"]:
+                        # if the path is relative, we add the root path
+                        if not os.path.isabs(inc):  # os.path.isabs returns True if the path is absolute
+                            # logger.debug("File path %s is relative, adding the root path" % inc)
+                            inc = os.path.join(parent_dir, inc)
+                            # logger.debug("New path: %s" % inc)
                         self.update(yaml.load(open(inc)))
 
     def get_data(self):
@@ -82,7 +69,8 @@ class IncludeImport(object):
         # print "cur_data: %s" % self.data
         # print "data to add %s" % data_to_add
         # we add each synapse inside the extended brain into the main brain data
-        for el in data_to_add:
-            self.data.append(el)
+        if data_to_add is not None:
+            for el in data_to_add:
+                self.data.append(el)
         # print "final data: %s" % self.data