Skip to content

Commit 597aa90

Browse files
Revert py7zr dependency (#38)
1 parent 94b2fd3 commit 597aa90

6 files changed

Lines changed: 50 additions & 19 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.8.4
3+
rev: v0.9.0
44
hooks:
55
- id: ruff-format
66
- id: ruff

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ Unlike other tagging tools, Perdoo employs a manual approach when metadata files
7474
- .cbr
7575
- .cbt
7676
- .cbz
77-
- .cb7
77+
- .cb7 _(Requires installing `cb7` dependencies: `pipx install perdoo[cb7]`)_
7878

7979
### Output Extensions
8080

8181
- .cbt
8282
- .cbz _(Default)_
83-
- .cb7
83+
- .cb7 _(Requires installing `cb7` dependencies: `pipx install perdoo[cb7]`)_
8484

8585
### Metadata Files
8686

perdoo/archives/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from tarfile import is_tarfile
1313
from zipfile import is_zipfile
1414

15-
from py7zr import is_7zfile
1615
from rarfile import is_rarfile
1716

1817
from perdoo.archives._base import BaseArchive
@@ -21,6 +20,13 @@
2120
from perdoo.archives.cbt import CBTArchive
2221
from perdoo.archives.cbz import CBZArchive
2322

23+
try:
24+
from py7zr import is_7zfile
25+
26+
py7zr_loaded = True
27+
except ImportError:
28+
py7zr_loaded = False
29+
2430

2531
def get_archive(path: Path) -> BaseArchive:
2632
if is_zipfile(path):
@@ -29,17 +35,15 @@ def get_archive(path: Path) -> BaseArchive:
2935
return CBRArchive(path=path)
3036
if is_tarfile(path):
3137
return CBTArchive(path=path)
32-
if is_7zfile(path):
38+
if py7zr_loaded and is_7zfile(path):
3339
return CB7Archive(path=path)
3440
raise NotImplementedError(f"{path.name} is an unsupported archive")
3541

3642

3743
def get_archive_class(extension: str) -> type[BaseArchive]:
38-
if archive_class := {
39-
"cbz": CBZArchive,
40-
"cbr": CBRArchive,
41-
"cbt": CBTArchive,
42-
"cb7": CB7Archive,
43-
}.get(extension):
44-
return archive_class
44+
archive_types = {"cbz": CBZArchive, "cbr": CBRArchive, "cbt": CBTArchive}
45+
if extension == "cb7" and py7zr_loaded:
46+
return CB7Archive
47+
if extension in archive_types:
48+
return archive_types[extension]
4549
raise NotImplementedError(f"{extension} is an unsupported archive")

perdoo/archives/cb7.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66
from tempfile import TemporaryDirectory
77
from typing import Optional
88

9-
from py7zr import Bad7zFile, SevenZipFile
10-
119
from perdoo.archives._base import BaseArchive
1210
from perdoo.utils import list_files
1311

12+
try:
13+
from py7zr import Bad7zFile, SevenZipFile
14+
15+
py7zr_loaded = True
16+
except ImportError:
17+
py7zr_loaded = False
18+
1419
LOGGER = logging.getLogger(__name__)
1520

1621

1722
class CB7Archive(BaseArchive):
23+
def __init__(self, path: Path):
24+
if not py7zr_loaded:
25+
raise ImportError("Install Perdoo with the cb7 dependency group to use CB7 files.")
26+
super().__init__(path=path)
27+
1828
def list_filenames(self) -> list[str]:
1929
try:
2030
with SevenZipFile(self.path, "r") as archive:
@@ -42,6 +52,9 @@ def extract_files(self, destination: Path) -> bool:
4252

4353
@classmethod
4454
def archive_files(cls, src: Path, output_name: str, files: list[Path]) -> Path | None:
55+
if not py7zr_loaded:
56+
raise ImportError("Install Perdoo with the cb7 dependency group to use CB7 files.")
57+
4558
output_file = src.parent / f"{output_name}.cb7"
4659
try:
4760
with SevenZipFile(output_file, "w") as archive:
@@ -54,6 +67,9 @@ def archive_files(cls, src: Path, output_name: str, files: list[Path]) -> Path |
5467

5568
@staticmethod
5669
def convert(old_archive: BaseArchive) -> Optional["CB7Archive"]:
70+
if not py7zr_loaded:
71+
raise ImportError("Install Perdoo with the cb7 dependency group to use CB7 files.")
72+
5773
with TemporaryDirectory(prefix=f"{old_archive.path.stem}_") as temp_str:
5874
temp_folder = Path(temp_str)
5975
if not old_archive.extract_files(destination=temp_folder):

perdoo/settings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
]
1313

1414
from enum import Enum
15+
from importlib.util import find_spec
1516
from pathlib import Path
1617
from typing import Any, ClassVar, Literal
1718

1819
import tomli_w as tomlwriter
19-
from pydantic import BaseModel
20+
from pydantic import BaseModel, field_validator
2021
from rich.panel import Panel
2122

2223
from perdoo import get_config_root, get_data_root
@@ -58,6 +59,12 @@ class Output(SettingsModel):
5859
format: Literal["cb7", "cbt", "cbz"] = "cbz"
5960
metadata: Metadata = Metadata()
6061

62+
@field_validator("format", mode="before")
63+
def validate_format(cls, value: str) -> str:
64+
if value == "cb7" and find_spec("py7zr") is None:
65+
raise ImportError("Install Perdoo with the cb7 dependency group to use CB7 files.")
66+
return value
67+
6168

6269
class Comicvine(SettingsModel):
6370
api_key: str | None = None

pyproject.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tests = [
1010
"pytest >= 8.3.4",
1111
"pytest-cov >= 6.0.0",
1212
"tox >= 4.23.2",
13-
"tox-uv >= 1.16.1"
13+
"tox-uv >= 1.17.0"
1414
]
1515

1616
[project]
@@ -39,9 +39,8 @@ dependencies = [
3939
"lxml >= 5.3.0",
4040
"mokkari >= 3.5.0",
4141
"natsort >= 8.4.0",
42-
"pillow >= 11.0.0",
43-
"py7zr >= 0.22.0",
44-
"pydantic >= 2.10.4",
42+
"pillow >= 11.1.0",
43+
"pydantic >= 2.10.5",
4544
"pydantic-xml >= 2.14.1",
4645
"rarfile >= 4.2",
4746
"rich >= 13.9.4",
@@ -58,6 +57,11 @@ name = "perdoo"
5857
readme = "README.md"
5958
requires-python = ">= 3.10"
6059

60+
[project.optional-dependencies]
61+
cb7 = [
62+
"py7zr >= 0.22.0"
63+
]
64+
6165
[project.scripts]
6266
Perdoo = "perdoo.__main__:app"
6367

0 commit comments

Comments
 (0)