ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
PyConfigReader.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include <pybind11/embed.h>
5// pybind11 includes should come first
6
8#include "PyConfigReader.h"
9
10#include <ChimeraTK/cppext/ranges.hpp>
11
12#include <pybind11/stl.h>
13
14namespace py = pybind11;
15
16namespace ChimeraTK {
17
18 /********************************************************************************************************************/
19
20 UserTypeVariantNoVoid PyConfigReader::get(
21 ChimeraTK::DataType dt, const std::string& path, std::optional<UserTypeVariantNoVoid> defaultValue) {
22 std::optional<UserTypeVariantNoVoid> rv;
23 ChimeraTK::callForTypeNoVoid(dt.getAsTypeInfo(), [&](auto t) {
24 using UserType = decltype(t);
25
26 if(defaultValue) {
27 UserType valAsUserType;
28 std::visit(
29 [&](auto value) { valAsUserType = ChimeraTK::userTypeToUserType<UserType>(value); }, defaultValue.value());
30 rv.emplace(_reader.get().get<UserType>(path, valAsUserType));
31 }
32 else {
33 rv.emplace(_reader.get().get<UserType>(path));
34 }
35 });
36
37 return std::move(rv.value());
38 }
39
40 /********************************************************************************************************************/
41
42 UserTypeTemplateVariantNoVoid<PyConfigReader::Vector> PyConfigReader::getArray(ChimeraTK::DataType dt,
43 const std::string& path, std::optional<UserTypeTemplateVariantNoVoid<Vector>> defaultValue) {
44 std::optional<UserTypeTemplateVariantNoVoid<Vector>> rv;
45 ChimeraTK::callForTypeNoVoid(dt.getAsTypeInfo(), [&](auto t) {
46 using UserType = decltype(t);
47
48 if(defaultValue) {
49 std::vector<UserType> valAsUserType = std::visit(
50 [&](auto value) {
51 return value | std::views::transform([](auto& v) { return ChimeraTK::userTypeToUserType<UserType>(v); }) |
52 cppext::ranges::to<std::vector>();
53 },
54 defaultValue.value());
55 rv.emplace(_reader.get().get<std::vector<UserType>>(path, valAsUserType));
56 }
57 else {
58 rv.emplace(_reader.get().get<std::vector<UserType>>(path));
59 }
60 });
61
62 return std::move(rv.value());
63 }
64
65 /********************************************************************************************************************/
66
67 void PyConfigReader::bind(py::module& m) {
68 // Global access to appConfig(), mimicking something like Application::appConfig() in C++
69 m.def("appConfig", []() { return PyConfigReader(PyApplicationModule::appConfig()); });
70 py::class_<PyConfigReader>(m, "ConfigReader")
71 .def("get", &PyConfigReader::get,
72 "Get value for given configuration variable.\n\nThis is already accessible right after construction of "
73 "this object. Throws ChimeraTK::logic_error if variable doesn't exist. To obtain the value of an array, "
74 "use an std::vector<T> as template argument.",
75 py::arg(), py::arg("variableName"), py::arg("defaultValue") = std::nullopt)
76 .def("getArray", &PyConfigReader::getArray,
77 "Get value for given configuration variable.\n\nThis is already accessible right after construction of "
78 "this object. Throws ChimeraTK::logic_error if variable doesn't exist. ",
79 py::arg(), py::arg("variableName"), py::arg("defaultValue") = std::nullopt)
80 .def("getModules", &PyConfigReader::getModules, py::arg("path") = "");
81 }
82
83 /********************************************************************************************************************/
84
85} // namespace ChimeraTK
UserTypeVariantNoVoid get(ChimeraTK::DataType dt, const std::string &path, std::optional< UserTypeVariantNoVoid > defaultValue)
InvalidityTracer application module.
module_ module