Merge pull request #58 from tboudreaux/feature/pythonInterface/config

Add config module bindings to python interface
This commit is contained in:
2025-05-05 14:43:11 -04:00
committed by GitHub
8 changed files with 95 additions and 12 deletions

View File

@@ -7,10 +7,12 @@ py_mod = py_installation.extension_module(
meson.project_source_root() + '/src/python/bindings.cpp',
meson.project_source_root() + '/src/python/composition/bindings.cpp',
meson.project_source_root() + '/src/python/const/bindings.cpp',
meson.project_source_root() + '/src/python/config/bindings.cpp',
],
dependencies : [
pybind11_dep,
const_dep,
config_dep,
composition_dep,
species_weight_dep
],

View File

@@ -1,3 +1,11 @@
# Build options for the project
# Usage:
# -Doption_name=value
# -Dbuild_tests=true (default) build tests
# -Duser_mode=false (default) enable user mode (set mode = 0) If user mode is enabled then the optimization level is set to 3 and the build type is set to release
# -Dbuild_python=true (default) build Python bindings
option('build_tests', type: 'boolean', value: true, description: 'Build tests')
option('user_mode', type: 'boolean', value: false, description: 'Enable user mode (set mode = 0)')
option('build_python', type: 'boolean', value: true, description: 'Build Python bindings')

View File

@@ -18,8 +18,7 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// *********************************************************************** */
#ifndef CONFIG_H
#define CONFIG_H
#pragma once
#include <string>
#include <iostream>
@@ -134,12 +133,6 @@ public:
*/
bool loadConfig(const std::string& configFilePath);
/**
* @brief Get the input table from the configuration.
* @return Input table as a string.
*/
std::string getInputTable() const;
/**
* @brief Get a configuration value by key.
* @tparam T Type of the value to retrieve.
@@ -234,8 +227,6 @@ public:
// Setup gTest class as a friend
friend class configTestPrivateAccessor;
// -- Resource Manager is a friend of config so it can create a seperate instance
// --- Resource Manager is a friend of config so it can create a separate instance
friend class ResourceManager;
};
#endif

View File

@@ -4,6 +4,7 @@
#include "const/bindings.h"
#include "composition/bindings.h"
#include "config/bindings.h"
PYBIND11_MODULE(fourdsse_bindings, m) {
m.doc() = "Python bindings for the 4DSSE project";
@@ -13,4 +14,7 @@ PYBIND11_MODULE(fourdsse_bindings, m) {
auto constMod = m.def_submodule("constants", "Constants-module bindings");
register_const_bindings(constMod);
auto configMod = m.def_submodule("config", "Configuration-module bindings");
register_config_bindings(configMod);
}

View File

@@ -0,0 +1,55 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h> // Needed for vectors, maps, sets, strings
#include <pybind11/stl_bind.h> // Needed for binding std::vector, std::map etc if needed directly
#include <string>
#include "bindings.h"
#include "config.h"
namespace py = pybind11;
// Helper function template for binding Config::get
template <typename T>
void def_config_get(py::module &m) {
m.def("get",
[](const std::string &key, T defaultValue) {
return Config::getInstance().get<T>(key, defaultValue);
},
py::arg("key"), py::arg("defaultValue"),
"Get configuration value (type inferred from default)");
}
void register_config_bindings(pybind11::module &config_submodule) {
def_config_get<int>(config_submodule);
def_config_get<double>(config_submodule);
def_config_get<std::string>(config_submodule);
def_config_get<bool>(config_submodule);
config_submodule.def("loadConfig",
[](const std::string& configFilePath) {
return Config::getInstance().loadConfig(configFilePath);
},
py::arg("configFilePath"),
"Load configuration from a YAML file.");
config_submodule.def("has",
[](const std::string &key) {
return Config::getInstance().has(key);
},
py::arg("key"),
"Check if a key exists in the configuration.");
config_submodule.def("keys",
[]() {
return py::cast(Config::getInstance().keys());
},
"Get a list of all configuration keys.");
config_submodule.def("__repr__",
[]() {
std::ostringstream oss;
oss << Config::getInstance(); // Use the existing operator<<
return std::string("<fourdsse_bindings.config module accessing C++ Singleton>\n") + oss.str();
});
}

View File

@@ -0,0 +1,5 @@
#pragma once
#include <pybind11/pybind11.h>
void register_config_bindings(pybind11::module &config_submodule);

View File

@@ -0,0 +1,17 @@
# Define the library
bindings_sources = files('bindings.cpp')
bindings_headers = files('bindings.h')
dependencies = [
config_dep,
python3_dep,
pybind11_dep,
]
shared_module('py_config',
bindings_sources,
include_directories: include_directories('.'),
cpp_args: ['-fvisibility=default'],
install : true,
dependencies: dependencies,
)

View File

@@ -1,2 +1,3 @@
subdir('composition')
subdir('const')
subdir('const')
subdir('config')