logger.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. import sys
  3. import re
  4. from .settings import settings
  5. sep = "-" * 70
  6. def colors(state):
  7. color = ''
  8. if (state == 'BLUE'):
  9. color = '\033[94m'
  10. if (state == 'GREEN'):
  11. color = '\033[92m'
  12. if (state == 'YELLOW'):
  13. color = '\033[93m'
  14. if (state == 'RED'):
  15. color = '\033[91m'
  16. if (state == 'ENDC'):
  17. color = '\033[0m'
  18. if (state == 'WHITE'):
  19. color = '\033[0m'
  20. return color
  21. def supports_color():
  22. """
  23. from https://github.com/django/django/blob/master/django/core/management/color.py
  24. Return True if the running system's terminal supports color,
  25. and False otherwise.
  26. """
  27. plat = sys.platform
  28. supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
  29. # isatty is not always implemented, #6223.
  30. is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
  31. if not supported_platform or not is_a_tty:
  32. return "No", False
  33. return "Yes", True
  34. def log(string, color):
  35. if supports_color()[1] == False:
  36. print(string)
  37. else:
  38. print('\033[1m{}{}{}'.format(colors(color), string, colors("ENDC")))
  39. if settings.log_to_file == 'True':
  40. try:
  41. with open("pyinstalive_{:s}.log".format(settings.user_to_download),"a+") as f:
  42. no_ansi = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
  43. f.write("{:s}\n".format(no_ansi.sub('', string)))
  44. f.close()
  45. except:
  46. pass
  47. sys.stdout.flush()
  48. def seperator(color):
  49. if supports_color()[1] == False:
  50. print(sep)
  51. else:
  52. print('\033[1m{}{}{}'.format(colors(color), sep, colors("ENDC")))
  53. if settings.log_to_file == 'True':
  54. try:
  55. with open("pyinstalive_{:s}.log".format(settings.user_to_download),"a+") as f:
  56. f.write("{:s}\n".format(sep))
  57. f.close()
  58. except:
  59. pass
  60. sys.stdout.flush()