attach annotations property to seq_span
[mussa.git] / py / annotations.cpp
index 4c0c53cca80a34ebd88019a079d8c25be5952b8c..14180899c6e8ad1a2d6f29534843c425db021f2a 100644 (file)
@@ -8,24 +8,44 @@
 using namespace boost::python;
 
 #include "alg/annotations.hpp"
-
 #include "stl_container_adapter.hpp"
 
+#include <stdexcept>
+
+typedef std::pair<std::string const, std::string> pair_string;
+
+struct pair_string_to_tuple 
+{
+  static PyObject * convert(pair_string const &p) {
+    PyObject *key = PyString_FromString(p.first.c_str());
+    PyObject *value = PyString_FromString(p.second.c_str());
+    PyObject *tuple = PyTuple_New(2);
+    if (key == 0 or value == 0 or tuple == 0) 
+      throw std::runtime_error("Allocation Error");
+
+    PyTuple_SetItem(tuple, 0, key);
+    PyTuple_SetItem(tuple, 1, value);
+    return tuple;
+  }
+};
+
 void export_annotations()
 {
+  to_python_converter<pair_string, pair_string_to_tuple>();
 
-  class_<Annotations>("annotations", init<std::string>())
+  class_<Annotations>("Annotations", init<std::string>())
     .add_property("name", &Annotations::name, &Annotations::setName)
     .def("__contains__", &map_item<Annotations>::in)
     .def("__delitem__", &map_item<Annotations>::del)
     .def("__getitem__", &map_item<Annotations>::get,
         return_value_policy<copy_non_const_reference>())
     .def("__setitem__", &map_item<Annotations>::set)
+    .def("__iter__", iterator<Annotations>())
     .def("__len__", &Annotations::size)
     .def("keys", &map_item<Annotations>::keys)
     .def("values", &map_item<Annotations>::values)
     .def("items", &map_item<Annotations>::items)
-    ;
+  ;
 
-  //register_ptr_to_python< AnnotationsRef >();
+  register_ptr_to_python< AnnotationsRef >();
 }