run_client.py 3.4 KB

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