Add basic logging

This commit is contained in:
Timmy Welch 2022-04-04 18:59:26 -07:00
parent 62baa79d23
commit 7256903da4
8 changed files with 69 additions and 37 deletions

View File

@ -14,11 +14,14 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from comicapi import utils from comicapi import utils
from comicapi.genericmetadata import GenericMetadata from comicapi.genericmetadata import GenericMetadata
logger = logging.getLogger(__name__)
class CoMet: class CoMet:

View File

@ -47,6 +47,9 @@ from comicapi.filenameparser import FileNameParser
from comicapi.genericmetadata import GenericMetadata, PageType from comicapi.genericmetadata import GenericMetadata, PageType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if not pil_available:
logger.exception("PIL unavalable")
sys.path.insert(0, os.path.abspath(".")) sys.path.insert(0, os.path.abspath("."))
@ -77,10 +80,10 @@ class SevenZipArchiver:
with py7zr.SevenZipFile(self.path, "r") as zf: with py7zr.SevenZipFile(self.path, "r") as zf:
data = zf.read(archive_file)[archive_file].read() data = zf.read(archive_file)[archive_file].read()
except py7zr.Bad7zFile as e: except py7zr.Bad7zFile as e:
logger.waning("bad 7zip file [%s]: %s :: %s", e, self.path, archive_file) logger.error("bad 7zip file [%s]: %s :: %s", e, self.path, archive_file)
raise IOError raise IOError
except Exception as e: except Exception as e:
logger.waning("bad 7zip file [%s]: %s :: %s", e, self.path, archive_file) logger.error("bad 7zip file [%s]: %s :: %s", e, self.path, archive_file)
raise IOError raise IOError
return data return data
@ -116,7 +119,7 @@ class SevenZipArchiver:
return namelist return namelist
except Exception as e: except Exception as e:
logger.warning("Unable to get 7zip file list [%s]: %s", e, self.path) logger.error("Unable to get 7zip file list [%s]: %s", e, self.path)
return [] return []
def rebuild_zip_file(self, exclude_list): def rebuild_zip_file(self, exclude_list):
@ -134,8 +137,8 @@ class SevenZipArchiver:
with py7zr.SevenZipFile(tmp_name, "w") as zout: with py7zr.SevenZipFile(tmp_name, "w") as zout:
for fname, bio in zin.read(targets).items(): for fname, bio in zin.read(targets).items():
zout.writef(bio, fname) zout.writef(bio, fname)
except Exception as e: except Exception:
logger.warning("Exception[%s]: %s", e, self.path) logger.exception("Error rebuilding 7zip file: %s", self.path)
return [] return []
# replace with the new file # replace with the new file
@ -151,7 +154,7 @@ class SevenZipArchiver:
if data is not None: if data is not None:
zout.writestr(data, fname) zout.writestr(data, fname)
except Exception as e: except Exception as e:
logger.warning("Error while copying to %s: %s", self.path, e) logger.exception("Error while copying to %s: %s", self.path, e)
return False return False
else: else:
return True return True
@ -180,10 +183,10 @@ class ZipArchiver:
try: try:
data = zf.read(archive_file) data = zf.read(archive_file)
except zipfile.BadZipfile as e: except zipfile.BadZipfile as e:
logger.warning("bad zipfile [%s]: %s :: %s", e, self.path, archive_file) logger.error("bad zipfile [%s]: %s :: %s", e, self.path, archive_file)
raise IOError from e raise IOError from e
except Exception as e: except Exception as e:
logger.warning("bad zipfile [%s]: %s :: %s", e, self.path, archive_file) logger.error("bad zipfile [%s]: %s :: %s", e, self.path, archive_file)
raise IOError from e raise IOError from e
return data return data
@ -209,7 +212,7 @@ class ZipArchiver:
zf.writestr(archive_file, data) zf.writestr(archive_file, data)
return True return True
except Exception as e: except Exception as e:
logger.warning("writing zip file failed [%s]: %s", e, self.path) logger.error("writing zip file failed [%s]: %s", e, self.path)
return False return False
def get_filename_list(self): def get_filename_list(self):
@ -218,7 +221,7 @@ class ZipArchiver:
namelist = zf.namelist() namelist = zf.namelist()
return namelist return namelist
except Exception as e: except Exception as e:
logger.warning("Unable to get zipfile list [%s]: %s", e, self.path) logger.error("Unable to get zipfile list [%s]: %s", e, self.path)
return [] return []
def rebuild_zip_file(self, exclude_list): def rebuild_zip_file(self, exclude_list):
@ -229,6 +232,7 @@ class ZipArchiver:
tmp_fd, tmp_name = tempfile.mkstemp(dir=os.path.dirname(self.path)) tmp_fd, tmp_name = tempfile.mkstemp(dir=os.path.dirname(self.path))
os.close(tmp_fd) os.close(tmp_fd)
try:
with zipfile.ZipFile(self.path, "r") as zin: with zipfile.ZipFile(self.path, "r") as zin:
with zipfile.ZipFile(tmp_name, "w", allowZip64=True) as zout: with zipfile.ZipFile(tmp_name, "w", allowZip64=True) as zout:
for item in zin.infolist(): for item in zin.infolist():
@ -238,6 +242,9 @@ class ZipArchiver:
# preserve the old comment # preserve the old comment
zout.comment = zin.comment zout.comment = zin.comment
except Exception:
logger.exception("Error rebuilding 7zip file: %s", self.path)
return []
# replace with the new file # replace with the new file
os.remove(self.path) os.remove(self.path)
@ -299,6 +306,7 @@ class ZipArchiver:
else: else:
raise Exception("Failed to write comment to zip file!") raise Exception("Failed to write comment to zip file!")
except Exception: except Exception:
logger.exception()
return False return False
else: else:
return True return True
@ -317,8 +325,8 @@ class ZipArchiver:
if comment is not None: if comment is not None:
if not self.write_zip_comment(self.path, comment): if not self.write_zip_comment(self.path, comment):
return False return False
except Exception as e: except Exception:
logger.warning("Error while copying to %s: %s", self.path, e) logger.exception("Error while copying to %s", self.path)
return False return False
else: else:
return True return True
@ -371,8 +379,8 @@ class RarArchiver:
if platform.system() == "Darwin": if platform.system() == "Darwin":
time.sleep(1) time.sleep(1)
os.remove(tmp_name) os.remove(tmp_name)
except Exception as e: except Exception:
logger.warning(e) logger.exception("Failed to set a comment")
return False return False
else: else:
return True return True
@ -401,10 +409,10 @@ class RarArchiver:
) )
continue continue
except (OSError, IOError) as e: except (OSError, IOError) as e:
logger.warning("read_file(): [%s] %s:%s attempt #%d", e, self.path, archive_file, tries) logger.error("read_file(): [%s] %s:%s attempt #%d", e, self.path, archive_file, tries)
time.sleep(1) time.sleep(1)
except Exception as e: except Exception as e:
logger.warning( logger.error(
"Unexpected exception in read_file(): [%s] for %s:%s attempt #%d", e, self.path, archive_file, tries "Unexpected exception in read_file(): [%s] for %s:%s attempt #%d", e, self.path, archive_file, tries
) )
break break
@ -489,7 +497,7 @@ class RarArchiver:
namelist.append(item.filename) namelist.append(item.filename)
except (OSError, IOError) as e: except (OSError, IOError) as e:
logger.warning(f"get_filename_list(): [{e}] {self.path} attempt #{tries}".format(str(e), self.path, tries)) logger.error(f"get_filename_list(): [{e}] {self.path} attempt #{tries}".format(str(e), self.path, tries))
time.sleep(1) time.sleep(1)
else: else:
@ -505,7 +513,7 @@ class RarArchiver:
rarc = rarfile.RarFile(self.path) rarc = rarfile.RarFile(self.path)
except (OSError, IOError) as e: except (OSError, IOError) as e:
logger.warning("getRARObj(): [%s] %s attempt #%s", e, self.path, tries) logger.error("getRARObj(): [%s] %s attempt #%s", e, self.path, tries)
time.sleep(1) time.sleep(1)
else: else:
@ -537,7 +545,7 @@ class FolderArchiver:
data = f.read() data = f.read()
f.close() f.close()
except IOError: except IOError:
pass logger.exception("Failed to read: %s", fname)
return data return data
@ -549,6 +557,7 @@ class FolderArchiver:
f.write(data) f.write(data)
f.close() f.close()
except: except:
logger.exception("Failed to read: %s", fname)
return False return False
else: else:
return True return True
@ -559,6 +568,7 @@ class FolderArchiver:
try: try:
os.remove(fname) os.remove(fname)
except: except:
logger.exception("Failed to read: %s", fname)
return False return False
else: else:
return True return True
@ -787,7 +797,7 @@ class ComicArchive:
try: try:
image_data = self.archiver.read_file(filename) image_data = self.archiver.read_file(filename)
except IOError: except IOError:
logger.warning("Error reading in page. Substituting logo page.") logger.exception("Error reading in page. Substituting logo page.")
image_data = ComicArchive.logo_data image_data = ComicArchive.logo_data
return image_data return image_data
@ -815,7 +825,7 @@ class ComicArchive:
if count < 5: if count < 5:
return None return None
# count the length of every filename, and count occurences # count the length of every filename, and count occurrences
length_buckets = {} length_buckets = {}
for name in name_list: for name in name_list:
fname = os.path.split(name)[1] fname = os.path.split(name)[1]
@ -828,7 +838,7 @@ class ComicArchive:
# sort by most common # sort by most common
sorted_buckets = sorted(iter(length_buckets.items()), key=lambda k_v: (k_v[1], k_v[0]), reverse=True) sorted_buckets = sorted(iter(length_buckets.items()), key=lambda k_v: (k_v[1], k_v[0]), reverse=True)
# statistical mode occurence is first # statistical mode occurrence is first
mode_length = sorted_buckets[0][0] mode_length = sorted_buckets[0][0]
# we are only going to consider the final image file: # we are only going to consider the final image file:
@ -955,7 +965,7 @@ class ComicArchive:
try: try:
raw_cix = self.archiver.read_file(self.ci_xml_filename) raw_cix = self.archiver.read_file(self.ci_xml_filename)
except IOError as e: except IOError as e:
logger.warning("Error reading in raw CIX!: %s", e) logger.error("Error reading in raw CIX!: %s", e)
raw_cix = "" raw_cix = ""
return raw_cix return raw_cix
@ -1022,15 +1032,13 @@ class ComicArchive:
def read_raw_comet(self): def read_raw_comet(self):
if not self.has_comet(): if not self.has_comet():
err_msg = self.path + " doesn't have CoMet data!" logger.info("%s doesn't have CoMet data!", self.path)
logger.info(err_msg)
return None return None
try: try:
raw_comet = self.archiver.read_file(self.comet_filename) raw_comet = self.archiver.read_file(self.comet_filename)
except IOError as e: except:
err_msg = f"Error reading in raw CoMet!: {e}" logger.exception("Error reading in raw CoMet!")
logger.warning(err_msg)
raw_comet = "" raw_comet = ""
return raw_comet return raw_comet
@ -1110,7 +1118,8 @@ class ComicArchive:
p["ImageSize"] = str(len(data)) p["ImageSize"] = str(len(data))
p["ImageHeight"] = str(h) p["ImageHeight"] = str(h)
p["ImageWidth"] = str(w) p["ImageWidth"] = str(w)
except IOError: except Exception as e:
logger.warning("decoding image failed: %s", e)
p["ImageSize"] = str(len(data)) p["ImageSize"] = str(len(data))
else: else:

