Add Mod class
This commit is contained in:
parent
796f0aa209
commit
a549e9a99c
1 changed files with 68 additions and 0 deletions
68
src/Mod.py
Normal file
68
src/Mod.py
Normal file
|
|
@ -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}"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue