| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2013, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
import sys |
| 5 |
import traceback |
| 6 |
import weakref |
| 7 |
from threading import Event, Thread |
| 8 |
|
| 9 |
from calibre.ebooks.metadata.opf2 import metadata_to_opf |
| 10 |
|
| 11 |
|
| 12 |
def prints(*a, **kw): |
| 13 |
kw['file'] = sys.stderr |
| 14 |
return print(*a, **kw) |
| 15 |
|
| 16 |
|
| 17 |
class Abort(Exception): |
| 18 |
pass |
| 19 |
|
| 20 |
|
| 21 |
class MetadataBackup(Thread): |
| 22 |
""" |
| 23 |
Continuously backup changed metadata into OPF files |
| 24 |
in the book directory. This class runs in its own |
| 25 |
thread. |
| 26 |
""" |
| 27 |
|
| 28 |
def __init__(self, db, interval=2, scheduling_interval=0.1): |
| 29 |
super().__init__(name='MetadataBackup', daemon=True) |
| 30 |
self.daemon = True |
| 31 |
self._db = weakref.ref(getattr(db, 'new_api', db)) |
| 32 |
self.stop_running = Event() |
| 33 |
self.interval = interval |
| 34 |
self.scheduling_interval = scheduling_interval |
| 35 |
self.check_dirtied_annotations = 0 |
| 36 |
|
| 37 |
@property |
| 38 |
def db(self): |
| 39 |
ans = self._db() |
| 40 |
if ans is None: |
| 41 |
raise Abort() |
| 42 |
return ans |
| 43 |
|
| 44 |
def stop(self): |
| 45 |
self.stop_running.set() |
| 46 |
|
| 47 |
def wait(self, interval): |
| 48 |
if self.stop_running.wait(interval): |
| 49 |
raise Abort() |
| 50 |
|
| 51 |
def run(self): |
| 52 |
while not self.stop_running.is_set(): |
| 53 |
try: |
| 54 |
if self.wait(self.interval): |
| 55 |
break |
| 56 |
self.do_one() |
| 57 |
except Abort: |
| 58 |
break |
| 59 |
|
| 60 |
def do_one(self): |
| 61 |
self.check_dirtied_annotations += 1 |
| 62 |
if self.check_dirtied_annotations > 2: |
| 63 |
self.check_dirtied_annotations = 0 |
| 64 |
try: |
| 65 |
self.db.check_dirtied_annotations() |
| 66 |
except Exception: |
| 67 |
if self.stop_running.is_set() or self.db.is_closed: |
| 68 |
return |
| 69 |
traceback.print_exc() |
| 70 |
|
| 71 |
try: |
| 72 |
book_id = self.db.get_a_dirtied_book() |
| 73 |
if book_id is None: |
| 74 |
return |
| 75 |
except Abort: |
| 76 |
raise |
| 77 |
except Exception: |
| 78 |
# Happens during interpreter shutdown |
| 79 |
return |
| 80 |
|
| 81 |
self.wait(0) |
| 82 |
|
| 83 |
try: |
| 84 |
mi, sequence = self.db.get_metadata_for_dump(book_id) |
| 85 |
except Exception: |
| 86 |
prints('Failed to get backup metadata for id:', book_id, 'once') |
| 87 |
traceback.print_exc() |
| 88 |
self.wait(self.interval) |
| 89 |
try: |
| 90 |
mi, sequence = self.db.get_metadata_for_dump(book_id) |
| 91 |
except Exception: |
| 92 |
prints('Failed to get backup metadata for id:', book_id, 'again, giving up') |
| 93 |
traceback.print_exc() |
| 94 |
return |
| 95 |
|
| 96 |
if mi is None: |
| 97 |
self.db.clear_dirtied(book_id, sequence) |
| 98 |
return |
| 99 |
|
| 100 |
# Give the GUI thread a chance to do something. Python threads don't |
| 101 |
# have priorities, so this thread would naturally keep the processor |
| 102 |
# until some scheduling event happens. The wait makes such an event |
| 103 |
self.wait(self.scheduling_interval) |
| 104 |
|
| 105 |
try: |
| 106 |
raw = metadata_to_opf(mi) |
| 107 |
except Exception: |
| 108 |
prints('Failed to convert to opf for id:', book_id) |
| 109 |
traceback.print_exc() |
| 110 |
self.db.clear_dirtied(book_id, sequence) |
| 111 |
return |
| 112 |
|
| 113 |
self.wait(self.scheduling_interval) |
| 114 |
|
| 115 |
try: |
| 116 |
self.db.write_backup(book_id, raw) |
| 117 |
except Exception: |
| 118 |
prints('Failed to write backup metadata for id:', book_id, 'once') |
| 119 |
traceback.print_exc() |
| 120 |
self.wait(self.interval) |
| 121 |
try: |
| 122 |
self.db.write_backup(book_id, raw) |
| 123 |
except Exception: |
| 124 |
prints('Failed to write backup metadata for id:', book_id, 'again, giving up') |
| 125 |
traceback.print_exc() |
| 126 |
return |
| 127 |
|
| 128 |
self.db.clear_dirtied(book_id, sequence) |
| 129 |
|
| 130 |
def break_cycles(self): |
| 131 |
# Legacy compatibility |
| 132 |
pass |