Skip to content

Commit 95ed2dc

Browse files
Dev (#13)
- Implement Marvel API - Filter files included in resulting file
1 parent 9d2ec08 commit 95ed2dc

15 files changed

Lines changed: 320 additions & 48 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.3.5
3+
rev: v0.4.4
44
hooks:
55
- id: ruff-format
66
- id: ruff

perdoo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"get_data_dir",
88
"setup_logging",
99
]
10-
__version__ = "0.1.0"
10+
__version__ = "0.2.0"
1111

1212
import logging
1313
import os

perdoo/__main__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ def fetch_from_services(
182182
return None, None, None
183183

184184
for service in (marvel, metron, comicvine, league):
185+
if not service:
186+
continue
187+
LOGGER.info("Fetching details from %s", type(service).__name__)
185188
metadata, metron_info, comic_info = service.fetch(details=details)
186189
if metadata and metron_info and comic_info:
187190
return metadata, metron_info, comic_info
@@ -299,13 +302,22 @@ def start(settings: Settings, force: bool = False) -> None:
299302
filename=new_file.stem,
300303
)
301304
metadata.meta = Meta(date_=date.today())
305+
files = list_files(temp_folder, *IMAGE_EXTENSIONS)
302306
if settings.output.create_metadata:
303-
metadata.to_file(file=temp_folder / "Metadata.xml")
307+
metadata_file = temp_folder / "Metadata.xml"
308+
metadata.to_file(file=metadata_file)
309+
files.append(metadata_file)
304310
if settings.output.create_metron_info:
305-
metron_info.to_file(file=temp_folder / "MetronInfo.xml")
311+
metron_info_file = temp_folder / "MetronInfo.xml"
312+
metron_info.to_file(file=metron_info_file)
313+
files.append(metron_info_file)
306314
if settings.output.create_comic_info:
307-
comic_info.to_file(file=temp_folder / "ComicInfo.xml")
308-
archive_file = archive.archive_files(src=temp_folder, filename=archive.path.stem)
315+
comic_info_file = temp_folder / "ComicInfo.xml"
316+
comic_info.to_file(file=comic_info_file)
317+
files.append(comic_info_file)
318+
archive_file = archive.archive_files(
319+
src=temp_folder, output_name=archive.path.stem, files=files
320+
)
309321
if not archive_file:
310322
LOGGER.critical("Unable to re-archive images")
311323
continue

perdoo/archives/_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def extract_files(self: BaseArchive, destination: Path) -> bool: ...
2121

2222
@classmethod
2323
@abstractmethod
24-
def archive_files(cls: type[BaseArchive], src: Path, filename: str) -> Path: ...
24+
def archive_files(
25+
cls: type[BaseArchive], src: Path, output_name: str, files: list[Path] | None = None
26+
) -> Path | None: ...
2527

2628
@staticmethod
2729
@abstractmethod

perdoo/archives/cb7.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ def extract_files(self: CB7Archive, destination: Path) -> bool:
5252
return False
5353

5454
@classmethod
55-
def archive_files(cls: type[CB7Archive], src: Path, filename: str) -> Path | None:
55+
def archive_files(
56+
cls: type[CB7Archive], src: Path, output_name: str, files: list[Path] | None = None
57+
) -> Path | None:
5658
if not py7zr_loaded:
5759
raise ImportError("Install Perdoo with the cb7 dependency group to use CB7 files.")
5860

59-
output_file = src.parent / f"{filename}.cb7"
61+
files = files or list_files(path=src)
62+
output_file = src.parent / f"{output_name}.cb7"
6063
try:
6164
with py7zr.SevenZipFile(output_file, "w") as archive:
62-
for file in list_files(path=src):
65+
for file in files:
6366
archive.write(file, arcname=str(file.relative_to(src)))
6467
return output_file
6568
except py7zr.Bad7zFile:

perdoo/archives/cbr.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def extract_files(self: CBRArchive, destination: Path) -> bool:
3939
return False
4040

4141
@classmethod
42-
def archive_files(cls: type[CBRArchive], src: Path, filename: str) -> Path | None:
42+
def archive_files(
43+
cls: type[CBRArchive], src: Path, output_name: str, files: list[Path] | None = None
44+
) -> Path | None:
4345
raise NotImplementedError("Unable to create archive in CBR format")
4446

4547
@staticmethod

perdoo/archives/cbt.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ def extract_files(self: CBTArchive, destination: Path) -> bool:
5353
return False
5454

