run_client.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, delay=0.100):
  7. if len(msg) >= 32:
  8. raise AssertionError('Loading bar label too large')
  9. msg += ': '
  10. print(msg, end='')
  11. sys.stdout.flush()
  12. for _ in range(78 - len(msg)):
  13. if not debug:
  14. time.sleep(delay)
  15. print('#', end='')
  16. sys.stdout.flush()
  17. print('\n', end='')
  18. sys.stdout.flush()
  19. def load():
  20. print('Loading...')
  21. fake_loading_bar('Initializing fake loading bars')
  22. fake_loading_bar('Loading data from disk')
  23. fake_loading_bar('Loading available commands')
  24. fake_loading_bar('Updating indices')
  25. fake_loading_bar('Waiting')
  26. print('Done.\n\n')
  27. def welcome():
  28. print('''
  29. $$$$$$\ $$\
  30. $$ __$$\ $$ |
  31. $$ / $$ | $$$$$$\ $$$$$$$ | $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\
  32. $$ | $$ |$$ __$$\ $$ __$$ |$$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\
  33. $$ | $$ |$$ | \__|$$ / $$ |$$$$$$$$ |$$ | \__|$$$$$$$$ |$$ | \__|
  34. $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ ____|$$ |
  35. $$$$$$ |$$ | \$$$$$$$ |\$$$$$$$\ $$ | \$$$$$$$\ $$ |
  36. \______/ \__| \_______| \_______|\__| \_______|\__|
  37. To display an overview of available commands type \'help\'.
  38. ''')
  39. allowed_commands = ['help',
  40. 'login',
  41. 'register',
  42. 'depot',
  43. 'orders',
  44. 'news',
  45. 'activate_key',
  46. 'buy',
  47. 'sell',
  48. 'transactions',
  49. 'orders_on',
  50. 'cancel_order',
  51. 'leaderboard']
  52. def one_command():
  53. cmd = input('*> ').strip()
  54. cmds = cmd.split(';')
  55. for cmd in cmds:
  56. cmd = cmd.split()
  57. # cmd = [cmd.strip() for cmd in cmd]
  58. # noinspection PySimplifyBooleanCheck
  59. if cmd == []:
  60. continue
  61. if cmd[0] not in allowed_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 Exception:
  71. print('An error occurred while executing a command.')
  72. if __name__ == '__main__':
  73. load()
  74. welcome()
  75. while True:
  76. one_command()