-
Notifications
You must be signed in to change notification settings - Fork 0
/
arc_reports.py
executable file
·266 lines (215 loc) · 8.5 KB
/
arc_reports.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
import csv
import collections
import tkinter
from tkinter import filedialog, messagebox
import os
import sys
import traceback
import requests
API_URL = 'https://rms.arc.gov.au/RMS/Report/ResearchOfficeAPI/{report_name}/'
API_KEY_PATH = '~/.arc-rms-api-key'
def download_report(report_name, scheme_round, api_key):
params = [
('schemeRound', scheme_round),
('apiKey', api_key)
]
response = requests.get(API_URL.format(report_name=report_name),
params=params)
response.raise_for_status()
response_json = response.json()
status = response_json.get('status')
if status != 'success':
if status == 'fail':
raise ValueError('Report failed. Reason: %s' %
response_json.get('data'))
elif status == 'error':
raise ValueError('We experienced an error. Reason: %s' %
response_json.get('message'))
else:
raise ValueError('Unknown error occurred.')
return response_json.get('data')
def get_file_path():
file_path = filedialog.asksaveasfilename(
defaultextension='.csv',
filetypes=(('Comma-Separated Value File', '.csv', "TEXT"),)
)
if not file_path:
messagebox.showerror(
title='No path chosen',
message='You must choose a path to save your file.')
return file_path
def unsuccessful_feedback(scheme_round, api_key, file_path):
""" Handle UnsuccessfulFeedback Banded and Comments.
See:
https://rms.arc.gov.au/RMS/Report/Home/ResearchOfficeAPI#Reports-UnsuccessfulBanded
https://rms.arc.gov.au/RMS/Report/Home/ResearchOfficeAPI#Reports-UnsuccessfulComment
"""
data = download_report('UnsuccessfulFeedback', scheme_round, api_key)
writer = None
with open(file_path, 'w', newline='') as output:
for item in data:
project = collections.OrderedDict()
project['Project ID'] = item.get('projectId')
project['Proposal Rating Band'] = item.get('proposalRatingBand')
for rating_band in item.get('criterionRatingBands'):
project[
'Criterion Rating Band (%s)' %
rating_band.get('criterionName')] = rating_band.get('band')
project['Feedback Comments'] = \
'; '.join(item.get('feedbackComments', ()))
if not writer:
writer = csv.DictWriter(output, project.keys())
writer.writeheader()
writer.writerow(project)
def ineligible_proposals(scheme_round, api_key, file_path):
"""
See https://rms.arc.gov.au/RMS/Report/Home/ResearchOfficeAPI#Reports-Ineligible
Example structure:
{
"status":"success",
"data":[
{
"projectId":"XX150200028",
"comment":"Comment"
},
]
}
"""
data = download_report('IneligibleProposals', scheme_round, api_key)
writer = None
with open(file_path, 'w', newline='') as output:
for item in data:
project = collections.OrderedDict()
project['Project ID'] = item.get('projectId')
project['Project ID'] = item.get('comment')
if not writer:
writer = csv.DictWriter(output, project.keys())
writer.writeheader()
writer.writerow(project)
def funding_details_for_successful_proposals(scheme_round,
api_key,
file_path):
"""
See https://rms.arc.gov.au/RMS/Report/Home/ResearchOfficeAPI#Reports-Success
"""
data = download_report('FundingDetailsForSuccessfulProposals',
scheme_round,
api_key)
writer = None
with open(file_path, 'w', newline='') as output:
for item in data:
project = collections.OrderedDict()
project['Project ID'] = item.get('projectId')
project['Project Summary'] = item.get('projectSummary')
project['Special Conditions'] = \
'; '.join(item.get('specialConditions', ()))
for period in \
item.get('fundingAmountApprovedForEachFinancialYear'):
project['%s (Financial Year)' %
period.get('year')] = period.get('fundingAmount')
for period in item.get('fundingAmountApprovedForEachCalendarYear'):
project['%s (Calendar Year)' %
period.get('year')] = period.get('fundingAmount')
awards = \
item.get('fundingAmountForAwardsApprovedForEachFinancialYear')
for award in awards:
for period in awards.get(award):
project['%s (%s Financial Year)' %
(award, period.get('year'))] = \
period.get('fundingAmount')
awards = \
item.get('fundingAmountForAwardsApprovedForEachCalendarYear')
for award in awards:
for period in awards.get(award):
project['%s (%s Calendar Year)' %
(award, period.get('year'))] = \
period.get('fundingAmount')
if not writer:
writer = csv.DictWriter(output, project.keys())
writer.writeheader()
writer.writerow(project)
REPORTS = (
('UnsuccessfulFeedback', unsuccessful_feedback),
('IneligibleProposals', ineligible_proposals),
('FundingDetailsForSuccessfulProposals',
funding_details_for_successful_proposals)
)
REPORTS_DICT = dict(REPORTS)
def main():
root = tkinter.Tk()
root.wm_title("ARC Research Management System (RMS)")
root.minsize(400, 400)
w = tkinter.Label(
root,
text="This app downloads and exports\nARC Research Office reports.\n\n")
w.pack()
w = tkinter.Label(root, text="Report Type:")
w.pack()
report_name = tkinter.StringVar(root)
report_name.set(REPORTS[0][0]) # default value
w = tkinter.OptionMenu(root, report_name, *(r[0] for r in REPORTS))
w.pack()
scheme_round = tkinter.StringVar(root)
w = tkinter.Label(root, text="Scheme Round:")
w.pack()
w = tkinter.Entry(root, textvariable=scheme_round, width=8)
w.pack()
api_key = tkinter.StringVar(root)
api_key_path = os.path.expanduser(API_KEY_PATH)
if os.path.exists(api_key_path):
with open(api_key_path, 'r') as api_key_file:
key = api_key_file.read()
if key:
api_key.set(key.strip())
w = tkinter.Label(root, text="API Key:")
w.pack()
w = tkinter.Entry(root, textvariable=api_key, width=40)
w.pack()
def download_export():
if not report_name.get() or not scheme_round.get() \
or not api_key.get():
messagebox.showerror(
title="Error",
message="All fields are required.")
return
file_path = get_file_path()
if not file_path:
return
# Run the given report!
try:
REPORTS_DICT[report_name.get()](
scheme_round.get(),
api_key.get(),
file_path)
messagebox.showinfo(
title="Done!",
message="Download complete! File saved to\n%s" % file_path)
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
message = ''.join(traceback.format_exception(exc_type,
exc_value,
exc_traceback))
messagebox.showerror(
title="Error",
message="We experienced an error:\n " + message)
def save_api_key():
with open(api_key_path, 'w') as api_key_file:
key = api_key.get()
if key:
api_key_file.write(key.strip())
messagebox.showinfo(
title="Done!",
message="Key saved to file %s" % api_key_path)
start = tkinter.Button(root, text='Download and export')
start['command'] = lambda: download_export()
start.pack()
save = tkinter.Button(root, text='Save API Key')
save['command'] = lambda: save_api_key()
save.pack()
quit = tkinter.Button(root, text='Quit')
quit['command'] = lambda: exit()
quit.pack()
root.mainloop()
if __name__ == '__main__':
main()