5555
@classmethod
56-
def archive_files(cls: type[CBTArchive], src: Path, filename: str) -> Path | None:
57-
output_file = src.parent / f"{filename}.cbt"
56+
def archive_files(
57+
cls: type[CBTArchive], src: Path, output_name: str, files: list[Path] | None = None
58+
) -> Path | None:
59+
files = files or list_files(path=src)
60+
output_file = src.parent / f"{output_name}.cbt"
5861
try:
5962
with tarfile.open(output_file, "w:gz") as tar:
60-
for file in list_files(path=src):
63+
for file in files:
6164
tar.add(file, arcname=file.relative_to(src))
6265
return output_file
6366
except tarfile.CompressionError:

perdoo/archives/cbz.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ def extract_files(self: CBZArchive, destination: Path) -> bool:
4141
return False
4242

4343
@classmethod
44-
def archive_files(cls: type[CBZArchive], src: Path, filename: str) -> Path | None:
45-
output_file = src.parent / f"{filename}.cbz"
44+
def archive_files(
45+
cls: type[CBZArchive], src: Path, output_name: str, files: list[Path] | None = None
46+
) -> Path | None:
47+
files = files or list_files(path=src)
48+
output_file = src.parent / f"{output_name}.cbz"
4649
try:
4750
with zipfile.ZipFile(output_file, "w", zipfile.ZIP_DEFLATED) as zip_file:
48-
for file in list_files(path=src):
51+
for file in files:
4952
zip_file.write(file, arcname=file.relative_to(src))
5053
return output_file
5154
except zipfile.BadZipFile:

perdoo/models/metron_info.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"Format",
99
"Genre",
1010
"GenreResource",
11+
"InformationList",
1112
"InformationSource",
1213
"MetronInfo",
1314
"Price",
@@ -16,21 +17,22 @@
1617
"RoleResource",
1718
"Series",
1819
"Source",
19-
"Sources",
2020
"Universe",
2121
]
2222

2323
from datetime import date
2424
from enum import Enum
2525
from pathlib import Path
26-
from typing import Any, ClassVar
26+
from typing import Any, ClassVar, Generic, TypeVar
2727

2828
import xmltodict
2929
from PIL import Image
3030
from pydantic import Field, HttpUrl, PositiveInt
3131

3232
from perdoo.models._base import InfoModel, PascalModel
3333

34+
T = TypeVar("T")
35+
3436

3537
class InformationSource(Enum):
3638
COMIC_VINE = "Comic Vine"
@@ -73,9 +75,9 @@ def __hash__(self: Source) -> int:
7375
return hash((type(self), self.source))
7476

7577

76-
class Sources(PascalModel):
77-
primary: Source
78-
alternative: list[Source] = Field(default_factory=list)
78+
class InformationList(PascalModel, Generic[T]):
79+
primary: T
80+
alternative: list[T] = Field(default_factory=list)
7981

8082

8183
class Resource(PascalModel):
@@ -448,7 +450,7 @@ def from_path(file: Path, index: int, is_final_page: bool, page: Page | None) ->
448450

449451

450452
class MetronInfo(PascalModel, InfoModel):
451-
id: Sources | None = Field(alias="ID", default=None)
453+
id: InformationList[Source] | None = Field(alias="ID", default=None)
452454
publisher: Resource
453455
series: Series
454456
collection_title: str | None = None
@@ -470,7 +472,7 @@ class MetronInfo(PascalModel, InfoModel):
470472
gtin: GTIN | None = Field(alias="GTIN", default=None)
471473
age_rating: AgeRating = Field(default=AgeRating.UNKNOWN)
472474
reprints: list[Resource] = Field(default_factory=list)
473-
url: HttpUrl | None = Field(alias="URL", default=None)
475+
url: InformationList[HttpUrl] | None = Field(alias="URL", default=None)
474476
credits: list[Credit] = Field(default_factory=list)
475477
pages: list[Page] = Field(default_factory=list)
476478

perdoo/services/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class BaseService(Generic[S, C]):
1616
@abstractmethod
17-
def _get_series_id(self: BaseService, title: str) -> int | None: ...
17+
def _get_series_id(self: BaseService, title: str | None) -> int | None: ...
1818

1919
@abstractmethod
2020
def fetch_series(self: BaseService, details: Details) -> S | None: ...

0 commit comments

Comments
 (0)