<commit_before>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 <NAME>, <NAME>
 *
 * 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
 *
 *****************************************************************************/
//$Id$

// boost

#include <boost/python/suite/indexing/indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>

#include <boost/python/iterator.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/to_python_converter.hpp>
#include <boost/python.hpp>
#include <boost/scoped_array.hpp>

// mapnik
#include <mapnik/feature.hpp>
#include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/wkb.hpp>
#include <mapnik/wkt/wkt_factory.hpp>

mapnik::geometry_type & (mapnik::Feature::*get_geom1)(unsigned) = &mapnik::Feature::get_geometry;

namespace {

using mapnik::Feature;
using mapnik::geometry_utils;
using mapnik::from_wkt;
using mapnik::context;
using mapnik::context_ptr;
using mapnik::feature_kv_iterator;

void feature_add_geometries_from_wkb(Feature &feature, std::string wkb)
{
    geometry_utils::from_wkb(feature.paths(), wkb.c_str(), wkb.size());
}

void feature_add_geometries_from_wkt(Feature &feature, std::string wkt)
{
    bool result = mapnik::from_wkt(wkt, feature.paths());
    if (!result) throw std::runtime_error("Failed to parse WKT");
}

mapnik::value  __getitem__(Feature const& feature, std::string const& name)
{
    return feature.get(name);
}

void __setitem__(Feature & feature, std::string const& name, mapnik::value const& val)
{
    feature.put(name,val);
}

feature_kv_iterator feature_kv_begin(Feature const& f)
{
    return feature_kv_iterator(f,true);
}

feature_kv_iterator feature_kv_end(Feature const& f)
{
    return feature_kv_iterator(f);
}

} // end anonymous namespace

namespace boost { namespace python {

struct mapnik_kv_to_python
{
    static PyObject* convert(feature_kv_iterator::value_type const& kv)
    {
        return boost::python::incref(boost::python::make_tuple(boost::get<0>(kv), boost::get<1>(kv)).ptr());
    }
};

// TODO  mapnik.Context : implement vector_indexing_suite - we don't want to expose internal key->index mapping 
// TODO  mapnik.Feature : implement map_indexing_suite 

}}

struct UnicodeString_from_python_str
{
    UnicodeString_from_python_str()
    {
        boost::python::converter::registry::push_back(
            &convertible,
            &construct,
            boost::python::type_id<UnicodeString>());
    }

    static void* convertible(PyObject* obj_ptr)
    {
        if (!(
#if PY_VERSION_HEX >= 0x03000000
                PyBytes_Check(obj_ptr)
#else
                PyString_Check(obj_ptr)
#endif
                || PyUnicode_Check(obj_ptr)))
            return 0;
        return obj_ptr;
    }

    static void construct(
        PyObject* obj_ptr,
        boost::python::converter::rvalue_from_python_stage1_data* data)
    {
        char * value=0;
        if (PyUnicode_Check(obj_ptr)) {
            PyObject *encoded = PyUnicode_AsEncodedString(obj_ptr, "utf8", "replace");
            if (encoded) {
#if PY_VERSION_HEX >= 0x03000000
                value = PyBytes_AsString(encoded);
#else
                value = PyString_AsString(encoded);
#endif
                Py_DecRef(encoded);
            }
        } else {
#if PY_VERSION_HEX >= 0x03000000
            value = PyBytes_AsString(obj_ptr);
#else
            value = PyString_AsString(obj_ptr);
#endif
        }
        if (value == 0) boost::python::throw_error_already_set();
        void* storage = (
            (boost::python::converter::rvalue_from_python_storage<UnicodeString>*)
            data)->storage.bytes;
        new (storage) UnicodeString(value);
        data->convertible = storage;
    }
};

void export_feature()
{
    using namespace boost::python;
    using mapnik::Feature;

    // Python to mapnik::value converters
    implicitly_convertible<int,mapnik::value>();
    implicitly_convertible<double,mapnik::value>();
    implicitly_convertible<UnicodeString,mapnik::value>();
    implicitly_convertible<bool,mapnik::value>();
    
    UnicodeString_from_python_str();
    to_python_converter<mapnik::feature_kv_iterator::value_type,mapnik_kv_to_python>();
    
    class_<context,context_ptr,boost::noncopyable>
        ("Context",init<>("Default ctor."))
        .def("push", &context::push)
        //.def("__iter__",iterator<context>())
        ;
    
    class_<Feature,boost::shared_ptr<Feature>,
           boost::noncopyable>("Feature",init<context_ptr,int>("Default ctor."))
        .def("id",&Feature::id)
        .def("__str__",&Feature::to_string)
        .def("add_geometries_from_wkb", &feature_add_geometries_from_wkb)
        .def("add_geometries_from_wkt", &feature_add_geometries_from_wkt)
        .def("add_geometry", &Feature::add_geometry)
        .def("num_geometries",&Feature::num_geometries)
        .def("get_geometry", make_function(get_geom1,return_value_policy<reference_existing_object>()))
        .def("geometries",make_function(&Feature::paths,return_value_policy<reference_existing_object>()))
        .def("envelope", &Feature::envelope)
        .def("has_key", &Feature::has_key)
        .def("__setitem__",&__setitem__)
        .def("__getitem__",&__getitem__)
        .def("__len__", &Feature::size)
        .def("context",&Feature::context)
        ;
}<commit_msg>+ cleanup + add __getitem__ back<commit_after>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 <NAME>, <NAME>
 *
 * 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
 *
 *****************************************************************************/
