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:
return os.path.join(binariesdir, binary)
else:
# TODO: Error handling
pass
raise ValueError("'{}' is not a supported Calibre binary".format(binary))
return ""
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:
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]
if all(values):
version = values[0].group(1)
@ -960,7 +961,8 @@ def check_calibre(calibre_location):
else:
return _('Calibre binaries not viable')
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:
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 py 3.x no encode and as string with quotes empty element for parameters is okay
# separate handling for windows and linux
quotes = [1, 2]
# TODO: Clean up.
# TODO: Maybe delete/clean-up tmp files directly.
quotes = [3, 5]
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")
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")
with open(path_tmp_opf, 'w') as fd:
copyfileobj(p.stdout, fd)
quotes = [1, 2, 4, 6]
command = [config.config_converterpath, (file_path + format_old_ext),
(file_path + format_new_ext), '--from-opf', path_tmp_opf,
'--cover', os.path.join(os.path.dirname(file_path), 'cover.jpg')]
@ -257,7 +258,7 @@ class TaskConvert(CalibreTask):
quotes_index += 1
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)
while p.poll() is None: