From 135544c0db011631f01a931287829e2739e4b622 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Wed, 20 Apr 2022 13:13:03 -0700 Subject: [PATCH] Code cleanup --- comicapi/comicarchive.py | 12 ++++------ comicapi/filenameparser.py | 2 -- comictaggerlib/autotagmatchwindow.py | 2 +- comictaggerlib/cli.py | 16 ++++++------- comictaggerlib/comicvinetalker.py | 32 ++++++------------------- comictaggerlib/filerenamer.py | 7 ++---- comictaggerlib/imagefetcher.py | 15 +++++------- comictaggerlib/imagehasher.py | 3 +-- comictaggerlib/issueidentifier.py | 2 +- comictaggerlib/main.py | 14 ++++------- comictaggerlib/options.py | 2 +- comictaggerlib/pagelisteditor.py | 13 ++++------ comictaggerlib/renamewindow.py | 5 ++-- comictaggerlib/settings.py | 2 +- comictaggerlib/settingswindow.py | 4 ++-- comictaggerlib/taggerwindow.py | 12 ++++------ comictaggerlib/volumeselectionwindow.py | 2 +- scripts/name_fixer.py | 2 -- tests/test_FilenameParser.py | 18 -------------- tests/test_comicarchive.py | 1 - 20 files changed, 52 insertions(+), 114 deletions(-) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index c209c19..660a8dc 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -83,10 +83,10 @@ class SevenZipArchiver: data = zf.read(archive_file)[archive_file].read() except py7zr.Bad7zFile as e: logger.error("bad 7zip file [%s]: %s :: %s", e, self.path, archive_file) - raise IOError + raise IOError from e except Exception as e: logger.error("bad 7zip file [%s]: %s :: %s", e, self.path, archive_file) - raise IOError + raise IOError from e return data @@ -143,7 +143,6 @@ class SevenZipArchiver: zout.writef(bio, fname) except Exception: logger.exception("Error rebuilding 7zip file: %s", self.path) - return [] # replace with the new file os.remove(self.path) @@ -248,8 +247,7 @@ class ZipArchiver: # preserve the old comment zout.comment = zin.comment except Exception: - logger.exception("Error rebuilding 7zip file: %s", self.path) - return [] + logger.exception("Error rebuilding zip file: %s", self.path) # replace with the new file os.remove(self.path) @@ -422,8 +420,7 @@ class RarArchiver: break else: - # Success" - # entries is a list of of tuples: ( rarinfo, filedata) + # Success. Entries is a list of of tuples: ( rarinfo, filedata) if tries > 1: logger.info("Attempted read_files() {%d} times", tries) if len(entries) == 1: @@ -593,7 +590,6 @@ class FolderArchiver: return itemlist -# noinspection PyUnusedLocal class UnknownArchiver: """Unknown implementation""" diff --git a/comicapi/filenameparser.py b/comicapi/filenameparser.py index 73d50de..9d67231 100644 --- a/comicapi/filenameparser.py +++ b/comicapi/filenameparser.py @@ -107,8 +107,6 @@ class FileNameParser: # some titles) filename = re.sub(r"of [\d]+", self.repl, filename) - # print u"[{0}]".format(filename) - # we should now have a cleaned up filename version with all the words in # the same positions as original filename diff --git a/comictaggerlib/autotagmatchwindow.py b/comictaggerlib/autotagmatchwindow.py index debdcf4..03fc7e9 100644 --- a/comictaggerlib/autotagmatchwindow.py +++ b/comictaggerlib/autotagmatchwindow.py @@ -91,7 +91,7 @@ class AutoTagMatchWindow(QtWidgets.QDialog): path = self.current_match_set.ca.path self.setWindowTitle( - "Select correct match or skip ({0} of {1}): {2}".format( + "Select correct match or skip ({} of {}): {}".format( self.current_match_set_idx + 1, len(self.match_set_list), os.path.split(path)[1], diff --git a/comictaggerlib/cli.py b/comictaggerlib/cli.py index c9a29b8..a8cded2 100644 --- a/comictaggerlib/cli.py +++ b/comictaggerlib/cli.py @@ -81,7 +81,7 @@ def display_match_set_for_choice(label, match_set: MultipleMatch, opts, settings for (counter, m) in enumerate(match_set.matches): counter += 1 print( - " {0}. {1} #{2} [{3}] ({4}/{5}) - {6}".format( + " {}. {} #{} [{}] ({}/{}) - {}".format( counter, m["series"], m["issue_number"], @@ -195,7 +195,7 @@ def process_file_cli(filename, opts, settings, match_results: OnlineMatchResults ca = ComicArchive(filename, settings.rar_exe_path, ComicTaggerSettings.get_graphic("nocover.png")) if not os.path.lexists(filename): - logger.error("Cannot find " + filename) + logger.error("Cannot find %s", filename) return if not ca.seems_to_be_a_comic_archive(): @@ -463,13 +463,13 @@ def process_file_cli(filename, opts, settings, match_results: OnlineMatchResults try: new_name = renamer.determine_name(ext=new_ext) - except Exception as e: - print( - msg_hdr + "Invalid format string!\nYour rename template is invalid!\n\n" - "{}\n\nPlease consult the template help in the settings " + except Exception: + logger.exception( + msg_hdr + "Invalid format string!\n" + "Your rename template is invalid!\n\n" + "Please consult the template help in the settings " "and the documentation on the format at " - "https://docs.python.org/3/library/string.html#format-string-syntax".format(e), - file=sys.stderr, + "https://docs.python.org/3/library/string.html#format-string-syntax" ) return diff --git a/comictaggerlib/comicvinetalker.py b/comictaggerlib/comicvinetalker.py index 9b2dfb4..12bc94e 100644 --- a/comictaggerlib/comicvinetalker.py +++ b/comictaggerlib/comicvinetalker.py @@ -144,8 +144,7 @@ class ComicVineTalker: cv_response = requests.get(test_url, headers={"user-agent": "comictagger/" + ctversion.version}).json() - # Bogus request, but if the key is wrong, you get error 100: "Invalid - # API Key" + # Bogus request, but if the key is wrong, you get error 100: "Invalid API Key" return cv_response["status_code"] != 100 except: return False @@ -208,8 +207,7 @@ class ComicVineTalker: # Sanitize the series name for comicvine searching, comicvine search ignore symbols search_series_name = utils.sanitize_title(series_name) - # before we search online, look in our cache, since we might have - # done this same search recently + # before we search online, look in our cache, since we might have done this same search recently cvc = ComicVineCacher() if not refresh_cache: cached_search_results = cvc.get_search_results(series_name) @@ -238,13 +236,11 @@ class ComicVineTalker: # 8 Dec 2018 - Comic Vine changed query results again. Terms are now # ORed together, and we get thousands of results. Good news is the - # results are sorted by relevance, so we can be smart about halting - # the search. + # results are sorted by relevance, so we can be smart about halting the search. # 1. Don't fetch more than some sane amount of pages. max_results = 500 # 2. Halt when not all of our search terms are present in a result - # 3. Halt when the results contain more (plus threshold) words than - # our search + # 3. Halt when the results contain more (plus threshold) words than our search result_word_count_max = len(search_series_name.split()) + 3 total_result_count = min(total_result_count, max_results) @@ -516,17 +512,6 @@ class ComicVineTalker: return metadata def cleanup_html(self, string, remove_html_tables): - """ - converter = html2text.HTML2Text() - #converter.emphasis_mark = '*' - #converter.ignore_links = True - converter.body_width = 0 - - print(html2text.html2text(string)) - return string - #return converter.handle(string) - """ - if string is None: return "" # find any tables @@ -663,8 +648,7 @@ class ComicVineTalker: def fetch_cached_issue_select_details(self, issue_id): - # before we search online, look in our cache, since we might already - # have this info + # before we search online, look in our cache, since we might already have this info cvc = ComicVineCacher() return cvc.get_issue_select_details(issue_id) @@ -692,8 +676,7 @@ class ComicVineTalker: alt_cover_url_list = [] # Using knowledge of the layout of the Comic Vine issue page here: - # look for the divs that are in the classes 'imgboxart' and - # 'issue-cover' + # look for the divs that are in the classes 'imgboxart' and 'issue-cover' div_list = soup.find_all("div") covers_found = 0 for d in div_list: @@ -713,8 +696,7 @@ class ComicVineTalker: def fetch_cached_alternate_cover_urls(self, issue_id): - # before we search online, look in our cache, since we might already - # have this info + # before we search online, look in our cache, since we might already have this info cvc = ComicVineCacher() url_list = cvc.get_alt_covers(issue_id) if url_list is not None: diff --git a/comictaggerlib/filerenamer.py b/comictaggerlib/filerenamer.py index 6570f64..a21b0c7 100644 --- a/comictaggerlib/filerenamer.py +++ b/comictaggerlib/filerenamer.py @@ -62,8 +62,7 @@ class FileRenamer: # (TODO: This could fail if there is more than one token appended together, I guess) text_list = text.split() - # special case for issuecount, remove preceding non-token word, - # as in "...(of %issuecount%)..." + # special case for issuecount, remove preceding non-token word, as in "...(of %issuecount%)..." if token == "%issuecount%": for idx, word in enumerate(text_list): if token in word and not is_token(text_list[idx - 1]): @@ -270,7 +269,6 @@ class FileRenamer2: md_dict["month_name"] = calendar.month_name[int(md.month)] md_dict["month_abbr"] = calendar.month_abbr[int(md.month)] else: - print(md.month) md_dict["month_name"] = "" md_dict["month_abbr"] = "" @@ -294,5 +292,4 @@ class FileRenamer2: md.issue = IssueString(md.issue).as_string() if self.move: return new_name.strip() - else: - return new_basename.strip() + return new_basename.strip() diff --git a/comictaggerlib/imagefetcher.py b/comictaggerlib/imagefetcher.py index 93704d0..fa8f176 100644 --- a/comictaggerlib/imagefetcher.py +++ b/comictaggerlib/imagefetcher.py @@ -15,6 +15,7 @@ # limitations under the License. import datetime +import logging import os import shutil import sqlite3 as lite @@ -22,6 +23,9 @@ import tempfile import requests +from comictaggerlib import ctversion +from comictaggerlib.settings import ComicTaggerSettings + try: from PyQt5 import QtCore, QtNetwork @@ -29,12 +33,6 @@ try: except ImportError: qt_available = False - -import logging - -from comictaggerlib import ctversion -from comictaggerlib.settings import ComicTaggerSettings - logger = logging.getLogger(__name__) @@ -145,9 +143,8 @@ class ImageFetcher: timestamp = datetime.datetime.now() tmp_fd, filename = tempfile.mkstemp(dir=self.cache_folder, prefix="img") - f = os.fdopen(tmp_fd, "w+b") - f.write(image_data) - f.close() + with os.fdopen(tmp_fd, "w+b") as f: + f.write(image_data) cur.execute("INSERT or REPLACE INTO Images VALUES(?, ?, ?)", (url, filename, timestamp)) diff --git a/comictaggerlib/imagehasher.py b/comictaggerlib/imagehasher.py index ddeb65d..bc45fda 100755 --- a/comictaggerlib/imagehasher.py +++ b/comictaggerlib/imagehasher.py @@ -72,8 +72,7 @@ class ImageHasher: def average_hash2(self): """ - # Got this one from somewhere on the net. Not a clue how the 'convolve2d' - # works! + # Got this one from somewhere on the net. Not a clue how the 'convolve2d' works! from numpy import array from scipy.signal import convolve2d diff --git a/comictaggerlib/issueidentifier.py b/comictaggerlib/issueidentifier.py index 21ccc1c..b3c6ed7 100644 --- a/comictaggerlib/issueidentifier.py +++ b/comictaggerlib/issueidentifier.py @@ -557,7 +557,7 @@ class IssueIdentifier: def print_match(item): self.log_msg( - "-----> {0} #{1} {2} ({3}/{4}) -- score: {5}".format( + "-----> {} #{} {} ({}/{}) -- score: {}".format( item["series"], item["issue_number"], item["issue_title"], diff --git a/comictaggerlib/main.py b/comictaggerlib/main.py index d88dfcf..f17955a 100755 --- a/comictaggerlib/main.py +++ b/comictaggerlib/main.py @@ -45,8 +45,8 @@ try: """ if QtWidgets.QApplication.instance() is not None: errorbox = QtWidgets.QMessageBox() - errorbox.setText("Oops. An unexpected error occured:\n{0}".format(log_msg)) - errorbox.exec_() + errorbox.setText(f"Oops. An unexpected error occured:\n{log_msg}") + errorbox.exec() QtWidgets.QApplication.exit(1) else: logger.debug("No QApplication instance available.") @@ -55,7 +55,7 @@ try: _exception_caught = QtCore.pyqtSignal(object) def __init__(self, *args, **kwargs): - super(UncaughtHook, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) # this registers the exception_hook() function as hook with the Python interpreter sys.excepthook = self.exception_hook @@ -72,12 +72,8 @@ try: sys.__excepthook__(exc_type, exc_value, exc_traceback) else: exc_info = (exc_type, exc_value, exc_traceback) - log_msg = "\n".join( - ["".join(traceback.format_tb(exc_traceback)), "{0}: {1}".format(exc_type.__name__, exc_value)] - ) - logger.critical( - "Uncaught exception: %s", "{0}: {1}".format(exc_type.__name__, exc_value), exc_info=exc_info - ) + log_msg = "\n".join(["".join(traceback.format_tb(exc_traceback)), f"{exc_type.__name__}: {exc_value}"]) + logger.critical("Uncaught exception: %s: %s", exc_type.__name__, exc_value, exc_info=exc_info) # trigger message box show self._exception_caught.emit(log_msg) diff --git a/comictaggerlib/options.py b/comictaggerlib/options.py index a19fcf0..394b3fd 100644 --- a/comictaggerlib/options.py +++ b/comictaggerlib/options.py @@ -241,7 +241,7 @@ For more help visit the wiki at: https://github.com/comictagger/comictagger/wiki input_args = sys.argv[1:] # first check if we're launching a script: - for n in range(len(input_args)): + for n, _ in enumerate(input_args): if input_args[n] in ["-S", "--script"] and n + 1 < len(input_args): # insert a "--" which will cause getopt to ignore the remaining args # so they will be passed to the script diff --git a/comictaggerlib/pagelisteditor.py b/comictaggerlib/pagelisteditor.py index 100e315..cabce0e 100644 --- a/comictaggerlib/pagelisteditor.py +++ b/comictaggerlib/pagelisteditor.py @@ -35,12 +35,9 @@ def item_move_events(widget): def eventFilter(self, obj, event): if obj == widget: - # print(event.type()) if event.type() == QtCore.QEvent.Type.ChildRemoved: - # print("ChildRemoved") self.mysignal.emit("finish") if event.type() == QtCore.QEvent.Type.ChildAdded: - # print("ChildAdded") self.mysignal.emit("start") return True @@ -241,7 +238,7 @@ class PageListEditor(QtWidgets.QWidget): front_cover = 0 for i in range(self.listWidget.count()): item = self.listWidget.item(i) - page_dict = item.data(QtCore.Qt.ItemDataRole.UserRole)[0] # .toPyObject()[0] + page_dict = item.data(QtCore.Qt.ItemDataRole.UserRole)[0] if "Type" in page_dict and page_dict["Type"] == PageType.FrontCover: front_cover = int(page_dict["Image"]) break @@ -249,7 +246,7 @@ class PageListEditor(QtWidgets.QWidget): def get_current_page_type(self): row = self.listWidget.currentRow() - page_dict = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)[0] # .toPyObject()[0] + page_dict = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)[0] if "Type" in page_dict: return page_dict["Type"] @@ -257,7 +254,7 @@ class PageListEditor(QtWidgets.QWidget): def set_current_page_type(self, t): row = self.listWidget.currentRow() - page_dict = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)[0] # .toPyObject()[0] + page_dict = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)[0] if t == "": if "Type" in page_dict: @@ -335,7 +332,7 @@ class PageListEditor(QtWidgets.QWidget): def list_entry_text(self, page_dict): text = str(int(page_dict["Image"]) + 1) if "Type" in page_dict: - if page_dict["Type"] in self.pageTypeNames.keys(): + if page_dict["Type"] in self.pageTypeNames: text += " (" + self.pageTypeNames[page_dict["Type"]] + ")" else: text += " (Error: " + page_dict["Type"] + ")" @@ -349,7 +346,7 @@ class PageListEditor(QtWidgets.QWidget): page_list = [] for i in range(self.listWidget.count()): item = self.listWidget.item(i) - page_list.append(item.data(QtCore.Qt.ItemDataRole.UserRole)[0]) # .toPyObject()[0] + page_list.append(item.data(QtCore.Qt.ItemDataRole.UserRole)[0]) return page_list def emit_front_cover_change(self): diff --git a/comictaggerlib/renamewindow.py b/comictaggerlib/renamewindow.py index cd4e9e9..b9bc3d4 100644 --- a/comictaggerlib/renamewindow.py +++ b/comictaggerlib/renamewindow.py @@ -95,11 +95,11 @@ class RenameWindow(QtWidgets.QDialog): self, "Invalid format string!", "Your rename template is invalid!" - "

