run_client.py 4.2 KB

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