Utils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. logger.debug("Run plugin %s with parameter %s" % (module_name, parameters))
  48. mod = __import__(package_name, fromlist=[module_name])
  49. try:
  50. klass = getattr(mod, module_name)
  51. except AttributeError:
  52. logger.debug("Error: No module named %s " % module_name)
  53. raise ModuleNotFoundError("The module %s does not exist in package %s" % (module_name, package_name))
  54. if klass is not None:
  55. # run the plugin
  56. if not parameters:
  57. return klass()
  58. elif isinstance(parameters, dict):
  59. return klass(**parameters)
  60. else:
  61. return klass(parameters)
  62. return None
  63. @classmethod
  64. def print_yaml_nicely(cls, to_print):
  65. """
  66. Used for debug
  67. :param to_print: Dict to print nicely
  68. :return:
  69. """
  70. import json
  71. print json.dumps(to_print, indent=2)