YAMLLoader.py 3.1 KB

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