Utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import logging
  2. logging.basicConfig()
  3. logger = logging.getLogger("kalliope")
  4. class ModuleNotFoundError(Exception):
  5. """
  6. The module can not been found
  7. .. notes: Check the case: must be in lower case.
  8. """
  9. pass
  10. class Utils(object):
  11. color_list = dict(
  12. PURPLE='\033[95m',
  13. BLUE='\033[94m',
  14. GREEN='\033[92m',
  15. YELLOW='\033[93m',
  16. RED='\033[91m',
  17. ENDLINE='\033[0m',
  18. BOLD='\033[1m',
  19. UNDERLINE='\033[4m'
  20. )
  21. @classmethod
  22. def print_info(cls, text_to_print):
  23. print cls.color_list["BLUE"] + text_to_print + cls.color_list["ENDLINE"]
  24. @classmethod
  25. def print_success(cls, text_to_print):
  26. print cls.color_list["GREEN"] + text_to_print + cls.color_list["ENDLINE"]
  27. @classmethod
  28. def print_warning(cls, text_to_print):
  29. print cls.color_list["YELLOW"] + text_to_print + cls.color_list["ENDLINE"]
  30. @classmethod
  31. def print_danger(cls, text_to_print):
  32. print cls.color_list["RED"] + text_to_print + cls.color_list["ENDLINE"]
  33. @classmethod
  34. def print_header(cls, text_to_print):
  35. print cls.color_list["HEADER"] + text_to_print + cls.color_list["ENDLINE"]
  36. @classmethod
  37. def print_header(cls, text_to_print):
  38. print cls.color_list["PURPLE"] + text_to_print + cls.color_list["ENDLINE"]
  39. @classmethod
  40. def print_bold(cls, text_to_print):
  41. print cls.color_list["BOLD"] + text_to_print + cls.color_list["ENDLINE"]
  42. @classmethod
  43. def print_underline(cls, text_to_print):
  44. print cls.color_list["UNDERLINE"] + text_to_print + cls.color_list["ENDLINE"]
  45. @classmethod
  46. def get_dynamic_class_instantiation(cls, package_name, module_name, parameters=None):
  47. """
  48. Load a python class dynamically
  49. :param package_name: name of the package where we will find the module to load (neurons, tts, stt, trigger)
  50. :param module_name: name of the module from the package_name to load
  51. :param parameters: dict parameters to send as argument to the module
  52. :return:
  53. """
  54. logger.debug("Run plugin %s with parameter %s" % (module_name, parameters))
  55. mod = __import__(package_name, fromlist=[module_name])
  56. try:
  57. klass = getattr(mod, module_name)
  58. except AttributeError:
  59. logger.debug("Error: No module named %s " % module_name)
  60. raise ModuleNotFoundError("The module %s does not exist in package %s" % (module_name, package_name))
  61. if klass is not None:
  62. # run the plugin
  63. if not parameters:
  64. return klass()
  65. elif isinstance(parameters, dict):
  66. return klass(**parameters)
  67. else:
  68. return klass(parameters)
  69. return None
  70. @classmethod
  71. def print_yaml_nicely(cls, to_print):
  72. """
  73. Used for debug
  74. :param to_print: Dict to print nicely
  75. :return:
  76. """
  77. import json
  78. print json.dumps(to_print, indent=2)