run_client.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. def one_command():
  51. cmd = input('*> ').strip()
  52. cmds = cmd.split(';')
  53. for cmd in cmds:
  54. cmd = cmd.split()
  55. # cmd = [cmd.strip() for cmd in cmd]
  56. # noinspection PySimplifyBooleanCheck
  57. if cmd == []:
  58. continue
  59. if cmd[0] not in allowed_commands:
  60. print('Invalid command:', cmd[0])
  61. else:
  62. method_to_call = getattr(client_controller, cmd[0])
  63. # noinspection PyBroadException
  64. try:
  65. method_to_call(*cmd[1:])
  66. except TypeError:
  67. print('Invalid command syntax.')
  68. except Exception:
  69. print('An error occurred while executing a command.')
  70. if __name__ == '__main__':
  71. load()
  72. welcome()
  73. while True:
  74. one_command()