DnaLoader.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import re
  2. from kalliope.core import Utils
  3. from kalliope.core.ConfigurationManager import YAMLLoader
  4. from kalliope.core.Models.Dna import Dna
  5. class InvalidDNAException(Exception):
  6. pass
  7. VALID_DNA_MODULE_TYPE = ["neuron", "stt", "tts", "trigger"]
  8. class DnaLoader(object):
  9. def __init__(self, file_path):
  10. """
  11. Load a DNA file and check the content of this one
  12. :param file_path: path the the DNA file to load
  13. """
  14. self.file_path = file_path
  15. if self.file_path is None:
  16. raise InvalidDNAException("[DnaLoader] You must set a file file")
  17. self.yaml_config = YAMLLoader.get_config(self.file_path)
  18. self.dna = self._load_dna()
  19. def get_yaml_config(self):
  20. """
  21. Class Methods which loads default or the provided YAML file and return it as a String
  22. :return: The loaded DNA YAML file
  23. :rtype: String
  24. """
  25. return self.yaml_config
  26. def get_dna(self):
  27. """
  28. Return the loaded DNA object if this one is valid
  29. :return:
  30. """
  31. return self.dna
  32. def _load_dna(self):
  33. """
  34. retur a DNA object from a loaded yaml file
  35. :return:
  36. """
  37. new_dna = None
  38. if self._check_dna_file(self.yaml_config):
  39. new_dna = Dna()
  40. new_dna.name = self.yaml_config["name"]
  41. new_dna.module_type = self.yaml_config["type"]
  42. new_dna.author = self.yaml_config["author"]
  43. new_dna.kalliope_supported_version = self.yaml_config["kalliope_supported_version"]
  44. new_dna.tags = self.yaml_config["tags"]
  45. return new_dna
  46. @staticmethod
  47. def _check_dna_file(dna_file):
  48. """
  49. Check the content of a DNA file
  50. :param dna_file: the dna to check
  51. :return: True if ok, False otherwise
  52. """
  53. success_loading = True
  54. if "name" not in dna_file:
  55. Utils.print_danger("The DNA of does not contains a \"name\" tag")
  56. success_loading = False
  57. if "type" not in dna_file:
  58. Utils.print_danger("The DNA of does not contains a \"type\" tag")
  59. success_loading = False
  60. else:
  61. # we have a type, check that is a valid one
  62. if dna_file["type"] not in VALID_DNA_MODULE_TYPE:
  63. Utils.print_danger("The DNA type %s is not valid" % dna_file["type"])
  64. Utils.print_danger("The DNA type must be one of the following: %s" % VALID_DNA_MODULE_TYPE)
  65. success_loading = False
  66. if "kalliope_supported_version" not in dna_file:
  67. Utils.print_danger("The DNA of does not contains a \"kalliope_supported_version\" tag")
  68. success_loading = False
  69. else:
  70. # kalliope_supported_version must be a non empty list
  71. if not isinstance(dna_file["kalliope_supported_version"], list):
  72. Utils.print_danger("kalliope_supported_version is not a list")
  73. success_loading = False
  74. else:
  75. if not dna_file["kalliope_supported_version"]:
  76. Utils.print_danger("kalliope_supported_version cannot be empty")
  77. success_loading = False
  78. else:
  79. for supported_version in dna_file["kalliope_supported_version"]:
  80. # check if major version is provided
  81. if not re.search('^[\d]*[.][\d]*$', str(supported_version)):
  82. Utils.print_danger("kalliope_supported_version cannot handle this format of version %s. "
  83. "Only major version should be provided" % supported_version)
  84. success_loading = False
  85. return success_loading