From befffc98b1d26179fda8750af4a1a4f49f6f2635 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Thu, 12 Sep 2024 13:11:14 -0700 Subject: [PATCH] Catch all exceptions when parsing metadata from the CLI --- comictaggerlib/ctsettings/types.py | 79 ++++++++++++++++-------------- comictaggerlib/quick_tag.py | 2 +- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/comictaggerlib/ctsettings/types.py b/comictaggerlib/ctsettings/types.py index 97ccf14..0b18895 100644 --- a/comictaggerlib/ctsettings/types.py +++ b/comictaggerlib/ctsettings/types.py @@ -1,6 +1,7 @@ from __future__ import annotations import argparse +import logging import pathlib import sys import types @@ -15,6 +16,8 @@ from comicapi import utils from comicapi.comicarchive import tags from comicapi.genericmetadata import REMOVE, GenericMetadata +logger = logging.getLogger(__name__) + if sys.version_info < (3, 10): @typing.no_type_check @@ -198,44 +201,48 @@ def parse_metadata_from_string(mdstr: str) -> GenericMetadata: md = GenericMetadata() - if not mdstr: - return md - if mdstr[0] == "@": - p = pathlib.Path(mdstr[1:]) - if not p.is_file(): - raise argparse.ArgumentTypeError("Invalid filepath") - mdstr = p.read_text() - if mdstr[0] != "{": - mdstr = "{" + mdstr + "}" + try: + if not mdstr: + return md + if mdstr[0] == "@": + p = pathlib.Path(mdstr[1:]) + if not p.is_file(): + raise argparse.ArgumentTypeError("Invalid filepath") + mdstr = p.read_text() + if mdstr[0] != "{": + mdstr = "{" + mdstr + "}" - md_dict = yaml.safe_load(mdstr) + md_dict = yaml.safe_load(mdstr) - empty = True - # Map the dict to the metadata object - for key, value in md_dict.items(): - if hasattr(md, key): - t = get_type(key) - if value is None: - value = REMOVE - elif isinstance(t, tuple): - if value == "": - value = t[0]() + empty = True + # Map the dict to the metadata object + for key, value in md_dict.items(): + if hasattr(md, key): + t = get_type(key) + if value is None: + value = REMOVE + elif isinstance(t, tuple): + if value == "": + value = t[0]() + else: + if isinstance(value, str): + value = [value] + if not isinstance(value, Collection): + raise argparse.ArgumentTypeError(f"Invalid syntax for tag '{key}'") + values = list(value) + for idx, v in enumerate(values): + if not isinstance(v, t[1]): + values[idx] = convert_value(t[1], v) + value = t[0](values) else: - if isinstance(value, str): - value = [value] - if not isinstance(value, Collection): - raise argparse.ArgumentTypeError(f"Invalid syntax for tag '{key}'") - values = list(value) - for idx, v in enumerate(values): - if not isinstance(v, t[1]): - values[idx] = convert_value(t[1], v) - value = t[0](values) - else: - value = convert_value(t, value) + value = convert_value(t, value) - empty = False - setattr(md, key, value) - else: - raise argparse.ArgumentTypeError(f"'{key}' is not a valid tag name") - md.is_empty = empty + empty = False + setattr(md, key, value) + else: + raise argparse.ArgumentTypeError(f"'{key}' is not a valid tag name") + md.is_empty = empty + except Exception as e: + logger.exception("Unable to read metadata from the commandline '%s'", mdstr) + raise Exception("Unable to read metadata from the commandline") from e return md diff --git a/comictaggerlib/quick_tag.py b/comictaggerlib/quick_tag.py index 7e73d6e..a1ab9fc 100644 --- a/comictaggerlib/quick_tag.py +++ b/comictaggerlib/quick_tag.py @@ -10,7 +10,6 @@ from urllib.parse import urljoin import requests import settngs -from PIL import Image from comicapi import comicarchive, utils from comicapi.genericmetadata import GenericMetadata @@ -125,6 +124,7 @@ class QuickTag: ) -> GenericMetadata | None: if not ca.seems_to_be_a_comic_archive(): raise Exception(f"{ca.path} is not an archive") + from PIL import Image cover_index = tags.get_cover_page_index_list()[0] cover_image = Image.open(BytesIO(ca.get_page(cover_index)))