commit 921c35a316aae71f368299567d70a59a541089b3 Author: Seaswimmer Date: Sun Jun 16 06:44:36 2024 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f1f765 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv +__pycache__ +dist diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..532c0a5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2024 Seaswimmer (seaswimmerthefsh@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e0792a0 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# StellarisModParser + +This package provides a simple parser for Stellaris's mod descriptor format. + +## Usage + +```python-repl +>>> import stellarismodparser +>>> path = "/home/seaswimmer/Projects/StellarisMods/No Menacing Ships.mod" +>>> mod = stellarismodparser.parse(path) +>>> mod.name +'No Menacing Ships' +>>> str(mod.supported_version) +'Andromeda 3.12.4' +>>> mod.tags +['Balance', 'Gameplay'] +``` diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a15219e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,22 @@ +[project] +name = "stellarismodparser" +version = "1.0.1" +authors = [ + { name="SeaswimmerTheFsh", email="seaswimmerthefsh@gmail.com" }, +] +description = "Parse Stellaris's mod descriptor files." +readme = "README.md" +requires-python = ">=3.8" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", +] + +[project.urls] +Homepage = "https://www.coastalcommits.com/Seaswimmer/stellarismodparser" +Issues = "https://www.coastalcommits.com/Seaswimmer/stellarismodparser/issues" + +[build-system] +requires = ["hatchling", "semver"] +build-backend = "hatchling.build" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cdd56f2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +semver==3.0.2 diff --git a/stellarismodparser/__init__.py b/stellarismodparser/__init__.py new file mode 100644 index 0000000..577afef --- /dev/null +++ b/stellarismodparser/__init__.py @@ -0,0 +1,3 @@ +from .stellarismodparser import Mod, StellarisVersion, parse + +__all__ = ['Mod', 'StellarisVersion', 'parse'] diff --git a/stellarismodparser/stellarismodparser.py b/stellarismodparser/stellarismodparser.py new file mode 100644 index 0000000..5fef8f8 --- /dev/null +++ b/stellarismodparser/stellarismodparser.py @@ -0,0 +1,158 @@ +from logging import getLogger +from os import PathLike +from typing import List, Optional + +from semver import Version + +logger = getLogger(__name__) + +class Mod: + 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(f"Mod {name} has more than 10 tags. This will prevent the mod from being uploaded to the Steam Workshop and Paradox Mods.") + 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): + def __init__(self, **kwargs) -> None: + # Build a dictionary of the arguments except prerelease and build + super().__init__(**kwargs) + + @property + def codename(self) -> str: + return self.CODENAME_LIST.get(f"{self.major}.{self.minor}", "Unknown") + + CODENAME_LIST = { + "1.0": "Release", + "1.1": "Clarke", + "1.2": "Asimov", + "1.3": "Heinlein", + "1.4": "Kennedy", + "1.5": "Banks", + "1.6": "Adams", + "1.7": "Bradbury", + "1.8": "Čapek", + "1.9": "Boulle", + "2.0": "Cherryh", + "2.1": "Niven", + "2.2": "Le Guin", + "2.3": "Wolfe", + "2.4": "Lee", + "2.5": "Shelley", + "2.6": "Verne", + "2.7": "Wells", + "2.8": "Butler", + "3.0": "Dick", # lol + "3.1": "Lem", + "3.2": "Herbert", + "3.3": "Libra", + "3.4": "Cepheus", + "3.5": "Fornax", + "3.6": "Orion", + "3.7": "Canis Minor", + "3.8": "Gemini", + "3.9": "Caelum", + "3.10": "Pyxis", + "3.11": "Eridanus", + "3.12": "Andromeda", + } + + def __str__(self) -> str: + version = self.codename + version += " %d.%d.%d" % (self.major, self.minor, self.patch) + if self.prerelease: + version += "-%s" % self.prerelease + if self.build: + version += "+%s" % self.build + return version + + def to_tuple(self) -> tuple: + return (self.codename, self.major, self.minor, self.patch, self.prerelease, self.build) + + def to_dict(self) -> dict: + return dict( + codename=self.codename, + major=self.major, + minor=self.minor, + patch=self.patch, + prerelease=self.prerelease, + build=self.build, + ) + +def parse(path: PathLike) -> Mod: + config = {} + current_key = None + in_multiline_value = False + multiline_value = [] + + with open(path, 'r') as file: + lines = file.readlines() + + for line in lines: + line = line.strip() + + if in_multiline_value: + if line == '}': + in_multiline_value = False + config[current_key] = multiline_value + else: + multiline_value.append(line.strip('"')) + elif '=' in line: + key, value = line.split('=', 1) + key = key.strip() + value = value.strip().strip('"') + if value == '{': + in_multiline_value = True + current_key = key + multiline_value = [] + else: + config[key] = value + + return Mod.from_dict(config)