Get metadata from meta.json, if it exists #12
2 changed files with 42 additions and 4 deletions
|
|
@ -1,10 +1,16 @@
|
|||
import tomlkit
|
||||
import json
|
||||
from pathlib import Path
|
||||
from tomlkit import TOMLDocument
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Mod:
|
||||
__config: TOMLDocument
|
||||
|
||||
__has_meta_json: bool
|
||||
__meta: dict[str, Any]
|
||||
|
||||
__path: Path
|
||||
__name: str
|
||||
__description: str
|
||||
|
|
@ -18,22 +24,37 @@ class Mod:
|
|||
# Mod metadata
|
||||
@property
|
||||
def name(self) -> str | None:
|
||||
if self.__has_meta_json:
|
||||
if "name" in self.__meta:
|
||||
return self.__meta.get("name", str)
|
||||
if "name" not in self.__config:
|
||||
return None
|
||||
return self.__config.get("name", str)
|
||||
|
||||
@property
|
||||
def description(self) -> str | None:
|
||||
if self.__has_meta_json:
|
||||
if "description" in self.__meta:
|
||||
return self.__meta.get("description", str)
|
||||
if "description" not in self.__config.keys():
|
||||
return None
|
||||
else:
|
||||
return self.__config.get("description", str)
|
||||
|
||||
@property
|
||||
def author(self) -> str | None:
|
||||
def long_description(self) -> str | None:
|
||||
if "descriptionLong" in self.__meta:
|
||||
return self.__meta.get("descriptionLong", str)
|
||||
return None
|
||||
|
||||
@property
|
||||
def authors(self) -> list[str] | None:
|
||||
if self.__has_meta_json:
|
||||
if "authors" in self.__meta:
|
||||
return self.__meta.get("authors", list[str])
|
||||
if "author" not in self.__config:
|
||||
return None
|
||||
return self.__config.get("author", str)
|
||||
return [self.__config.get("author", str)]
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
|
|
@ -53,6 +74,7 @@ class Mod:
|
|||
|
||||
def __init__(self, path: Path) -> None:
|
||||
self.__path = path
|
||||
self.__meta = {}
|
||||
|
||||
try:
|
||||
config_toml = Path(self.__path, "config.toml")
|
||||
|
|
@ -61,11 +83,21 @@ class Mod:
|
|||
|
||||
if "enabled" not in self.__config:
|
||||
raise InvalidModError("config.toml does not contain the enabled key")
|
||||
else:
|
||||
self.__enabled = self.__config.get("enabled", bool)
|
||||
except FileNotFoundError:
|
||||
raise InvalidModError("config.toml does not exist")
|
||||
|
||||
meta_json: Path = Path(self.__path, "meta.json")
|
||||
if meta_json.exists():
|
||||
self.__has_meta_json = True
|
||||
try:
|
||||
with meta_json.open() as file:
|
||||
self.__meta = json.load(file)
|
||||
except json.JSONDecodeError as e:
|
||||
print("Failed to parse meta.json!: ", e.msg)
|
||||
else:
|
||||
self.__has_meta_json = False
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Mod({self.__path})"
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ QML_IMPORT_NAME = "Leek"
|
|||
QML_IMPORT_MAJOR_VERSION = 1
|
||||
|
||||
|
||||
# Qt follows C++ naming conventions
|
||||
# ruff: noqa: N802
|
||||
@QmlElement
|
||||
@QmlUncreatable("QMod is intended to be returned by a QModListModel")
|
||||
class QMod(QObject):
|
||||
|
|
@ -34,6 +36,10 @@ class QMod(QObject):
|
|||
def description(self) -> str | None:
|
||||
return self.__mod.description
|
||||
|
||||
@Property(str, constant=True)
|
||||
def longDescription(self) -> str | None:
|
||||
return self.__mod.long_description
|
||||
|
||||
mod_enabled = Signal(name="enabled")
|
||||
|
||||
@Property(bool, notify=mod_enabled)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue