dynamic-graph-py.cc 4.41 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/exception-factory.h>
12
#include <dynamic-graph/interpreter-helper.h>
13
#include <dynamic-graph/functions.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
32
    }

florent's avatar
florent committed
33
34
35
    namespace factory {
      PyObject* getEntityClassList(PyObject* self, PyObject* args);
    }
36
37
    PyObject* error;

38
    static dynamicgraph::InterpreterHelper interpreter;
39
40
41
42
43
44
45

    /**
       \brief plug a signal into another one.
    */
    PyObject*
    plug(PyObject* self, PyObject* args)
    {
46
47
48
49
      char* objOut = NULL;
      char* objIn = NULL;
      char* sigOut = NULL;
      char* sigIn = NULL;
50
      if (!PyArg_ParseTuple(args,"ssss", &objIn, &sigIn, &objOut, &sigOut))
51
52
53
54
	return NULL;

      std::ostringstream os;
      try {
55
	interpreter.cmdPlug(objIn, sigIn, objOut, sigOut, os);
56
57
58
59
      } catch (dynamicgraph::ExceptionFactory& exc) {
	PyErr_SetString(error, exc.getStringMessage().c_str());
	return NULL;
      }
florent's avatar
florent committed
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      return Py_BuildValue("");
    }

    PyObject*
    enableTrace(PyObject* self, PyObject* args)
    {
      char* trueFalse = NULL;
      char* filename = NULL;
      std::stringstream ss;
      std::ostringstream os;

      if (PyArg_ParseTuple(args,"ss", &trueFalse, &filename)) {
	ss << std::string(trueFalse) << " " << std::string(filename);
      } else if (PyArg_ParseTuple(args,"s", &trueFalse)) {
	ss << std::string(trueFalse);
      } else {
	return NULL;
      }

      std::istringstream cmdArg(ss.str());
      try {
	ShellFunctions::cmdEnableTrace(std::string("debugtrace"), cmdArg, os);
      } catch (dynamicgraph::ExceptionFactory& exc) {
	PyErr_SetString(error, exc.getStringMessage().c_str());
	return NULL;
      }

      return Py_BuildValue("");
    }
  }
florent's avatar
florent committed
91
92
93
94
95
}

/**
   \brief List of python functions
*/
96
static PyMethodDef dynamicGraphMethods[] = {
florent's avatar
florent committed
97
  {"w_plug",  dynamicgraph::python::plug, METH_VARARGS,
98
   "plug an output signal into an input signal"},
florent's avatar
florent committed
99
  {"w_debugtrace",  dynamicgraph::python::enableTrace, METH_VARARGS,
100
101
102
103
104
105
   "Enable or disable tracing debug info in a file"},
  // Signals
  {"create_signal_base", dynamicgraph::python::signalBase::create, METH_VARARGS,
   "create a SignalBase C++ object"},
  {"signalbase_get_time", dynamicgraph::python::signalBase::getTime,
   METH_VARARGS, "Get time of  a SignalBase"},
florent's avatar
florent committed
106
107
108
109
  {"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"},
110
111
112
113
114
115
116
117
118
119
  // 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
120
121
122
123
  {"entity_execute_command",
   dynamicgraph::python::entity::executeCommand,
   METH_VARARGS,
   "execute a command"},
florent's avatar
florent committed
124
125
126
127
  {"factory_get_entity_class_list",
   dynamicgraph::python::factory::getEntityClassList,
   METH_VARARGS,
   "return the list of entity classes"},
florent's avatar
florent committed
128
129
130
131
132
133
134
135
  {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initwrap(void)
{
    PyObject *m;

136
    m = Py_InitModule("wrap", dynamicGraphMethods);
florent's avatar
florent committed
137
138
    if (m == NULL)
        return;
139

140
141
142
143
    dynamicgraph::python::error =
      PyErr_NewException("dynamic_graph.wrap.error", NULL, NULL);
    Py_INCREF(dynamicgraph::python::error);
    PyModule_AddObject(m, "error", dynamicgraph::python::error);
florent's avatar
florent committed
144
}