mapnik/bindings/python/python_to_value.hpp

123 lines
3.8 KiB
C++
Raw Normal View History

2014-05-11 20:08:24 +00:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_PYTHON_BINDING_PYTHON_TO_VALUE
#define MAPNIK_PYTHON_BINDING_PYTHON_TO_VALUE
// boost
2014-10-21 23:37:27 +00:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
2014-05-11 20:08:24 +00:00
#include <boost/python.hpp>
2014-10-21 23:37:27 +00:00
#pragma GCC diagnostic pop
2014-05-11 20:08:24 +00:00
// mapnik
#include <mapnik/value.hpp>
2014-08-09 04:58:08 +00:00
#include <mapnik/unicode.hpp>
2014-05-11 20:08:24 +00:00
#include <mapnik/attribute.hpp>
namespace mapnik {
static mapnik::attributes dict2attr(boost::python::dict const& d)
{
using namespace boost::python;
mapnik::attributes vars;
mapnik::transcoder tr_("utf8");
boost::python::list keys=d.keys();
for (int i=0; i < len(keys); ++i)
{
2014-10-25 00:50:40 +00:00
std::string key;
object obj_key = keys[i];
if (PyUnicode_Check(obj_key.ptr()))
{
PyObject* temp = PyUnicode_AsUTF8String(obj_key.ptr());
if (temp)
{
#if PY_VERSION_HEX >= 0x03000000
char* c_str = PyBytes_AsString(temp);
#else
char* c_str = PyString_AsString(temp);
#endif
key = c_str;
Py_DecRef(temp);
}
}
else
{
key = extract<std::string>(keys[i]);
}
2014-05-11 20:08:24 +00:00
object obj = d[key];
if (PyUnicode_Check(obj.ptr()))
{
PyObject* temp = PyUnicode_AsUTF8String(obj.ptr());
if (temp)
{
#if PY_VERSION_HEX >= 0x03000000
char* c_str = PyBytes_AsString(temp);
#else
char* c_str = PyString_AsString(temp);
#endif
vars[key] = tr_.transcode(c_str);
Py_DecRef(temp);
}
continue;
}
if (PyBool_Check(obj.ptr()))
2014-05-11 20:08:24 +00:00
{
extract<mapnik::value_bool> ex(obj);
if (ex.check())
{
vars[key] = ex();
}
2014-05-11 20:08:24 +00:00
}
else if (PyFloat_Check(obj.ptr()))
2014-05-11 20:08:24 +00:00
{
extract<mapnik::value_double> ex(obj);
if (ex.check())
{
vars[key] = ex();
}
2014-05-11 20:08:24 +00:00
}
else
2014-05-11 20:08:24 +00:00
{
extract<mapnik::value_integer> ex(obj);
if (ex.check())
{
vars[key] = ex();
}
else
{
extract<std::string> ex0(obj);
if (ex0.check())
{
vars[key] = tr_.transcode(ex0().c_str());
}
}
2014-05-11 20:08:24 +00:00
}
}
return vars;
}
}
#endif // MAPNIK_PYTHON_BINDING_PYTHON_TO_VALUE