//$Id$

// boost

#include <boost/python/suite/indexing/indexing_suite.hpp>
//#include <boost/python/suite/indexing/map_indexing_suite.hpp>

#include <boost/python/iterator.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/to_python_converter.hpp>
#include <boost/python.hpp>
#include <boost/scoped_array.hpp>

// mapnik
#include <mapnik/feature.hpp>
#include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/wkb.hpp>
#include <mapnik/wkt/wkt_factory.hpp>

mapnik::geometry_type & (mapnik::Feature::*get_geom1)(unsigned) = &mapnik::Feature::get_geometry;

namespace {

using mapnik::Feature;
using mapnik::geometry_utils;
using mapnik::from_wkt;
using mapnik::context;
using mapnik::context_ptr;
using mapnik::feature_kv_iterator;

void feature_add_geometries_from_wkb(Feature &feature, std::string wkb)
{
    geometry_utils::from_wkb(feature.paths(), wkb.c_str(), wkb.size());
}

void feature_add_geometries_from_wkt(Feature &feature, std::string wkt)
{
    bool result = mapnik::from_wkt(wkt, feature.paths());
    if (!result) throw std::runtime_error("Failed to parse WKT");
}

mapnik::value  __getitem__(Feature const& feature, std::string const& name)
{
    return feature.get(name);
}

void __setitem__(Feature & feature, std::string const& name, mapnik::value const& val)
{
    feature.put(name,val);
}

} // end anonymous namespace

namespace boost { namespace python {

}}

struct UnicodeString_from_python_str
{
    UnicodeString_from_python_str()
    {
        boost::python::converter::registry::push_back(
            &convertible,
            &construct,
            boost::python::type_id<UnicodeString>());
    }

    static void* convertible(PyObject* obj_ptr)
    {
        if (!(
#if PY_VERSION_HEX >= 0x03000000
                PyBytes_Check(obj_ptr)
#else
                PyString_Check(obj_ptr)
#endif
                || PyUnicode_Check(obj_ptr)))
            return 0;
        return obj_ptr;
    }

    static void construct(
        PyObject* obj_ptr,
        boost::python::converter::rvalue_from_python_stage1_data* data)
    {
        char * value=0;
        if (PyUnicode_Check(obj_ptr)) {
            PyObject *encoded = PyUnicode_AsEncodedString(obj_ptr, "utf8", "replace");
            if (encoded) {
#if PY_VERSION_HEX >= 0x03000000
                value = PyBytes_AsString(encoded);
#else
                value = PyString_AsString(encoded);
#endif
                Py_DecRef(encoded);
            }
        } else {
#if PY_VERSION_HEX >= 0x03000000
            value = PyBytes_AsString(obj_ptr);
#else
            value = PyString_AsString(obj_ptr);
#endif
        }
        if (value == 0) boost::python::throw_error_already_set();
        void* storage = (
            (boost::python::converter::rvalue_from_python_storage<UnicodeString>*)
            data)->storage.bytes;
        new (storage) UnicodeString(value);
        data->convertible = storage;
    }
};

void export_feature()
{
    using namespace boost::python;
    using mapnik::Feature;

    // Python to mapnik::value converters
    implicitly_convertible<int,mapnik::value>();
    implicitly_convertible<double,mapnik::value>();
    implicitly_convertible<UnicodeString,mapnik::value>();
    implicitly_convertible<bool,mapnik::value>();
    
    UnicodeString_from_python_str();
    
    class_<context,context_ptr,boost::noncopyable>
        ("Context",init<>("Default ctor."))
        .def("push", &context::push)
        ;
    
    class_<Feature,boost::shared_ptr<Feature>,
           boost::noncopyable>("Feature",init<context_ptr,int>("Default ctor."))
        .def("id",&Feature::id)
        .def("__str__",&Feature::to_string)
        .def("add_geometries_from_wkb", &feature_add_geometries_from_wkb)
        .def("add_geometries_from_wkt", &feature_add_geometries_from_wkt)
        .def("add_geometry", &Feature::add_geometry)
        .def("num_geometries",&Feature::num_geometries)
        .def("get_geometry", make_function(get_geom1,return_value_policy<reference_existing_object>()))
        .def("geometries",make_function(&Feature::paths,return_value_policy<reference_existing_object>()))
        .def("envelope", &Feature::envelope)
        .def("has_key", &Feature::has_key)
        .def("__setitem__",&__setitem__)
         .def("__getitem__",&__getitem__)
        .def("__len__", &Feature::size)
        .def("context",&Feature::context)
        ;
}
{'fraction_non_alphanumeric': 0.14775252093659202, 'fraction_numerical': 0.008887369680396513, 'mean_word_length': 3.8640897755610975, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4534237', 'n_tokens_mistral': 3693, 'n_tokens_neox': 3604, 'n_words': 736}