|
1 | | -__all__ = ["Comic", "ComicArchiveError", "ComicMetadataError", "MetadataFormat"] |
| 1 | +__all__ = [ |
| 2 | + "SUPPORTED_IMAGE_EXTENSIONS", |
| 3 | + "Comic", |
| 4 | + "ComicArchiveError", |
| 5 | + "ComicMetadataError", |
| 6 | + "MetadataFormat", |
| 7 | +] |
2 | 8 |
|
3 | 9 | import logging |
4 | 10 | import shutil |
5 | 11 | from pathlib import Path |
6 | | -from typing import Final, TypeVar |
| 12 | +from typing import Final, Literal, TypeVar |
7 | 13 |
|
8 | 14 | from darkseid.archivers import ( |
9 | 15 | PY7ZR_AVAILABLE, |
10 | 16 | Archiver, |
11 | 17 | ArchiverFactory, |
12 | 18 | SevenZipArchiver, |
| 19 | + TarArchiver, |
13 | 20 | ZipArchiver, |
14 | 21 | ) |
15 | 22 | from darkseid.comic import ( |
@@ -98,11 +105,15 @@ def read_metadata(self, metadata_format: MetadataFormat) -> None: |
98 | 105 | else: |
99 | 106 | raise ComicMetadataError(f"Unsupported metadata format: {metadata_format}") |
100 | 107 |
|
101 | | - def convert_to_cbz(self) -> None: |
102 | | - if self.is_cbz(): |
| 108 | + def convert(self, extension: Literal["cbt", "cbz"]) -> None: |
| 109 | + check, archiver = { |
| 110 | + "cbt": (self.is_cbt, TarArchiver), |
| 111 | + "cbz": (self.is_cbz, ZipArchiver), |
| 112 | + }.get(extension) |
| 113 | + if check(): |
103 | 114 | return |
104 | | - output_file = self.path.with_suffix(".cbz") |
105 | | - with self.archive as source, ZipArchiver(path=output_file) as destination: |
| 115 | + output_file = self.path.with_suffix(f".{extension}") |
| 116 | + with self.archive as source, archiver(path=output_file) as destination: |
106 | 117 | LOGGER.debug("Converting '%s' to '%s'", source.path.name, destination.path.name) |
107 | 118 | if destination.copy_from_archive(other_archive=source): |
108 | 119 | self._archiver = destination |
@@ -176,7 +187,7 @@ def rename(self, naming: Naming, output_folder: Path) -> None: |
176 | 187 | "Renaming '%s' to '%s'", self.path.name, output.relative_to(output_folder.parent) |
177 | 188 | ) |
178 | 189 | shutil.move(self.path, output) |
179 | | - self._archiver = ZipArchiver(path=output) |
| 190 | + self.archive._path = output # noqa: SLF001 |
180 | 191 |
|
181 | 192 | new_filename = self.path.stem |
182 | 193 | if all( |
|
0 commit comments