database.py
14.1 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# -*- coding: utf-8 -*-
import time
from datetime import date, datetime
from collections import defaultdict
from xml.etree.ElementTree import Element, ElementTree, tostring
import xml.etree.ElementTree as ET
class Database:
def __init__(self, db, anno_per_file, from_file=True):
if from_file:
self.db_name = db
self.tree = ET.parse(db)
self.root = self.tree.getroot()
else:
self.db_name = None
self.tree = None
self.root = ET.fromstring(db)
self.anno_per_file = anno_per_file
self.make_index()
def owned(self, ann_elem):
return not self.finished(ann_elem) and not self.returned(ann_elem)
def finished(self, ann_elem):
return ann_elem.find("checkinDate") is not None
def returned(self, ann_elem):
return ann_elem.find("return") is not None
def get_reason(self, ann_elem):
reas = ann_elem.find("return").find("reason").text
if reas is None:
reas = ""
return reas
def rejected(self, file_elem):
return file_elem.find("rejected") is not None
def fixed(self, ann_elem):
ret = ann_elem.find("return")
if ret is not None and ret.find("fixed") is not None:
return True
return False
def ann_elem_for_user(self, file_elem, user):
idx = 0
for ann_elem in file_elem.findall("ann"):
if ann_elem.find("annName").text == user:
return ann_elem, idx
idx = idx + 1
return None, None
def sann_elem_for_user(self, file_elem, user):
for ann_elem in file_elem.findall("s_ann"):
if ann_elem.find("annName").text == user:
return ann_elem
return None
def fixed_for_user(self, file_elem, user, is_adj):
if is_adj:
ann = self.sann_elem_for_user(file_elem, user)
else:
ann, idx = self.ann_elem_for_user(file_elem, user)
if ann is not None:
return self.fixed(ann)
return False
def make_index(self):
self.file_index = {}
self.ann_owned_index = defaultdict(list)
self.adj_owned_index = defaultdict(list)
for file_elem in self.root:
name_elem = file_elem.find("name")
if name_elem == None:
raise Exception("No name assigned to a file element !")
self.file_index[name_elem.text] = file_elem
if self.rejected(file_elem):
continue
for ann_elem in file_elem.findall("ann"):
if self.owned(ann_elem):
ann_name = ann_elem.find("annName").text
self.ann_owned_index[ann_name].append(file_elem)
for ann_elem in file_elem.findall("s_ann"):
if self.owned(ann_elem):
ann_name = ann_elem.find("annName").text
self.adj_owned_index[ann_name].append(file_elem)
def fix(self, ann_elem):
if not self.returned(ann_elem):
raise Exception("File not returned in database!")
if self.fixed(ann_elem):
raise Exception("File already fixed in database!")
ret = ann_elem.find("return")
fixed = ET.SubElement(ret, "fixed")
def add(self, file_name):
if self.file_index.has_key(file_name):
raise Exception("File name already in database !")
file_elem = ET.SubElement(self.root, "file")
name_elem = ET.SubElement(file_elem, "name")
name_elem.text = file_name
date_elem = ET.SubElement(file_elem, "addDate")
date_elem.text = time.strftime("%c")
self.file_index[file_name] = file_elem
def remove(self, file_name):
if not self.file_index.has_key(file_name):
raise Exception("File name not in database !")
file_elem = self.file_index[file_name]
self.root.remove(file_elem)
self.make_index()
def reject(self, file_name, reason):
if not self.file_index.has_key(file_name):
raise Exception("File name not in database !")
file_elem = self.file_index[file_name]
if self.rejected(file_elem):
raise Exception("File already rejected !")
rej_elem = ET.SubElement(file_elem, "rejected")
rej_elem.text = reason
self.make_index()
# For prettyprint, used in 'save' method.
def indent(self, elem, level=0):
i = "\n" + level * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
self.indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def save(self):
if not self.db_name:
raise Exception("XML not read from file !")
self.indent(self.root)
self.tree.write(self.db_name, encoding="utf-8")
def upload_prevention(self, file_name, user, is_super):
if not self.file_index.has_key(file_name):
return "Filename " + file_name.encode('utf-8') + " not known by server."
file_elem = self.file_index[file_name]
if self.rejected(file_elem):
return "File rejected: " + file_name.encode('utf-8')
if is_super:
ann_elem = file_elem.find("s_ann")
if ann_elem is None:
return "No superannotator assigned to file " + file_name.encode('utf-8')
if user != ann_elem.find("annName").text:
return "Different superannotator assigned to file " + file_name.encode('utf-8')
if ann_elem.find("checkinDate") is not None:
return "File " + file_name.encode('utf-8') + " already checked in!"
return None
else:
annotations = file_elem.findall("ann")
owners = map(lambda ann: ann.find("annName").text, annotations)
if not user in owners:
return "User " + user + " doesn't own the file " + file_name.encode('utf-8')
i = owners.index(user)
ann_elem = annotations[i]
if ann_elem.find("checkinDate") is not None:
return "User " + user + " already checked in the file " + file_name.encode('utf-8')
return None
def upload_dest(self, file_name, user):
"""Upload destination (annName element and ID -- 1 or 2) directory."""
file_elem = self.file_index[file_name]
annotations = file_elem.findall("ann")
owners = map(lambda ann: ann.find("annName").text, annotations)
exc = Exception("Cannot upload " + file_name.encode('utf-8')
+ " file by " + user + " annotator !")
if not user in owners:
raise exc
i = owners.index(user)
ann_elem = annotations[i]
if ann_elem.find("checkinDate") is not None:
raise exc
return (ann_elem, i)
def upload_id(self, file_name, user):
return self.upload_dest(file_name, user)[1]
def return_file(self, file_name, user, reason):
(ann_elem, i) = self.upload_dest(file_name, user)
ret_elem = ET.SubElement(ann_elem, "return")
date_elem = ET.SubElement(ret_elem, "date")
date_elem.text = time.strftime("%c")
date_elem = ET.SubElement(ret_elem, "reason")
if reason is None:
reason = ""
date_elem.text = reason
return i
def return_file_prim(self, file_name, user, reason):
file_elem = self.file_index[file_name]
ann_elem = file_elem.find("s_ann")
if (user != ann_elem.find("annName").text or
ann_elem.find("checkinDate") is not None):
raise Exception("Cannot upload " + file_name.encode('utf-8')
+ " file by " + user + " annotator !")
ret_elem = ET.SubElement(ann_elem, "return")
date_elem = ET.SubElement(ret_elem, "date")
date_elem.text = time.strftime("%c")
date_elem = ET.SubElement(ret_elem, "reason")
if reason is None:
reason = ""
date_elem.text = reason
def upload(self, file_name, user):
(ann_elem, i) = self.upload_dest(file_name, user)
date_elem = ET.SubElement(ann_elem, "checkinDate")
date_elem.text = time.strftime("%c")
return i
def upload_prim(self, file_name, user):
file_elem = self.file_index[file_name]
ann_elem = file_elem.find("s_ann")
if (user != ann_elem.find("annName").text or self.finished(ann_elem)):
raise Exception("Cannot upload " + file_name.encode('utf-8')
+ " file by " + user + " annotator !")
date_elem = ET.SubElement(ann_elem, "checkinDate")
date_elem.text = time.strftime("%c")
def download(self, file_name, user):
file_elem = self.file_index[file_name]
annotations = file_elem.findall("ann")
owners = map(lambda ann: ann.find("annName").text, annotations)
if self.fixed_for_user(file_elem, user, False):
ann, idx = self.ann_elem_for_user(file_elem, user)
date = ann.find("return").find("date").text
ann.remove(ann.find("return"))
date_elem = ET.SubElement(ann, "returnDate")
date_elem.text = date
date_elem = ET.SubElement(ann, "checkoutDate")
date_elem.text = time.strftime("%c")
return idx
else:
if user in owners or len(owners) + 1 > self.anno_per_file:
raise Exception("Cannot set " + user + " annotator to '"
+ file_name.encode('utf-8') + "' file !")
ann_elem = ET.SubElement(file_elem, "ann")
ann_name_elem = ET.SubElement(ann_elem, "annName")
ann_name_elem.text = user
date_elem = ET.SubElement(ann_elem, "checkoutDate")
date_elem.text = time.strftime("%c")
return None
def download_prim(self, file_name, user):
file_elem = self.file_index[file_name]
if self.fixed_for_user(file_elem, user, True):
sann = self.sann_elem_for_user(file_elem, user)
date = sann.find("return").find("date").text
sann.remove(sann.find("return"))
date_elem = ET.SubElement(sann, "returnDate")
date_elem.text = date
date_elem = ET.SubElement(sann, "checkoutDate")
date_elem.text = time.strftime("%c")
return True
else:
if len(file_elem.findall("s_ann")) > 0:
raise Exception("Cannot set " + user + " adjudicator to '"
+ file_name.encode('utf-8') + "' file !")
ann_elem = ET.SubElement(file_elem, "s_ann")
ann_name_elem = ET.SubElement(ann_elem, "annName")
ann_name_elem.text = user
date_elem = ET.SubElement(ann_elem, "checkoutDate")
date_elem.text = time.strftime("%c")
return False
def for_annotation(self, user):
priority = []
result = []
for file_elem in self.root:
if self.rejected(file_elem) or file_elem.find("s_ann") is not None:
continue
anns = file_elem.findall("ann")
owners = map(lambda ann: ann.find("annName").text, anns)
if len(owners) < self.anno_per_file and user not in owners:
result.append(file_elem.find("name").text)
if self.fixed_for_user(file_elem, user, False):
priority.append(file_elem.find("name").text)
return priority, result
def for_adjudication(self, user):
priority = []
result = []
for file_elem in self.root:
if self.rejected(file_elem):
continue
anns = file_elem.findall("ann")
owners = map(lambda ann: ann.find("annName").text, anns)
checked = map(lambda ann: ann.find("checkinDate") != None, anns)
if (len(owners) == self.anno_per_file
and file_elem.find("s_ann") is None
and user not in owners
and all(checked)):
result.append(file_elem.find("name").text)
if self.fixed_for_user(file_elem, user, True):
priority.append(file_elem.find("name").text)
return priority, result
def owns_normal(self, user):
return [ file_elem.find("name").text
for file_elem
in self.ann_owned_index[user] ]
def owns_super(self, user):
return [ file_elem.find("name").text
for file_elem
in self.adj_owned_index[user] ]
def owns(self, user):
return self.owns_normal(user) + self.owns_super(user)
def finished_count(self, user):
n = 0
for file_elem in self.root:
if self.rejected(file_elem):
continue
items = file_elem.findall("ann")
items.extend(file_elem.findall("s_ann"))
for ann_elem in items:
ann_name = ann_elem.find("annName").text
if ann_name == user and self.finished(ann_elem) and not self.returned(ann_elem):
n += 1
return n