Hey,
Awesome binding. Currently working on a project that uses libconfig and embeds Python, so it was a huge time-saver.
Anyways, during the process of working on the project, I wrote a simple Python class wrapping the existing Config object. It allows reading and writing to the configuration file a bit easier.
For example:
config = Config('abc.cfg', True)
config.test_list = [0, 'a', 2, 3.0, 4, 5L]
# Currently these two wont work.
#config.test_array = (0, 1, 2, 3, 4,)
#config.useStuff = True
config.name = __file__
config.add_group('aliases')
config.aliases.out = 'echo!'
config.aliases.pop = 'popd'
config.cmds = {
'abc' : 1,
'abc2' : 2.0,
'abc3' : 3L,
}
config.remove_me = 'Hello World'
del config.remove_me
Would give you a file named "abc.cfg" containing:
test_list = ( "0", "a", "2", "3.0", "4", "5" );
name = "G:\\cpp.workspace\\yawap\\thirdparty\\libconfig\\python-libconfig\\libconfig.py";
aliases :
{
out = "echo!";
pop = "popd";
};
cmds :
{
abc = 1;
abc2 = 2.0;
abc3 = 3;
};
I figured I'd pass it along to you to see if you'd like to use it at all. Should be attached.
That being said, I noticed a few issues with calls pertaining to assignments. Additionally, using setValue with a bool value throws a SettingException, even after calling addBoolean. Haven't done that much work with boost_python, so I'm not sure if it automatically converts a Python bool object to a C++ bool, but that's the only thing I could think of. Additionally there doesn't seem to be any signatures for setValue pertaining to lists or arrays
I could put together a couple of quick functions accepting input for lists and arrays if you'd like - I just need to know which types you'd like to map. I figured I'd use Python lists and sets for libconfig lists, and tuples for libconfig arrays, but let me know if you have a different idea.
Lastly, I noticed there were no signatures for adding a non-string to a list. Here's a few that should work:
void appendToList_int ( const char * path, int value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeInt ) = value;
}
void appendToList_long ( const char * path, long value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeInt ) = value;
}
void appendToList_longlong ( const char * path, long long value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeInt64 ) = value;
}
void appendToList_float ( const char * path, float value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeFloat ) = value;
}
void appendToList_double ( const char * path, double value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeFloat ) = value;
}
And registering them with boost_python:
.def("appendToList", &pyConfig::appendToList_int )
.def("appendToList", &pyConfig::appendToList_long )
.def("appendToList", &pyConfig::appendToList_longlong )
.def("appendToList", &pyConfig::appendToList_double )
.def("appendToList", &pyConfig::appendToList_float )
Anyways, thanks again.
Figured out the issue with using setValue for a bool value. Apparently boost::python converts the Python bool to a int, so it's being run through setValue_int. Changing setValue_int to the following seems to make it work correctly: void setValue_int ( const char * path, int value ) { libconfig::Setting &setting = config->lookup ( path ); if(setting.isNumber()) { setting = value; } else { setting = (value) ? true : false; } }