util.py 875 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import contextlib
  2. import sys
  3. import os
  4. import platform
  5. def term():
  6. """Get the Terminal reference to make output pretty
  7. Returns:
  8. (blessings.Terminal): Returns
  9. a `blessings <https://blessings.readthedocs.io/en/latest>`_ terminal
  10. instance. If running in windows and not cygwin it will return an
  11. `intercessions <https://pypi.org/project/intercessions>`_ terminal
  12. instance instead
  13. """
  14. if not hasattr(term, '_handle'):
  15. if sys.platform != "cygwin" and platform.system() == 'Windows':
  16. from intercessions import Terminal
  17. else:
  18. from blessings import Terminal
  19. term._handle = Terminal()
  20. return term._handle
  21. @contextlib.contextmanager
  22. def pushd(newDir):
  23. previousDir = os.getcwd()
  24. os.chdir(newDir)
  25. try:
  26. yield
  27. finally:
  28. os.chdir(previousDir)