dynamic-graph-py.cc 12.4 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
florent's avatar
florent committed
2
3
4

#include <Python.h>
#include <iostream>
florent's avatar
florent committed
5
#include <sstream>
florent's avatar
florent committed
6
7
#include <string>

8
#include <dynamic-graph/debug.h>
9
#include <dynamic-graph/exception-factory.h>
florent's avatar
florent committed
10
#include <dynamic-graph/signal-base.h>
florent's avatar
florent committed
11

12
#include "exception.hh"
13
#include "signal-wrapper.hh"
14

15
namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
namespace python {

// Declare functions defined in other source files
namespace signalBase {
extern PyObject* create(PyObject* self, PyObject* args);
extern PyObject* createSignalWrapper(PyObject* self, PyObject* args);
extern PyObject* getTime(PyObject* self, PyObject* args);
extern PyObject* setTime(PyObject* self, PyObject* args);
extern PyObject* getName(PyObject* self, PyObject* args);
extern PyObject* getClassName(PyObject* self, PyObject* args);
extern PyObject* display(PyObject* self, PyObject* args);
extern PyObject* displayDependencies(PyObject* self, PyObject* args);
extern PyObject* getValue(PyObject* self, PyObject* args);
extern PyObject* setValue(PyObject* self, PyObject* args);
extern PyObject* recompute(PyObject* self, PyObject* args);
extern PyObject* unplug(PyObject* self, PyObject* args);
extern PyObject* isPlugged(PyObject* self, PyObject* args);
extern PyObject* getPlugged(PyObject* self, PyObject* args);
}  // namespace signalBase
namespace entity {
extern PyObject* create(PyObject* self, PyObject* args);
extern PyObject* display(PyObject* self, PyObject* args);
extern PyObject* display(PyObject* self, PyObject* args);
extern PyObject* getName(PyObject* self, PyObject* args);
extern PyObject* getClassName(PyObject* self, PyObject* args);
extern PyObject* hasSignal(PyObject* self, PyObject* args);
extern PyObject* getSignal(PyObject* self, PyObject* args);
extern PyObject* listSignals(PyObject* self, PyObject* args);
extern PyObject* executeCommand(PyObject* self, PyObject* args);
extern PyObject* listCommands(PyObject* self, PyObject* args);
extern PyObject* getCommandDocstring(PyObject* self, PyObject* args);
extern PyObject* getDocString(PyObject* self, PyObject* args);
extern PyObject* setLoggerVerbosityLevel(PyObject* self, PyObject* args);
extern PyObject* getLoggerVerbosityLevel(PyObject* self, PyObject* args);
extern PyObject* setTimeSample(PyObject* self, PyObject* args);
extern PyObject* getTimeSample(PyObject* self, PyObject* args);
extern PyObject* setStreamPrintPeriod(PyObject* self, PyObject* args);
extern PyObject* getStreamPrintPeriod(PyObject* self, PyObject* args);
}  // namespace entity

namespace factory {
extern PyObject* getEntityClassList(PyObject* self, PyObject* args);
}
namespace signalCaster {
extern PyObject* getSignalTypeList(PyObject* self, PyObject* args);
}
namespace pool {
extern PyObject* writeGraph(PyObject* self, PyObject* args);
extern PyObject* getEntityList(PyObject* self, PyObject* args);
}  // namespace pool
namespace debug {
extern PyObject* addLoggerFileOutputStream(PyObject* self, PyObject* args);
extern PyObject* addLoggerCoutOutputStream(PyObject* self, PyObject* args);
extern PyObject* closeLoggerFileOutputStream(PyObject* self, PyObject* args);
extern PyObject* realTimeLoggerSpinOnce(PyObject* self, PyObject* args);
extern PyObject* realTimeLoggerDestroy(PyObject* self, PyObject* args);
extern PyObject* realTimeLoggerInstance(PyObject* self, PyObject* args);
}  // namespace debug

75
76
77
struct module_state {
  PyObject* dgpyError;
};
78

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
79
80
81
82
83
84
85
86
87
88
89
/**
   \brief plug a signal into another one.
*/
PyObject* plug(PyObject* /*self*/, PyObject* args) {
  PyObject* objOut = NULL;
  PyObject* objIn = NULL;
  void* pObjOut;
  void* pObjIn;

  if (!PyArg_ParseTuple(args, "OO", &objOut, &objIn)) return NULL;

90
  if (!PyCapsule_CheckExact(objOut)) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
91
92
93
94
95
    PyErr_SetString(PyExc_TypeError,
                    "first argument should be a pointer to"
                    " signalBase<int>.");
    return NULL;
  }
96
  if (!PyCapsule_CheckExact(objIn)) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
97
98
99
100
101
    PyErr_SetString(PyExc_TypeError,
                    "second argument should be a pointer to"
                    " signalBase<int>.");
    return NULL;
  }
102

