[robotpy] Run wpiformat on non-python robotpy files (#8362)

This turns the styleguide on for the non-python robotpy files.

The overwhelming amount of changes were
related to whitespace, followed by some IWYU for standard library
headers.
This commit is contained in:
PJ Reiniger
2026-06-21 22:36:03 -04:00
committed by GitHub
parent 6bc7051e23
commit 4a2cd3e5d0
71 changed files with 877 additions and 739 deletions

View File

@@ -1,3 +1,9 @@
#pragma once
#include <string>
#include <utility>
// clang-format off
cls_NetworkTable
.def("getValue", [](const NetworkTable &self, std::string_view key, py::object defaultValue) -> py::object {
wpi::nt::NetworkTableEntry entry;
@@ -54,3 +60,4 @@ cls_NetworkTable
return self.ContainsKey(key);
}, release_gil())
;
// clang-format on

View File

@@ -1,3 +1,8 @@
#pragma once
#include <string>
// clang-format off
cls_NetworkTableEntry
.def_property_readonly("value", [](const wpi::nt::NetworkTableEntry &self) {
wpi::nt::Value v;
@@ -46,3 +51,4 @@ cls_NetworkTableEntry
return self->SetDefaultValue(pyntcore::py2ntvalue(value));
}, py::arg("value"))
;
// clang-format on

View File

@@ -1,56 +1,61 @@
#include <semiwrap.h>
#include "nt_instance.h"
#include "wpi/nt/ntcore_cpp.hpp"
#include <set>
#include <semiwrap.h>
#include "wpi/nt/ntcore_cpp.hpp"
// only accessed under GIL
static std::set<NT_Inst> g_known_instances;
namespace pyntcore {
void onInstanceStart(wpi::nt::NetworkTableInstance *instance) {
g_known_instances.emplace(instance->GetHandle());
void onInstanceStart(wpi::nt::NetworkTableInstance* instance) {
g_known_instances.emplace(instance->GetHandle());
py::module::import("ntcore._logutil")
.attr("NtLogForwarder").attr("onInstanceStart")(instance);
py::module::import("ntcore._logutil")
.attr("NtLogForwarder")
.attr("onInstanceStart")(instance);
}
void onInstancePreReset(wpi::nt::NetworkTableInstance *instance) {
py::module::import("ntcore._logutil")
.attr("NtLogForwarder").attr("onInstanceDestroy")(instance);
void onInstancePreReset(wpi::nt::NetworkTableInstance* instance) {
py::module::import("ntcore._logutil")
.attr("NtLogForwarder")
.attr("onInstanceDestroy")(instance);
}
void onInstancePostReset(wpi::nt::NetworkTableInstance *instance) {
py::module::import("ntcore.util")
.attr("_NtProperty").attr("onInstancePostReset")(instance);
void onInstancePostReset(wpi::nt::NetworkTableInstance* instance) {
py::module::import("ntcore.util")
.attr("_NtProperty")
.attr("onInstancePostReset")(instance);
}
void onInstanceDestroy(wpi::nt::NetworkTableInstance *instance) {
py::module::import("ntcore._logutil")
.attr("NtLogForwarder").attr("onInstanceDestroy")(instance);
py::module::import("ntcore.util")
.attr("_NtProperty").attr("onInstanceDestroy")(instance);
void onInstanceDestroy(wpi::nt::NetworkTableInstance* instance) {
py::module::import("ntcore._logutil")
.attr("NtLogForwarder")
.attr("onInstanceDestroy")(instance);
py::module::import("ntcore.util")
.attr("_NtProperty")
.attr("onInstanceDestroy")(instance);
g_known_instances.erase(instance->GetHandle());
g_known_instances.erase(instance->GetHandle());
}
// reset all instances to clear out any potential python references that
// might be hanging around in a callback or something
void resetAllInstances()
{
std::set<NT_Inst> known_instances;
known_instances.swap(g_known_instances);
void resetAllInstances() {
std::set<NT_Inst> known_instances;
known_instances.swap(g_known_instances);
// always reset the default instance
known_instances.emplace(wpi::nt::GetDefaultInstance());
// always reset the default instance
known_instances.emplace(wpi::nt::GetDefaultInstance());
py::gil_scoped_release unlock;
py::gil_scoped_release unlock;
for (auto &inst: known_instances) {
wpi::nt::ResetInstance(inst);
}
for (auto& inst : known_instances) {
wpi::nt::ResetInstance(inst);
}
}
}; // namespace pyntcore
} // namespace pyntcore

View File

@@ -1,16 +1,16 @@
#pragma once
#include "wpi/nt/ntcore.h"
#include "wpi/nt/NetworkTableInstance.hpp"
#include "wpi/nt/ntcore.h"
namespace pyntcore {
void onInstanceStart(wpi::nt::NetworkTableInstance *instance);
void onInstancePreReset(wpi::nt::NetworkTableInstance *instance);
void onInstancePostReset(wpi::nt::NetworkTableInstance *instance);
void onInstanceDestroy(wpi::nt::NetworkTableInstance *instance);
void onInstanceStart(wpi::nt::NetworkTableInstance* instance);
void onInstancePreReset(wpi::nt::NetworkTableInstance* instance);
void onInstancePostReset(wpi::nt::NetworkTableInstance* instance);
void onInstanceDestroy(wpi::nt::NetworkTableInstance* instance);
void resetAllInstances();
}; // namespace pyntcore
} // namespace pyntcore

View File

@@ -1,39 +1,41 @@
#pragma once
#include <vector>
#include <pybind11/pybind11.h>
namespace pybind11 {
namespace detail {
namespace pybind11::detail {
// ntcore uses std::vector<uint8_t> anytime there is a raw value, so
// add this specialization to convert to/from bytes directly
template<>
template <>
struct type_caster<std::vector<uint8_t>> {
using vector_type = std::vector<uint8_t>;
PYBIND11_TYPE_CASTER(vector_type, const_name("bytes"));
using vector_type = std::vector<uint8_t>;
PYBIND11_TYPE_CASTER(vector_type, const_name("bytes"));
bool load(handle src, bool convert) {
if (!isinstance<buffer>(src)) {
return false;
}
auto buf = reinterpret_borrow<buffer>(src);
auto req = buf.request();
if (req.ndim != 1) {
return false;
}
auto begin = (const uint8_t*)req.ptr;
auto end = begin + req.size*req.itemsize;
value = std::vector<uint8_t>(begin, end);
return true;
bool load(handle src, bool convert) {
if (!isinstance<buffer>(src)) {
return false;
}
auto buf = reinterpret_borrow<buffer>(src);
auto req = buf.request();
if (req.ndim != 1) {
return false;
}
static handle cast(const std::vector<uint8_t> &src, return_value_policy policy, handle parent) {
return py::bytes((char*)src.data(), src.size()).release();
}
auto begin = (const uint8_t*)req.ptr;
auto end = begin + req.size * req.itemsize;
value = std::vector<uint8_t>(begin, end);
return true;
}
static handle cast(const std::vector<uint8_t>& src,
return_value_policy policy, handle parent) {
return py::bytes(reinterpret_cast<const char*>(src.data()), src.size())
.release();
}
};
}
}
} // namespace pybind11::detail

View File

@@ -1,14 +1,12 @@
#include <semiwrap_init.ntcore._ntcore.hpp>
#include "nt_instance.h"
#include "semiwrap_init.ntcore._ntcore.hpp"
SEMIWRAP_PYBIND11_MODULE(m) {
initWrapper(m);
static int unused;
py::capsule cleanup(&unused, [](void *) {
pyntcore::resetAllInstances();
});
py::capsule cleanup(&unused, [](void*) { pyntcore::resetAllInstances(); });
m.add_object("_st_cleanup", cleanup);
}
}

View File

@@ -1,111 +1,110 @@
#include "py2value.h"
#include <string>
#include <utility>
#include <vector>
// type casters
#include <pybind11/stl.h>
#include <wpi_span_type_caster.h>
namespace pyntcore {
const char * nttype2str(NT_Type type) {
const char* nttype2str(NT_Type type) {
switch (type) {
case NT_BOOLEAN:
return "bool";
case NT_DOUBLE:
return "double";
case NT_STRING:
return "string";
case NT_RAW:
return "raw";
case NT_BOOLEAN_ARRAY:
return "bool[]";
case NT_DOUBLE_ARRAY:
return "double[]";
case NT_STRING_ARRAY:
return "string[]";
case NT_INTEGER:
return "int";
case NT_FLOAT:
return "float";
case NT_INTEGER_ARRAY:
return "int[]";
case NT_FLOAT_ARRAY:
return "float[]";
default:
return "invalid";
case NT_BOOLEAN:
return "bool";
case NT_DOUBLE:
return "double";
case NT_STRING:
return "string";
case NT_RAW:
return "raw";
case NT_BOOLEAN_ARRAY:
return "bool[]";
case NT_DOUBLE_ARRAY:
return "double[]";
case NT_STRING_ARRAY:
return "string[]";
case NT_INTEGER:
return "int";
case NT_FLOAT:
return "float";
case NT_INTEGER_ARRAY:
return "int[]";
case NT_FLOAT_ARRAY:
return "float[]";
default:
return "invalid";
}
}
py::object ntvalue2py(const wpi::nt::Value &ntvalue) {
auto &v = ntvalue.value();
py::object ntvalue2py(const wpi::nt::Value& ntvalue) {
auto& v = ntvalue.value();
switch (v.type) {
case NT_BOOLEAN:
return py::bool_(v.data.v_boolean);
case NT_BOOLEAN:
return py::bool_(v.data.v_boolean);
case NT_DOUBLE:
return py::float_(v.data.v_double);
case NT_DOUBLE:
return py::float_(v.data.v_double);
case NT_STRING:
return py::str(v.data.v_string.str, v.data.v_string.len);
case NT_STRING:
return py::str(v.data.v_string.str, v.data.v_string.len);
case NT_RAW:
return py::bytes((const char *)v.data.v_raw.data, v.data.v_raw.size);
case NT_RAW:
return py::bytes((const char*)v.data.v_raw.data, v.data.v_raw.size);
case NT_BOOLEAN_ARRAY: {
py::list l(v.data.arr_boolean.size);
for (size_t i = 0; i < v.data.arr_boolean.size; i++) {
auto b = py::bool_(v.data.arr_boolean.arr[i]);
PyList_SET_ITEM(l.ptr(), i, b.release().ptr());
case NT_BOOLEAN_ARRAY: {
py::list l(v.data.arr_boolean.size);
for (size_t i = 0; i < v.data.arr_boolean.size; i++) {
auto b = py::bool_(v.data.arr_boolean.arr[i]);
PyList_SET_ITEM(l.ptr(), i, b.release().ptr());
}
return std::move(l);
}
return std::move(l);
}
case NT_DOUBLE_ARRAY: {
py::list l(v.data.arr_double.size);
for (size_t i = 0; i < v.data.arr_double.size; i++) {
auto d = py::float_(v.data.arr_double.arr[i]);
PyList_SET_ITEM(l.ptr(), i, d.release().ptr());
case NT_DOUBLE_ARRAY: {
py::list l(v.data.arr_double.size);
for (size_t i = 0; i < v.data.arr_double.size; i++) {
auto d = py::float_(v.data.arr_double.arr[i]);
PyList_SET_ITEM(l.ptr(), i, d.release().ptr());
}
return std::move(l);
}
return std::move(l);
}
case NT_STRING_ARRAY: {
return py::cast(ntvalue.GetStringArray());
}
case NT_INTEGER: {
return py::int_(v.data.v_int);
}
case NT_FLOAT: {
return py::float_(v.data.v_float);
}
case NT_INTEGER_ARRAY: {
py::list l(v.data.arr_int.size);
for (size_t i = 0; i < v.data.arr_int.size; i++) {
auto d = py::int_(v.data.arr_int.arr[i]);
PyList_SET_ITEM(l.ptr(), i, d.release().ptr());
case NT_STRING_ARRAY: {
return py::cast(ntvalue.GetStringArray());
}
return std::move(l);
}
case NT_FLOAT_ARRAY: {
py::list l(v.data.arr_float.size);
for (size_t i = 0; i < v.data.arr_float.size; i++) {
auto d = py::float_(v.data.arr_float.arr[i]);
PyList_SET_ITEM(l.ptr(), i, d.release().ptr());
case NT_INTEGER: {
return py::int_(v.data.v_int);
}
return std::move(l);
}
default:
return py::none();
case NT_FLOAT: {
return py::float_(v.data.v_float);
}
case NT_INTEGER_ARRAY: {
py::list l(v.data.arr_int.size);
for (size_t i = 0; i < v.data.arr_int.size; i++) {
auto d = py::int_(v.data.arr_int.arr[i]);
PyList_SET_ITEM(l.ptr(), i, d.release().ptr());
}
return std::move(l);
}
case NT_FLOAT_ARRAY: {
py::list l(v.data.arr_float.size);
for (size_t i = 0; i < v.data.arr_float.size; i++) {
auto d = py::float_(v.data.arr_float.arr[i]);
PyList_SET_ITEM(l.ptr(), i, d.release().ptr());
}
return std::move(l);
}
default:
return py::none();
}
}
@@ -143,38 +142,39 @@ wpi::nt::Value py2ntvalue(py::handle h) {
auto v = h.cast<std::vector<std::string>>();
return wpi::nt::Value::MakeStringArray(v);
} else {
throw py::value_error("Can only put bool/int/float/str/bytes or lists/tuples of them");
throw py::value_error(
"Can only put bool/int/float/str/bytes or lists/tuples of them");
}
}
py::function valueFactoryByType(wpi::nt::NetworkTableType type) {
py::object PyNtValue = py::module::import("ntcore").attr("Value");
switch (type) {
case wpi::nt::NetworkTableType::BOOLEAN:
return PyNtValue.attr("makeBoolean");
case wpi::nt::NetworkTableType::DOUBLE:
return PyNtValue.attr("makeDouble");
case wpi::nt::NetworkTableType::STRING:
return PyNtValue.attr("makeString");
case wpi::nt::NetworkTableType::RAW:
return PyNtValue.attr("makeRaw");
case wpi::nt::NetworkTableType::BOOLEAN_ARRAY:
return PyNtValue.attr("makeBooleanArray");
case wpi::nt::NetworkTableType::DOUBLE_ARRAY:
return PyNtValue.attr("makeDoubleArray");
case wpi::nt::NetworkTableType::STRING_ARRAY:
return PyNtValue.attr("makeStringArray");
case wpi::nt::NetworkTableType::INTEGER:
return PyNtValue.attr("makeInteger");
case wpi::nt::NetworkTableType::FLOAT:
return PyNtValue.attr("makeFloat");
case wpi::nt::NetworkTableType::INTEGER_ARRAY:
return PyNtValue.attr("makeIntegerArray");
case wpi::nt::NetworkTableType::FLOAT_ARRAY:
return PyNtValue.attr("makeFloatArray");
default:
throw py::type_error("empty nt value");
case wpi::nt::NetworkTableType::BOOLEAN:
return PyNtValue.attr("makeBoolean");
case wpi::nt::NetworkTableType::DOUBLE:
return PyNtValue.attr("makeDouble");
case wpi::nt::NetworkTableType::STRING:
return PyNtValue.attr("makeString");
case wpi::nt::NetworkTableType::RAW:
return PyNtValue.attr("makeRaw");
case wpi::nt::NetworkTableType::BOOLEAN_ARRAY:
return PyNtValue.attr("makeBooleanArray");
case wpi::nt::NetworkTableType::DOUBLE_ARRAY:
return PyNtValue.attr("makeDoubleArray");
case wpi::nt::NetworkTableType::STRING_ARRAY:
return PyNtValue.attr("makeStringArray");
case wpi::nt::NetworkTableType::INTEGER:
return PyNtValue.attr("makeInteger");
case wpi::nt::NetworkTableType::FLOAT:
return PyNtValue.attr("makeFloat");
case wpi::nt::NetworkTableType::INTEGER_ARRAY:
return PyNtValue.attr("makeIntegerArray");
case wpi::nt::NetworkTableType::FLOAT_ARRAY:
return PyNtValue.attr("makeFloatArray");
default:
throw py::type_error("empty nt value");
}
}
}
} // namespace pyntcore

View File

@@ -1,25 +1,27 @@
#pragma once
#include <semiwrap.h>
#include "wpi/nt/NetworkTableValue.hpp"
#include "wpi/nt/NetworkTableType.hpp"
#include <fmt/format.h>
#include <semiwrap.h>
#include "wpi/nt/NetworkTableType.hpp"
#include "wpi/nt/NetworkTableValue.hpp"
namespace pyntcore {
const char * nttype2str(NT_Type type);
const char* nttype2str(NT_Type type);
py::object ntvalue2py(const wpi::nt::Value &ntvalue);
py::object ntvalue2py(const wpi::nt::Value& ntvalue);
wpi::nt::Value py2ntvalue(py::handle h);
py::function valueFactoryByType(wpi::nt::NetworkTableType type);
inline void ensure_value_is(NT_Type expected, wpi::nt::Value *v) {
if (v->type() != expected) {
throw py::value_error(fmt::format(
"Value type is {}, not {}", nttype2str(v->type()), nttype2str(expected)
));
}
inline void ensure_value_is(NT_Type expected, wpi::nt::Value* v) {
if (v->type() != expected) {
throw py::value_error(fmt::format("Value type is {}, not {}",
nttype2str(v->type()),
nttype2str(expected)));
}
}
};
} // namespace pyntcore

View File

@@ -1,141 +1,179 @@
#include "pyentry.h"
#include "py2value.h"
#include <string>
#include <utility>
#include <pybind11/stl.h>
#include <wpi_span_type_caster.h>
#include "py2value.h"
namespace pyntcore {
py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_BOOLEAN) return defaultValue;
return py::cast(value.GetBoolean());
py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_BOOLEAN) {
return defaultValue;
}
return py::cast(value.GetBoolean());
}
py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_DOUBLE) return defaultValue;
return py::cast(value.GetDouble());
py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_DOUBLE) {
return defaultValue;
}
return py::cast(value.GetDouble());
}
py::object GetFloatEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_FLOAT) return defaultValue;
return py::cast(value.GetFloat());
py::object GetFloatEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_FLOAT) {
return defaultValue;
}
return py::cast(value.GetFloat());
}
py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_INTEGER) return defaultValue;
return py::cast(value.GetInteger());
py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_INTEGER) {
return defaultValue;
}
return py::cast(value.GetInteger());
}
py::object GetStringEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_STRING) return defaultValue;
auto s = value.GetString();
return py::str(s.data(), s.size());
py::object GetStringEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_STRING) {
return defaultValue;
}
auto s = value.GetString();
return py::str(s.data(), s.size());
}
py::object GetRawEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_RAW) return defaultValue;
return py::cast(value.GetRaw());
py::object GetRawEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_RAW) {
return defaultValue;
}
return py::cast(value.GetRaw());
}
py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_BOOLEAN_ARRAY) return defaultValue;
// ntcore will return bit vector by default. Convert to List[bool]
auto v = value.value();
py::list l(v.data.arr_boolean.size);
for (size_t i = 0; i < v.data.arr_boolean.size; i++) {
auto b = py::bool_(v.data.arr_boolean.arr[i]);
PyList_SET_ITEM(l.ptr(), i, b.release().ptr());
}
return std::move(l);
py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_BOOLEAN_ARRAY) {
return defaultValue;
}
// ntcore will return bit vector by default. Convert to List[bool]
auto v = value.value();
py::list l(v.data.arr_boolean.size);
for (size_t i = 0; i < v.data.arr_boolean.size; i++) {
auto b = py::bool_(v.data.arr_boolean.arr[i]);
PyList_SET_ITEM(l.ptr(), i, b.release().ptr());
}
return std::move(l);
}
py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_DOUBLE_ARRAY) return defaultValue;
return py::cast(value.GetDoubleArray());
py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_DOUBLE_ARRAY) {
return defaultValue;
}
return py::cast(value.GetDoubleArray());
}
py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_FLOAT_ARRAY) return defaultValue;
return py::cast(value.GetFloatArray());
py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_FLOAT_ARRAY) {
return defaultValue;
}
return py::cast(value.GetFloatArray());
}
py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_INTEGER_ARRAY) return defaultValue;
return py::cast(value.GetIntegerArray());
py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_INTEGER_ARRAY) {
return defaultValue;
}
return py::cast(value.GetIntegerArray());
}
py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_STRING_ARRAY) return defaultValue;
std::span<const std::string> rval = value.GetStringArray();
return py::cast(rval);
py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value || value.type() != NT_STRING_ARRAY) {
return defaultValue;
}
std::span<const std::string> rval = value.GetStringArray();
return py::cast(rval);
}
py::object GetValueEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value) return defaultValue;
return ntvalue2py(value);
py::object GetValueEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue) {
wpi::nt::Value value;
{
py::gil_scoped_release release;
value = wpi::nt::GetEntryValue(entry.GetHandle());
}
if (!value) {
return defaultValue;
}
return ntvalue2py(value);
}
}; // pyntcore
} // namespace pyntcore

