[go: up one dir, main page]

Menu

[d66369]: / src / worker_threads.py  Maximize  Restore  History

Download this file

529 lines (416 with data), 21.8 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# -*- coding: utf-8 -*-
'''
Created on 21.01.2012
@author: vlkv
'''
from PyQt4 import QtCore, QtGui
import traceback
from integrity_fixer import IntegrityFixer
from repo_mgr import *
import sys
from db_schema import Thumbnail
from exceptions import *
from commands import *
import zipfile
import os
class ImportItemsThread(QtCore.QThread):
def __init__(self, parent, repo, importFromFilename, userLogin):
super(ImportItemsThread, self).__init__(parent)
self.repo = repo
self.srcFile = importFromFilename
self.userLogin = userLogin
def run(self):
srcArchive = zipfile.ZipFile(self.srcFile, "r")
try:
filenames = self.__getListOfMetadataFiles(srcArchive)
for i in range(len(filenames)):
filename = filenames[i]
#Restore item from json state
itemState = str(srcArchive.read(filename), "utf-8")
item = memento.Decoder().decode(itemState)
#Imported item will be owned by that user, who is performing the import
item.user_login = self.userLogin
#Move physical file to repository
dstAbsPath = None
if item.data_ref is not None:
dstAbsPath = os.path.join(self.repo.base_path, item.data_ref.url)
if os.path.exists(dstAbsPath):
#Skip this item. Count and remember this error
continue
srcArchive.extract(item.data_ref.url_raw, self.repo.base_path)
uow = self.repo.create_unit_of_work()
try:
itemSrcAbsPath = dstAbsPath
itemDstRelPath = os.path.relpath(dstAbsPath, self.repo.base_path) \
if dstAbsPath is not None else None
cmd = SaveNewItemCommand(item, itemSrcAbsPath, itemDstRelPath)
uow.executeCommand(cmd)
except Exception as ex:
if dstAbsPath is not None:
os.remove(dstAbsPath)
#count and remember error details to show it later in GUI
finally:
uow.close()
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i) / len(filenames)))
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), traceback.format_exc())
finally:
srcArchive.close()
self.emit(QtCore.SIGNAL("finished"))
def __getListOfMetadataFiles(self, srcArchive):
result = []
filenames = srcArchive.namelist()
for filename in filenames:
#TODO use regexp here, for more precise check
if not filename.startswith(".reggata"):
continue
result.append(filename)
return result
class ExportItemsThread(QtCore.QThread):
def __init__(self, parent, repo, itemIds, destinationFile):
super(ExportItemsThread, self).__init__(parent)
self.repo = repo
self.itemIds = list(itemIds)
self.dstFile = destinationFile
def run(self):
dstArchive = zipfile.ZipFile(self.dstFile, "w")
try:
for i in range(len(self.itemIds)):
item = self.__getItemById(self.itemIds[i])
if not self.__isItemIntegrityOk(item):
#TODO report about this problem to log and to list of errors
# Number of errors should be displayed in status bar at the end
continue
self.__putItemStateToArchive(item, dstArchive)
self.__putItemFileToArchive(item, dstArchive)
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i) / len(self.itemIds)))
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), traceback.format_exc())
finally:
dstArchive.close()
self.emit(QtCore.SIGNAL("finished"))
def __isItemIntegrityOk(self, item):
result = False;
uow = self.repo.create_unit_of_work()
try:
error_set = uow._check_item_integrity(uow.session, item, self.repo.base_path)
result = (len(error_set) == 0)
finally:
uow.close()
return result
def __putItemFileToArchive(self, item, archive):
if item.data_ref is None:
return
itemFileAbsPath = os.path.join(self.repo.base_path, item.data_ref.url)
archive.write(itemFileAbsPath, item.data_ref.url)
def __putItemStateToArchive(self, item, archive):
encoder = memento.Encoder()
itemState = encoder.encode(item)
itemStateFilename = "id=" + str(item.id) + "_title=" + item.title
archive.writestr(os.path.join(".reggata/items", itemStateFilename), itemState)
def __getItemById(self, itemId):
item = None
uow = self.repo.create_unit_of_work()
try:
item = uow.executeCommand(GetExpungedItemCommand(itemId))
finally:
uow.close()
assert item is not None
return item
class ExportItemsFilesThread(QtCore.QThread):
def __init__(self, parent, repo, item_ids, destination_path):
super(ExportItemsFilesThread, self).__init__(parent)
self.repo = repo
self.item_ids = item_ids
self.dst_path = destination_path
def run(self):
self.errors = 0
self.detailed_message = ""
uow = self.repo.create_unit_of_work()
try:
i = 0
for id in self.item_ids:
item = uow.executeCommand(GetExpungedItemCommand(id))
if item.is_data_ref_null():
continue
src_file_path = os.path.join(self.repo.base_path, item.data_ref.url)
unique_path = dst_file_path = os.path.join(self.dst_path, os.path.basename(src_file_path))
filename_suffix = 1
#Generate unique file name. I don't want different files with same name to overwrite each other
while os.path.exists(unique_path):
name, ext = os.path.splitext(dst_file_path)
unique_path = name + str(filename_suffix) + ext
filename_suffix += 1
shutil.copy(src_file_path, unique_path)
i += 1
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i) / len(self.item_ids)))
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), traceback.format_exc())
finally:
self.emit(QtCore.SIGNAL("finished"))
uow.close()
class ItemIntegrityFixerThread(QtCore.QThread):
'''
Поток, выполняющий исправление целостности выбранной группы элементов (обычно из
результатов поискового запроса).
Нужно сделать функцию, чтобы запускать данный поток для выделенной группы элементов.
Для всех элементов хранилища тоже надо бы (но это потом может быть сделаю).
'''
def __init__(self, parent, repo, items, lock, strategy, user_login):
super(ItemIntegrityFixerThread, self).__init__(parent)
self.repo = repo
self.items = items
#Замок, для того, чтобы поток смог изменять передаваемые ему объекты, содержащиеся в списке items
self.lock = lock
self.interrupt = False
#Это словарь, в котором ключи - это коды ошибок, а значения - способ исправления данной ошибки
self.strategy = strategy
#Задавать стратегию исправления ошибок хотелось бы несложным образом:
#strategy = {ERROR_FILE_NOT_FOUND: STRATEGY_1, ERROR_FILE_SIZE_MISMATCH: STRATEGY_2} и т.п.
self.user_login = user_login
def run(self):
uow = self.repo.create_unit_of_work()
fixers = dict()
for error_code, strategy in self.strategy.items():
fixers[error_code] = IntegrityFixer.create_fixer(error_code, strategy, uow, self.repo.base_path, self.lock)
try:
#Список self.items должен содержать только что извлеченные из БД элементы
#(вместе с data_ref объектами).
for i in range(len(self.items)):
item = self.items[i]
print("fixing item " + str(item.id))
#Сначала смотрим, проверялся ли item на целостность данных?
if item.error is None:
try:
self.lock.lockForWrite()
#Если нет, то проверяем
item.error = UnitOfWork._check_item_integrity(uow.session, item, self.repo.base_path)
finally:
self.lock.unlock()
#Смотрим, есть ли у item-а ошибки
for error_code in list(item.error):
#Для каждой ошибки item-а нужно
#глянуть, есть ли стратегия исправления в поле self.strategy для данной ошибки?
#если нет, то пропускаем, ничего не делаем
#если есть, то выполняем исправление ошибки
#сообщаем, что элемент нужно обновить
fixer = fixers.get(error_code)
if fixer is not None:
fixed = fixer.fix_error(item, self.user_login)
uow.session.commit()
if fixed:
try:
self.lock.lockForWrite()
#Убираем ошибку из списка
item.error.remove(error_code)
finally:
self.lock.unlock()
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i) / len(self.items)), item.table_row)
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), sys.exc_info())
finally:
uow.close()
self.emit(QtCore.SIGNAL("finished"))
class ItemIntegrityCheckerThread(QtCore.QThread):
'''
Поток, выполняющий в фоне проверку целостности группы элементов (обычно
результатов поискового запроса).
Нужно сделать функцию, чтобы запускать данный поток для выделенной группы элементов.
Для всех элементов хранилища тоже надо бы (но это потом может быть сделаю).
'''
def __init__(self, parent, repo, items, lock):
super(ItemIntegrityCheckerThread, self).__init__(parent)
self.repo = repo
self.items = items
self.lock = lock
self.interrupt = False
def run(self):
error_count = 0
uow = self.repo.create_unit_of_work()
try:
#Список self.items должен содержать только что извлеченные из БД элементы
#(вместе с data_ref объектами).
for i in range(len(self.items)):
item = self.items[i]
error_set = UnitOfWork._check_item_integrity(uow.session, item, self.repo._base_path)
try:
self.lock.lockForWrite()
#Сохраняем результат
item.error = error_set
if len(error_set) > 0:
error_count += 1
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i) / len(self.items)), item.table_row)
finally:
self.lock.unlock()
except:
print(traceback.format_exc())
finally:
uow.close()
self.emit(QtCore.SIGNAL("finished"), error_count)
class ThumbnailBuilderThread(QtCore.QThread):
'''
Поток, выполняющий построение миниатюр изображений и сохранение их в БД.
Данный поток автоматически запускается после выполнение любого запроса элементов
(т.к. в результате любого запроса могут оказаться изображения.
'''
def __init__(self, parent, repo, items, lock, rebuild=False):
super(ThumbnailBuilderThread, self).__init__(parent)
self.repo = repo
self.items = items
self.lock = lock
self.interrupt = False
self.rebuild = rebuild
def run(self):
uow = self.repo.create_unit_of_work()
try:
thumbnail_size = int(UserConfig().get("thumbnail_size", consts.THUMBNAIL_DEFAULT_SIZE))
for i in range(len(self.items)):
item = self.items[i]
if self.interrupt:
print("ThumbnailBuilderThread interrupted!")
break
if not item.data_ref or not item.data_ref.is_image():
continue
if self.rebuild == False and len(item.data_ref.thumbnails) > 0:
continue
elif self.rebuild:
#Delete ALL existing thumbnails linked with current item.data_ref from database
uow.session.query(Thumbnail).filter(Thumbnail.data_ref_id==item.data_ref.id)\
.delete(synchronize_session=False)
uow.session.flush()
#Clear item.data_ref.thumbnails collection
try:
self.lock.lockForWrite()
item.data_ref.thumbnails[:] = []
finally:
self.lock.unlock()
try:
#Read image from file
pixmap = QtGui.QImage(os.path.join(self.repo.base_path, item.data_ref.url))
if pixmap.isNull():
continue
#Scale image to thumbnail size
if (pixmap.height() > pixmap.width()):
pixmap = pixmap.scaledToHeight(thumbnail_size)
else:
pixmap = pixmap.scaledToWidth(thumbnail_size)
buffer = QtCore.QBuffer()
buffer.open(QtCore.QIODevice.WriteOnly)
pixmap.save(buffer, "JPG")
th = Thumbnail()
th.data = buffer.buffer().data()
th.size = thumbnail_size
uow.executeCommand(SaveThumbnailCommand(item.data_ref.id, th))
#Update items collection
try:
self.lock.lockForWrite()
item.data_ref.thumbnails.append(th)
finally:
self.lock.unlock()
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i)/len(self.items)), item.table_row if hasattr(item, 'table_row') else i)
except:
if self.rebuild:
#Stop thumbnails rebuilding
raise
else:
#Continue generating thumbnails in this case
print(traceback.format_exc())
except:
self.emit(QtCore.SIGNAL("exception"), sys.exc_info())
finally:
uow.close()
self.emit(QtCore.SIGNAL("finished"))
class DeleteGroupOfItemsThread(QtCore.QThread):
def __init__(self, parent, repo, item_ids, user_login):
super(DeleteGroupOfItemsThread, self).__init__(parent)
self.repo = repo
self.item_ids = item_ids
self.user_login = user_login
self.errors = 0
self.detailed_message = None
def run(self):
self.errors = 0
self.detailed_message = ""
uow = self.repo.create_unit_of_work()
try:
i = 0
for id in self.item_ids:
try:
cmd = DeleteItemCommand(id, self.user_login)
uow.executeCommand(cmd)
except AccessError as ex:
#У пользователя self.user_login нет прав удалять данный элемент
self.errors += 1
self.detailed_message += str(ex) + os.linesep
i = i + 1
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i)/len(self.item_ids)))
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), traceback.format_exc())
finally:
uow.close()
self.emit(QtCore.SIGNAL("finished"))
class CreateGroupIfItemsThread(QtCore.QThread):
def __init__(self, parent, repo, items):
super(CreateGroupIfItemsThread, self).__init__(parent)
self.repo = repo
self.items = items
self.error_log = []
self.created_objects_count = 0
def run(self):
self.error_log = []
self.created_objects_count = 0
uow = self.repo.create_unit_of_work()
try:
i = 0
for item in self.items:
try:
#Every item is saved in a separate transaction
srcAbsPath = None
dstRelPath = None
if item.data_ref:
srcAbsPath = item.data_ref.srcAbsPath
dstRelPath = item.data_ref.dstRelPath
uow.executeCommand(SaveNewItemCommand(item, srcAbsPath, dstRelPath))
self.created_objects_count += 1
except (ValueError, DataRefAlreadyExistsError):
#Skip this item and remember it's data_ref.url)
if item.data_ref:
self.error_log.append(item.data_ref.url)
i = i + 1
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i)/len(self.items)))
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), traceback.format_exc())
finally:
uow.close()
self.emit(QtCore.SIGNAL("finished"), len(self.error_log))
class UpdateGroupOfItemsThread(QtCore.QThread):
def __init__(self, parent, repo, items):
super(UpdateGroupOfItemsThread, self).__init__(parent)
self.repo = repo
self.items = items
def run(self):
uow = self.repo.create_unit_of_work()
try:
i = 0
for item in self.items:
cmd = UpdateExistingItemCommand(item, item.user_login)
uow.executeCommand(cmd)
i = i + 1
self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i)/len(self.items)))
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), traceback.format_exc())
finally:
uow.close()
self.emit(QtCore.SIGNAL("finished"))
class BackgrThread(QtCore.QThread):
def __init__(self, parent, callable, *args):
super(BackgrThread, self).__init__(parent)
self.args = args
self.callable = callable
def run(self):
try:
self.callable(*self.args)
except Exception as ex:
self.emit(QtCore.SIGNAL("exception"), traceback.format_exc())
finally:
self.emit(QtCore.SIGNAL("finished"))