1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-29 04:49:58 +00:00

cleanup and better error handling

This commit is contained in:
Thore Schillmann 2022-06-29 19:54:53 +00:00
parent 03359599ed
commit e39c6130c3
3 changed files with 11 additions and 9 deletions

View File

@ -278,8 +278,7 @@ class _ConfigSQL(object):
if binary in constants.SUPPORTED_CALIBRE_BINARIES: if binary in constants.SUPPORTED_CALIBRE_BINARIES:
return os.path.join(binariesdir, binary) return os.path.join(binariesdir, binary)
else: else:
# TODO: Error handling raise ValueError("'{}' is not a supported Calibre binary".format(binary))
pass
return "" return ""
def set_from_dictionary(self, dictionary, field, convertor=None, default=None, encode=None): def set_from_dictionary(self, dictionary, field, convertor=None, default=None, encode=None):

View File

@ -952,7 +952,8 @@ def check_calibre(calibre_location):
try: try:
supported_binary_paths = [os.path.join(calibre_location, binary) for binary in SUPPORTED_CALIBRE_BINARIES] supported_binary_paths = [os.path.join(calibre_location, binary) for binary in SUPPORTED_CALIBRE_BINARIES]
if all(os.path.isfile(binary_path) and os.access(binary_path, os.X_OK) for binary_path in supported_binary_paths): binaries_available=[os.path.isfile(binary_path) and os.access(binary_path, os.X_OK) for binary_path in supported_binary_paths]
if all(binaries_available):
values = [process_wait([binary_path, "--version"], pattern='\(calibre (.*)\)') for binary_path in supported_binary_paths] values = [process_wait([binary_path, "--version"], pattern='\(calibre (.*)\)') for binary_path in supported_binary_paths]
if all(values): if all(values):
version = values[0].group(1) version = values[0].group(1)
@ -960,7 +961,8 @@ def check_calibre(calibre_location):
else: else:
return _('Calibre binaries not viable') return _('Calibre binaries not viable')
else: else:
return _('Missing calibre binaries in the specified directory') missing_binaries=[path for path, available in zip(SUPPORTED_CALIBRE_BINARIES, binaries_available) if not available]
return _('Missing calibre binaries: %(missing)s', missing=", ".join(missing_binaries))
except (OSError, UnicodeDecodeError) as err: except (OSError, UnicodeDecodeError) as err:
log.error_or_exception(err) log.error_or_exception(err)

View File

@ -233,18 +233,19 @@ class TaskConvert(CalibreTask):
# windows py2.7 encode as string with quotes empty element for parameters is okay # windows py2.7 encode as string with quotes empty element for parameters is okay
# windows py 3.x no encode and as string with quotes empty element for parameters is okay # windows py 3.x no encode and as string with quotes empty element for parameters is okay
# separate handling for windows and linux # separate handling for windows and linux
quotes = [1, 2]
# TODO: Clean up. quotes = [3, 5]
# TODO: Maybe delete/clean-up tmp files directly.
tmp_dir = os.path.join(gettempdir(), 'calibre_web') tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir)
calibredb_binarypath = config.get_calibre_binarypath("calibredb") calibredb_binarypath = config.get_calibre_binarypath("calibredb")
opf_command = [calibredb_binarypath, 'show_metadata', '--as-opf', str(book_id), '--with-library', config.config_calibre_dir] opf_command = [calibredb_binarypath, 'show_metadata', '--as-opf', str(book_id), '--with-library', config.config_calibre_dir]
p = process_open(opf_command) p = process_open(opf_command, quotes)
path_tmp_opf = os.path.join(tmp_dir, "metadata_" + str(current_milli_time()) + ".opf") path_tmp_opf = os.path.join(tmp_dir, "metadata_" + str(current_milli_time()) + ".opf")
with open(path_tmp_opf, 'w') as fd: with open(path_tmp_opf, 'w') as fd:
copyfileobj(p.stdout, fd) copyfileobj(p.stdout, fd)
quotes = [1, 2, 4, 6]
command = [config.config_converterpath, (file_path + format_old_ext), command = [config.config_converterpath, (file_path + format_old_ext),
(file_path + format_new_ext), '--from-opf', path_tmp_opf, (file_path + format_new_ext), '--from-opf', path_tmp_opf,
'--cover', os.path.join(os.path.dirname(file_path), 'cover.jpg')] '--cover', os.path.join(os.path.dirname(file_path), 'cover.jpg')]
@ -257,7 +258,7 @@ class TaskConvert(CalibreTask):
quotes_index += 1 quotes_index += 1
p = process_open(command, quotes, newlines=False) p = process_open(command, quotes, newlines=False)
except OSError as e: except (ValueError, OSError) as e:
return 1, N_(u"Ebook-converter failed: %(error)s", error=e) return 1, N_(u"Ebook-converter failed: %(error)s", error=e)
while p.poll() is None: while p.poll() is None: