Add list in config python module

parent 36d747d9
......@@ -101,6 +101,7 @@ namespace lima
// --- settings
Type getType() const;
int getLength() const;
const char* getName() const;
bool isGroup() const;
......
......@@ -269,9 +269,18 @@ void Setting::set(const char* alias,
CHECK_NULL(); \
try \
{ \
libconfig::Setting &alias_setting = \
m_setting->add(alias,type); \
return Setting(&alias_setting); \
if(alias) \
{ \
libconfig::Setting &alias_setting = \
m_setting->add(alias,type); \
return Setting(&alias_setting); \
} \
else \
{ \
libconfig::Setting &alias_setting = \
m_setting->add(type); \
return Setting(&alias_setting); \
} \
} \
catch(libconfig::SettingNameException& exp) \
{ \
......@@ -484,6 +493,11 @@ Setting::Type Setting::getType() const
return aReturnType;
}
int Setting::getLength() const
{
return m_setting->getLength();
}
const char* Setting::getName() const
{
CHECK_NULL();
......
......@@ -37,6 +37,79 @@ class Setting;
#pragma warning( disable : 4800 )
#endif
inline long _from_python_long_2_c(PyObject* value,int& return_value)
{
#if PY_VERSION_HEX >= 0x02070000
int overflow;
long cValue = PyLong_AsLongAndOverflow(value,&overflow);
if(overflow == 1 || overflow == -1)
{
PyErr_SetString(PyExc_ValueError,
"don't manage big int (overflow)");
return_value = 1;
return -1;
}
#else
long cValue = PyLong_AsLong(value);
#endif
return cValue;
}
int _fill_setting_with_dict(Setting &setting,PyObject *dict);
inline int _fill_setting_with_list(Setting &setting,
PyObject *list)
{
int return_value = 0;
try
{
Py_ssize_t list_lenght = PyList_GET_SIZE(list);
for(Py_ssize_t index = 0;
!return_value && index < list_lenght;++index)
{
PyObject* value = PyList_GetItem(list,index);
if(PyString_Check(value))
setting.append(PyString_AS_STRING(value));
else if(PyFloat_Check(value))
setting.append(PyFloat_AS_DOUBLE(value));
else if(PyLong_Check(value))
{
long cValue = _from_python_long_2_c(value,return_value);
setting.append(cValue);
}
else if(PyInt_Check(value))
setting.append(int(PyInt_AsLong(value)));
else if(PyBool_Check(value))
setting.append(PyObject_IsTrue(value));
else if(PyDict_Check(value))
{
Setting child = setting.addChild(NULL);
return_value = _fill_setting_with_dict(child,value);
}
else if(PyList_Check(value))
{
Setting child = setting.addList(NULL);
return_value = _fill_setting_with_list(child,value);
}
else
{
PyErr_SetString(PyExc_ValueError,
"Type not yet managed");
return_value = 1;
break;
}
}
}
catch(...)
{
Py_BEGIN_ALLOW_THREADS;
throw;
Py_END_ALLOW_THREADS;
}
return return_value;
}
inline int _fill_setting_with_dict(Setting &setting,
PyObject *dict)
{
......@@ -63,19 +136,7 @@ inline int _fill_setting_with_dict(Setting &setting,
PyFloat_AS_DOUBLE(value));
else if(PyLong_Check(value))
{
#if PY_VERSION_HEX >= 0x02070000
int overflow;
long cValue = PyLong_AsLongAndOverflow(value,&overflow);
if(overflow == 1 || overflow == -1)
{
PyErr_SetString(PyExc_ValueError,
"don't manage big int (overflow)");
return_value = 1;
break;
}
#else
long cValue = PyLong_AsLong(value);
#endif
long cValue = _from_python_long_2_c(value,return_value);
setting.set(PyString_AS_STRING(key),
cValue);
}
......@@ -90,6 +151,11 @@ inline int _fill_setting_with_dict(Setting &setting,
Setting child = setting.addChild(PyString_AS_STRING(key));
return_value = _fill_setting_with_dict(child,value);
}
else if(PyList_Check(value))
{
Setting child = setting.addList(PyString_AS_STRING(key));
return_value = _fill_setting_with_list(child,value);
}
else if(sipCanConvertToType(value,sipType_Data,SIP_NOT_NONE))
{
int state;
......@@ -183,6 +249,12 @@ inline bool _build_dict_from_setting(const Setting &setting,
py_value);
break;
}
case Setting::List:
{
py_value = PyList_New(0);
return_value = _build_dict_from_setting(sub_setting,py_value);
break;
}
case Setting::DataType:
{
Data value;
......@@ -200,9 +272,14 @@ inline bool _build_dict_from_setting(const Setting &setting,
}
if(!return_value)
return_value = PyDict_SetItemString(dict,
sub_setting.getName(),
py_value);
{
if(PyDict_Check(dict))
return_value = PyDict_SetItemString(dict,
sub_setting.getName(),
py_value);
else
return_value = PyList_Append(dict,py_value);
}
if(py_value)
Py_DECREF(py_value);
......@@ -326,4 +403,4 @@ sipIsErr = 1; // should never happen
private:
CtConfig(const CtConfig &);
};
%End
\ No newline at end of file
%End
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment