-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqparsetables.py
213 lines (165 loc) · 6.47 KB
/
eqparsetables.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""Formats and filters GamParse spells and disc forum output."""
import argparse
import os
import sys
import castgrapher as cg
import casttable
import enjinformatter
import format
import gamparsecastreader as gpc
import gamparsedpsreader as gpd
import playerdata
import ttyformatter
__author__ = 'Andrew Quinn'
__copyright__ = 'Copyright 2015-2016, Andrew Quinn'
__credits__ = ['Andrew Quinn']
__license__ = 'Simplified BSD'
__version__ = '0.1'
__maintainer__ = 'Andrew Quinn'
__email__ = 'andrew@under.co.nz'
__status__ = 'Prototype'
def get_arg_parser():
parser = argparse.ArgumentParser(description='Transform GamParse output into your favorite forum table format.')
parser.add_argument('paths', help='a list of paths containing GamParse output', nargs='*', metavar='PATHS')
parser.add_argument('-b', '--blocklist', help='path to blocklist', metavar='PATH')
parser.add_argument('-c', '--config', help='path to config CSV file', metavar='PATH')
parser.add_argument('--dps', action='store_true', help='force dps formatting')
parser.add_argument('--tty', action='store_true', help='output text (default is enjin post format)')
parser.add_argument('--attn', action='store_true', help='reconstruct attendance list') # Work in progress...
parser.add_argument('-f', '--dpsfirst', help='highest ranking dpser to show', metavar='FIRST')
parser.add_argument('-l', '--dpslast', help='lowest ranking dpser to show', metavar='LAST')
return parser
def main(argv):
parser = get_arg_parser()
args = parser.parse_args()
paths = get_input_paths(args)
player_data = get_player_data(args)
make_table = get_table_maker(args)
if args.dps:
dps_first, dps_last = get_dps_bounds(args)
handle_dps(paths, player_data, dps_first, dps_last, make_table)
else:
blocked_spells = get_blocklist(args)
handle_casts(paths, player_data, blocked_spells, make_table)
def get_input_paths(args):
default_path = f'{os.getcwd()}/parse.txt'
paths = list()
if args.paths:
for path in args.paths:
check_file(path)
paths.append(path)
else:
check_default_file(default_path)
paths.append(default_path)
return paths
def get_blocklist(args):
blocklist_path = f'{os.getcwd()}/blocklist.ini'
if args.blocklist:
check_file(args.blocklist)
blocklist_path = args.blocklist
else:
check_default_file(blocklist_path)
blocklist = []
with open(blocklist_path, 'r') as bl_handle:
for row in bl_handle.read().splitlines():
blocklist.append(row.strip())
return blocklist
def get_player_data(args):
config_path = f'{os.getcwd()}/config.ini'
if args.config:
check_file(args.config)
config_path = args.config
else:
check_default_file(config_path)
return playerdata.PlayerData(config_path)
def get_table_maker(args):
if args.tty:
return ttyformatter.make_table
else:
return enjinformatter.make_table
def get_dps_bounds(args):
"""
Get dps placement bounds from args.
:param args: parsed arguments
:return: placement indices of the first and last players to be shown
"""
dps_first = 0
dps_last = 10
if args.dpsfirst:
dps_first = int(args.dpsfirst) - 1
if args.dpslast:
dps_last = int(args.dpslast)
return sorted([0, dps_first, dps_last, sys.maxsize])[1:3]
def check_file(path):
if not os.path.isfile(path):
print(f'Could not find the file {path}. Exiting.')
sys.exit()
def check_default_file(path):
if not os.path.isfile(path):
answer = input(f'Could not find the file {path}. Would you like to create a blank version now? [y/N] ')
if str(answer).lower() == 'y':
with open(path, 'a+') as _:
pass
else:
print('Exiting.')
sys.exit()
def handle_casts(paths, player_data, blocked, make_table):
"""
Generate formatted spell cast output.
:param paths: a list of paths to GamParse output
:param player_data: a PlayerData object
:param blocked: a Blocklist object of spells to be ignored
:param make_table: a function: f(eq_class, [[header strings...], ...], [[row strings], ...] -> string
"""
cast_table = get_cast_table(paths, player_data, blocked)
padding = '\n\n'
classes = cast_table.get_classes()
for i, eq_class in enumerate(sorted(classes)):
if i > 0:
print(padding)
totals = ['Total'] + [str(t) for t in cast_table.get_totals(eq_class)]
spells, rows = cast_table.get_rows(eq_class)
cg.generate_class_graphs(spells, rows, eq_class)
print(make_table(eq_class, [spells, totals], rows))
def get_cast_table(paths, player_data, blocklist):
"""
Create an aggregated CastTable from GamParse output file(s)
:param paths: a list of paths to GamParse output
:param player_data: a PlayerData object
:param blocklist: a list of spells to be ignored
:return: a CastTable object
"""
reader = gpc.GPCastReader(player_data)
cast_tables = list()
for path in paths:
cast_tables.append(reader.get_cast_table(path, blocklist))
return casttable.aggregate(cast_tables)
def handle_dps(paths, player_data, dps_first, dps_last, make_table):
"""
Generate formatted dps output.
:param paths: a list of paths to GamParse output
:param player_data: a PlayerData object
:param dps_first: the index of the first player to be shown
:param dps_last: the index of the last player to be shown
:param make_table: a function: f(eq_class, [[header strings...], ...], [[row strings], ...] -> string
"""
dps_table = get_dps_table(paths, player_data)
headers, rows = dps_table.get_rows()
if dps_last > len(rows):
dps_last = len(rows)
formatted_rows = [[format.humanize(cell) for cell in row] for row in rows]
print(make_table("DPS", [headers], formatted_rows[dps_first:dps_last]))
players = dps_table.get_players()
chart_rows = [[row[1], int(row[3])] for row in rows]
cg.graph_dps(chart_rows[dps_first:dps_last])
def get_dps_table(paths, player_data):
if len(paths) > 1:
print(f'Combining DPS parses is not currently supported. '
f'Ignoring input files {", ".join(paths[1:])}...')
path = paths[0]
reader = gpd.GPDPSReader(player_data)
dps_table = reader.get_dps_table(path)
return dps_table
if __name__ == '__main__':
main(sys.argv[1:])