66from tempfile import TemporaryDirectory
77from typing import Optional
88
9- from py7zr import Bad7zFile , SevenZipFile
10-
119from perdoo .archives ._base import BaseArchive
1210from 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+
1419LOGGER = logging .getLogger (__name__ )
1520
1621
1722class 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 ):
0 commit comments