This commit is contained in:
Kovid Goyal 2010-09-25 23:35:12 -06:00
parent 2d406112a4
commit 04ccd041e9
3 changed files with 13 additions and 9 deletions

View file

@ -156,8 +156,7 @@ def col_idx(name):
self.cover_cache.stop()
self.cover_cache = CoverCache(db, FunctionDispatcher(self.db.cover))
self.cover_cache.start()
self.metadata_backup = MetadataBackup(db,
self.db.dump_metadata)
self.metadata_backup = MetadataBackup(db)
self.metadata_backup.start()
def refresh_cover(event, ids):
if event == 'cover' and self.cover_cache is not None:

View file

@ -22,12 +22,17 @@
from calibre import fit_image, prints
class MetadataBackup(Thread): # {{{
'''
Continuously backup changed metadata into OPF files
in the book directory. This class runs in its own
thread and makes sure that the actual file write happens in the
GUI thread to prevent Windows' file locking from causing problems.
'''
def __init__(self, db, dump_func):
def __init__(self, db):
Thread.__init__(self)
self.daemon = True
self.db = db
self.dump_func = dump_func
self.keep_running = True
from calibre.gui2 import FunctionDispatcher
self.do_write = FunctionDispatcher(self.write)
@ -47,7 +52,7 @@ def run(self):
dump = []
try:
self.dump_func([id_], dump_queue=dump)
self.db.dump_metadata([id_], dump_to=dump)
except:
prints('Failed to get backup metadata for id:', id_, 'once')
import traceback
@ -55,7 +60,7 @@ def run(self):
time.sleep(2)
dump = []
try:
self.dump_func([id_], dump_queue=dump)
self.db.dump_metadata([id_], dump_to=dump)
except:
prints('Failed to get backup metadata for id:', id_, 'again, giving up')
traceback.print_exc()

View file

@ -567,7 +567,7 @@ def metadata_for_field(self, key):
return self.field_metadata[key]
def dump_metadata(self, book_ids=None, remove_from_dirtied=True,
commit=True, dump_queue=None):
commit=True, dump_to=None):
'Write metadata for each record to an individual OPF file'
if book_ids is None:
book_ids = [x[0] for x in self.conn.get(
@ -583,11 +583,11 @@ def dump_metadata(self, book_ids=None, remove_from_dirtied=True,
raw = metadata_to_opf(mi)
path = os.path.join(self.abspath(book_id, index_is_id=True),
'metadata.opf')
if dump_queue is None:
if dump_to is None:
with open(path, 'wb') as f:
f.write(raw)
else:
dump_queue.append((path, raw))
dump_to.append((path, raw))
if remove_from_dirtied:
self.conn.execute('DELETE FROM metadata_dirtied WHERE book=?',
(book_id,))