From 34a759b8044123bbf5d0aa66dedd3b563fe3a014 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Thu, 14 Nov 2024 16:27:26 +0100 Subject: backend/lint: Implement flake8-simplify suggestions --- src/hircine/plugins/__init__.py | 2 +- src/hircine/plugins/scrapers/gallery_dl.py | 2 +- src/hircine/scanner.py | 27 +++++++++++++-------------- src/hircine/scraper/utils.py | 5 ++--- tests/api/test_sort.py | 2 +- tests/scanner/test_scanner.py | 23 +++++++++++------------ 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/hircine/plugins/__init__.py b/src/hircine/plugins/__init__.py index 27e55a7..ea1a262 100644 --- a/src/hircine/plugins/__init__.py +++ b/src/hircine/plugins/__init__.py @@ -8,7 +8,7 @@ transformers = [] def get_scraper(name): - return scraper_registry.get(name, None) + return scraper_registry.get(name) def get_scrapers(): diff --git a/src/hircine/plugins/scrapers/gallery_dl.py b/src/hircine/plugins/scrapers/gallery_dl.py index a6cebc4..17f85d0 100644 --- a/src/hircine/plugins/scrapers/gallery_dl.py +++ b/src/hircine/plugins/scrapers/gallery_dl.py @@ -36,7 +36,7 @@ class GalleryDLScraper(Scraper): self.data = self.load() category = self.data.get("category") - if category in HANDLERS.keys(): + if category in HANDLERS: self.is_available = True self.handler = HANDLERS.get(category)() diff --git a/src/hircine/scanner.py b/src/hircine/scanner.py index 162e1f0..d1077ed 100644 --- a/src/hircine/scanner.py +++ b/src/hircine/scanner.py @@ -302,19 +302,18 @@ class Scanner: def process_member(self, input): path, name = input - with ZipFile(path, mode="r") as zip: - with zip.open(name, mode="r") as member: - _, ext = os.path.splitext(name) - digest = file_digest(member, blake3).digest() - - if self.thumbnailer.can_process(ext): - hash = digest.hex() - - width, height = self.thumbnailer.process( - member, hash, reprocess=self.reprocess - ) - return digest, Member( - path=member.name, hash=hash, width=width, height=height - ) + with ZipFile(path, mode="r") as zip, zip.open(name, mode="r") as member: + _, ext = os.path.splitext(name) + digest = file_digest(member, blake3).digest() + + if self.thumbnailer.can_process(ext): + hash = digest.hex() + + width, height = self.thumbnailer.process( + member, hash, reprocess=self.reprocess + ) + return digest, Member( + path=member.name, hash=hash, width=width, height=height + ) return digest, None diff --git a/src/hircine/scraper/utils.py b/src/hircine/scraper/utils.py index 1c1fbee..d3fa6d6 100644 --- a/src/hircine/scraper/utils.py +++ b/src/hircine/scraper/utils.py @@ -57,6 +57,5 @@ def open_archive_file(archive, member, check_sidecar=True): # pragma: no cover return - with ZipFile(archive.path, "r") as zip: - with zip.open(member, "r") as file: - yield file + with ZipFile(archive.path, "r") as zip, zip.open(member, "r") as file: + yield file diff --git a/tests/api/test_sort.py b/tests/api/test_sort.py index 9cafe82..404e3d6 100644 --- a/tests/api/test_sort.py +++ b/tests/api/test_sort.py @@ -135,4 +135,4 @@ async def test_query_namespace_sort_sort_name(query_namespace_sort): response = Response(await query_namespace_sort({"on": "SORT_NAME"})) response.assert_is("NamespaceFilterResult") - assert ["two", "one"] == [edge["name"] for edge in response.edges] + assert [edge["name"] for edge in response.edges] == ["two", "one"] diff --git a/tests/scanner/test_scanner.py b/tests/scanner/test_scanner.py index a70c4d8..6fc6650 100644 --- a/tests/scanner/test_scanner.py +++ b/tests/scanner/test_scanner.py @@ -104,18 +104,17 @@ async def test_scanner_dedups_archive_contents(archive, scanner, capsys): archive = await DB.add(archive) dedup_path = archive.path + ".dedup" - with ZipFile(archive.path, "r") as zin: - with ZipFile(dedup_path, "w") as zout: - for info in zin.infolist(): - base, ext = os.path.splitext(info.filename) - - if base == "03": - continue - - if ext == ".png": - zout.writestr(f"0{base}.png", zin.read(info)) - else: - zout.writestr(info.filename, zin.read(info)) + with ZipFile(archive.path, "r") as zin, ZipFile(dedup_path, "w") as zout: + for info in zin.infolist(): + base, ext = os.path.splitext(info.filename) + + if base == "03": + continue + + if ext == ".png": + zout.writestr(f"0{base}.png", zin.read(info)) + else: + zout.writestr(info.filename, zin.read(info)) await scanner.scan() added_archive = await DB.get(Archive, 2, full=True) -- cgit v1.2.3-2-gb3c3