run_client.py 3.8 KB

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