Handle more exceptions

Handle exceptions during metadata save fixes #309
Handle exceptions during metadata read fixes #126 and #309
This commit is contained in:
Timmy Welch 2022-06-06 19:33:02 -07:00
parent bb7fbb4e38
commit 04409a55c7
8 changed files with 108 additions and 54 deletions

View File

@ -133,7 +133,8 @@ class SevenZipArchiver(UnknownArchiver):
# another solution can be found
files = self.get_filename_list()
if archive_file in files:
self.rebuild([archive_file])
if not self.rebuild([archive_file]):
return False
try:
# now just add the archive file as a new one
@ -229,7 +230,8 @@ class ZipArchiver(UnknownArchiver):
# another solution can be found
files = self.get_filename_list()
if archive_file in files:
self.rebuild([archive_file])
if not self.rebuild([archive_file]):
return False
try:
# now just add the archive file as a new one
@ -614,7 +616,6 @@ class FolderArchiver(UnknownArchiver):
return True
def write_file(self, archive_file: str, data: bytes) -> bool:
logger.error("Fuck this: %s", archive_file)
try:
file_path = self.path / archive_file
file_path.parent.mkdir(exist_ok=True, parents=True)
@ -848,8 +849,8 @@ class ComicArchive:
if filename:
try:
image_data = self.archiver.read_file(filename) or bytes()
except OSError:
logger.exception("Error reading in page %d. Substituting logo page.", index)
except (OSError, Exception):
logger.error("Error reading in page %d. Substituting logo page.", index)
image_data = ComicArchive.logo_data
return image_data
@ -970,14 +971,17 @@ class ComicArchive:
def write_cbi(self, metadata: GenericMetadata) -> bool:
if metadata is not None:
self.apply_archive_info_to_metadata(metadata)
cbi_string = ComicBookInfo().string_from_metadata(metadata)
write_success = self.archiver.set_comment(cbi_string)
if write_success:
self._has_cbi = True
self.cbi_md = metadata
self.reset_cache()
return write_success
try:
self.apply_archive_info_to_metadata(metadata)
cbi_string = ComicBookInfo().string_from_metadata(metadata)
write_success = self.archiver.set_comment(cbi_string)
if write_success:
self._has_cbi = True
self.cbi_md = metadata
self.reset_cache()
return write_success
except Exception as e:
logger.error("Error saving CBI! for %s: %s", self.path, e)
return False
@ -1016,22 +1020,25 @@ class ComicArchive:
return b""
try:
raw_cix = self.archiver.read_file(self.ci_xml_filename) or b""
except OSError as e:
except (OSError, Exception) as e:
logger.error("Error reading in raw CIX! for %s: %s", self.path, e)
raw_cix = bytes()
return raw_cix
def write_cix(self, metadata: GenericMetadata) -> bool:
if metadata is not None:
self.apply_archive_info_to_metadata(metadata, calc_page_sizes=True)
raw_cix = self.read_raw_cix()
cix_string = ComicInfoXml().string_from_metadata(metadata, xml=raw_cix)
write_success = self.archiver.write_file(self.ci_xml_filename, cix_string.encode("utf-8"))
if write_success:
self._has_cix = True
self.cix_md = metadata
self.reset_cache()
return write_success
try:
self.apply_archive_info_to_metadata(metadata, calc_page_sizes=True)
raw_cix = self.read_raw_cix()
cix_string = ComicInfoXml().string_from_metadata(metadata, xml=raw_cix)
write_success = self.archiver.write_file(self.ci_xml_filename, cix_string.encode("utf-8"))
if write_success:
self._has_cix = True
self.cix_md = metadata
self.reset_cache()
return write_success
except Exception as e:
logger.error("Error saving CIX! for %s: %s", self.path, e)
return False

View File

@ -307,7 +307,6 @@ class FileNameParser:
self.volume = issuestring.IssueString(self.volume).as_string()
if self.issue != "":
self.issue = issuestring.IssueString(self.issue).as_string()
print(self.issue, self.volume)
class FilenameInfo(TypedDict, total=False):