{}

" + f"

{e}

" "Please consult the template help in the " "settings and the documentation on the format at " "" - "https://docs.python.org/3/library/string.html#format-string-syntax".format(e), + "https://docs.python.org/3/library/string.html#format-string-syntax", ) return @@ -176,7 +176,6 @@ class RenameWindow(QtWidgets.QDialog): new_abs_path = utils.unique_file(os.path.join(folder, item["new_name"])) if os.path.join(folder, item["new_name"]) == item["archive"].path: - print(item["new_name"], "Filename is already good!") logger.info(item["new_name"], "Filename is already good!") continue diff --git a/comictaggerlib/settings.py b/comictaggerlib/settings.py index 1ab5929..e1a438c 100644 --- a/comictaggerlib/settings.py +++ b/comictaggerlib/settings.py @@ -243,7 +243,7 @@ class ComicTaggerSettings: def reset(self): os.unlink(self.settings_file) - self.__init__() + self.__init__(ComicTaggerSettings.folder) def load(self): def readline_generator(f): diff --git a/comictaggerlib/settingswindow.py b/comictaggerlib/settingswindow.py index 30ab344..dafd2b1 100644 --- a/comictaggerlib/settingswindow.py +++ b/comictaggerlib/settingswindow.py @@ -304,11 +304,11 @@ class SettingsWindow(QtWidgets.QDialog): self, "Invalid format string!", "Your rename template is invalid!" - "

