PollDisplay.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  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,cfg):
  21. self.cfg = cfg
  22. self.info_lock = threading.Lock() # self.info accessed by 2 threads
  23. self.display_kill_flag = threading.Event()
  24. def get_input(self):
  25. return get_char(immed_chars='q',prehold_protect=False,num_bytes=1)
  26. async def process_input(self,rpc):
  27. return True
  28. async def run(self,rpc):
  29. async def do_display():
  30. with self.info_lock:
  31. self.info = None
  32. self.input = None
  33. self.enable_display = True
  34. count = 1
  35. while True:
  36. with self.info_lock:
  37. if self.enable_display:
  38. self.info = await self.get_info(rpc)
  39. self.display(count)
  40. if self.display_kill_flag.wait(self.poll_secs):
  41. self.display_kill_flag.clear()
  42. return
  43. count += 1
  44. async def process_input():
  45. if self.input == None:
  46. sys.exit(1)
  47. elif self.input == 'q':
  48. msg('')
  49. sys.exit(0)
  50. elif self.info:
  51. if await self.process_input(rpc):
  52. return True
  53. else:
  54. return True
  55. def get_input():
  56. self.input = self.get_input()
  57. self.display_kill_flag.set()
  58. while True:
  59. threading.Thread(target=get_input,daemon=True).start()
  60. await do_display()
  61. if await process_input():
  62. break