Utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from my_package.my_module import my_class
  50. mod = __import__('my_package.my_module', fromlist=['my_class'])
  51. klass = getattr(mod, 'my_class')
  52. :param package_name: name of the package where we will find the module to load (neurons, tts, stt, trigger)
  53. :param module_name: name of the module from the package_name to load. This one is capitalized. Eg: Snowboy
  54. :param parameters: dict parameters to send as argument to the module
  55. :return:
  56. """
  57. logger.debug("Run plugin %s with parameter %s" % (module_name, parameters))
  58. module_name_with_path = package_name + "." + module_name.lower() + "." + module_name.lower()
  59. mod = __import__(module_name_with_path, fromlist=[module_name])
  60. try:
  61. klass = getattr(mod, module_name)
  62. except AttributeError:
  63. logger.debug("Error: No module named %s " % module_name)
  64. raise ModuleNotFoundError("The module %s does not exist in package %s" % (module_name, package_name))
  65. if klass is not None:
  66. # run the plugin
  67. if not parameters:
  68. return klass()
  69. elif isinstance(parameters, dict):
  70. return klass(**parameters)
  71. else:
  72. return klass(parameters)
  73. return None
  74. @classmethod
  75. def print_yaml_nicely(cls, to_print):
  76. """
  77. Used for debug
  78. :param to_print: Dict to print nicely
  79. :return:
  80. """
  81. import json
  82. print json.dumps(to_print, indent=2)