python: Add access to the property_tree from Python

Example:
>>> usrp = uhd.usrp.multi_usrp("")
>>> tree = usrp.get_tree()
>>> print(tree.access_int("/name").get())
This commit is contained in:
Martin Braun 2020-10-12 12:22:00 +02:00 committed by Aaron Rossetto
parent 0167ad7608
commit 7ee106187e
7 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,61 @@
//
// Copyright 2020 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include "property_tree_python.hpp"
#include <uhd/property_tree.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <string>
namespace py = pybind11;
template <typename T>
void export_property(py::module& m, const std::string& type_str)
{
const std::string classname = std::string("property__") + type_str;
py::class_<uhd::property<T>>(m, classname.c_str())
.def("get", &uhd::property<T>::get)
.def("get_desired", &uhd::property<T>::get_desired)
.def("set", &uhd::property<T>::set)
.def("set_coerced", &uhd::property<T>::set_coerced);
}
void export_property_tree(py::module& m)
{
using property_tree = uhd::property_tree;
using fs_path = uhd::fs_path;
py::class_<fs_path>(m, "fs_path")
// Constructors
.def(py::init<>())
.def(py::init<std::string>());
py::implicitly_convertible<std::string, fs_path>();
// Per type we want to expose, add one line here, and one accessor below
export_property<int>(m, "int");
export_property<double>(m, "double");
export_property<std::string>(m, "str");
export_property<bool>(m, "bool");
py::class_<property_tree>(m, "property_tree")
.def("subtree", &property_tree::subtree, py::arg("path"))
.def("exists", &property_tree::exists, py::arg("path"))
.def("list", &property_tree::list, py::arg("path"))
// One line per type
.def(
"access_int", &property_tree::access<int>, py::return_value_policy::reference)
.def("access_double",
&property_tree::access<double>,
py::return_value_policy::reference)
.def("access_str",
&property_tree::access<std::string>,
py::return_value_policy::reference)
.def("access_bool",
&property_tree::access<bool>,
py::return_value_policy::reference)
// End of types
;
}

View File

@ -0,0 +1,11 @@
//
// Copyright 2020 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#pragma once
#include <pybind11/pybind11.h>
void export_property_tree(pybind11::module& m);

View File

@ -5,7 +5,9 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include <pybind11/complex.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
@ -27,6 +29,8 @@ void export_multi_usrp(py::module& m)
.def(py::init(&multi_usrp::make))
// clang-format off
.def("get_tree" , &multi_usrp::get_tree)
// General USRP methods
.def("get_rx_freq" , &multi_usrp::get_rx_freq, py::arg("chan") = 0)
.def("get_rx_num_channels" , &multi_usrp::get_rx_num_channels)

View File

@ -31,6 +31,7 @@ execute_process(
# Build pyuhd library
add_library(pyuhd SHARED
pyuhd.cpp
${CMAKE_SOURCE_DIR}/lib/property_tree_python.cpp
${CMAKE_SOURCE_DIR}/lib/usrp/multi_usrp_python.cpp
)
# python expects extension modules with a particular suffix

View File

@ -14,6 +14,7 @@
namespace py = pybind11;
#include "cal/cal_python.hpp"
#include "property_tree_python.hpp"
#include "rfnoc/ddc_block_control_python.hpp"
#include "rfnoc/duc_block_control_python.hpp"
#include "rfnoc/fft_block_control_python.hpp"
@ -110,4 +111,7 @@ PYBIND11_MODULE(libpyuhd, m)
auto chdr_module = m.def_submodule("chdr", "CHDR Parsing");
export_utils(chdr_module);
// Register property tree
export_property_tree(m);
}

View File

@ -14,3 +14,6 @@ from . import rfnoc
from . import dsp
from . import chdr
from .libpyuhd.paths import *
from .property_tree import PropertyTree

View File

@ -0,0 +1,12 @@
#
# Copyright 2020 Ettus Research, a National Instruments Brand
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
"""
Access to the property tree
"""
from . import libpyuhd as lib
PropertyTree = lib.property_tree