src.plugins.plugin_archives
Plugin to work with archives
1"""Plugin to work with archives""" 2import shutil 3from pathlib import Path 4from typing import Literal 5import src.decorators.commands_register as cmd_register 6import src.decorators.handlers as handlers 7from src.cmd_types.commands import ExecutableCommand 8from src.cmd_types.output import CommandOutput 9from src.extra.utils import create_path_obj 10import src.constants as cst 11 12__author__ = "default" 13__version__ = "1.0.0" 14 15TYPES = tuple(cst.TYPE_EXTENSION_ENUM.keys()) 16 17class UnpackCommand(ExecutableCommand): 18 """ 19 Command class to unpack archives 20 """ 21 archive_type: str 22 """type of archive""" 23 24 def _parse_args(self) -> tuple[str, str]: 25 return self.args[0], self.args[1] 26 27 @handlers.handle_all_default 28 def execute(self): 29 ext = "." + cst.TYPE_EXTENSION_ENUM[self.archive_type] 30 if len(self.args)==1: 31 self.args.append(self.args[0][:-len(ext)]) 32 if len(self.args)!=2: 33 self._log_error(f"{self.name} command requires exactly at least 2 arguments. Given: {len(self.args)}") 34 return None 35 if not self.args[0].endswith(ext): 36 self._log_error(f"{self.name} command requires {ext} extension. Given: {self.args[0]}") 37 return None 38 source = create_path_obj(self.args[0]) 39 40 try: 41 shutil.unpack_archive(source, self.args[1]) 42 except shutil.ReadError as e: 43 self._log_error(str(e)) 44 return None 45 46 47class PackCommand(ExecutableCommand): 48 """Command to pack archives""" 49 50 archive_type: Literal["gztar", "zip"] 51 52 def _parse_args(self) -> tuple[str, str]: 53 return self.args[0], self.args[1] 54 55 def make_archive(self, source: str | Path, destination: str | Path, archive_type: Literal["gztar", "zip"]): 56 source, destination = str(source), str(destination) 57 58 ext = '.'+cst.TYPE_EXTENSION_ENUM[archive_type] 59 60 if destination.endswith(ext): 61 destination = destination[:-len(ext)] 62 try: 63 shutil.make_archive(destination, archive_type, source) 64 except NotADirectoryError: 65 msg = f"not a directory: {source}" 66 return CommandOutput(stderr = msg, errcode = 2) 67 68 @handlers.handle_all_default 69 def execute(self): 70 71 ext = "." + cst.TYPE_EXTENSION_ENUM[self.archive_type] 72 if len(self.args) == 1: 73 self.args.append(self.args[0]+ext) 74 75 if len(self.args)!=2: 76 msg = f"{self.name} command requires at leat 1 argument. Given: {len(self.args)}" 77 return CommandOutput(stderr = msg, errcode = 2) 78 79 80 if not self.args[1].endswith(f"{ext}"): 81 msg = f"{self.name} command requires {ext} extension. Given: {self.args[1]}" 82 return CommandOutput(stderr= msg, errcode=2) 83 84 source = create_path_obj(self.args[0]) 85 86 self.make_archive(source, self.args[1], self.archive_type) 87 return None 88 89 90@cmd_register.command("tar") 91class TarCommand(PackCommand): 92 archive_type = "gztar" 93 94 95@cmd_register.command("zip") 96class ZipCommand(PackCommand): 97 archive_type = "zip" 98 99 100@cmd_register.command("untar") 101class UntarCommand(UnpackCommand): 102 archive_type = "gztar" 103 104 105@cmd_register.command("unzip") 106class UnzipCommand(UnpackCommand): 107 archive_type = "zip"
TYPES =
('gztar', 'zip')
18class UnpackCommand(ExecutableCommand): 19 """ 20 Command class to unpack archives 21 """ 22 archive_type: str 23 """type of archive""" 24 25 def _parse_args(self) -> tuple[str, str]: 26 return self.args[0], self.args[1] 27 28 @handlers.handle_all_default 29 def execute(self): 30 ext = "." + cst.TYPE_EXTENSION_ENUM[self.archive_type] 31 if len(self.args)==1: 32 self.args.append(self.args[0][:-len(ext)]) 33 if len(self.args)!=2: 34 self._log_error(f"{self.name} command requires exactly at least 2 arguments. Given: {len(self.args)}") 35 return None 36 if not self.args[0].endswith(ext): 37 self._log_error(f"{self.name} command requires {ext} extension. Given: {self.args[0]}") 38 return None 39 source = create_path_obj(self.args[0]) 40 41 try: 42 shutil.unpack_archive(source, self.args[1]) 43 except shutil.ReadError as e: 44 self._log_error(str(e)) 45 return None
Command class to unpack archives
@handlers.handle_all_default
def
execute(self):
28 @handlers.handle_all_default 29 def execute(self): 30 ext = "." + cst.TYPE_EXTENSION_ENUM[self.archive_type] 31 if len(self.args)==1: 32 self.args.append(self.args[0][:-len(ext)]) 33 if len(self.args)!=2: 34 self._log_error(f"{self.name} command requires exactly at least 2 arguments. Given: {len(self.args)}") 35 return None 36 if not self.args[0].endswith(ext): 37 self._log_error(f"{self.name} command requires {ext} extension. Given: {self.args[0]}") 38 return None 39 source = create_path_obj(self.args[0]) 40 41 try: 42 shutil.unpack_archive(source, self.args[1]) 43 except shutil.ReadError as e: 44 self._log_error(str(e)) 45 return None
Executes command
48class PackCommand(ExecutableCommand): 49 """Command to pack archives""" 50 51 archive_type: Literal["gztar", "zip"] 52 53 def _parse_args(self) -> tuple[str, str]: 54 return self.args[0], self.args[1] 55 56 def make_archive(self, source: str | Path, destination: str | Path, archive_type: Literal["gztar", "zip"]): 57 source, destination = str(source), str(destination) 58 59 ext = '.'+cst.TYPE_EXTENSION_ENUM[archive_type] 60 61 if destination.endswith(ext): 62 destination = destination[:-len(ext)] 63 try: 64 shutil.make_archive(destination, archive_type, source) 65 except NotADirectoryError: 66 msg = f"not a directory: {source}" 67 return CommandOutput(stderr = msg, errcode = 2) 68 69 @handlers.handle_all_default 70 def execute(self): 71 72 ext = "." + cst.TYPE_EXTENSION_ENUM[self.archive_type] 73 if len(self.args) == 1: 74 self.args.append(self.args[0]+ext) 75 76 if len(self.args)!=2: 77 msg = f"{self.name} command requires at leat 1 argument. Given: {len(self.args)}" 78 return CommandOutput(stderr = msg, errcode = 2) 79 80 81 if not self.args[1].endswith(f"{ext}"): 82 msg = f"{self.name} command requires {ext} extension. Given: {self.args[1]}" 83 return CommandOutput(stderr= msg, errcode=2) 84 85 source = create_path_obj(self.args[0]) 86 87 self.make_archive(source, self.args[1], self.archive_type) 88 return None
Command to pack archives
def
make_archive( self, source: str | pathlib._local.Path, destination: str | pathlib._local.Path, archive_type: Literal['gztar', 'zip']):
56 def make_archive(self, source: str | Path, destination: str | Path, archive_type: Literal["gztar", "zip"]): 57 source, destination = str(source), str(destination) 58 59 ext = '.'+cst.TYPE_EXTENSION_ENUM[archive_type] 60 61 if destination.endswith(ext): 62 destination = destination[:-len(ext)] 63 try: 64 shutil.make_archive(destination, archive_type, source) 65 except NotADirectoryError: 66 msg = f"not a directory: {source}" 67 return CommandOutput(stderr = msg, errcode = 2)
@handlers.handle_all_default
def
execute(self):
69 @handlers.handle_all_default 70 def execute(self): 71 72 ext = "." + cst.TYPE_EXTENSION_ENUM[self.archive_type] 73 if len(self.args) == 1: 74 self.args.append(self.args[0]+ext) 75 76 if len(self.args)!=2: 77 msg = f"{self.name} command requires at leat 1 argument. Given: {len(self.args)}" 78 return CommandOutput(stderr = msg, errcode = 2) 79 80 81 if not self.args[1].endswith(f"{ext}"): 82 msg = f"{self.name} command requires {ext} extension. Given: {self.args[1]}" 83 return CommandOutput(stderr= msg, errcode=2) 84 85 source = create_path_obj(self.args[0]) 86 87 self.make_archive(source, self.args[1], self.archive_type) 88 return None
Executes command
Command to pack archives
Command to pack archives
Command class to unpack archives
Command class to unpack archives