View File

@ -15,12 +15,15 @@
# limitations under the License. # limitations under the License.
import json import json
import logging
from collections import defaultdict from collections import defaultdict
from datetime import datetime from datetime import datetime
from comicapi import utils from comicapi import utils
from comicapi.genericmetadata import GenericMetadata from comicapi.genericmetadata import GenericMetadata
logger = logging.getLogger(__name__)
class ComicBookInfo: class ComicBookInfo:
def metadata_from_string(self, string): def metadata_from_string(self, string):

View File

@ -14,12 +14,15 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from comicapi import utils from comicapi import utils
from comicapi.genericmetadata import GenericMetadata from comicapi.genericmetadata import GenericMetadata
from comicapi.issuestring import IssueString from comicapi.issuestring import IssueString
logger = logging.getLogger(__name__)
class ComicInfoXml: class ComicInfoXml:

View File

@ -20,10 +20,13 @@ This should probably be re-written, but, well, it mostly works!
# Some portions of this code were modified from pyComicMetaThis project # Some portions of this code were modified from pyComicMetaThis project
# http://code.google.com/p/pycomicmetathis/ # http://code.google.com/p/pycomicmetathis/
import logging
import os import os
import re import re
from urllib.parse import unquote from urllib.parse import unquote
logger = logging.getLogger(__name__)
class FileNameParser: class FileNameParser:
def __init__(self): def __init__(self):

View File

@ -20,10 +20,13 @@ possible, however lossy it might be
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging
from typing import List, TypedDict from typing import List, TypedDict
from comicapi import utils from comicapi import utils
logger = logging.getLogger(__name__)
class PageType: class PageType:

View File

@ -20,6 +20,11 @@ comics industry throws at us.
# limitations under the License. # limitations under the License.
import logging
logger = logging.getLogger(__name__)
class IssueString: class IssueString:
def __init__(self, text): def __init__(self, text):

View File

@ -16,6 +16,7 @@
import codecs import codecs
import locale import locale
import logging
import os import os
import platform import platform
import re import re
@ -25,6 +26,8 @@ from collections import defaultdict
import pycountry import pycountry
logger = logging.getLogger(__name__)
class UtilsVars: class UtilsVars:
already_fixed_encoding = False already_fixed_encoding = False