103
  pObjIn = PyCapsule_GetPointer(objIn, "dynamic_graph.Signal");
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
104
  SignalBase<int>* signalIn = (SignalBase<int>*)pObjIn;
105
  pObjOut = PyCapsule_GetPointer(objOut, "dynamic_graph.Signal");
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
106
107
  SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut;
  std::ostringstream os;
florent's avatar
florent committed
108

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
109
110
111
112
113
114
  try {
    signalIn->plug(signalOut);
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
florent's avatar
florent committed
115

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
PyObject* enableTrace(PyObject* /*self*/, PyObject* args) {
  PyObject* boolean;
  char* filename = NULL;

  if (PyArg_ParseTuple(args, "Os", &boolean, &filename)) {
    if (!PyBool_Check(boolean)) {
      PyErr_SetString(PyExc_TypeError,
                      "enableTrace takes as first "
                      "argument True or False,\n"
                      "           and as "
                      "second argument a filename.");
      return NULL;
    }
    if (PyObject_IsTrue(boolean)) {
      try {
        DebugTrace::openFile(filename);
132
      }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
133
134
      CATCH_ALL_EXCEPTIONS();
    } else {
135
      try {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
136
        DebugTrace::closeFile(filename);
137
      }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
138
      CATCH_ALL_EXCEPTIONS();
139
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
140
141
  } else {
    return NULL;
142
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
143
  return Py_BuildValue("");
florent's avatar
florent committed
144
}
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct dynamicgraph::python::module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&dynamicgraph::python::_state)
static struct dynamicgraph::python::module_state _state;
#endif

static PyObject *
#if PY_MAJOR_VERSION >= 3
  error_out(PyObject* m, PyObject*) {
    struct module_state *st = GETSTATE(m);
#else
  error_out(PyObject*, PyObject*) {
    struct module_state *st = &dynamicgraph::python::_state;
#endif
    PyErr_SetString(st->dgpyError, "something bad happened");
    return NULL;
  }

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
164
165
}  // namespace python
}  // namespace dynamicgraph
florent's avatar
florent committed
166
167
168
169

/**
   \brief List of python functions
*/
170
static PyMethodDef dynamicGraphMethods[] = {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    {"w_plug", dynamicgraph::python::plug, METH_VARARGS, "plug an output signal into an input signal"},
    {"enableTrace", dynamicgraph::python::enableTrace, METH_VARARGS, "Enable or disable tracing debug info in a file"},
    // Signals
    {"create_signal_base", dynamicgraph::python::signalBase::create, METH_VARARGS, "create a SignalBase C++ object"},
    {"create_signal_wrapper", dynamicgraph::python::signalBase::createSignalWrapper, METH_VARARGS,
     "create a SignalWrapper C++ object"},
    {"signal_base_get_time", dynamicgraph::python::signalBase::getTime, METH_VARARGS, "Get time of  a SignalBase"},
    {"signal_base_set_time", dynamicgraph::python::signalBase::setTime, METH_VARARGS, "Set time of  a SignalBase"},
    {"signal_base_get_name", dynamicgraph::python::signalBase::getName, METH_VARARGS, "Get the name of a signal"},
    {"signal_base_get_class_name", dynamicgraph::python::signalBase::getClassName, METH_VARARGS,
     "Get the class name of a signal"},
    {"signal_base_display", dynamicgraph::python::signalBase::display, METH_VARARGS, "Print the signal in a string"},
    {"signal_base_display_dependencies", dynamicgraph::python::signalBase::displayDependencies, METH_VARARGS,
     "Print the signal dependencies in a string"},
    {"signal_base_get_value", dynamicgraph::python::signalBase::getValue, METH_VARARGS, "Read the value of a signal"},
    {"signal_base_set_value", dynamicgraph::python::signalBase::setValue, METH_VARARGS, "Set the value of a signal"},
    {"signal_base_recompute", dynamicgraph::python::signalBase::recompute, METH_VARARGS,
     "Recompute the signal at given time"},
    {"signal_base_unplug", dynamicgraph::python::signalBase::unplug, METH_VARARGS, "Unplug the signal"},
    {"signal_base_isPlugged", dynamicgraph::python::signalBase::isPlugged, METH_VARARGS,
     "Whether the signal is plugged"},
    {"signal_base_getPlugged", dynamicgraph::python::signalBase::getPlugged, METH_VARARGS,
     "To which signal the signal is plugged"},
    // Entity
    {"create_entity", dynamicgraph::python::entity::create, METH_VARARGS, "create an Entity C++ object"},
    {"display_entity", dynamicgraph::python::entity::display, METH_VARARGS, "print an Entity C++ object"},
    {"entity_get_name", dynamicgraph::python::entity::getName, METH_VARARGS, "get the name of an Entity"},
    {"entity_get_class_name", dynamicgraph::python::entity::getClassName, METH_VARARGS,
     "get the class name of an Entity"},
    {"entity_has_signal", dynamicgraph::python::entity::hasSignal, METH_VARARGS,
     "return True if the entity has a signal with the given name"},
    {"entity_get_signal", dynamicgraph::python::entity::getSignal, METH_VARARGS, "get signal by name from an Entity"},
    {"entity_list_signals", dynamicgraph::python::entity::listSignals, METH_VARARGS,
     "Return the list of signals of an entity."},
    {"entity_execute_command", dynamicgraph::python::entity::executeCommand, METH_VARARGS, "execute a command"},
    {"entity_list_commands", dynamicgraph::python::entity::listCommands, METH_VARARGS,
     "list the commands of an entity"},
    {"entity_get_command_docstring", dynamicgraph::python::entity::getCommandDocstring, METH_VARARGS,
     "get the docstring of an entity command"},
    {"entity_get_docstring", dynamicgraph::python::entity::getDocString, METH_VARARGS,
     "get the doc string of an entity type"},
    {"factory_get_entity_class_list", dynamicgraph::python::factory::getEntityClassList, METH_VARARGS,
     "return the list of entity classes"},
    {"signal_caster_get_type_list", dynamicgraph::python::signalCaster::getSignalTypeList, METH_VARARGS,
     "return the list of signal type names"},
    {"writeGraph", dynamicgraph::python::pool::writeGraph, METH_VARARGS, "Write the graph of entities in a filename."},
    {"get_entity_list", dynamicgraph::python::pool::getEntityList, METH_VARARGS,
     "return the list of instanciated entities"},
    {"entity_set_logger_verbosity", dynamicgraph::python::entity::setLoggerVerbosityLevel, METH_VARARGS,
     "set the verbosity level of the entity"},
    {"entity_get_logger_verbosity", dynamicgraph::python::entity::getLoggerVerbosityLevel, METH_VARARGS,
     "get the verbosity level of the entity"},
    {"addLoggerFileOutputStream", dynamicgraph::python::debug::addLoggerFileOutputStream, METH_VARARGS,
     "add a output file stream to the logger by filename"},
    {"addLoggerCoutOutputStream", dynamicgraph::python::debug::addLoggerCoutOutputStream, METH_VARARGS,
     "add std::cout as output stream to the logger"},
    {"closeLoggerFileOutputStream", dynamicgraph::python::debug::closeLoggerFileOutputStream, METH_VARARGS,
     "close all the loggers file output streams."},
    {"entity_set_time_sample", dynamicgraph::python::entity::setTimeSample, METH_VARARGS,
     "set the time sample for printing debugging information"},
    {"entity_get_time_sample", dynamicgraph::python::entity::getTimeSample, METH_VARARGS,
     "get the time sample for printing debugging information"},
    {"entity_set_stream_print_period", dynamicgraph::python::entity::setStreamPrintPeriod, METH_VARARGS,
     "set the period at which debugging information are printed"},
    {"entity_get_stream_print_period", dynamicgraph::python::entity::getStreamPrintPeriod, METH_VARARGS,
     "get the period at which debugging information are printed"},
    {"real_time_logger_destroy", dynamicgraph::python::debug::realTimeLoggerDestroy, METH_VARARGS,
     "Destroy the real time logger."},
    {"real_time_logger_spin_once", dynamicgraph::python::debug::realTimeLoggerSpinOnce, METH_VARARGS,
     "Destroy the real time logger."},
    {"real_time_logger_instance", dynamicgraph::python::debug::realTimeLoggerInstance, METH_VARARGS,
     "Starts the real time logger."},
243
    {"error_out", (PyCFunction)dynamicgraph::python::error_out, METH_NOARGS, NULL},
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
244
    {NULL, NULL, 0, NULL} /* Sentinel */
florent's avatar
florent committed
245
246
};

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#if PY_MAJOR_VERSION >= 3

static struct PyModuleDef moduledef = {
  PyModuleDef_HEAD_INIT,
  "dynamic_graph",
  NULL,
  sizeof(struct dynamicgraph::python::module_state),
  dynamicGraphMethods,
  NULL,
  NULL,
  NULL,
  NULL
};

#define INITERROR return NULL

  PyMODINIT_FUNC
PyInit_dynamic_graph(void)
florent's avatar
florent committed
265

266
267
#else
#define INITERROR return
268

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  void
initdynamic_graph(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
  PyObject *module = PyModule_Create(&moduledef);
#else
  PyObject *module = Py_InitModule("dynamic_graph", dynamicGraphMethods);
#endif

  if (module == NULL)
    INITERROR;
  struct dynamicgraph::python::module_state *st = GETSTATE(module);

  st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
  if (st->dgpyError == NULL) {
    Py_DECREF(module);
    INITERROR;
  }
florent's avatar
florent committed
288

289
290
291
#if PY_MAJOR_VERSION >= 3
  return module;
#endif
florent's avatar
florent committed
292
}