dynamic-graph-py.cc 1.25 KB
Newer Older
florent's avatar
florent committed
1
2
3
4
5
6
7
/*
 *  Copyright 2010 (C) CNRS
 *  Author: Florent Lamiraux
 */

#include <Python.h>
#include <iostream>
florent's avatar
florent committed
8
#include <sstream>
florent's avatar
florent committed
9
10
#include <string>

florent's avatar
florent committed
11
#include <dynamic-graph/interpreter.h>
florent's avatar
florent committed
12

13
14
static PyObject* error;

florent's avatar
florent committed
15
static dynamicgraph::Interpreter interpreter;
florent's avatar
florent committed
16
17
18
19
20
21
22
23
24

static PyObject*
plug(PyObject* self, PyObject* args)
{
  char* out = NULL;
  char* in = NULL;
  if (!PyArg_ParseTuple(args,"ss", &out, &in))
    return NULL;

florent's avatar
florent committed
25
26
27
28
  std::stringstream ss;
  std::ostringstream os;
  ss << std::string(out) << " " << std::string(in);
  std::istringstream cmdArg(ss.str());
29
30
31
32
33
34
  try {
    interpreter.cmdPlug(std::string("plug"), cmdArg, os);
  } catch (dynamicgraph::ExceptionFactory& exc) {
    PyErr_SetString(error, exc.getStringMessage().c_str());
    return NULL;
  }
florent's avatar
florent committed
35

florent's avatar
florent committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  return Py_BuildValue("");
}

/**
   \brief List of python functions
*/
static PyMethodDef sotTutorialMethods[] = {
  {"plug",  plug, METH_VARARGS,
     "plug an output signal into an input signal"},
  {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initwrap(void)
{
    PyObject *m;

    m = Py_InitModule("wrap", sotTutorialMethods);
    if (m == NULL)
        return;
56
57
58
59

    error = PyErr_NewException("dynamic_graph.wrap.error", NULL, NULL);
    Py_INCREF(error);
    PyModule_AddObject(m, "error", error);
florent's avatar
florent committed
60
}