run_client.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, main_wrapper
  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. 'summarize_banking_rules',
  62. 'buy_banking_license']
  63. def one_command():
  64. try:
  65. cmd = input('*> ').strip()
  66. except KeyboardInterrupt:
  67. if yn_dialog('Do you want to exit Orderer?'):
  68. print('Then type in `exit` :P')
  69. return
  70. else:
  71. return
  72. cmds = cmd.split(';')
  73. for cmd in cmds:
  74. cmd = cmd.split()
  75. # cmd = [cmd.strip() for cmd in cmd]
  76. # noinspection PySimplifyBooleanCheck
  77. if cmd == []:
  78. continue
  79. cmd[0] = cmd[0].lower()
  80. if cmd[0] not in allowed_commands:
  81. print('Invalid command:', cmd[0])
  82. else:
  83. method_to_call = getattr(client_controller, cmd[0])
  84. # noinspection PyBroadException
  85. try:
  86. method_to_call(*cmd[1:])
  87. except TypeError:
  88. print('Invalid command syntax.')
  89. except ConnectionError:
  90. print('There has been a problem connecting when to the server.')
  91. except KeyboardInterrupt:
  92. print('Interrupted')
  93. except Exception as _:
  94. if debug:
  95. print_exc_plus()
  96. print('An unknown error occurred while executing a command.')
  97. def main():
  98. load()
  99. welcome()
  100. while not client_controller.exiting:
  101. one_command()
  102. if debug:
  103. main = main_wrapper(main)
  104. if __name__ == '__main__':
  105. main()