diff --git a/src/leek/mod_installer.py b/src/leek/mod_installer.py new file mode 100644 index 0000000..f837778 --- /dev/null +++ b/src/leek/mod_installer.py @@ -0,0 +1,74 @@ +from pathlib import Path +from zipfile import Path as ZipPath +from zipfile import ZipFile, is_zipfile + + +class ModInstaller: + __archive_format: str + + def __init__(self, mod_path: Path) -> None: + if is_zipfile(mod_path): + self.__archive_format = "zip" + archive: ZipFile = ZipFile(mod_path) + path: ZipPath = ZipPath(archive) + + # Make sure archive contains a mod + things_in_root: list[ZipPath] = list(path.iterdir()) + config_toml: ZipPath + match len(things_in_root): + case 0: + raise EmptyArchiveError + case 1: + config_toml = things_in_root[0] / "config.toml" + if config_toml.exists() and config_toml.is_file(): + # Mod can be extracted as is + return + case _: + config_toml = path / "config.toml" + if config_toml.exists() and config_toml.is_file(): + # Mod needs to be extracted in a folder + return + + # If we ever get here there's either no mod on the archive, or we failed to find it + raise NoModExceptionError(str(mod_path)) + + else: + raise UnsupportedArchiveTypeError(str(mod_path)) + + def install(self) -> None: + """ + Install the mod to Project Diva's mod folder + """ + raise NotImplementedError + + +class UnsupportedArchiveTypeError(Exception): + """ + Rased when ModInstaller runs into an archive that it can't extract + """ + + __message: str + + def __init__(self, archive_path: str) -> None: + self.__message = f"Don't know how to unpack archive at {archive_path}" + + def __str__(self) -> str: + return f"{self.__message}" + + +class EmptyArchiveError(Exception): + pass + + +class NoModExceptionError(Exception): + """ + Raised if the archive does not have a mod inside + """ + + __message: str + + def __init__(self, archive_path: str) -> None: + self.__message = f"Archive at {archive_path} does not have a mod" + + def __str__(self) -> str: + return f"{self.__message}"