-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_quickref.py
116 lines (87 loc) · 3.43 KB
/
generate_quickref.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
import json
import os
from prettyprinter import pprint
from io import StringIO
import d2api
from functools import partial
def path_to_doc(x = ''):
return os.path.abspath(os.path.join(os.path.dirname(__file__), 'doc', x))
def resolve_path(path_inp):
ret = []
for p in path_inp.split('$')[1:]:
if p[-2:] == '[]':
ret.append('list({})'.format(p[:-2]))
else:
ret.append(p)
return ret
class QuickRef:
def __init__(self, funcs = []):
self.partials = funcs
self.md = []
def add_func(self, name, partialfunc):
self.partials.append((name, partialfunc))
def generate_all(self):
for name, func in self.partials:
self.generate_quickref(func(), name)
self.generate_markdown()
def _find_structure(self, inp_json, cur_path = ""):
self.all_paths.add(cur_path)
cur_iter = []
t = None
if issubclass(inp_json.__class__, dict):
cur_iter = list(inp_json.keys())
t = 'dict'
if isinstance(inp_json, list):
cur_iter = list(range(len(inp_json)))
t = 'list'
for x in cur_iter:
if t == 'list':
new_path = "{}[]".format(cur_path)
else:
new_path = "{}${}".format(cur_path, x)
self._find_structure(inp_json[x], new_path)
def generate_quickref(self, curdict, fname):
self.all_paths = set()
self._find_structure(curdict)
refined = []
for p in sorted(self.all_paths):
if p[-2:] == '[]':
refined.pop()
if p:
refined.append(p)
jsonroot = {}
for p in refined:
cur = jsonroot
cur_paths = resolve_path(p)
leaf = cur_paths.pop()
for x in cur_paths:
cur = cur[x]
cur[leaf] = {}
output = StringIO()
pprint(jsonroot, stream=output)
output.seek(0)
output = output.read()
output = output.replace(": {}", "").replace("'", "")
self.md.append((fname, output))
def generate_markdown(self):
with open(path_to_doc("quickref.rst"), 'w') as f:
f.write("Quick Reference\n")
f.write("###############\n")
f.write("Below, is the generic dictionary access structure for any of the given endpoints.\n")
for fname, content in self.md:
f.write("\n{}\n{}\n::\n\n".format(fname, "="*len(fname)))
for c in content.split("\n"):
f.write(" {}\n".format(c))
api = d2api.APIWrapper()
qr = QuickRef()
qr.add_func("get_match_history()", api.get_match_history)
qr.add_func("get_match_details()", partial(api.get_match_details, '4176987886'))
qr.add_func("get_heroes()", partial(api.get_heroes, language = "en_us"))
qr.add_func("get_game_items()", partial(api.get_game_items, language = "en_us"))
qr.add_func("get_tournament_prize_pool()", api.get_tournament_prize_pool)
qr.add_func("get_top_live_game()", api.get_top_live_game)
qr.add_func("get_team_info_by_team_id()", api.get_team_info_by_team_id)
qr.add_func("get_live_league_games()", api.get_live_league_games)
qr.add_func("get_broadcaster_info()", partial(api.get_broadcaster_info, account_id = '76561198088874284'))
qr.add_func("get_player_summaries()", partial(api.get_player_summaries, account_ids = [1, 2]))
qr.generate_all()