Skip to content

Commit 94b2fd3

Browse files
Mark py7zr as a required dependency (#35)
1 parent a093874 commit 94b2fd3

5 files changed

Lines changed: 24 additions & 65 deletions

File tree

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 _(Requires installing `cb7` dependencies: `pipx install perdoo[cb7]`)_
77+
- .cb7
7878

7979
### Output Extensions
8080

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

8585
### Metadata Files
8686

perdoo/archives/__init__.py

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

15+
from py7zr import is_7zfile
1516
from rarfile import is_rarfile
1617

1718
from perdoo.archives._base import BaseArchive
@@ -20,13 +21,6 @@
2021
from perdoo.archives.cbt import CBTArchive
2122
from perdoo.archives.cbz import CBZArchive
2223

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

3125
def get_archive(path: Path) -> BaseArchive:
3226
if is_zipfile(path):
@@ -35,15 +29,17 @@ def get_archive(path: Path) -> BaseArchive:
3529
return CBRArchive(path=path)
3630
if is_tarfile(path):
3731
return CBTArchive(path=path)
38-
if py7zr_loaded and is_7zfile:
32+
if is_7zfile(path):
3933
return CB7Archive(path=path)
4034
raise NotImplementedError(f"{path.name} is an unsupported archive")
4135

4236

4337
def get_archive_class(extension: str) -> type[BaseArchive]:
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]
38+
if archive_class := {
39+
"cbz": CBZArchive,
40+
"cbr": CBRArchive,
41+
"cbt": CBTArchive,
42+
"cb7": CB7Archive,
43+
}.get(extension):
44+
return archive_class
4945
raise NotImplementedError(f"{extension} is an unsupported archive")

perdoo/archives/cb7.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,54 @@
66
from tempfile import TemporaryDirectory
77
from typing import Optional
88

9+
from py7zr import Bad7zFile, SevenZipFile
10+
911
from perdoo.archives._base import BaseArchive
1012
from perdoo.utils import list_files
1113

12-
try:
13-
import py7zr
14-
15-
py7zr_loaded = True
16-
except ImportError:
17-
py7zr_loaded = False
18-
1914
LOGGER = logging.getLogger(__name__)
2015

2116

2217
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-
2818
def list_filenames(self) -> list[str]:
2919
try:
30-
with py7zr.SevenZipFile(self.path, "r") as archive:
20+
with SevenZipFile(self.path, "r") as archive:
3121
return archive.getnames()
32-
except py7zr.Bad7zFile:
22+
except Bad7zFile:
3323
LOGGER.exception("Unable to read %s", self.path.name)
3424
return []
3525

3626
def read_file(self, filename: str) -> bytes:
3727
try:
38-
with py7zr.SevenZipFile(self.path, "r") as archive, archive.open(filename) as file:
28+
with SevenZipFile(self.path, "r") as archive, archive.open(filename) as file:
3929
return file.read()
40-
except (py7zr.Bad7zFile, KeyError):
30+
except (Bad7zFile, KeyError):
4131
LOGGER.exception("Unable to read %s", self.path.name)
4232
return b""
4333

4434
def extract_files(self, destination: Path) -> bool:
4535
try:
46-
with py7zr.SevenZipFile(self.path, "r") as archive:
36+
with SevenZipFile(self.path, "r") as archive:
4737
archive.extractall(path=destination)
4838
return True
49-
except py7zr.Bad7zFile:
39+
except Bad7zFile:
5040
LOGGER.exception("")
5141
return False
5242

5343
@classmethod
5444
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-
5845
output_file = src.parent / f"{output_name}.cb7"
5946
try:
60-
with py7zr.SevenZipFile(output_file, "w") as archive:
47+
with SevenZipFile(output_file, "w") as archive:
6148
for file in files:
6249
archive.write(file, arcname=file.name)
6350
return output_file
64-
except py7zr.Bad7zFile:
51+
except Bad7zFile:
6552
LOGGER.exception("")
6653
return None
6754

6855
@staticmethod
6956
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-
7357
with TemporaryDirectory(prefix=f"{old_archive.path.stem}_") as temp_str:
7458
temp_folder = Path(temp_str)
7559
if not old_archive.extract_files(destination=temp_folder):

perdoo/settings.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
]
1313

1414
from enum import Enum
15-
from importlib.util import find_spec
1615
from pathlib import Path
1716
from typing import Any, ClassVar, Literal
1817

1918
import tomli_w as tomlwriter
20-
from pydantic import BaseModel, field_validator
19+
from pydantic import BaseModel
2120
from rich.panel import Panel
2221

2322
from perdoo import get_config_root, get_data_root
@@ -59,12 +58,6 @@ class Output(SettingsModel):
5958
format: Literal["cb7", "cbt", "cbz"] = "cbz"
6059
metadata: Metadata = Metadata()
6160

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-
6861

6962
class Comicvine(SettingsModel):
7063
api_key: str | None = None
@@ -80,21 +73,11 @@ class Metron(SettingsModel):
8073
username: str | None = None
8174

8275

83-
class Service(Enum):
76+
class Service(str, Enum):
8477
COMICVINE = "Comicvine"
8578
MARVEL = "Marvel"
8679
METRON = "Metron"
8780

88-
@staticmethod
89-
def load(value: str) -> "Service":
90-
for entry in Service:
91-
if entry.value.replace(" ", "").casefold() == value.replace(" ", "").casefold():
92-
return entry
93-
raise ValueError(f"'{value}' isn't a valid Service")
94-
95-
def __str__(self) -> str:
96-
return self.value
97-
9881

9982
class Services(SettingsModel):
10083
comicvine: Comicvine = Comicvine()

pyproject.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies = [
4040
"mokkari >= 3.5.0",
4141
"natsort >= 8.4.0",
4242
"pillow >= 11.0.0",
43+
"py7zr >= 0.22.0",
4344
"pydantic >= 2.10.4",
4445
"pydantic-xml >= 2.14.1",
4546
"rarfile >= 4.2",
@@ -57,11 +58,6 @@ name = "perdoo"
5758
readme = "README.md"
5859
requires-python = ">= 3.10"
5960

60-
[project.optional-dependencies]
61-
cb7 = [
62-
"py7zr >= 0.22.0"
63-
]
64-
6561
[project.scripts]
6662
Perdoo = "perdoo.__main__:app"
6763

0 commit comments

Comments
 (0)