1
0

utils.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """
  2. tasks.utils
  3. ~~~~~~~~~~~
  4. Invoke utilities
  5. """
  6. import sys
  7. try:
  8. from blessings import Terminal
  9. try:
  10. terminal = Terminal()
  11. HAS_BLESSINGS = True
  12. except Exception: # pylint: disable=broad-except
  13. terminal = None
  14. HAS_BLESSINGS = False
  15. except ImportError:
  16. terminal = None
  17. HAS_BLESSINGS = False
  18. def exit_invoke(exitcode, message=None, *args, **kwargs):
  19. if message is not None:
  20. if exitcode > 0:
  21. warn(message, *args, **kwargs)
  22. else:
  23. info(message, *args, **kwargs)
  24. sys.exit(exitcode)
  25. def info(message, *args, **kwargs):
  26. if not isinstance(message, str):
  27. message = str(message)
  28. message = message.format(*args, **kwargs)
  29. if terminal:
  30. message = terminal.bold(terminal.green(message))
  31. write_message(message)
  32. def warn(message, *args, **kwargs):
  33. if not isinstance(message, str):
  34. message = str(message)
  35. message = message.format(*args, **kwargs)
  36. if terminal:
  37. message = terminal.bold(terminal.yellow(message))
  38. write_message(message)
  39. def error(message, *args, **kwargs):
  40. if not isinstance(message, str):
  41. message = str(message)
  42. message = message.format(*args, **kwargs)
  43. if terminal:
  44. message = terminal.bold(terminal.red(message))
  45. write_message(message)
  46. def write_message(message):
  47. sys.stderr.write(message)
  48. if not message.endswith("\n"):
  49. sys.stderr.write("\n")
  50. sys.stderr.flush()