From b8845093cfd434ee8f598bec193b3ef85fb01d0e Mon Sep 17 00:00:00 2001 From: jmd Date: Sun, 8 Dec 2024 10:50:04 -0800 Subject: [PATCH] [python] add: Palette.save --- docs/fake/rollnw/__init__.py | 12 ++++-------- notebooks/06_palettes.ipynb | 23 ++++++++++++++++------- rollnw-py/wrapper_formats.cpp | 22 ++++++++++++++++++---- rollnw-stubs/__init__.pyi | 1 + 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/docs/fake/rollnw/__init__.py b/docs/fake/rollnw/__init__.py index dc23f8470..2e232036e 100644 --- a/docs/fake/rollnw/__init__.py +++ b/docs/fake/rollnw/__init__.py @@ -975,17 +975,13 @@ class Palette: #: Tileset resref if restype is set tileset: str - def add_branch(self, name: str, strref: int) -> "PaletteTreeNode": - """Add branch node to root""" - pass - - def add_category(self, name: str, strref: int) -> "PaletteTreeNode": - """Add category to root""" - pass - def is_skeleton(self) -> bool: """Determines if palette is skeleton""" + def save(self, path: str, format: str = "json") -> None: + """Saves palette to path in 'gff' or 'json' format""" + pass + def to_dict(self) -> dict: """Converts palette to python dict, same layout as json""" diff --git a/notebooks/06_palettes.ipynb b/notebooks/06_palettes.ipynb index f38e6fbc1..ef1717d24 100644 --- a/notebooks/06_palettes.ipynb +++ b/notebooks/06_palettes.ipynb @@ -62,7 +62,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -71,14 +71,13 @@ "text": [ "{'$type': 'ITP',\n", " '$version': 1,\n", - " 'next_available_id': 51,\n", + " 'next_available_id': 3,\n", " 'resource_type': 'utc',\n", " 'root': [{'name': 'The Office',\n", " 'strref': 1,\n", - " '|children': [{'display': 0, 'id': 0, 'name': 'Bears', 'strref': 2},\n", - " {'display': 0, 'id': 1, 'name': 'Beets', 'strref': 3},\n", - " {'display': 0,\n", - " 'id': 2,\n", + " '|children': [{'id': 0, 'name': 'Bears', 'strref': 2},\n", + " {'id': 1, 'name': 'Beets', 'strref': 3},\n", + " {'id': 2,\n", " 'name': 'Battlestar Galactica',\n", " 'strref': 4}]}]}\n", "1\n" @@ -96,7 +95,17 @@ "source": [ "### Saving the Palette\n", "\n", - "Not yet complete." + "Palette supports being saved in two formats GFF and JSON. The below lines are commented out solely not to produce files in the note book." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# palette.save(\"creaturepal.itp\", \"gff\")\n", + "# palette.save(\"creaturepal1.itp\")" ] } ], diff --git a/rollnw-py/wrapper_formats.cpp b/rollnw-py/wrapper_formats.cpp index db9baf661..3a6cd8bb5 100644 --- a/rollnw-py/wrapper_formats.cpp +++ b/rollnw-py/wrapper_formats.cpp @@ -233,11 +233,26 @@ void init_formats_palette(py::module& nw) .def_readonly("tileset", &nw::Palette::tileset) .def_readonly("children", &nw::Palette::children) + .def("save", [](const nw::Palette& self, const std::string& path, const std::string& format) { + std::filesystem::path out{path}; + if (nw::string::icmp(format, "gff")) { + nw::GffBuilder oa = nw::serialize(self); + oa.write_to(out); + } else if (nw::string::icmp(format, "json")) { + nlohmann::json j = self.to_json(); + std::filesystem::path temp_path = std::filesystem::temp_directory_path() / out.filename(); + std::ofstream f{temp_path}; + f << std::setw(4) << j; + f.close(); + nw::move_file_safely(temp_path, out); + } else { + throw std::runtime_error(fmt::format("[palette] unknown format: {}", format)); + } }, py::arg("path"), py::arg("format") = "json") + .def_static("from_dict", [](const nlohmann::json& json) -> nw::Palette* { auto result = new nw::Palette; result->from_json(json); - return result; - }) + return result; }) .def_static("from_file", [](const std::string& path) -> nw::Palette* { auto p = nw::expand_path(path); @@ -257,8 +272,7 @@ void init_formats_palette(py::module& nw) auto result = new nw::Palette; result->from_json(archive); return result; - } - }) + } }) ; } diff --git a/rollnw-stubs/__init__.pyi b/rollnw-stubs/__init__.pyi index 20b8af5b3..1a2d7e3ab 100644 --- a/rollnw-stubs/__init__.pyi +++ b/rollnw-stubs/__init__.pyi @@ -1285,6 +1285,7 @@ class Palette: tileset: str def is_skeleton(self) -> bool: ... + def save(self, path: str, format: str = "json") -> None: ... def to_dict(self) -> dict: ... def valid(self) -> bool: ...