|
4 | 4 | #include <iostream> |
5 | 5 | #include <sstream> |
6 | 6 |
|
| 7 | +#include <boost/python.hpp> |
| 8 | + |
7 | 9 | #include <dynamic-graph/debug.h> |
8 | 10 | #include <dynamic-graph/exception-factory.h> |
9 | 11 | #include <dynamic-graph/signal-base.h> |
10 | 12 |
|
11 | 13 | #include "dynamic-graph/python/signal-wrapper.hh" |
12 | 14 |
|
| 15 | +namespace bp = boost::python; |
| 16 | + |
13 | 17 | namespace dynamicgraph { |
14 | 18 | namespace python { |
15 | 19 |
|
| 20 | +template <typename Iterator> |
| 21 | +inline bp::list to_py_list(Iterator begin, Iterator end) { |
| 22 | + typedef typename Iterator::value_type T; |
| 23 | + bp::list lst; |
| 24 | + std::for_each(begin, end, [&](const T& t) { lst.append(t); }); |
| 25 | + return lst; |
| 26 | +} |
| 27 | + |
| 28 | +template <typename Iterator> |
| 29 | +inline bp::tuple to_py_tuple(Iterator begin, Iterator end) { |
| 30 | + return bp::tuple(to_py_list(begin, end)); |
| 31 | +} |
| 32 | + |
| 33 | +template <typename T> |
| 34 | +inline std::vector<T> to_std_vector(const bp::object& iterable) { |
| 35 | + return std::vector<T>(bp::stl_input_iterator<T>(iterable), bp::stl_input_iterator<T>()); |
| 36 | +} |
| 37 | + |
| 38 | +void exposeSignals(); |
| 39 | + |
16 | 40 | // Declare functions defined in other source files |
17 | 41 | namespace signalBase { |
18 | | -PyObject* create(PyObject* self, PyObject* args); |
19 | | -PyObject* createSignalWrapper(PyObject* self, PyObject* args); |
20 | | -PyObject* getTime(PyObject* self, PyObject* args); |
21 | | -PyObject* setTime(PyObject* self, PyObject* args); |
22 | | -PyObject* getName(PyObject* self, PyObject* args); |
23 | | -PyObject* getClassName(PyObject* self, PyObject* args); |
24 | | -PyObject* display(PyObject* self, PyObject* args); |
25 | | -PyObject* displayDependencies(PyObject* self, PyObject* args); |
26 | | -PyObject* getValue(PyObject* self, PyObject* args); |
27 | | -PyObject* setValue(PyObject* self, PyObject* args); |
28 | | -PyObject* recompute(PyObject* self, PyObject* args); |
29 | | -PyObject* unplug(PyObject* self, PyObject* args); |
30 | | -PyObject* isPlugged(PyObject* self, PyObject* args); |
31 | | -PyObject* getPlugged(PyObject* self, PyObject* args); |
| 42 | +SignalBase<int>* createSignalWrapper(const char* name, const char* type, bp::object object); |
32 | 43 | } // namespace signalBase |
33 | 44 | namespace entity { |
34 | | -PyObject* create(PyObject* self, PyObject* args); |
35 | | -PyObject* display(PyObject* self, PyObject* args); |
36 | | -PyObject* display(PyObject* self, PyObject* args); |
37 | | -PyObject* getName(PyObject* self, PyObject* args); |
38 | | -PyObject* getClassName(PyObject* self, PyObject* args); |
39 | | -PyObject* hasSignal(PyObject* self, PyObject* args); |
40 | | -PyObject* getSignal(PyObject* self, PyObject* args); |
41 | | -PyObject* listSignals(PyObject* self, PyObject* args); |
42 | | -PyObject* executeCommand(PyObject* self, PyObject* args); |
43 | | -PyObject* listCommands(PyObject* self, PyObject* args); |
44 | | -PyObject* getCommandDocstring(PyObject* self, PyObject* args); |
45 | | -PyObject* getDocString(PyObject* self, PyObject* args); |
46 | | -PyObject* setLoggerVerbosityLevel(PyObject* self, PyObject* args); |
47 | | -PyObject* getLoggerVerbosityLevel(PyObject* self, PyObject* args); |
48 | | -PyObject* setTimeSample(PyObject* self, PyObject* args); |
49 | | -PyObject* getTimeSample(PyObject* self, PyObject* args); |
50 | | -PyObject* setStreamPrintPeriod(PyObject* self, PyObject* args); |
51 | | -PyObject* getStreamPrintPeriod(PyObject* self, PyObject* args); |
| 45 | + |
| 46 | +/// \param obj an Entity object |
| 47 | +void addCommands(boost::python::object obj); |
| 48 | +void addSignals(boost::python::object obj); |
| 49 | + |
| 50 | +Entity* create(const char* type, const char* name); |
| 51 | +bp::object executeCmd(bp::tuple args, bp::dict); |
52 | 52 | } // namespace entity |
53 | 53 |
|
54 | 54 | namespace factory { |
55 | | -PyObject* getEntityClassList(PyObject* self, PyObject* args); |
56 | | -} |
57 | | -namespace signalCaster { |
58 | | -PyObject* getSignalTypeList(PyObject* self, PyObject* args); |
| 55 | +bp::tuple getEntityClassList(); |
59 | 56 | } |
60 | 57 | namespace pool { |
61 | | -PyObject* writeGraph(PyObject* self, PyObject* args); |
62 | | -PyObject* getEntityList(PyObject* self, PyObject* args); |
| 58 | +void writeGraph(const char* filename); |
| 59 | +bp::list getEntityList(); |
| 60 | +const std::map<std::string, Entity*>* getEntityMap(); |
63 | 61 | } // namespace pool |
64 | 62 | namespace debug { |
65 | | -PyObject* addLoggerFileOutputStream(PyObject* self, PyObject* args); |
66 | | -PyObject* addLoggerCoutOutputStream(PyObject* self, PyObject* args); |
67 | | -PyObject* closeLoggerFileOutputStream(PyObject* self, PyObject* args); |
68 | | -PyObject* realTimeLoggerSpinOnce(PyObject* self, PyObject* args); |
69 | | -PyObject* realTimeLoggerDestroy(PyObject* self, PyObject* args); |
70 | | -PyObject* realTimeLoggerInstance(PyObject* self, PyObject* args); |
| 63 | +void addLoggerFileOutputStream(const char* filename); |
| 64 | +void addLoggerCoutOutputStream(); |
| 65 | +void closeLoggerFileOutputStream(); |
| 66 | +void realTimeLoggerSpinOnce(); |
| 67 | +void realTimeLoggerDestroy(); |
| 68 | +void realTimeLoggerInstance(); |
71 | 69 | } // namespace debug |
72 | 70 |
|
73 | | -struct module_state { |
74 | | - PyObject* dgpyError; |
75 | | -}; |
76 | | - |
77 | | -PyObject* plug(PyObject* /*self*/, PyObject* args); |
78 | | - |
79 | | -PyObject* enableTrace(PyObject* /*self*/, PyObject* args); |
80 | | - |
81 | | -PyObject* error_out( |
82 | | -#if PY_MAJOR_VERSION >= 3 |
83 | | - PyObject* m, PyObject* |
84 | | -#else |
85 | | - PyObject*, PyObject* |
86 | | -#endif |
87 | | -); |
88 | | - |
89 | | -/** |
90 | | - \brief List of python functions |
91 | | -*/ |
92 | | -__attribute__((unused)) static PyMethodDef dynamicGraphMethods[] = { |
93 | | - {"w_plug", dynamicgraph::python::plug, METH_VARARGS, "plug an output signal into an input signal"}, |
94 | | - {"enableTrace", dynamicgraph::python::enableTrace, METH_VARARGS, "Enable or disable tracing debug info in a file"}, |
95 | | - // Signals |
96 | | - {"create_signal_base", dynamicgraph::python::signalBase::create, METH_VARARGS, "create a SignalBase C++ object"}, |
97 | | - {"create_signal_wrapper", dynamicgraph::python::signalBase::createSignalWrapper, METH_VARARGS, |
98 | | - "create a SignalWrapper C++ object"}, |
99 | | - {"signal_base_get_time", dynamicgraph::python::signalBase::getTime, METH_VARARGS, "Get time of a SignalBase"}, |
100 | | - {"signal_base_set_time", dynamicgraph::python::signalBase::setTime, METH_VARARGS, "Set time of a SignalBase"}, |
101 | | - {"signal_base_get_name", dynamicgraph::python::signalBase::getName, METH_VARARGS, "Get the name of a signal"}, |
102 | | - {"signal_base_get_class_name", dynamicgraph::python::signalBase::getClassName, METH_VARARGS, |
103 | | - "Get the class name of a signal"}, |
104 | | - {"signal_base_display", dynamicgraph::python::signalBase::display, METH_VARARGS, "Print the signal in a string"}, |
105 | | - {"signal_base_display_dependencies", dynamicgraph::python::signalBase::displayDependencies, METH_VARARGS, |
106 | | - "Print the signal dependencies in a string"}, |
107 | | - {"signal_base_get_value", dynamicgraph::python::signalBase::getValue, METH_VARARGS, "Read the value of a signal"}, |
108 | | - {"signal_base_set_value", dynamicgraph::python::signalBase::setValue, METH_VARARGS, "Set the value of a signal"}, |
109 | | - {"signal_base_recompute", dynamicgraph::python::signalBase::recompute, METH_VARARGS, |
110 | | - "Recompute the signal at given time"}, |
111 | | - {"signal_base_unplug", dynamicgraph::python::signalBase::unplug, METH_VARARGS, "Unplug the signal"}, |
112 | | - {"signal_base_isPlugged", dynamicgraph::python::signalBase::isPlugged, METH_VARARGS, |
113 | | - "Whether the signal is plugged"}, |
114 | | - {"signal_base_getPlugged", dynamicgraph::python::signalBase::getPlugged, METH_VARARGS, |
115 | | - "To which signal the signal is plugged"}, |
116 | | - // Entity |
117 | | - {"create_entity", dynamicgraph::python::entity::create, METH_VARARGS, "create an Entity C++ object"}, |
118 | | - {"display_entity", dynamicgraph::python::entity::display, METH_VARARGS, "print an Entity C++ object"}, |
119 | | - {"entity_get_name", dynamicgraph::python::entity::getName, METH_VARARGS, "get the name of an Entity"}, |
120 | | - {"entity_get_class_name", dynamicgraph::python::entity::getClassName, METH_VARARGS, |
121 | | - "get the class name of an Entity"}, |
122 | | - {"entity_has_signal", dynamicgraph::python::entity::hasSignal, METH_VARARGS, |
123 | | - "return True if the entity has a signal with the given name"}, |
124 | | - {"entity_get_signal", dynamicgraph::python::entity::getSignal, METH_VARARGS, "get signal by name from an Entity"}, |
125 | | - {"entity_list_signals", dynamicgraph::python::entity::listSignals, METH_VARARGS, |
126 | | - "Return the list of signals of an entity."}, |
127 | | - {"entity_execute_command", dynamicgraph::python::entity::executeCommand, METH_VARARGS, "execute a command"}, |
128 | | - {"entity_list_commands", dynamicgraph::python::entity::listCommands, METH_VARARGS, |
129 | | - "list the commands of an entity"}, |
130 | | - {"entity_get_command_docstring", dynamicgraph::python::entity::getCommandDocstring, METH_VARARGS, |
131 | | - "get the docstring of an entity command"}, |
132 | | - {"entity_get_docstring", dynamicgraph::python::entity::getDocString, METH_VARARGS, |
133 | | - "get the doc string of an entity type"}, |
134 | | - {"factory_get_entity_class_list", dynamicgraph::python::factory::getEntityClassList, METH_VARARGS, |
135 | | - "return the list of entity classes"}, |
136 | | - {"signal_caster_get_type_list", dynamicgraph::python::signalCaster::getSignalTypeList, METH_VARARGS, |
137 | | - "return the list of signal type names"}, |
138 | | - {"writeGraph", dynamicgraph::python::pool::writeGraph, METH_VARARGS, "Write the graph of entities in a filename."}, |
139 | | - {"get_entity_list", dynamicgraph::python::pool::getEntityList, METH_VARARGS, |
140 | | - "return the list of instanciated entities"}, |
141 | | - {"entity_set_logger_verbosity", dynamicgraph::python::entity::setLoggerVerbosityLevel, METH_VARARGS, |
142 | | - "set the verbosity level of the entity"}, |
143 | | - {"entity_get_logger_verbosity", dynamicgraph::python::entity::getLoggerVerbosityLevel, METH_VARARGS, |
144 | | - "get the verbosity level of the entity"}, |
145 | | - {"addLoggerFileOutputStream", dynamicgraph::python::debug::addLoggerFileOutputStream, METH_VARARGS, |
146 | | - "add a output file stream to the logger by filename"}, |
147 | | - {"addLoggerCoutOutputStream", dynamicgraph::python::debug::addLoggerCoutOutputStream, METH_VARARGS, |
148 | | - "add std::cout as output stream to the logger"}, |
149 | | - {"closeLoggerFileOutputStream", dynamicgraph::python::debug::closeLoggerFileOutputStream, METH_VARARGS, |
150 | | - "close all the loggers file output streams."}, |
151 | | - {"entity_set_time_sample", dynamicgraph::python::entity::setTimeSample, METH_VARARGS, |
152 | | - "set the time sample for printing debugging information"}, |
153 | | - {"entity_get_time_sample", dynamicgraph::python::entity::getTimeSample, METH_VARARGS, |
154 | | - "get the time sample for printing debugging information"}, |
155 | | - {"entity_set_stream_print_period", dynamicgraph::python::entity::setStreamPrintPeriod, METH_VARARGS, |
156 | | - "set the period at which debugging information are printed"}, |
157 | | - {"entity_get_stream_print_period", dynamicgraph::python::entity::getStreamPrintPeriod, METH_VARARGS, |
158 | | - "get the period at which debugging information are printed"}, |
159 | | - {"real_time_logger_destroy", dynamicgraph::python::debug::realTimeLoggerDestroy, METH_VARARGS, |
160 | | - "Destroy the real time logger."}, |
161 | | - {"real_time_logger_spin_once", dynamicgraph::python::debug::realTimeLoggerSpinOnce, METH_VARARGS, |
162 | | - "Destroy the real time logger."}, |
163 | | - {"real_time_logger_instance", dynamicgraph::python::debug::realTimeLoggerInstance, METH_VARARGS, |
164 | | - "Starts the real time logger."}, |
165 | | - {"error_out", (PyCFunction)dynamicgraph::python::error_out, METH_NOARGS, NULL}, |
166 | | - {NULL, NULL, 0, NULL} /* Sentinel */ |
167 | | -}; |
168 | | - |
169 | | -#if PY_MAJOR_VERSION >= 3 |
170 | | -__attribute__((unused)) static struct PyModuleDef dynamicGraphModuleDef = { |
171 | | - PyModuleDef_HEAD_INIT, |
172 | | - "wrap", |
173 | | - NULL, |
174 | | - sizeof(struct dynamicgraph::python::module_state), |
175 | | - dynamicGraphMethods, |
176 | | - NULL, |
177 | | - NULL, |
178 | | - NULL, |
179 | | - NULL}; |
180 | | -#define GETSTATE(m) ((struct dynamicgraph::python::module_state*)PyModule_GetState(m)) |
181 | | -#define DGPYERROR(m) GETSTATE(m)->dgpyError |
182 | | -#define INITERROR return NULL |
183 | | -#else |
184 | | -__attribute__((unused)) static struct module_state _state; |
185 | | -#define GETSTATE(m) (&dynamicgraph::python::_state) |
186 | | -#define DGPYERROR(m) dynamicgraph::python::dgpyError |
187 | | -#define INITERROR return |
188 | | -#endif |
189 | | - |
190 | 71 | } // namespace python |
191 | 72 | } // namespace dynamicgraph |
192 | 73 |
|
|
0 commit comments