dynamic-graph-py.cc 5.1 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>

11
#include <dynamic-graph/debug.h>
12
#include <dynamic-graph/exception-factory.h>
florent's avatar
florent committed
13
#include <dynamic-graph/signal-base.h>
florent's avatar
florent committed
14

15
16
namespace dynamicgraph {
  namespace python {
17

18
19
20
21
    // Declare functions defined in other source files
    namespace signalBase {
      extern PyObject* create(PyObject* self, PyObject* args);
      extern PyObject* getTime(PyObject* self, PyObject* args);
florent's avatar
florent committed
22
23
      extern PyObject* getValue(PyObject* self, PyObject* args);
      extern PyObject* setValue(PyObject* self, PyObject* args);
24
25
26
27
28
29
    }
    namespace entity {
      extern PyObject* create(PyObject* self, PyObject* args);
      extern PyObject* getName(PyObject* self, PyObject* args);
      extern PyObject* getSignal(PyObject* self, PyObject* args);
      extern PyObject* displaySignals(PyObject* self, PyObject* args);
florent's avatar
florent committed
30
      extern PyObject* executeCommand(PyObject* self, PyObject* args);
31
      extern PyObject* listCommands(PyObject* self, PyObject* args);
32
      extern PyObject* getCommandDocstring(PyObject* self, PyObject* args);
33
34
    }

florent's avatar
florent committed
35
36
37
    namespace factory {
      PyObject* getEntityClassList(PyObject* self, PyObject* args);
    }
38
39
40
41
42
43
44
45
    PyObject* error;

    /**
       \brief plug a signal into another one.
    */
    PyObject*
    plug(PyObject* self, PyObject* args)
    {
florent's avatar
florent committed
46
47
48
49
50
51
52
53
54
55
56
      PyObject* objOut = NULL;
      PyObject* objIn = NULL;
      void* pObjOut;
      void* pObjIn;

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

      if (!PyCObject_Check(objOut))
	return NULL;
      if (!PyCObject_Check(objIn))
57
58
	return NULL;

florent's avatar
florent committed
59
60
61
62
      pObjIn = PyCObject_AsVoidPtr(objIn);
	SignalBase<int>* signalIn = (SignalBase<int>*)pObjIn;
      pObjOut = PyCObject_AsVoidPtr(objOut);
	SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut;
63
      std::ostringstream os;
florent's avatar
florent committed
64

65
      try {
florent's avatar
florent committed
66
	signalIn->plug(signalOut);
florent's avatar
florent committed
67
68
      } catch (std::exception& exc) {
	PyErr_SetString(error, exc.what());
69
70
71
72
73
74
75
76
	return NULL;
      }
      return Py_BuildValue("");
    }

    PyObject*
    enableTrace(PyObject* self, PyObject* args)
    {
77
      PyObject* boolean;
78
79
      char* filename = NULL;

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
      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 (boolean == Py_True) {
	  try {
	    DebugTrace::openFile(filename);
	  } catch (const std::exception& exc) {
	    PyErr_SetString(PyExc_IOError, exc.what());
	    return NULL;
	  }
	} else {
	  try {
	    DebugTrace::closeFile(filename);
	  } catch (const std::exception& exc) {
	    PyErr_SetString(PyExc_IOError, exc.what());
	    return NULL;
	  }
	}
102
103
104
105
106
107
      } else {
	return NULL;
      }
      return Py_BuildValue("");
    }
  }
florent's avatar
florent committed
108
109
110
111
112
}

/**
   \brief List of python functions
*/
113
static PyMethodDef dynamicGraphMethods[] = {
florent's avatar
florent committed
114
  {"w_plug",  dynamicgraph::python::plug, METH_VARARGS,
115
   "plug an output signal into an input signal"},
116
  {"enableTrace",  dynamicgraph::python::enableTrace, METH_VARARGS,
117
118
119
120
   "Enable or disable tracing debug info in a file"},
  // Signals
  {"create_signal_base", dynamicgraph::python::signalBase::create, METH_VARARGS,
   "create a SignalBase C++ object"},
florent's avatar
florent committed
121
  {"signal_base_get_time", dynamicgraph::python::signalBase::getTime,
122
   METH_VARARGS, "Get time of  a SignalBase"},
florent's avatar
florent committed
123
124
125
126
  {"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"},
127
128
129
130
131
132
133
134
135
136
  // Entity
  {"create_entity", dynamicgraph::python::entity::create, METH_VARARGS,
   "create an Entity C++ object"},
  {"entity_get_name", dynamicgraph::python::entity::getName, METH_VARARGS,
   "get the name of an Entity"},
  {"entity_get_signal", dynamicgraph::python::entity::getSignal, METH_VARARGS,
   "get signal by name from an Entity"},
  {"entity_display_signals", dynamicgraph::python::entity::displaySignals,
   METH_VARARGS,
   "Display the list of signals of an entity in standard output"},
florent's avatar
florent committed
137
138
139
140
  {"entity_execute_command",
   dynamicgraph::python::entity::executeCommand,
   METH_VARARGS,
   "execute a command"},
141
142
143
144
  {"entity_list_commands",
   dynamicgraph::python::entity::listCommands,
   METH_VARARGS,
   "list the commands of an entity"},
145
146
147
148
  {"entity_get_command_docstring",
   dynamicgraph::python::entity::getCommandDocstring,
   METH_VARARGS,
   "get the docstring of an entity command"},
florent's avatar
florent committed
149
150
151
152
  {"factory_get_entity_class_list",
   dynamicgraph::python::factory::getEntityClassList,
   METH_VARARGS,
   "return the list of entity classes"},
florent's avatar
florent committed
153
154
155
156
157
158
159
160
  {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initwrap(void)
{
    PyObject *m;

161
    m = Py_InitModule("wrap", dynamicGraphMethods);
florent's avatar
florent committed
162
163
    if (m == NULL)
        return;
164

florent's avatar
florent committed
165
    std::string msg("dynamic_graph.error");
florent's avatar
florent committed
166

167
    dynamicgraph::python::error =
florent's avatar
florent committed
168
      PyErr_NewException(const_cast<char*>(msg.c_str()), NULL, NULL);
169
170
    Py_INCREF(dynamicgraph::python::error);
    PyModule_AddObject(m, "error", dynamicgraph::python::error);
florent's avatar
florent committed
171
}