text_window.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. import os
  3. import time
  4. import subprocess
  5. from openpilot.common.basedir import BASEDIR
  6. class TextWindow:
  7. def __init__(self, text):
  8. try:
  9. self.text_proc = subprocess.Popen(["./text", text],
  10. stdin=subprocess.PIPE,
  11. cwd=os.path.join(BASEDIR, "selfdrive", "ui"),
  12. close_fds=True)
  13. except OSError:
  14. self.text_proc = None
  15. def get_status(self):
  16. if self.text_proc is not None:
  17. self.text_proc.poll()
  18. return self.text_proc.returncode
  19. return None
  20. def __enter__(self):
  21. return self
  22. def close(self):
  23. if self.text_proc is not None:
  24. self.text_proc.terminate()
  25. self.text_proc = None
  26. def wait_for_exit(self):
  27. if self.text_proc is not None:
  28. while True:
  29. if self.get_status() == 1:
  30. return
  31. time.sleep(0.1)
  32. def __del__(self):
  33. self.close()
  34. def __exit__(self, exc_type, exc_value, traceback):
  35. self.close()
  36. if __name__ == "__main__":
  37. text = """Traceback (most recent call last):
  38. File "./controlsd.py", line 608, in <module>
  39. main()
  40. File "./controlsd.py", line 604, in main
  41. controlsd_thread(sm, pm, logcan)
  42. File "./controlsd.py", line 455, in controlsd_thread
  43. 1/0
  44. ZeroDivisionError: division by zero"""
  45. print(text)
  46. with TextWindow(text) as s:
  47. for _ in range(100):
  48. if s.get_status() == 1:
  49. print("Got exit button")
  50. break
  51. time.sleep(0.1)
  52. print("gone")