forked from oxfordmmm/catsup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
64 lines (51 loc) · 1.88 KB
/
query.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
import json
import logging
import pathlib
import sys
import argh
from dialog import Dialog
def change_out_key(submission_path, key, value):
submission_path = pathlib.Path(submission_path)
# some basic checks
if not submission_path.is_dir():
logging.error("{submission_path} is not a directory")
sys.exit(1)
if not (submission_path / "upload").is_dir():
logging.error("{submission_path}/upload is not a directory")
sys.exit(1)
if not (submission_path / "upload" / "sp3data.csv").is_file():
logging.error("{submission_path}/upload/sp3data.csv is not a file")
sys.exit(1)
# change key
if (submission_path / "upload" / "upload.json").is_file():
with open(submission_path / "upload" / "upload.json") as f:
try:
data = json.loads(f.read())
except:
data = dict()
else:
data = dict()
data[key] = value
with open(submission_path / "upload" / "upload.json", "w") as f:
f.write(json.dumps(data, indent=4))
class Question:
def __init__(self, qtype, qstr, key):
self.qtype, self.qstr, self.key = qtype, qstr, key
def execute(self, d, submission_name):
if self.qtype == "yn":
if d.yesno(self.qstr) == d.OK:
change_out_key(submission_name, self.key, True)
else:
change_out_key(submission_name, self.key, False)
def gui(submission_name):
d = Dialog(dialog="dialog")
questions = [
Question("yn", "can we use your data for good?", "data-use-good"),
Question("yn", "can we use your data for food?", "data-use-grey"),
Question("yn", "can we use your data for evil?", "data-use-evil"),
]
for q in questions:
q.execute(d, submission_name)
d.msgbox("Thanks!")
if __name__ == "__main__":
argh.dispatch_commands([change_out_key, gui])