run_client.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import print_function
  2. import requests
  3. from packaging import version as v
  4. import client_controller
  5. import version
  6. from connection import client_request, host
  7. from debug import debug
  8. from lib.print_exc_plus import print_exc_plus
  9. from routes import client_commands
  10. from util import yn_dialog, main_wrapper
  11. assert all(getattr(client_controller, route) for route in client_commands)
  12. def check_for_updates():
  13. server_version = client_request('server_version')['version']
  14. client_version = version.__version__
  15. if v.parse(server_version) != v.parse(client_version):
  16. print(f'WARNING: You have Orderer version {client_version} installed while the server is running version {server_version}.')
  17. print(f' This may or may not lead to problems.')
  18. print(f' A recent client version should be available for download at '
  19. f' {host}/orderer.zip')
  20. def load():
  21. print('Loading...')
  22. client_controller._fake_loading_bar('Initializing fake loading bars', duration=5)
  23. client_controller._fake_loading_bar('Loading data from disk', duration=1)
  24. client_controller._fake_loading_bar('Loading available commands', duration=3.5)
  25. client_controller._fake_loading_bar('Checking for updates', duration=0.4)
  26. try:
  27. check_for_updates()
  28. except (ConnectionError, requests.exceptions.ConnectionError):
  29. print('WARNING: There has been a problem connecting when to the server.')
  30. client_controller._fake_loading_bar('Updating indices', duration=2)
  31. client_controller._fake_loading_bar('Waiting', duration=5)
  32. print('Done.\n\n')
  33. def welcome():
  34. print(r'''
  35. $$$$$$\ $$\
  36. $$ __$$\ $$ |
  37. $$ / $$ | $$$$$$\ $$$$$$$ | $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\
  38. $$ | $$ |$$ __$$\ $$ __$$ |$$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\
  39. $$ | $$ |$$ | \__|$$ / $$ |$$$$$$$$ |$$ | \__|$$$$$$$$ |$$ | \__|
  40. $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ ____|$$ |
  41. $$$$$$ |$$ | \$$$$$$$ |\$$$$$$$\ $$ | \$$$$$$$\ $$ |
  42. \______/ \__| \_______| \_______|\__| \_______|\__|
  43. To display an overview of available commands type 'help'.
  44. ''')
  45. def one_command():
  46. try:
  47. cmd = input('*> ').strip()
  48. except KeyboardInterrupt:
  49. if yn_dialog('Do you want to exit Orderer?'):
  50. print('Then type in `exit` :P')
  51. return
  52. else:
  53. return
  54. cmds = cmd.split(';')
  55. for cmd in cmds:
  56. cmd = cmd.split()
  57. # cmd = [cmd.strip() for cmd in cmd]
  58. # noinspection PySimplifyBooleanCheck
  59. if cmd == []:
  60. continue
  61. cmd[0] = cmd[0].lower()
  62. if cmd[0] not in client_commands:
  63. print('Invalid command:', cmd[0])
  64. else:
  65. method_to_call = getattr(client_controller, cmd[0])
  66. # noinspection PyBroadException
  67. try:
  68. method_to_call(*cmd[1:])
  69. except (ConnectionError, requests.exceptions.ConnectionError):
  70. print('There has been a problem connecting when to the server.')
  71. except KeyboardInterrupt:
  72. print('Interrupted')
  73. except Exception as _:
  74. if debug:
  75. print_exc_plus()
  76. print('An unknown error occurred while executing a command.')
  77. def main():
  78. load()
  79. welcome()
  80. while not client_controller.exiting:
  81. one_command()
  82. if debug:
  83. main = main_wrapper(main)
  84. if __name__ == '__main__':
  85. main()