YAMLLoader.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import logging
  2. import os
  3. import yaml
  4. logging.basicConfig()
  5. logger = logging.getLogger("kalliope")
  6. class YAMLFileNotFound(Exception):
  7. pass
  8. class YAMLLoader:
  9. """
  10. Simple Class to Verify / Load a YAML file.
  11. """
  12. def __init__(self):
  13. pass
  14. @classmethod
  15. def get_config(cls, yaml_file):
  16. """
  17. Return the provided YAML configuration file
  18. :param yaml_file: The path of the configuration file
  19. :type yaml_file: String
  20. :return: the configuration file
  21. :rtype: String
  22. :Example:
  23. YAMLLoader.get_config(brain_file_path)
  24. .. seealso:: SettingLoader, BrainLoader
  25. .. raises:: YAMLFileNotFound
  26. .. warnings:: Class Method and Public
  27. """
  28. current_dir = os.path.dirname(os.path.abspath(__file__))
  29. logger.debug("Current dir: %s " % current_dir)
  30. root_dir = os.path.join(current_dir, "../../")
  31. root_dir = os.path.normpath(root_dir)
  32. logger.debug("Root dir: %s " % root_dir)
  33. cls.file_path_to_load = os.path.join(root_dir, yaml_file)
  34. logger.debug("File path to load: %s " % cls.file_path_to_load)
  35. if os.path.isfile(cls.file_path_to_load):
  36. inc_import = IncludeImport(cls.file_path_to_load)
  37. data = inc_import.get_data()
  38. return data
  39. else:
  40. raise YAMLFileNotFound("File %s not found" % cls.file_path_to_load)
  41. class IncludeImport(object):
  42. """
  43. This class manages the Include Import statement in the brain.yml file
  44. """
  45. def __init__(self, file_path):
  46. """
  47. Load yaml file, with includes statement
  48. :param file_path: path to the yaml file to load
  49. """
  50. # get the parent dir. will be used in case of relative path
  51. parent_dir = os.path.normpath(file_path + os.sep + os.pardir)
  52. # load the yaml file
  53. self.data = yaml.load(open(file_path, 'r'))
  54. # add included brain
  55. if isinstance(self.data, list):
  56. for el in self.data:
  57. if "includes" in el:
  58. for inc in el["includes"]:
  59. # if the path is relative, we add the root path
  60. if not os.path.isabs(inc): # os.path.isabs returns True if the path is absolute
  61. # logger.debug("File path %s is relative, adding the root path" % inc)
  62. inc = os.path.join(parent_dir, inc)
  63. # logger.debug("New path: %s" % inc)
  64. self.update(yaml.load(open(inc)))
  65. def get_data(self):
  66. """
  67. :return: the data for the IncludeImport
  68. """
  69. return self.data
  70. def update(self, data_to_add):
  71. """
  72. Method to Add an other Include statement to the original brain.yml file
  73. :param data_to_add: the data to add to the current brain.yml, provided by an Include Statement
  74. """
  75. # we add each synapse inside the extended brain into the main brain data
  76. if data_to_add is not None:
  77. for el in data_to_add:
  78. self.data.append(el)