-
Notifications
You must be signed in to change notification settings - Fork 0
/
datactl.py
122 lines (93 loc) · 3.42 KB
/
datactl.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
from queue import Queue
import printing
import system
from dataconstants import HEADERS, DATA_FILE, ABS_DATA_DIR, TEAM, MEDIA_DIR, EDIT_TRIGGER, NAME, MATCH
# Thread-safe queue to add lines to the file without concurrent modification
to_add = Queue()
# Keeps track of data changes since the flash drive was updated
datachange = True
# Keeps track of lines that have been removed
# Important for when a match is submitted without connection, then edited with connection, then 'newdata' is uploaded
void = []
# Check if there is already a data file, if not, make one
def makefile():
try:
d = _readfile()
if not d:
raise FileNotFoundError
except FileNotFoundError:
_writefile(HEADERS + '\n')
# Parse a string with lines of data
def _parsedata(data, info):
for line in data.split('\n'):
line = line.strip()
if line[:len(EDIT_TRIGGER)] == EDIT_TRIGGER:
# If this is an edit, remove the old version
trimmedline = line[len(EDIT_TRIGGER):]
removefromdatafile(trimmedline)
void.append(trimmedline)
_summarize(trimmedline, info, action='Edit')
elif line:
# If this wasn't already edited (could happen if this is an upload not a submit)
if line not in void:
_addtodatafile(line)
_summarize(line, info)
# Print a summary of the data received
def _summarize(line, info, action='Data'):
try:
match = line.split(',')
printing.printf(action + ' from ' + match[NAME] + ' on ' + info + ' for team ' +
match[TEAM] + ' in match ' + match[MATCH],
style=printing.NEW_DATA if action == 'Data' else printing.EDIT,
log=True, logtag='datactl._summarize')
except IndexError:
printing.printf('Incomplete line:', line, style=printing.ERROR,
log=True, logtag='datactl._summarize.error')
def addtoqueue(match, info):
to_add.put((match, info))
def _addtodatafile(match):
_writefile(match + '\n')
def removefromdatafile(match):
_writefile(_readfile().replace(match + '\n', ''), mode='w')
def _readfile():
f = open(ABS_DATA_DIR)
s = f.read()
f.close()
return s
def _writefile(s, mode='a'):
global datachange
datachange = True
f = open(ABS_DATA_DIR, mode)
f.write(s)
f.close()
def update():
# While there is data to add, parse it
while not to_add.empty():
_parsedata(*to_add.get())
# If there is a flash drive and there is new data for it, upload the data
if datachange and system.checkdev():
_updatedrive()
# Writes data to a removable device
def _updatedrive():
global datachange
if system.mount():
system.copy(ABS_DATA_DIR, MEDIA_DIR + DATA_FILE)
datachange = False
system.unmount()
def driveupdaterequest():
global datachange
datachange = True
def findmissing():
q=_readfile().strip().split('\n')
w=list(map(lambda x: x.split(','),q))
a=list(map(lambda x:(int(x[1]),int(x[0]),x[27 if len(x)>27 else 0]),w[1:]))
s = [set() for i in range(max(map(lambda x:x[0],a)))]
l = [list() for i in range(max(map(lambda x:x[0],a)))]
for p in a:
l[p[0]-1].append(p[1:])
s[p[0]-1].add(p[1:2])
for i in range(len(l)):
if len(l[i])!=6 or len(s[i])!=6:
print(i+1,':',len(l[i]))
print(' ',l[i])
print()