Skip to content

Commit

Permalink
Remove amulet_nbt._nbt
Browse files Browse the repository at this point in the history
All of the docstrings were amulet_nbt._nbt.ByteTag which was a bit ugly.
This removes __init__.py and moves _nbt into its place.
Things previously defined in the python __init__ have been moved into C++
  • Loading branch information
gentlegiantJGC committed Aug 1, 2024
1 parent 1d57227 commit 0da28e9
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 152 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
ext_modules=[
Extension(
name="amulet_nbt._nbt",
name="amulet_nbt.__init__",
sources=glob.glob("src/amulet_nbt/pybind/**/*.cpp", recursive=True),
include_dirs=["src/amulet_nbt/include", pybind11.get_include()],
libraries=["amulet_nbt"],
Expand Down
149 changes: 0 additions & 149 deletions src/amulet_nbt/__init__.py

This file was deleted.

127 changes: 125 additions & 2 deletions src/amulet_nbt/pybind/amulet_nbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <pybind11/pybind11.h>

#include <amulet_nbt/common.hpp>
#include <iostream>

namespace py = pybind11;

Expand All @@ -21,8 +22,7 @@ void init_named_tag(py::module&);
void init_bnbt(py::module& m);
void init_snbt(py::module& m);


PYBIND11_MODULE(_nbt, m) {
void init_amulet_nbt(py::module& m) {
// Convert cast_error to type_error
py::register_local_exception_translator([](std::exception_ptr p) {
try {
Expand Down Expand Up @@ -53,8 +53,131 @@ PYBIND11_MODULE(_nbt, m) {
init_compound(m);
init_list(m);

// Tag Alias's
m.attr("TAG_Byte") = m.attr("ByteTag");
m.attr("TAG_Short") = m.attr("ShortTag");
m.attr("TAG_Int") = m.attr("IntTag");
m.attr("TAG_Long") = m.attr("LongTag");
m.attr("TAG_Float") = m.attr("FloatTag");
m.attr("TAG_Double") = m.attr("DoubleTag");
m.attr("TAG_Byte_Array") = m.attr("ByteArrayTag");
m.attr("TAG_String") = m.attr("StringTag");
m.attr("TAG_List") = m.attr("ListTag");
m.attr("TAG_Compound") = m.attr("CompoundTag");
m.attr("TAG_Int_Array") = m.attr("IntArrayTag");
m.attr("TAG_Long_Array") = m.attr("LongArrayTag");

init_named_tag(m);

init_bnbt(m);
init_snbt(m);

// Paths
py::object path_join = py::module::import("os.path").attr("join");
m.def(
"get_include",
[m, path_join](){
return path_join(m.attr("__path__")[0], py::str("include"));
},
py::doc("C++ include directory")
);
m.def(
"get_source",
[m, path_join](){
return path_join(m.attr("__path__")[0], py::str("cpp"));
},
py::doc("C++ source directory")
);

// NBT types
py::object PyStr = py::module::import("builtins").attr("str");
m.attr("SNBTType") = PyStr;
m.attr("IntType") = m.attr("ByteTag") | m.attr("ShortTag") | m.attr("IntTag") | m.attr("LongTag");
m.attr("FloatType") = m.attr("FloatTag") | m.attr("DoubleTag");
m.attr("NumberType") = m.attr("IntType") | m.attr("FloatType");
m.attr("ArrayType") = m.attr("ByteArrayTag") | m.attr("IntArrayTag") | m.attr("LongArrayTag");
m.attr("AnyNBT") = m.attr("NumberType") | m.attr("StringTag") | m.attr("ListTag") | m.attr("CompoundTag") | m.attr("ArrayType");

py::list all;
all.append("__version__");
all.append("__major__");
all.append("get_include");
all.append("AbstractBaseTag");
all.append("AbstractBaseImmutableTag");
all.append("AbstractBaseMutableTag");
all.append("AbstractBaseNumericTag");
all.append("AbstractBaseIntTag");
all.append("ByteTag");
all.append("TAG_Byte");
all.append("ShortTag");
all.append("TAG_Short");
all.append("IntTag");
all.append("TAG_Int");
all.append("LongTag");
all.append("TAG_Long");
all.append("AbstractBaseFloatTag");
all.append("FloatTag");
all.append("TAG_Float");
all.append("DoubleTag");
all.append("TAG_Double");
all.append("AbstractBaseArrayTag");
all.append("ByteArrayTag");
all.append("TAG_Byte_Array");
all.append("IntArrayTag");
all.append("TAG_Int_Array");
all.append("LongArrayTag");
all.append("TAG_Long_Array");
all.append("StringTag");
all.append("TAG_String");
all.append("ListTag");
all.append("TAG_List");
all.append("CompoundTag");
all.append("TAG_Compound");
all.append("NamedTag");
all.append("read_nbt");
all.append("read_nbt_array");
all.append("ReadOffset");
all.append("read_snbt");
all.append("SNBTType");
all.append("IntType");
all.append("FloatType");
all.append("NumberType");
all.append("ArrayType");
all.append("AnyNBT");
all.append("StringEncoding");
all.append("mutf8_encoding");
all.append("utf8_encoding");
all.append("utf8_escape_encoding");
all.append("EncodingPreset");
all.append("java_encoding");
all.append("bedrock_encoding");
m.attr("__all__") = all;

// Version numbers
// For some reason the package is not initialised and you can't import submodules.
m.def(
"__getattr__",
[m](py::object attr) -> py::object {
if (py::isinstance<py::str>(attr)) {
std::string name = attr.cast<std::string>();
if (name == "__version__") {
m.attr("__version__") = py::module::import("amulet_nbt._version").attr("get_versions")()["version"];
return m.attr("__version__");
}
else if (name == "__major__") {
py::object PyInt = py::module::import("builtins").attr("int");
py::object re_match = py::module::import("re").attr("match");
py::object version = py::module::import("amulet_nbt._version").attr("get_versions")()["version"];
m.attr("__major__") = PyInt(re_match("\\d+", version).attr("group")());
return m.attr("__major__");
}
}
throw py::attribute_error("module amulet_nbt has no attribute " + py::repr(attr).cast<std::string>());
}
);
}

// The compiler requires __init__ and the runtime requires amulet_nbt.
// There may be a better way to do this but defining both seems to work.
PYBIND11_MODULE(__init__, m) { init_amulet_nbt(m); }
PYBIND11_MODULE(amulet_nbt, m) { init_amulet_nbt(m); }

0 comments on commit 0da28e9

Please sign in to comment.