|
6 | 6 | from tempfile import TemporaryDirectory |
7 | 7 | from typing import Optional |
8 | 8 |
|
| 9 | +from py7zr import Bad7zFile, SevenZipFile |
| 10 | + |
9 | 11 | from perdoo.archives._base import BaseArchive |
10 | 12 | from perdoo.utils import list_files |
11 | 13 |
|
12 | | -try: |
13 | | - import py7zr |
14 | | - |
15 | | - py7zr_loaded = True |
16 | | -except ImportError: |
17 | | - py7zr_loaded = False |
18 | | - |
19 | 14 | LOGGER = logging.getLogger(__name__) |
20 | 15 |
|
21 | 16 |
|
22 | 17 | 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 | | - |
28 | 18 | def list_filenames(self) -> list[str]: |
29 | 19 | try: |
30 | | - with py7zr.SevenZipFile(self.path, "r") as archive: |
| 20 | + with SevenZipFile(self.path, "r") as archive: |
31 | 21 | return archive.getnames() |
32 | | - except py7zr.Bad7zFile: |
| 22 | + except Bad7zFile: |
33 | 23 | LOGGER.exception("Unable to read %s", self.path.name) |
34 | 24 | return [] |
35 | 25 |
|
36 | 26 | def read_file(self, filename: str) -> bytes: |
37 | 27 | 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: |
39 | 29 | return file.read() |
40 | | - except (py7zr.Bad7zFile, KeyError): |
| 30 | + except (Bad7zFile, KeyError): |
41 | 31 | LOGGER.exception("Unable to read %s", self.path.name) |
42 | 32 | return b"" |
43 | 33 |
|
44 | 34 | def extract_files(self, destination: Path) -> bool: |
45 | 35 | try: |
46 | | - with py7zr.SevenZipFile(self.path, "r") as archive: |
| 36 | + with SevenZipFile(self.path, "r") as archive: |
47 | 37 | archive.extractall(path=destination) |
48 | 38 | return True |
49 | | - except py7zr.Bad7zFile: |
| 39 | + except Bad7zFile: |
50 | 40 | LOGGER.exception("") |
51 | 41 | return False |
52 | 42 |
|
53 | 43 | @classmethod |
54 | 44 | 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 | | - |
58 | 45 | output_file = src.parent / f"{output_name}.cb7" |
59 | 46 | try: |
60 | | - with py7zr.SevenZipFile(output_file, "w") as archive: |
| 47 | + with SevenZipFile(output_file, "w") as archive: |
61 | 48 | for file in files: |
62 | 49 | archive.write(file, arcname=file.name) |
63 | 50 | return output_file |
64 | | - except py7zr.Bad7zFile: |
| 51 | + except Bad7zFile: |
65 | 52 | LOGGER.exception("") |
66 | 53 | return None |
67 | 54 |
|
68 | 55 | @staticmethod |
69 | 56 | 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 | | - |
73 | 57 | with TemporaryDirectory(prefix=f"{old_archive.path.stem}_") as temp_str: |
74 | 58 | temp_folder = Path(temp_str) |
75 | 59 | if not old_archive.extract_files(destination=temp_folder): |
|
0 commit comments