View File

@ -196,8 +196,11 @@ def create_local_metadata(
md.overlay(f_md)
if has_desired_tags:
t_md = ca.read_metadata(opts.type if opts.type is not None else 0)
md.overlay(t_md)
try:
t_md = ca.read_metadata(opts.type if opts.type is not None else 0)
md.overlay(t_md)
except Exception as e:
logger.error("Failed to load metadata for %s: %s", ca.path, e)
# finally, use explicit stuff
md.overlay(opts.metadata)
@ -275,26 +278,35 @@ def process_file_cli(
if opts.type is None or opts.type == MetaDataStyle.CIX:
if has[MetaDataStyle.CIX]:
print("--------- ComicRack tags ---------")
if opts.raw:
print(ca.read_raw_cix())
else:
print(ca.read_cix())
try:
if opts.raw:
print(ca.read_raw_cix())
else:
print(ca.read_cix())
except Exception as e:
logger.error("Failed to load metadata for %s: %s", ca.path, e)
if opts.type is None or opts.type == MetaDataStyle.CBI:
if has[MetaDataStyle.CBI]:
print("------- ComicBookLover tags -------")
if opts.raw:
pprint(json.loads(ca.read_raw_cbi()))
else:
print(ca.read_cbi())
try:
if opts.raw:
pprint(json.loads(ca.read_raw_cbi()))
else:
print(ca.read_cbi())
except Exception as e:
logger.error("Failed to load metadata for %s: %s", ca.path, e)
if opts.type is None or opts.type == MetaDataStyle.COMET:
if has[MetaDataStyle.COMET]:
print("----------- CoMet tags -----------")
if opts.raw:
print(ca.read_raw_comet())
else:
print(ca.read_comet())
try:
if opts.raw:
print(ca.read_raw_comet())
else:
print(ca.read_comet())
except Exception as e:
logger.error("Failed to load metadata for %s: %s", ca.path, e)
elif opts.delete:
style_name = MetaDataStyle.name[opts.type]
@ -321,7 +333,11 @@ def process_file_cli(
src_style_name = MetaDataStyle.name[opts.copy]
if has[opts.copy]:
if not opts.dryrun:
md = ca.read_metadata(opts.copy)
try:
md = ca.read_metadata(opts.copy)
except Exception as e:
md = GenericMetadata()
logger.error("Failed to load metadata for %s: %s", ca.path, e)
if settings.apply_cbl_transform_on_bulk_operation and opts.type == MetaDataStyle.CBI:
md = CBLTransformer(md, settings).apply()

View File

@ -341,7 +341,10 @@ class FileSelectionList(QtWidgets.QWidget):
readonly_item.setCheckState(QtCore.Qt.CheckState.Unchecked)
# Reading these will force them into the ComicArchive's cache
fi.ca.read_cix()
try:
fi.ca.read_cix()
except Exception:
...
fi.ca.has_cbi()
def get_selected_archive_list(self) -> list[ComicArchive]:

View File

@ -196,7 +196,10 @@ class IssueIdentifier:
# see if the archive has any useful meta data for searching with
if ca.has_cix():
internal_metadata = ca.read_cix()
try:
internal_metadata = ca.read_cix()
except Exception as e:
logger.error("Failed to load metadata for %s: %s", ca.path, e)
elif ca.has_cbi():
internal_metadata = ca.read_cbi()
else:

View File

@ -36,6 +36,7 @@ from comictaggerlib.settings import ComicTaggerSettings
logger = logging.getLogger("comictagger")
logging.getLogger("comicapi").setLevel(logging.DEBUG)
logging.getLogger("comictaggerlib").setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
try:

View File

@ -1697,7 +1697,11 @@ Have fun!
ii = IssueIdentifier(ca, self.settings)
# read in metadata, and parse file name if not there
md = ca.read_metadata(self.save_data_style)
try:
md = ca.read_metadata(self.save_data_style)
except Exception as e:
md = GenericMetadata()
logger.error("Failed to load metadata for %s: %s", ca.path, e)
if md.is_empty:
md = ca.metadata_from_filename(
self.settings.complicated_parser,
@ -1844,7 +1848,11 @@ Have fun!
self.auto_tag_log("==========================================================================\n")
self.auto_tag_log(f"Auto-Tagging {prog_idx + 1} of {len(ca_list)}\n")
self.auto_tag_log(f"{ca.path}\n")
cover_idx = ca.read_metadata(style).get_cover_page_index_list()[0]
try:
cover_idx = ca.read_metadata(style).get_cover_page_index_list()[0]
except Exception as e:
cover_idx = 0
logger.error("Failed to load metadata for %s: %s", ca.path, e)
image_data = ca.get_page(cover_idx)
self.atprogdialog.set_archive_image(image_data)
self.atprogdialog.set_test_image(bytes())
@ -1923,6 +1931,11 @@ Have fun!
QtWidgets.QMessageBox.information(self, self.tr("Auto-Tag Summary"), self.tr(summary))
logger.info(summary)
def exception(self, message):
errorbox = QtWidgets.QMessageBox()
errorbox.setText(message)
errorbox.exec()
def dirty_flag_verification(self, title: str, desc: str) -> bool:
if self.dirty_flag:
reply = QtWidgets.QMessageBox.question(
@ -2056,8 +2069,11 @@ Have fun!
self.settings.last_opened_folder = os.path.abspath(os.path.split(comic_archive.path)[0])
self.comic_archive = comic_archive
self.metadata = self.comic_archive.read_metadata(self.load_data_style)
if self.metadata is None:
try:
self.metadata = self.comic_archive.read_metadata(self.load_data_style)
except Exception as e:
logger.error("Failed to load metadata for %s: %s", self.comic_archive.path, e)
self.exception(f"Failed to load metadata for {self.comic_archive.path}:\n\n{e}")
self.metadata = GenericMetadata()
self.actual_load_current_archive()

View File

@ -27,7 +27,7 @@ def test_getPageNameList():
]
def test_set_default_page_list(tmpdir):
def test_set_default_page_list(tmp_path):
md = comicapi.genericmetadata.GenericMetadata()
md.overlay(comicapi.genericmetadata.md_test)
md.pages = []
@ -53,9 +53,8 @@ def test_metadata_read():
assert md_dict == md_test_dict
def test_save_cix(tmpdir):
comic_path = tmpdir.mkdir("cbz") / cbz_path.name
print(comic_path)
def test_save_cix(tmp_path):
comic_path = tmp_path / cbz_path.name
shutil.copy(cbz_path, comic_path)
c = comicapi.comicarchive.ComicArchive(comic_path)
@ -67,9 +66,8 @@ def test_save_cix(tmpdir):
md = c.read_cix()
def test_page_type_save(tmpdir):
comic_path = tmpdir.mkdir("cbz") / cbz_path.name
print(comic_path)
def test_page_type_save(tmp_path):
comic_path = tmp_path / cbz_path.name
shutil.copy(cbz_path, comic_path)
@ -83,6 +81,17 @@ def test_page_type_save(tmpdir):
md = c.read_cix()
def test_invalid_zip(tmp_path):
comic_path = tmp_path / cbz_path.name
with open(cbz_path, mode="b+r") as f:
comic_path.write_bytes(b"PK\003\004" + f.read()[4:].replace(b"PK\003\004", b"PK\000\000"))
c = comicapi.comicarchive.ComicArchive(comic_path)
assert not c.write_cix(comicapi.genericmetadata.md_test)
archivers = [
comicapi.comicarchive.ZipArchiver,
comicapi.comicarchive.SevenZipArchiver,
@ -95,8 +104,8 @@ archivers = [
@pytest.mark.parametrize("archiver", archivers)
def test_copy_to_archive(archiver, tmpdir):
comic_path = tmpdir / cbz_path.with_suffix("").name
def test_copy_to_archive(archiver, tmp_path):
comic_path = tmp_path / cbz_path.with_suffix("").name
cbz = comicapi.comicarchive.ComicArchive(cbz_path)
archive = archiver(comic_path)