PollDisplay.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. mmgen_node_tools.PollDisplay: update and display RPC data; get input from user
  12. """
  13. import sys,threading
  14. from mmgen.util import msg
  15. from mmgen.term import get_char
  16. class PollDisplay():
  17. info = None
  18. input = None
  19. poll_secs = 1
  20. def __init__(self):
  21. self.info_lock = threading.Lock() # self.info accessed by 2 threads
  22. self.display_kill_flag = threading.Event()
  23. def get_input(self):
  24. return get_char(immed_chars='q',prehold_protect=False,num_chars=1)
  25. async def process_input(self,rpc):
  26. return True
  27. async def run(self,rpc):
  28. async def do_display():
  29. with self.info_lock:
  30. self.info = None
  31. self.input = None
  32. self.enable_display = True
  33. count = 1
  34. while True:
  35. with self.info_lock:
  36. if self.enable_display:
  37. self.info = await self.get_info(rpc)
  38. self.display(count)
  39. if self.display_kill_flag.wait(self.poll_secs):
  40. self.display_kill_flag.clear()
  41. return
  42. count += 1
  43. async def process_input():
  44. if self.input == None:
  45. sys.exit(1)
  46. elif self.input == 'q':
  47. msg('')
  48. sys.exit(0)
  49. elif self.info:
  50. if await self.process_input(rpc):
  51. return True
  52. else:
  53. return True
  54. def get_input():
  55. self.input = self.get_input()
  56. self.display_kill_flag.set()
  57. while True:
  58. threading.Thread(target=get_input,daemon=True).start()
  59. await do_display()
  60. if await process_input():
  61. break