Backup metadata 3rd step

This commit is contained in:
Ozzie Isaacs 2022-09-19 18:56:22 +02:00
parent 241aa77d41
commit 26be5ee237
4 changed files with 58 additions and 25 deletions

View File

@ -1310,7 +1310,7 @@ def edit_scheduledtasks():
time_field.append((n, format_time(datetime_time(hour=n), format="short",))) time_field.append((n, format_time(datetime_time(hour=n), format="short",)))
for n in range(5, 65, 5): for n in range(5, 65, 5):
t = timedelta(hours=n // 60, minutes=n % 60) t = timedelta(hours=n // 60, minutes=n % 60)
duration_field.append((n, format_timedelta(t, threshold=.9))) duration_field.append((n, format_timedelta(t, threshold=.95)))
return render_title_template("schedule_edit.html", return render_title_template("schedule_edit.html",
config=content, config=content,

View File

@ -675,12 +675,14 @@ class CalibreDB:
def get_book_format(self, book_id, file_format): def get_book_format(self, book_id, file_format):
return self.session.query(Data).filter(Data.book == book_id).filter(Data.format == file_format).first() return self.session.query(Data).filter(Data.book == book_id).filter(Data.format == file_format).first()
def set_metadata_dirty(self, book_id): def set_metadata_dirty(self, book_id):
if not self.session.query(Metadata_Dirtied).filter(Metadata_Dirtied.book==book_id).one_or_none(): if not self.session.query(Metadata_Dirtied).filter(Metadata_Dirtied.book == book_id).one_or_none():
self.session.add(Metadata_Dirtied(book_id)) self.session.add(Metadata_Dirtied(book_id))
def delete_dirty_metadata(self, book_id): def delete_dirty_metadata(self, book_id):
try: try:
self.session.query(Metadata_Dirtied).filter(Metadata_Dirtied.book==book_id).delete() self.session.query(Metadata_Dirtied).filter(Metadata_Dirtied.book == book_id).delete()
self.session.commit() self.session.commit()
except (OperationalError) as e: except (OperationalError) as e:
self.session.rollback() self.session.rollback()

View File

@ -31,6 +31,7 @@ import unidecode
from flask import send_from_directory, make_response, redirect, abort, url_for from flask import send_from_directory, make_response, redirect, abort, url_for
from flask_babel import gettext as _ from flask_babel import gettext as _
from flask_babel import lazy_gettext as N_ from flask_babel import lazy_gettext as N_
from flask_babel import get_locale
from flask_login import current_user from flask_login import current_user
from sqlalchemy.sql.expression import true, false, and_, or_, text, func from sqlalchemy.sql.expression import true, false, and_, or_, text, func
from sqlalchemy.exc import InvalidRequestError, OperationalError from sqlalchemy.exc import InvalidRequestError, OperationalError
@ -57,6 +58,7 @@ from .subproc_wrapper import process_wait
from .services.worker import WorkerThread from .services.worker import WorkerThread
from .tasks.mail import TaskEmail from .tasks.mail import TaskEmail
from .tasks.thumbnail import TaskClearCoverThumbnailCache, TaskGenerateCoverThumbnails from .tasks.thumbnail import TaskClearCoverThumbnailCache, TaskGenerateCoverThumbnails
from .tasks.metadata_backup import TaskBackupMetadata
log = logger.create() log = logger.create()
@ -1031,3 +1033,10 @@ def add_book_to_thumbnail_cache(book_id):
def update_thumbnail_cache(): def update_thumbnail_cache():
if config.schedule_generate_book_covers: if config.schedule_generate_book_covers:
WorkerThread.add(None, TaskGenerateCoverThumbnails()) WorkerThread.add(None, TaskGenerateCoverThumbnails())
def set_all_metadata_dirty():
WorkerThread.add(None, TaskBackupMetadata(export_language=get_locale(),
translated_title=_("cover"),
set_dirty=True),
hidden=False)

View File

@ -41,21 +41,43 @@ NSMAP = {'dc': PURL_NAMESPACE, 'opf': OPF_NAMESPACE}
class TaskBackupMetadata(CalibreTask): class TaskBackupMetadata(CalibreTask):
def __init__(self, export_language="en", translated_title="cover", task_message=N_('Backing up Metadata')): def __init__(self, export_language="en",
translated_title="cover",
set_dirty=False,
task_message=N_('Backing up Metadata')):
super(TaskBackupMetadata, self).__init__(task_message) super(TaskBackupMetadata, self).__init__(task_message)
self.log = logger.create() self.log = logger.create()
self.calibre_db = db.CalibreDB(expire_on_commit=False, init=True) self.calibre_db = db.CalibreDB(expire_on_commit=False, init=True)
self.export_language = export_language self.export_language = export_language
self.translated_title = translated_title self.translated_title = translated_title
self.set_dirty=set_dirty
def run(self, worker_thread): def run(self, worker_thread):
if self.set_dirty:
self.set_all_books_dirty()
else:
self.backup_metadata()
def set_all_books_dirty(self):
try:
books = self.calibre_db.session.query(db.Books).all()
for book in books:
self.calibre_db.set_metadata_dirty(book)
self._handleSuccess()
except Exception as ex:
self.log.debug('Error adding book for backup: ' + str(ex))
self._handleError('Error adding book for backup: ' + str(ex))
self.calibre_db.session.rollback()
self.calibre_db.session.close()
def backup_metadata(self):
try: try:
metadata_backup = self.calibre_db.session.query(db.Metadata_Dirtied).all() metadata_backup = self.calibre_db.session.query(db.Metadata_Dirtied).all()
custom_columns = self.calibre_db.session.query(db.CustomColumns).order_by(db.CustomColumns.label).all() custom_columns = self.calibre_db.session.query(db.CustomColumns).order_by(db.CustomColumns.label).all()
for backup in metadata_backup: for backup in metadata_backup:
book = self.calibre_db.session.query(db.Books).filter(db.Books.id == backup.book).one_or_none() book = self.calibre_db.session.query(db.Books).filter(db.Books.id == backup.book).one_or_none()
# self.calibre_db.session.query(db.Metadata_Dirtied).filter(db.Metadata_Dirtied == backup.id).delete() self.calibre_db.session.query(db.Metadata_Dirtied).filter(db.Metadata_Dirtied == backup.id).delete()
# self.calibre_db.session.commit() self.calibre_db.session.commit()
if book: if book:
self.open_metadata(book, custom_columns) self.open_metadata(book, custom_columns)
self._handleSuccess() self._handleSuccess()
@ -85,7 +107,7 @@ class TaskBackupMetadata(CalibreTask):
stream = urlopen(web_content_link) stream = urlopen(web_content_link)
except Exception as ex: except Exception as ex:
# Bubble exception to calling function # Bubble exception to calling function
self.log.debug('Error reading metadata.opf: ' + str(ex)) # ToDo Chek whats going on self.log.debug('Error reading metadata.opf: ' + str(ex)) # ToDo Check whats going on
raise ex raise ex
finally: finally:
if stream is not None: if stream is not None:
@ -96,24 +118,24 @@ class TaskBackupMetadata(CalibreTask):
#if not os.path.isfile(book_metadata_filepath): #if not os.path.isfile(book_metadata_filepath):
self.create_new_metadata_backup(book, custom_columns, book_metadata_filepath) self.create_new_metadata_backup(book, custom_columns, book_metadata_filepath)
# else: # else:
'''namespaces = {'dc': PURL_NAMESPACE, 'opf': OPF_NAMESPACE} '''namespaces = {'dc': PURL_NAMESPACE, 'opf': OPF_NAMESPACE}
test = etree.parse(book_metadata_filepath) test = etree.parse(book_metadata_filepath)
root = test.getroot() root = test.getroot()
for i in root.iter(): for i in root.iter():
self.log.info(i) self.log.info(i)
title = root.find("dc:metadata", namespaces) title = root.find("dc:metadata", namespaces)
pass''' pass
with open(book_metadata_filepath, "rb") as f: with open(book_metadata_filepath, "rb") as f:
xml = f.read() xml = f.read()
root = objectify.fromstring(xml) root = objectify.fromstring(xml)
# root.metadata['{http://purl.org/dc/elements/1.1/}title'] # root.metadata['{http://purl.org/dc/elements/1.1/}title']
# root.metadata[PURL + 'title'] # root.metadata[PURL + 'title']
# getattr(root.metadata, PURL +'title') # getattr(root.metadata, PURL +'title')
# test = objectify.parse() # test = objectify.parse()
pass pass
# backup not found has to be created # backup not found has to be created
#raise Exception('Book cover file not found') #raise Exception('Book cover file not found')'''
def create_new_metadata_backup(self, book, custom_columns, book_metadata_filepath): def create_new_metadata_backup(self, book, custom_columns, book_metadata_filepath):
# generate root package element # generate root package element
@ -195,7 +217,7 @@ class TaskBackupMetadata(CalibreTask):
with open(book_metadata_filepath, 'wb') as f: with open(book_metadata_filepath, 'wb') as f:
doc.write(f, xml_declaration=True, encoding='utf-8', pretty_print=True) doc.write(f, xml_declaration=True, encoding='utf-8', pretty_print=True)
except Exception: except Exception:
# ToDo: Folder not writeable errror # ToDo: Folder not writeable error
pass pass
@property @property
def name(self): def name(self):