From 7ee106187e1d0991e7ae79abd905a79dc5760b5f Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 12 Oct 2020 12:22:00 +0200 Subject: 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()) --- host/lib/property_tree_python.cpp | 61 +++++++++++++++++++++++++++++++++++++ host/lib/property_tree_python.hpp | 11 +++++++ host/lib/usrp/multi_usrp_python.cpp | 4 +++ host/python/CMakeLists.txt | 1 + host/python/pyuhd.cpp | 4 +++ host/python/uhd/__init__.py | 3 ++ host/python/uhd/property_tree.py | 12 ++++++++ 7 files changed, 96 insertions(+) create mode 100644 host/lib/property_tree_python.cpp create mode 100644 host/lib/property_tree_python.hpp create mode 100644 host/python/uhd/property_tree.py (limited to 'host') diff --git a/host/lib/property_tree_python.cpp b/host/lib/property_tree_python.cpp new file mode 100644 index 000000000..0b7f18aa2 --- /dev/null +++ b/host/lib/property_tree_python.cpp @@ -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 +#include +#include +#include + +namespace py = pybind11; + +template +void export_property(py::module& m, const std::string& type_str) +{ + const std::string classname = std::string("property__") + type_str; + py::class_>(m, classname.c_str()) + .def("get", &uhd::property::get) + .def("get_desired", &uhd::property::get_desired) + .def("set", &uhd::property::set) + .def("set_coerced", &uhd::property::set_coerced); +} + +void export_property_tree(py::module& m) +{ + using property_tree = uhd::property_tree; + using fs_path = uhd::fs_path; + + py::class_(m, "fs_path") + // Constructors + .def(py::init<>()) + .def(py::init()); + py::implicitly_convertible(); + + // Per type we want to expose, add one line here, and one accessor below + export_property(m, "int"); + export_property(m, "double"); + export_property(m, "str"); + export_property(m, "bool"); + + py::class_(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, py::return_value_policy::reference) + .def("access_double", + &property_tree::access, + py::return_value_policy::reference) + .def("access_str", + &property_tree::access, + py::return_value_policy::reference) + .def("access_bool", + &property_tree::access, + py::return_value_policy::reference) + // End of types + ; +} diff --git a/host/lib/property_tree_python.hpp b/host/lib/property_tree_python.hpp new file mode 100644 index 000000000..c16dacfe3 --- /dev/null +++ b/host/lib/property_tree_python.hpp @@ -0,0 +1,11 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#pragma once + +#include + +void export_property_tree(pybind11::module& m); diff --git a/host/lib/usrp/multi_usrp_python.cpp b/host/lib/usrp/multi_usrp_python.cpp index 602fc5178..c1ee17d0b 100644 --- a/host/lib/usrp/multi_usrp_python.cpp +++ b/host/lib/usrp/multi_usrp_python.cpp @@ -5,7 +5,9 @@ // SPDX-License-Identifier: GPL-3.0-or-later // +#include #include +#include 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) diff --git a/host/python/CMakeLists.txt b/host/python/CMakeLists.txt index d4417903d..1e9fefa43 100644 --- a/host/python/CMakeLists.txt +++ b/host/python/CMakeLists.txt @@ -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 diff --git a/host/python/pyuhd.cpp b/host/python/pyuhd.cpp index bb564c276..eadb88d40 100644 --- a/host/python/pyuhd.cpp +++ b/host/python/pyuhd.cpp @@ -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); } diff --git a/host/python/uhd/__init__.py b/host/python/uhd/__init__.py index 99c10c3b1..bf11ac513 100644 --- a/host/python/uhd/__init__.py +++ b/host/python/uhd/__init__.py @@ -14,3 +14,6 @@ from . import rfnoc from . import dsp from . import chdr from .libpyuhd.paths import * +from .property_tree import PropertyTree + + diff --git a/host/python/uhd/property_tree.py b/host/python/uhd/property_tree.py new file mode 100644 index 000000000..02c6d108c --- /dev/null +++ b/host/python/uhd/property_tree.py @@ -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 -- cgit v1.2.3