From a549e9a99cd92839ee740eb5f35c74710ab56b18 Mon Sep 17 00:00:00 2001 From: Toast Date: Wed, 7 May 2025 00:17:46 +0200 Subject: [PATCH] Add Mod class --- src/Mod.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/Mod.py diff --git a/src/Mod.py b/src/Mod.py new file mode 100644 index 0000000..4b990dc --- /dev/null +++ b/src/Mod.py @@ -0,0 +1,68 @@ +import tomlkit + + +class Mod: + @property + def path(self) -> str: + return self.__path + + # Mod metadata + @property + def name(self) -> str | None: + if "name" not in self.__config: + return None + return self.__config["name"] + + @property + def description(self) -> str | None: + if "description" not in self.__config.keys(): + return None + else: + return self.__config["description"] + + @property + def author(self) -> str | None: + if "author" not in self.__config: + return None + return self.__config["author"] + + @property + def enabled(self) -> str: + return self.__enabled + + def __init__(self, path: str) -> None: + self.__path = path + + with open(path + "config.toml") as configFile: + self.__config = tomlkit.load(configFile) + + if "enabled" not in self.__config: + raise InvalidModError("config.toml does not contain the enabled key") + else: + self.__enabled = self.__config["enabled"] + + @enabled.setter + def enabled(self, value: bool) -> None: + if value == self.__enabled: + # Nothing to do + return + + with open(self.__path + "config.toml", "w") as configFile: + self.__config["enabled"] = value + tomlkit.dump(self.__config, configFile) + + def __str__(self) -> str: + return f"Mod({self.__path})" + + +class InvalidModError(Exception): + """ + This exception is raised when the Mod class gets given a path of something that's not a valid mod + """ + + def __init__(self, message: str) -> None: + super().__init__(message) + self.message = message + + def __str__(self) -> str: + return f"{self.message}"