run_client.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from __future__ import print_function
  2. import sys
  3. import time
  4. import client_controller
  5. from debug 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. 'change_password',
  44. 'depot',
  45. 'orders',
  46. 'news',
  47. 'activate_key',
  48. 'buy',
  49. 'sell',
  50. 'transactions',
  51. 'orders_on',
  52. 'cancel_order',
  53. 'leaderboard',
  54. 'exit']
  55. def one_command():
  56. cmd = input('*> ').strip()
  57. cmds = cmd.split(';')
  58. for cmd in cmds:
  59. cmd = cmd.split()
  60. # cmd = [cmd.strip() for cmd in cmd]
  61. # noinspection PySimplifyBooleanCheck
  62. if cmd == []:
  63. continue
  64. if cmd[0] not in allowed_commands:
  65. print('Invalid command:', cmd[0])
  66. else:
  67. method_to_call = getattr(client_controller, cmd[0])
  68. # noinspection PyBroadException
  69. try:
  70. method_to_call(*cmd[1:])
  71. except TypeError:
  72. print('Invalid command syntax.')
  73. except Exception:
  74. print('An error occurred while executing a command.')
  75. def exit_client():
  76. global exiting
  77. exiting = True
  78. if __name__ == '__main__':
  79. load()
  80. welcome()
  81. while not client_controller.exiting:
  82. one_command()