-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·107 lines (93 loc) · 3.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
""" Main module """
import argparse
from sys import argv
from bot import HqTriviaBot
from utils import get_stats, generate_token
import replay
import cache
import server
class Main(object):
""" Main class for defining command line arguments """
def __init__(self):
self.bot = HqTriviaBot()
self.parser = argparse.ArgumentParser(
usage=f'''pipenv run <command> [<args>]
Valid commands are:
bot {self.bot.run.__doc__}
cache {cache.__doc__}
replay {replay.__doc__}
server {server.__doc__}
stats {get_stats.__doc__}
token {generate_token.__doc__}''',
prog='pipenv run'
)
self.parser.add_argument('command', help=argparse.SUPPRESS)
args = self.parser.parse_args(argv[1:2])
if not hasattr(self, args.command):
self.parser.print_help()
exit(1)
getattr(self, args.command)()
def run(self):
""" Run the bot with a live game websocket """
parser = argparse.ArgumentParser(description=self.bot.run.__doc__,
prog=f'{self.parser.prog} bot')
parser.add_argument('--test-server', help="Use local test websocket",
action='store_true')
args = vars(parser.parse_args(argv[2:]))
if args.get('test_server', '') is True:
self.bot.api_url = 'http://localhost:8732'
self.bot.run()
def cache(self):
""" Perform caching operations """
cacher = cache.Cache()
parser = argparse.ArgumentParser(
prog=f'{self.parser.prog} cache',
usage=f'''pipenv run cache <operation>
Valid cache commands are:
prune {cacher.prune.__doc__}
refresh {cacher.refresh.__doc__}
vacuum {cacher.vacuum.__doc__}
import_sql {cacher.import_sql.__doc__}
export {cacher.export.__doc__}
''')
parser.add_argument('operation', help=argparse.SUPPRESS)
args = parser.parse_args(argv[2:3])
if not hasattr(cacher, args.operation):
parser.print_help()
exit(1)
getattr(cacher, args.operation)()
def server(self):
""" Websocket server that simulates live games """
parser = argparse.ArgumentParser(
prog=f'{self.parser.prog} server',
usage=f'pipenv run server <game_id>[,<game_id>]'
)
parser.add_argument('game_ids', help="Comma separated list of game IDs")
args = vars(parser.parse_args(argv[2:]))
game = server.Server()
game.run(**args)
@staticmethod
def replay():
""" Replay a game and generate report """
replayer = replay.Replayer()
replayer.play()
replayer.gen_report()
def stats(self):
""" Query play stats for a given user """
parser = argparse.ArgumentParser(description=get_stats.__doc__,
prog=f'{self.parser.prog} stats')
parser.add_argument('username', help="Username of the user to query")
args = vars(parser.parse_args(argv[2:]))
args['headers'] = self.bot.headers
get_stats(**args)
def token(self):
""" Generate an auth token for number """
parser = argparse.ArgumentParser(description=generate_token.__doc__,
prog=f'{self.parser.prog} token')
parser.add_argument('number', help='Number in international format e.g. +353861230000')
args = vars(parser.parse_args(argv[2:]))
args['headers'] = self.bot.headers
generate_token(**args)
if __name__ == '__main__':
Main()