{}

" + f"

{self.rename_error}

" "Please consult the template help in the " "settings and the documentation on the format at " "" - "https://docs.python.org/3/library/string.html#format-string-syntax".format(self.rename_error), + "https://docs.python.org/3/library/string.html#format-string-syntax", ) return diff --git a/comictaggerlib/taggerwindow.py b/comictaggerlib/taggerwindow.py index 6cffdea..e1e87fe 100644 --- a/comictaggerlib/taggerwindow.py +++ b/comictaggerlib/taggerwindow.py @@ -82,12 +82,12 @@ class TaggerWindow(QtWidgets.QMainWindow): alive = socket.waitForConnected(3000) if alive: print(f"Another application with key [{settings.install_id}] is already running") - logger.info(f"Another application with key [{settings.install_id}] is already running") + logger.info("Another application with key [%s] is already running", settings.install_id) # send file list to other instance if file_list: socket.write(pickle.dumps(file_list)) if not socket.waitForBytesWritten(3000): - print(socket.errorString()) + logger.error(socket.errorString()) socket.disconnectFromServer() sys.exit() else: @@ -493,8 +493,7 @@ Please choose options below, and select OK. os.unlink(ca.path) else: - # last export failed, so remove the zip, if it - # exists + # last export failed, so remove the zip, if it exists failed_list.append(ca.path) if os.path.lexists(export_name): os.remove(export_name) @@ -1092,7 +1091,6 @@ Please choose options below, and select OK. def update_credit_colors(self): # !!!ATB qt5 porting TODO - # return inactive_color = QtGui.QColor(255, 170, 150) active_palette = self.leSeries.palette() active_color = active_palette.color(QtGui.QPalette.ColorRole.Base) @@ -2067,8 +2065,8 @@ Please choose options below, and select OK to Auto-Tag. # the top win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, x, y, w, h, 0) win32gui.SetWindowPos(hwnd, win32con.HWND_NOTOPMOST, x, y, w, h, 0) - except Exception as e: - print("Whoops", e) + except Exception: + logger.exception("Fail to bring window to top") elif platform.system() == "Darwin": self.raise_() self.showNormal() diff --git a/comictaggerlib/volumeselectionwindow.py b/comictaggerlib/volumeselectionwindow.py index 17dcb68..e0a7ad5 100644 --- a/comictaggerlib/volumeselectionwindow.py +++ b/comictaggerlib/volumeselectionwindow.py @@ -348,7 +348,7 @@ class VolumeSelectionWindow(QtWidgets.QDialog): # pre sort the data - so that we can put exact matches first afterwards # compare as str incase extra chars ie. '1976?' - # - missing (none) values being converted to 'None' - consistant with prior behaviour in v1.2.3 + # - missing (none) values being converted to 'None' - consistent with prior behaviour in v1.2.3 # sort by start_year if set if self.settings.sort_series_by_year: try: diff --git a/scripts/name_fixer.py b/scripts/name_fixer.py index d9a65b8..fdbfa1d 100755 --- a/scripts/name_fixer.py +++ b/scripts/name_fixer.py @@ -92,8 +92,6 @@ def main(): else: xform_list = default_xform_list - # pprint( xform_list, indent=4) - filelist = utils.get_recursive_filelist(parsed_args.paths) # first find all comics diff --git a/tests/test_FilenameParser.py b/tests/test_FilenameParser.py index b953407..80de5e9 100644 --- a/tests/test_FilenameParser.py +++ b/tests/test_FilenameParser.py @@ -3,32 +3,14 @@ from filenames import fnames import comicapi.filenameparser -# def test_filename_parser(): -# p = comicapi.filenameparser.FileNameParser() -# p.parse_filename("Cory Doctorows Futuristic Tales of the Here and Now #1 andas game.rar") -# fp = p.__dict__ - -# assert fp["issue"] == "1" -# assert fp["series"] == "Cory Doctorows Futuristic Tales of the Here and Now" -# assert fp["remainder"] == "andas game" -# assert fp["volume"] == "" -# assert fp["year"] == "" -# assert fp["issue_count"] == "" - @pytest.mark.parametrize("filename,reason,expected", fnames) def test_file_name_parser(filename, reason, expected): p = comicapi.filenameparser.FileNameParser() p.parse_filename(filename) fp = p.__dict__ - # del expected["remainder"] - # del expected["title"] - # del fp["archive"] for s in ["title"]: if s in expected: - # expected[s] = "" del expected[s] - # if s not in fp: - # fp[s] = "" assert fp == expected diff --git a/tests/test_comicarchive.py b/tests/test_comicarchive.py index f922527..0339127 100644 --- a/tests/test_comicarchive.py +++ b/tests/test_comicarchive.py @@ -29,7 +29,6 @@ def test_set_default_page_list(tmpdir): md = GenericMetadata() md.overlay(md_test) md.pages = [] - print(md_test.pages, md.pages) md.set_default_page_list(len(md_test.pages)) assert isinstance(md.pages[0]["Image"], int)