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"

9
namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
10
namespace python {
11

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
12
13
extern PyObject* dgpyError;
namespace pool {
14

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
15
16
17
18
19
20
21
22
23
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("");
}
24

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

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

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
39
40
41
42
    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();
43

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

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