1
0

utils.py 1.4 KB

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