Skip to content

Commit

Permalink
[python] add: Palette.save
Browse files Browse the repository at this point in the history
  • Loading branch information
jd28 committed Dec 8, 2024
1 parent 1187b1e commit b884509
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
12 changes: 4 additions & 8 deletions docs/fake/rollnw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
23 changes: 16 additions & 7 deletions notebooks/06_palettes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -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"
Expand All @@ -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\")"
]
}
],
Expand Down
22 changes: 18 additions & 4 deletions rollnw-py/wrapper_formats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -257,8 +272,7 @@ void init_formats_palette(py::module& nw)
auto result = new nw::Palette;
result->from_json(archive);
return result;
}
})
} })

;
}
Expand Down
1 change: 1 addition & 0 deletions rollnw-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...

Expand Down

0 comments on commit b884509

Please sign in to comment.