logger.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os
  2. import sys
  3. def colors(state):
  4. color = ''
  5. if (state == 'BLUE'):
  6. color = '\033[94m'
  7. if (state == 'GREEN'):
  8. color = '\033[92m'
  9. if (state == 'YELLOW'):
  10. color = '\033[93m'
  11. if (state == 'RED'):
  12. color = '\033[91m'
  13. if (state == 'ENDC'):
  14. color = '\033[0m'
  15. if (state == 'WHITE'):
  16. color = '\033[0m'
  17. return color
  18. def supports_color():
  19. """
  20. from https://github.com/django/django/blob/master/django/core/management/color.py
  21. Return True if the running system's terminal supports color,
  22. and False otherwise.
  23. """
  24. plat = sys.platform
  25. supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
  26. # isatty is not always implemented, #6223.
  27. is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
  28. if not supported_platform or not is_a_tty:
  29. return False
  30. return True
  31. def log(string, color):
  32. if not supports_color():
  33. print(string)
  34. else:
  35. print('\033[1m' + colors(color) + string + colors("ENDC"))
  36. def seperator(color):
  37. if not supports_color():
  38. print("-" * 50)
  39. else:
  40. print('\033[1m' + colors(color) + ("-" * 50) + colors("ENDC"))