run_client.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import print_function
  2. import sys
  3. import time
  4. import client_controller
  5. from util import debug
  6. def fake_loading_bar(msg, duration=5.):
  7. if len(msg) >= 50:
  8. raise AssertionError('Loading bar label too large')
  9. msg += ': '
  10. print(msg, end='')
  11. sys.stdout.flush()
  12. bar_length = 78 - len(msg)
  13. for _ in range(bar_length):
  14. if not debug:
  15. time.sleep(duration / bar_length)
  16. print('#', end='')
  17. sys.stdout.flush()
  18. print('\n', end='')
  19. sys.stdout.flush()
  20. def load():
  21. print('Loading...')
  22. fake_loading_bar('Initializing fake loading bars', duration=5)
  23. fake_loading_bar('Loading data from disk', duration=1)
  24. fake_loading_bar('Loading available commands', duration=3.5)
  25. fake_loading_bar('Updating indices', duration=2)
  26. fake_loading_bar('Waiting', duration=5)
  27. print('Done.\n\n')
  28. def welcome():
  29. print('''
  30. $$$$$$\ $$\
  31. $$ __$$\ $$ |
  32. $$ / $$ | $$$$$$\ $$$$$$$ | $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\
  33. $$ | $$ |$$ __$$\ $$ __$$ |$$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\
  34. $$ | $$ |$$ | \__|$$ / $$ |$$$$$$$$ |$$ | \__|$$$$$$$$ |$$ | \__|
  35. $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ ____|$$ |
  36. $$$$$$ |$$ | \$$$$$$$ |\$$$$$$$\ $$ | \$$$$$$$\ $$ |
  37. \______/ \__| \_______| \_______|\__| \_______|\__|
  38. To display an overview of available commands type \'help\'.
  39. ''')
  40. allowed_commands = ['help',
  41. 'login',
  42. 'register',
  43. 'depot',
  44. 'orders',
  45. 'news',
  46. 'activate_key',
  47. 'buy',
  48. 'sell',
  49. 'transactions',
  50. 'orders_on',
  51. 'cancel_order',
  52. 'leaderboard']
  53. def one_command():
  54. cmd = input('*> ').strip()
  55. cmds = cmd.split(';')
  56. for cmd in cmds:
  57. cmd = cmd.split()
  58. # cmd = [cmd.strip() for cmd in cmd]
  59. # noinspection PySimplifyBooleanCheck
  60. if cmd == []:
  61. continue
  62. if cmd[0] not in allowed_commands:
  63. print('Invalid command:', cmd[0])
  64. else:
  65. method_to_call = getattr(client_controller, cmd[0])
  66. # noinspection PyBroadException
  67. try:
  68. method_to_call(*cmd[1:])
  69. except TypeError:
  70. print('Invalid command syntax.')
  71. except Exception:
  72. print('An error occurred while executing a command.')
  73. if __name__ == '__main__':
  74. load()
  75. welcome()
  76. while True:
  77. one_command()