Skip to content

Commit

Permalink
Merge pull request #310 from Amulet-Team/fix-package
Browse files Browse the repository at this point in the history
Fix package
  • Loading branch information
gentlegiantJGC authored Sep 25, 2024
2 parents a96388d + 9db739c commit fd16640
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 119 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ requires = [
"wheel",
"versioneer",
"pybind11 ~= 2.12",
"amulet_nbt ~= 4.0a2"
"amulet_nbt ~= 4.0a3"
]
build-backend = "setuptools.build_meta"
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ packages = find_namespace:
python_requires = >=3.11
install_requires =
numpy~=2.0
amulet-nbt~=4.0a2
amulet-nbt~=4.0a3
portalocker~=2.4
amulet-leveldb~=1.0b0
platformdirs~=3.1
Expand Down
4 changes: 4 additions & 0 deletions src/amulet/__init__.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void init_amulet(py::module m){
if (init_run){ return; }
init_run = true;

// This is normally added after initilsation but we need it to pass to subpackages.
// This may cause issues with frozen installs.
m.attr("__path__") = py::module::import("importlib.util").attr("find_spec")("amulet").attr("submodule_search_locations");

py::module::import("amulet_nbt");

py::module::import("amulet._init").attr("init")(m);
Expand Down
8 changes: 2 additions & 6 deletions src/amulet/level/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import typing

from amulet.level._load import (
NoValidLevel,
get_level,
Expand All @@ -11,17 +9,15 @@ from amulet.level._load import (
from amulet.level.abc._level._level import Level
from amulet.level.java._level import JavaLevel

from . import java
from . import _load, abc, java

__all__ = [
"JavaLevel",
"Level",
"NoValidLevel",
"abc",
"get_level",
"java",
"register_level_class",
"unregister_level_class",
]

def __dir__() -> list[str]: ...
def __getattr__(arg0: typing.Any) -> typing.Any: ...
31 changes: 13 additions & 18 deletions src/amulet/level/_init_level.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
#include <amulet/pybind11/py_module.hpp>
namespace py = pybind11;

void init_java(py::module);
py::module init_java(py::module);

void init_level(py::module m_parent) {
auto m = m_parent.def_submodule("level");
auto m = py::def_subpackage(m_parent, "level");

//from ._load import register_level_class, unregister_level_class, get_level, NoValidLevel
//from .temporary_level import TemporaryLevel
m.attr("Level") = py::module::import("amulet.level.abc").attr("Level");

m.attr("register_level_class") = py::module::import("amulet.level._load").attr("register_level_class");
m.attr("unregister_level_class") = py::module::import("amulet.level._load").attr("unregister_level_class");
m.attr("get_level") = py::module::import("amulet.level._load").attr("get_level");
m.attr("NoValidLevel") = py::module::import("amulet.level._load").attr("NoValidLevel");

py::def_deferred(
m,
{
py::deferred_package_path(m_parent, m, "level"),
py::deferred_import("amulet.level.abc", "Level"),
py::deferred_import("amulet.level.java", "JavaLevel"),
//py::deferred_import("amulet.level.bedrock", "BedrockLevel")
py::deferred_import("amulet.level._load", "register_level_class"),
py::deferred_import("amulet.level._load", "unregister_level_class"),
py::deferred_import("amulet.level._load", "get_level"),
py::deferred_import("amulet.level._load", "NoValidLevel")
}
);
//from .temporary_level import TemporaryLevel

// Submodules
init_java(m);
auto java_module = init_java(m);
m.attr("JavaLevel") = java_module.attr("JavaLevel");

//m.attr("BedrockLevel") = py::module::import("amulet.level.bedrock").attr("BedrockLevel");
}
5 changes: 0 additions & 5 deletions src/amulet/level/java/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import typing

from amulet.level.java._level import JavaLevel

from . import (
Expand All @@ -16,6 +14,3 @@ from . import (
)

__all__ = ["JavaLevel", "anvil", "chunk", "chunk_components", "long_array"]

def __dir__() -> list[str]: ...
def __getattr__(arg0: typing.Any) -> typing.Any: ...
15 changes: 6 additions & 9 deletions src/amulet/level/java/__init_java.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ void init_java_chunk_components(py::module);
void init_java_chunk(py::module);
void init_java_raw(py::module);

void init_java(py::module m_parent) {
auto m = m_parent.def_submodule("java");
py::def_deferred(
m,
{
py::deferred_package_path(m_parent, m, "java"),
py::deferred_import("amulet.level.java._level", "JavaLevel")
}
);
py::module init_java(py::module m_parent) {
auto m = py::def_subpackage(m_parent, "java");

init_long_array(m);
init_java_chunk_components(m);
init_java_chunk(m);
init_java_raw(m);

m.attr("JavaLevel") = py::module::import("amulet.level.java._level").attr("JavaLevel");

return m;
}
4 changes: 0 additions & 4 deletions src/amulet/level/java/_raw/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import typing
from builtins import str as InternalDimensionId

from amulet.level.java._raw._dimension import JavaRawDimension
Expand All @@ -14,6 +13,3 @@ __all__ = [
"JavaRawDimension",
"JavaRawLevel",
]

def __dir__() -> list[str]: ...
def __getattr__(arg0: typing.Any) -> typing.Any: ...
17 changes: 6 additions & 11 deletions src/amulet/level/java/_raw/__init_java_raw.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ void init_java_chunk_decode(py::module);
void init_java_chunk_encode(py::module);

void init_java_raw(py::module m_parent) {
auto m = m_parent.def_submodule("_raw");
py::def_deferred(
m,
{
py::deferred_package_path(m_parent, m, "_raw"),
py::deferred_import("amulet.level.java._raw._level", "JavaRawLevel"),
py::deferred_import("amulet.level.java._raw._level", "JavaCreateArgsV1"),
py::deferred_import("amulet.level.java._raw._dimension", "JavaRawDimension"),
py::deferred_import("amulet.level.java._raw._typing", "InternalDimensionId")
}
);
auto m = py::def_subpackage(m_parent, "_raw");

auto m_chunk = m.def_submodule("_chunk");
m_chunk.def(
Expand All @@ -33,4 +23,9 @@ void init_java_raw(py::module m_parent) {
"encode_chunk",
&Amulet::encode_java_chunk
);

m.attr("JavaRawLevel") = py::module::import("amulet.level.java._raw._level").attr("JavaRawLevel");
m.attr("JavaCreateArgsV1") = py::module::import("amulet.level.java._raw._level").attr("JavaCreateArgsV1");
m.attr("JavaRawDimension") = py::module::import("amulet.level.java._raw._dimension").attr("JavaRawDimension");
m.attr("InternalDimensionId") = py::module::import("amulet.level.java._raw._typing").attr("InternalDimensionId");
}
66 changes: 15 additions & 51 deletions src/amulet/pybind11/py_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,23 @@
namespace py = pybind11;

namespace pybind11 {
// Define deferred variables
inline void def_deferred(
py::module m,
std::map<std::string, std::function<py::object()>> attrs
){
m.def(
"__getattr__",
[attrs](py::object attr) -> py::object {
if (py::isinstance<py::str>(attr)) {
std::string attr_str = attr.cast<std::string>();
auto it = attrs.find(attr_str);
if (it != attrs.end()) {
return it->second();
}
}
throw py::attribute_error(py::repr(attr));
inline void def_package_path(py::module m_parent, py::module m, std::string name) {
py::list paths;
py::list parent_paths = m_parent.attr("__path__").cast<py::list>();
for (auto py_path : parent_paths) {
if (py::isinstance<py::str>(py_path)) {
std::string path = py_path.cast<std::string>();
path.push_back(std::filesystem::path::preferred_separator);
path.append(name);
paths.append(py::cast(path));
}
);
m.def(
"__dir__",
[attrs, m]() -> py::typing::List<py::str> {
py::set names;
// Add the variables defined in the module
py::object dict = m.attr("__dict__");
for (auto it = dict.begin(); it != dict.end(); it++) {
names.add(*it);
}
// Add the deferred variables
for (const auto& [name, _] : attrs) {
names.add(py::str(name));
}
// Return as list
return py::module::import("builtins").attr("list")(names);
}
);
}

inline std::pair<std::string, std::function<py::object()>> deferred_package_path(py::module m_parent, py::module m, std::string name) {
auto getter = [m_parent, m, name]() {
std::string path = m_parent.attr("__path__").attr("__getitem__")(0).cast<std::string>();
path.push_back(std::filesystem::path::preferred_separator);
path.append(name);
py::list __path__;
__path__.append(py::cast(path));
return __path__;
};
return std::make_pair("__path__", getter);
}
m.attr("__path__") = paths;
}

inline std::pair<std::string, std::function<py::object()>> deferred_import(std::string module_name, std::string name) {
auto getter = [module_name, name]() {
return py::module::import(module_name.c_str()).attr(name.c_str());
};
return std::make_pair(name, getter);
inline module def_subpackage(py::module m_parent, std::string name) {
auto m = m_parent.def_submodule(name.c_str());
def_package_path(m_parent, m, name);
return m;
}
}
5 changes: 0 additions & 5 deletions src/amulet/utils/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import typing

from . import (
call_spec,
matrix,
Expand All @@ -23,6 +21,3 @@ __all__ = [
"weakref",
"world_utils",
]

def __dir__() -> list[str]: ...
def __getattr__(arg0: typing.Any) -> typing.Any: ...
9 changes: 1 addition & 8 deletions src/amulet/utils/_init_utils.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ namespace py = pybind11;
void init_utils_numpy(py::module);

void init_utils(py::module m_parent){
auto m = m_parent.def_submodule("utils");
auto m = py::def_subpackage(m_parent, "utils");

init_utils_numpy(m);

py::def_deferred(
m,
{
py::deferred_package_path(m_parent, m, "utils")
}
);
}

0 comments on commit fd16640

Please sign in to comment.