View File

@@ -1,21 +1,35 @@
#pragma once
#include <semiwrap.h>
#include "wpi/nt/NetworkTableEntry.hpp"
#include "wpi/nt/NetworkTableValue.hpp"
namespace pyntcore {
py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetFloatEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetStringEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetRawEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetValueEntry(const wpi::nt::NetworkTableEntry &entry, py::object defaultValue);
py::object GetBooleanEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetDoubleEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetFloatEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetIntegerEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetStringEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetRawEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetBooleanArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetDoubleArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetFloatArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetIntegerArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetStringArrayEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
py::object GetValueEntry(const wpi::nt::NetworkTableEntry& entry,
py::object defaultValue);
};
} // namespace pyntcore

View File

@@ -21,8 +21,6 @@
#include "wpi/nt/NetworkTableValue.hpp"
#include "wpi/util/raw_ostream.hpp"
using namespace std::string_literals;
namespace wpi::nt::net {
namespace {
@@ -119,6 +117,7 @@ std::pair<int, Value> DecodeBinary(std::span<const uint8_t> data,
}
ClientMessage Publish(int pubuid, std::string_view name) {
using namespace std::literals;
return ClientMessage{PublishMsg{
pubuid, std::string{name}, "double"s, wpi::util::json::object(), {}}};
}