make Mod a dataclass
This commit is contained in:
parent
f5cdd61b89
commit
2fa798b69c
1 changed files with 51 additions and 68 deletions
|
@ -1,3 +1,4 @@
|
|||
from dataclasses import dataclass
|
||||
from logging import getLogger
|
||||
from os import PathLike
|
||||
from typing import List, Optional
|
||||
|
@ -6,73 +7,6 @@ from semver import Version
|
|||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
class Mod:
|
||||
"""This class represents a Stellaris mod's descriptor file.
|
||||
|
||||
Attributes:
|
||||
name (str): The name of the mod.
|
||||
path (PathLike): The path to the mod's directory.
|
||||
dependencies (List[str]): A list of the mod's dependencies.
|
||||
picture (PathLike): The path to the mod's thumbnail picture.
|
||||
tags (List[str]): A list of the mod's tags.
|
||||
version (str): The mod's version.
|
||||
supported_version (StellarisVersion): The version of Stellaris the mod supports.
|
||||
remote_file_id (int): The mod's remote file ID.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
path: Optional[PathLike],
|
||||
dependencies: Optional[List[str]],
|
||||
picture: Optional[PathLike],
|
||||
tags: Optional[List[str]],
|
||||
version: Optional[str],
|
||||
supported_version: Optional[str],
|
||||
remote_file_id: Optional[int],
|
||||
) -> None:
|
||||
self.name = name
|
||||
self.path = path
|
||||
self.dependencies = dependencies
|
||||
self.picture = picture
|
||||
self.tags = tags
|
||||
if len(tags) > 10:
|
||||
logger.warning("Mod %s has more than 10 tags. This will prevent the mod from being uploaded to the Steam Workshop and Paradox Mods.", self.name)
|
||||
self.version = version
|
||||
self.supported_version = StellarisVersion.parse(supported_version) if supported_version else None
|
||||
self.remote_file_id = remote_file_id
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Mod(name={self.name}, path={self.path}, dependencies={self.dependencies}, picture={self.picture}, tags={self.tags}, version={self.version}, supported_version={self.supported_version}, remote_file_id={self.remote_file_id})"
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, mod_dict: dict) -> "Mod":
|
||||
return cls(
|
||||
mod_dict["name"],
|
||||
mod_dict.get("path"),
|
||||
mod_dict.get("dependencies"),
|
||||
mod_dict.get("picture"),
|
||||
mod_dict.get("tags"),
|
||||
mod_dict.get("version"),
|
||||
mod_dict.get("supported_version"),
|
||||
mod_dict.get("remote_file_id"),
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"name": self.name,
|
||||
"path": self.path,
|
||||
"dependencies": self.dependencies,
|
||||
"picture": self.picture,
|
||||
"tags": self.tags,
|
||||
"version": self.version,
|
||||
"supported_version": self.supported_version.to_dict() if self.supported_version else None,
|
||||
"remote_file_id": self.remote_file_id,
|
||||
}
|
||||
|
||||
class StellarisVersion(Version):
|
||||
"""This class represents a Stellaris version.
|
||||
|
||||
|
@ -123,7 +57,8 @@ class StellarisVersion(Version):
|
|||
"3.10": "Pyxis",
|
||||
"3.11": "Eridanus",
|
||||
"3.12": "Andromeda",
|
||||
"3.13": "Vela,"
|
||||
"3.13": "Vela",
|
||||
"3.14": "Circinus",
|
||||
}
|
||||
|
||||
def __str__(self) -> str:
|
||||
|
@ -148,6 +83,54 @@ class StellarisVersion(Version):
|
|||
"build": self.build
|
||||
}
|
||||
|
||||
@dataclass
|
||||
class Mod:
|
||||
"""This class represents a Stellaris mod's descriptor file.
|
||||
|
||||
Attributes:
|
||||
name (str): The name of the mod.
|
||||
path (PathLike): The path to the mod's directory.
|
||||
dependencies (List[str]): A list of the mod's dependencies.
|
||||
picture (PathLike): The path to the mod's thumbnail picture.
|
||||
tags (List[str]): A list of the mod's tags.
|
||||
version (str): The mod's version.
|
||||
supported_version (StellarisVersion): The version of Stellaris the mod supports.
|
||||
remote_file_id (int): The mod's remote file ID.
|
||||
"""
|
||||
name: str
|
||||
path: Optional[PathLike] = None
|
||||
dependencies: Optional[List[str]] = None
|
||||
picture: Optional[PathLike] = None
|
||||
tags: Optional[List[str]] = None
|
||||
version: Optional[str] = None
|
||||
supported_version: Optional[StellarisVersion] = None
|
||||
remote_file_id: Optional[int] = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.tags and len(self.tags) > 10:
|
||||
logger.warning("Mod %s has more than 10 tags. This will prevent the mod from being uploaded to the Steam Workshop and Paradox Mods.", self.name)
|
||||
if self.supported_version and isinstance(self.supported_version, str):
|
||||
self.supported_version = StellarisVersion.parse(self.supported_version)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, mod_dict: dict) -> "Mod":
|
||||
return cls(**mod_dict)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"name": self.name,
|
||||
"path": self.path,
|
||||
"dependencies": self.dependencies,
|
||||
"picture": self.picture,
|
||||
"tags": self.tags,
|
||||
"version": self.version,
|
||||
"supported_version": self.supported_version.to_dict() if self.supported_version else None,
|
||||
"remote_file_id": self.remote_file_id,
|
||||
}
|
||||
|
||||
def parse(path: PathLike) -> Mod:
|
||||
"""Parse a Stellaris mod descriptor file into a Mod object."""
|
||||
config = {}
|
||||
|
|
Loading…
Reference in a new issue