Skip to content

Commit

Permalink
feat: add python bindings for Permissible
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 14, 2024
1 parent 615ad68 commit d8663f7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
46 changes: 45 additions & 1 deletion python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
import typing
__all__ = ['ColorFormat', 'Command', 'CommandExecutor', 'CommandSender', 'Logger', 'PermissionDefault', 'Plugin', 'PluginCommand', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server', 'ServerOperator']
__all__ = ['ColorFormat', 'Command', 'CommandExecutor', 'CommandSender', 'Logger', 'Permissible', 'PermissionDefault', 'Plugin', 'PluginCommand', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server', 'ServerOperator']
class ColorFormat:
AQUA: typing.ClassVar[str] = '§b'
BLACK: typing.ClassVar[str] = '§0'
Expand Down Expand Up @@ -206,6 +206,50 @@ class Logger:
"""
Get the name of this Logger instance.
"""
class Permissible(ServerOperator):
@typing.overload
def add_attachment(self, plugin: Plugin, name: str, value: bool) -> ...:
"""
Adds a new PermissionAttachment.
"""
@typing.overload
def add_attachment(self, plugin: Plugin) -> ...:
"""
Adds a new PermissionAttachment.
"""
@typing.overload
def has_permission(self, name: str) -> bool:
"""
Checks if a permissions is available by name.
"""
@typing.overload
def has_permission(self, perm: ...) -> bool:
"""
Checks if a permissions is available by permission.
"""
@typing.overload
def is_permission_set(self, name: str) -> bool:
"""
Checks if a permissions is set by name.
"""
@typing.overload
def is_permission_set(self, perm: ...) -> bool:
"""
Checks if a permissions is set by permission.
"""
def recalculate_permissions(self) -> None:
"""
Recalculates the permissions.
"""
def remove_attachment(self, attachment: ...) -> bool:
"""
Removes a given PermissionAttachment.
"""
@property
def effective_permissions(self) -> set[...]:
"""
Gets effective permissions.
"""
class PermissionDefault:
"""
Members:
Expand Down
4 changes: 2 additions & 2 deletions src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace endstone::detail {

void init_command(py::module_ &);
void init_logger(py::module_ &);
void init_permissions(py::module_ &);
void init_plugin(py::module_ &);
void init_permissions(py::module_ &);
void init_server(py::class_<Server> &server);
void init_util(py::module_ &);

Expand All @@ -35,8 +35,8 @@ PYBIND11_MODULE(endstone_python, m) // NOLINT(*-use-anonymous-namespace)
auto server = py::class_<Server>(m, "Server");
init_command(m);
init_logger(m);
init_permissions(m);
init_plugin(m);
init_permissions(m);
init_server(server);
init_util(m);
}
Expand Down
29 changes: 28 additions & 1 deletion src/endstone_python/permissions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone/permissions/permissions.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "endstone/permissions/permissible.h"
#include "endstone/permissions/permission_attachment.h"
#include "endstone/permissions/permission_attachment_info.h"
#include "endstone/permissions/permission_default.h"
#include "endstone/permissions/server_operator.h"
#include "endstone/plugin/plugin.h"

namespace py = pybind11;

namespace endstone::detail {

void init_permissions(py::module &m)
{
py::class_<ServerOperator, std::shared_ptr<ServerOperator>>(m, "ServerOperator")
py::class_<ServerOperator>(m, "ServerOperator")
.def_property("op", &ServerOperator::isOp, &ServerOperator::setOp, "The operator status of this object");

py::enum_<PermissionDefault>(m, "PermissionDefault")
Expand All @@ -33,6 +40,26 @@ void init_permissions(py::module &m)
.value("OPERATOR", PermissionDefault::Operator)
.value("NOT_OP", PermissionDefault::Operator)
.value("NOT_OPERATOR", PermissionDefault::NotOperator);

py::class_<Permissible, ServerOperator>(m, "Permissible")
.def("is_permission_set", py::overload_cast<std::string>(&Permissible::isPermissionSet, py::const_),
py::arg("name"), "Checks if a permissions is set by name.")
.def("is_permission_set", py::overload_cast<const Permission &>(&Permissible::isPermissionSet, py::const_),
py::arg("perm"), "Checks if a permissions is set by permission.")
.def("has_permission", py::overload_cast<std::string>(&Permissible::hasPermission, py::const_), py::arg("name"),
"Checks if a permissions is available by name.")
.def("has_permission", py::overload_cast<const Permission &>(&Permissible::hasPermission, py::const_),
py::arg("perm"), "Checks if a permissions is available by permission.")
.def("add_attachment", py::overload_cast<Plugin &, const std::string &, bool>(&Permissible::addAttachment),
py::arg("plugin"), py::arg("name"), py::arg("value"), py::return_value_policy::reference,
"Adds a new PermissionAttachment.")
.def("add_attachment", py::overload_cast<Plugin &>(&Permissible::addAttachment), py::arg("plugin"),
py::return_value_policy::reference, "Adds a new PermissionAttachment.")
.def("remove_attachment", &Permissible::removeAttachment, py::arg("attachment"),
"Removes a given PermissionAttachment.")
.def("recalculate_permissions", &Permissible::recalculatePermissions, "Recalculates the permissions.")
.def_property_readonly("effective_permissions", &Permissible::getEffectivePermissions,
py::return_value_policy::reference_internal, "Gets effective permissions.");
}

} // namespace endstone::detail

0 comments on commit d8663f7

Please sign in to comment.