pool-py.cc 1.48 KB
Newer Older
1 2 3 4
// Copyright 2011, 2012, Florent Lamiraux, LAAS-CNRS.

#include <Python.h>
#include <dynamic-graph/pool.h>
5 6
#include <dynamic-graph/entity.h>
#include <vector>
7 8
#include "exception.hh"

Guilhem Saurel's avatar
Guilhem Saurel committed
9 10
#include "dynamic-graph-py.hh"

11
namespace dynamicgraph {
Guilhem Saurel's avatar
format  
Guilhem Saurel committed
12
namespace python {
13

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
14
namespace pool {
15

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
16 17 18 19 20 21 22 23 24
PyObject* writeGraph(PyObject* /*self*/, PyObject* args) {
  char* filename;
  if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
  try {
    PoolStorage::getInstance()->writeGraph(filename);
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
25

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
26 27 28 29 30
/**
   \brief Get list of entities
*/
PyObject* getEntityList(PyObject* /*self*/, PyObject* args) {
  if (!PyArg_ParseTuple(args, "")) return NULL;
31

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
32 33 34
  std::vector<std::string> entityNames;
  try {
    const PoolStorage::Entities& listOfEntities = dynamicgraph::PoolStorage::getInstance()->getEntityMap();
35

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
36 37 38
    Py_ssize_t classNumber = listOfEntities.size();
    // Build a tuple object
    PyObject* classTuple = PyTuple_New(classNumber);
39

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
40 41 42 43
    Py_ssize_t iEntity = 0;
    for (PoolStorage::Entities::const_iterator entity_it = listOfEntities.begin(); entity_it != listOfEntities.end();
         ++entity_it) {
      const std::string& aname = entity_it->second->getName();
44

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
45 46 47 48 49 50 51 52 53
      PyObject* className = Py_BuildValue("s", aname.c_str());
      PyTuple_SetItem(classTuple, iEntity, className);
      iEntity++;
    }
    return Py_BuildValue("O", classTuple);
  }
  CATCH_ALL_EXCEPTIONS();
  return NULL;
}
54

Guilhem Saurel's avatar
format  
Guilhem Saurel committed
55 56 57
}  // namespace pool
}  // namespace python
}  // namespace dynamicgraph