run_client.py 4.0 KB

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