dynamic-graph-py.cc
Go to the documentation of this file.
1 // Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
2 
4 
6 #include <dynamic-graph/debug.h>
7 #include <dynamic-graph/entity.h>
9 #include <dynamic-graph/factory.h>
10 #include <dynamic-graph/pool.h>
13 #include <dynamic-graph/signal.h>
14 #include <dynamic-graph/tracer.h>
15 
16 #include <Eigen/Geometry>
17 #include <boost/python.hpp>
18 #include <boost/python/raw_function.hpp>
19 #include <boost/python/suite/indexing/map_indexing_suite.hpp>
20 #include <eigenpy/eigenpy.hpp>
21 #include <eigenpy/geometry.hpp>
22 #include <iostream>
23 #include <sstream>
24 
28 
29 namespace dynamicgraph {
30 namespace python {
31 
35 void plug(SignalBase<sigtime_t>* signalOut, SignalBase<sigtime_t>* signalIn) {
36  signalIn->plug(signalOut);
37 }
38 
39 void enableTrace(bool enable, const char* filename) {
40  if (enable)
41  DebugTrace::openFile(filename);
42  else
43  DebugTrace::closeFile(filename);
44 }
45 
46 } // namespace python
47 } // namespace dynamicgraph
48 
49 namespace bp = boost::python;
50 namespace dg = dynamicgraph;
51 
52 typedef bp::return_value_policy<bp::manage_new_object> manage_new_object;
53 typedef bp::return_value_policy<bp::reference_existing_object>
55 
57 
59  static PyObject* convert(const MapOfEntities::value_type& pair) {
60  return bp::incref(bp::make_tuple(pair.first, bp::ptr(pair.second)).ptr());
61  }
62 };
63 
65  return const_cast<MapOfEntities*>(
67 }
68 
70  const std::string& name) {
71  return &e.getSignal(name);
72 }
73 
74 class PythonEntity : public dg::Entity {
76 
77  public:
78  using dg::Entity::Entity;
79 
82  }
83  void signalDeregistration(const std::string& name) {
85  }
86 };
87 
89 
91  using namespace dynamicgraph;
92  bp::enum_<LoggerVerbosity>("LoggerVerbosity")
93  .value("VERBOSITY_ALL", VERBOSITY_ALL)
94  .value("VERBOSITY_INFO_WARNING_ERROR", VERBOSITY_INFO_WARNING_ERROR)
95  .value("VERBOSITY_WARNING_ERROR", VERBOSITY_WARNING_ERROR)
96  .value("VERBOSITY_ERROR", VERBOSITY_ERROR)
97  .value("VERBOSITY_NONE", VERBOSITY_NONE)
98  .export_values();
99 
100  bp::class_<Entity, boost::noncopyable>("Entity", bp::no_init)
101  .add_property("name",
102  bp::make_function(
104  bp::return_value_policy<bp::copy_const_reference>()))
105  .add_property("className",
106  bp::make_function(
108  bp::return_value_policy<bp::copy_const_reference>()),
109  "the class name of the Entity")
110  .add_property("__doc__", &Entity::getDocString)
111 
112  .def("setLoggerVerbosityLevel", &Entity::setLoggerVerbosityLevel)
113  .def("getLoggerVerbosityLevel", &Entity::getLoggerVerbosityLevel)
114  .add_property("loggerVerbosityLevel", &Entity::setLoggerVerbosityLevel,
116  "the verbosity level of the entity")
117  .def("setTimeSample", &Entity::setTimeSample)
118  .def("getTimeSample", &Entity::getTimeSample)
119  .add_property("timeSample", &Entity::getTimeSample,
121  "the time sample for printing debugging information")
122  .def("setStreamPrintPeriod", &Entity::setStreamPrintPeriod)
123  .def("getStreamPrintPeriod", &Entity::getStreamPrintPeriod)
124  .add_property("streamPrintPeriod", &Entity::getStreamPrintPeriod,
126  "set the period at which debugging information are printed")
127 
128  .def(
129  "__str__",
130  +[](const Entity& e) -> std::string {
131  std::ostringstream os;
132  e.display(os);
133  return os.str();
134  })
135  .def(
136  "signals",
137  +[](const Entity& e) -> bp::list {
138  bp::list ret;
139  for (auto& el : e.getSignalMap()) ret.append(bp::ptr(el.second));
140  return ret;
141  },
142  "Return the list of signals.")
143  //.def("signal", +[](Entity& e, const std::string &name) { return
144  //&e.getSignal(name); },
145  // reference_existing_object())
146  .def("signal", &getSignal, reference_existing_object(),
147  "get signal by name from an Entity", bp::arg("name"))
148  .def("hasSignal", &Entity::hasSignal,
149  "return True if the entity has a signal with the given name")
150 
151  .def(
152  "displaySignals",
153  +[](const Entity& e) {
154  Entity::SignalMap signals(e.getSignalMap());
155  std::cout << "--- <" << e.getName();
156  if (signals.empty())
157  std::cout << "> has no signal\n";
158  else
159  std::cout << "> signal list:\n";
160  for (const auto& el : signals)
161  el.second->display(std::cout << " |-- <") << '\n';
162  },
163  "Print the list of signals into standard output: temporary.")
164 
165  /*
166  .def("__getattr__", +[](Entity& e, const std::string &name) ->
167  SignalBase<int>* { return &e.getSignal(name); },
168  reference_existing_object())
169  def __getattr__(self, name):
170  try:
171  return self.signal(name)
172  except Exception:
173  try:
174  object.__getattr__(self, name)
175  except AttributeError:
176  raise AttributeError("'%s' entity has no attribute %s\n" %
177  (self.name, name) + ' entity attributes are usually either\n' + ' -
178  commands,\n' + ' - signals or,\n' + ' - user defined attributes')
179  */
180  /*
181  .def("__setattr__", +[](bp::object self, const std::string &name,
182  bp::object value) { Entity& e = bp::extract<Entity&> (self); if
183  (e.hasSignal(name)) throw std::invalid_argument(name + " already
184  designates a signal. " "It is not advised to set a new attribute of the
185  same name.");
186  // TODO How do you do that ? I am sure it is possible.
187  //object.__setattr__(self, name, value)
188  })
189  */
190 
191  /* TODO ?
192  def boundNewCommand(self, cmdName):
193  """
194  At construction, all existing commands are bound directly in the
195  class. This method enables to bound new commands dynamically. These new
196  bounds are not made with the class, but directly with the object instance.
197  """
198  def boundAllNewCommands(self):
199  """
200  For all commands that are not attribute of the object instance nor of
201  the class, a new attribute of the instance is created to bound the
202  command.
203  """
204  */
205 
206  // For backward compat
207  .add_static_property(
208  "entities",
209  bp::make_function(&getEntityMap, reference_existing_object()));
210 
211  python::exposeEntity<PythonEntity, bp::bases<Entity>, 0>()
212  .def("signalRegistration", &PythonEntity::signalRegistration)
213  .def("signalDeregistration", &PythonEntity::signalDeregistration);
214 
215  python::exposeEntity<python::PythonSignalContainer, bp::bases<Entity>, 0>()
216  .def("rmSignal", &python::PythonSignalContainer::rmSignal,
217  "Remove a signal", bp::arg("signal_name"));
218 }
219 
221  using dg::command::Command;
222  bp::class_<Command, boost::noncopyable>("Command", bp::no_init)
223  .def("__call__", bp::raw_function(dg::python::entity::executeCmd, 1),
224  "execute the command")
225  .add_property("__doc__", &Command::getDocstring);
226 }
227 
228 void exposeOldAPI() {
229  bp::def("plug", dynamicgraph::python::plug,
230  "plug an output signal into an input signal",
231  (bp::arg("signalOut"), "signalIn"));
232  bp::def("enableTrace", dynamicgraph::python::enableTrace,
233  "Enable or disable tracing debug info in a file");
234  // Signals
235  bp::def("create_signal_wrapper",
237  reference_existing_object(), "create a SignalWrapper C++ object");
238  // Entity
239  bp::def("factory_get_entity_class_list",
241  "return the list of entity classes");
242  bp::def("writeGraph", dynamicgraph::python::pool::writeGraph,
243  "Write the graph of entities in a filename.");
244  bp::def("get_entity_list", dynamicgraph::python::pool::getEntityList,
245  "return the list of instanciated entities");
246  bp::def("addLoggerFileOutputStream",
248  "add a output file stream to the logger by filename");
249  bp::def("addLoggerCoutOutputStream",
251  "add std::cout as output stream to the logger");
252  bp::def("closeLoggerFileOutputStream",
254  "close all the loggers file output streams.");
255  bp::def("real_time_logger_destroy",
257  "Destroy the real time logger.");
258  bp::def("real_time_logger_spin_once",
260  "Destroy the real time logger.");
261  bp::def("real_time_logger_instance",
263  "Starts the real time logger.");
264 }
265 
268 
269  if (!eigenpy::register_symbolic_link_to_registered_type<Eigen::Quaterniond>())
271  if (!eigenpy::register_symbolic_link_to_registered_type<Eigen::AngleAxisd>())
273 
274  eigenpy::enableEigenPySpecific<Eigen::Matrix4d>();
275 }
276 
278  enableEigenPy();
279 
280  exposeOldAPI();
281 
284  exposeCommand();
285 
287  bp::class_<MapOfEntities>("MapOfEntities")
288  .def("__len__", &MapOfEntities::size)
289  .def(
290  "keys",
291  +[](const MapOfEntities& m) -> bp::tuple {
292  bp::list res;
293  for (const auto& el : m) res.append(el.first);
294  return bp::tuple(res);
295  })
296  .def(
297  "values",
298  +[](const MapOfEntities& m) -> bp::tuple {
299  bp::list res;
300  for (const auto& el : m) res.append(bp::ptr(el.second));
301  return bp::tuple(res);
302  })
303  .def("__getitem__",
304  static_cast<dg::Entity*& (MapOfEntities::*)(const std::string& k)>(
305  &MapOfEntities::at),
307  .def(
308  "__setitem__", +[](MapOfEntities& m, const std::string& n,
309  dg::Entity* e) { m.emplace(n, e); })
310  .def("__iter__", bp::iterator<MapOfEntities>())
311  .def(
312  "__contains__",
313  +[](const MapOfEntities& m, const std::string& n) -> bool {
314  return m.count(n);
315  });
316  bp::to_python_converter<MapOfEntities::value_type,
318 }
eigenpy::exposeAngleAxis
void EIGENPY_DLLAPI exposeAngleAxis()
signal-time-dependent.h
dynamicgraph::Entity::hasSignal
bool hasSignal(const std::string &signame) const
boost::python
dynamicgraph::python::debug::realTimeLoggerSpinOnce
void realTimeLoggerSpinOnce()
Definition: debug-py.cc:45
eigenpy::exposeQuaternion
void EIGENPY_DLLAPI exposeQuaternion()
eigenpy.hpp
module.hh
manage_new_object
bp::return_value_policy< bp::manage_new_object > manage_new_object
Definition: dynamic-graph-py.cc:52
eigenpy::enableEigenPy
void EIGENPY_DLLAPI enableEigenPy()
signal-base.h
dynamicgraph::python::debug::closeLoggerFileOutputStream
void closeLoggerFileOutputStream()
Definition: debug-py.cc:37
dynamicgraph
getSignal
dg::SignalBase< dg::sigtime_t > * getSignal(dg::Entity &e, const std::string &name)
Definition: dynamic-graph-py.cc:69
exception-factory.h
dynamicgraph::DebugTrace::closeFile
static void closeFile(const char *filename=DEBUG_FILENAME_DEFAULT)
exposeEntityBase
void exposeEntityBase()
Definition: dynamic-graph-py.cc:90
dynamicgraph::Entity
dynamicgraph::python::enableTrace
void enableTrace(bool enable, const char *filename)
Definition: dynamic-graph-py.cc:39
dynamicgraph::Entity::setStreamPrintPeriod
bool setStreamPrintPeriod(double t)
dynamicgraph::Entity::name
std::string name
dynamicgraph::Entity::getSignalMap
SignalMap getSignalMap() const
exposeCommand
void exposeCommand()
Definition: dynamic-graph-py.cc:220
dynamicgraph::VERBOSITY_INFO_WARNING_ERROR
VERBOSITY_INFO_WARNING_ERROR
dynamicgraph::python::debug::addLoggerCoutOutputStream
void addLoggerCoutOutputStream()
Definition: debug-py.cc:41
dynamicgraph::Entity::getDocString
virtual std::string getDocString() const
dynamicgraph::python::debug::addLoggerFileOutputStream
void addLoggerFileOutputStream(const char *filename)
Definition: debug-py.cc:27
PythonEntity
Definition: dynamic-graph-py.cc:74
enableEigenPy
void enableEigenPy()
Definition: dynamic-graph-py.cc:266
dynamicgraph::python::entity::executeCmd
bp::object executeCmd(bp::tuple args, bp::dict)
Definition: entity-py.cc:79
MapOfEntitiesPairToPythonConverter
Definition: dynamic-graph-py.cc:58
dynamicgraph::Entity::SignalMap
std::map< std::string, SignalBase< sigtime_t > * > SignalMap
dynamicgraph::Entity::getName
const std::string & getName() const
dynamic-graph-py.hh
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(PythonEntity, "PythonEntity")
res
res
BOOST_PYTHON_MODULE
BOOST_PYTHON_MODULE(wrap)
Definition: dynamic-graph-py.cc:277
MapOfEntitiesPairToPythonConverter::convert
static PyObject * convert(const MapOfEntities::value_type &pair)
Definition: dynamic-graph-py.cc:59
signal-wrapper.hh
dynamicgraph::python::signalBase::createSignalWrapper
SignalBase< sigtime_t > * createSignalWrapper(const char *name, const char *type, bp::object object)
Create an instance of SignalWrapper.
Definition: signal-base-py.cc:158
python
PythonEntity::DYNAMIC_GRAPH_ENTITY_DECL
DYNAMIC_GRAPH_ENTITY_DECL()
PythonEntity::signalRegistration
void signalRegistration(dg::SignalBase< dg::sigtime_t > &signal)
Definition: dynamic-graph-py.cc:80
dynamicgraph::python::exposeSignals
void exposeSignals()
Definition: signal-base-py.cc:107
dynamicgraph::Entity::setTimeSample
bool setTimeSample(double t)
tracer.h
dynamicgraph::Entity::getClassName
virtual const std::string & getClassName() const
dynamicgraph::VERBOSITY_WARNING_ERROR
VERBOSITY_WARNING_ERROR
SignalBase< sigtime_t >::plug
virtual void plug(SignalBase< sigtime_t > *sigarg)
dynamicgraph::Entity::display
virtual void display(std::ostream &os) const
debug.h
dynamicgraph::python::factory::getEntityClassList
bp::tuple getEntityClassList()
Get name of entity.
Definition: factory-py.cc:20
dynamicgraph::python::debug::realTimeLoggerDestroy
void realTimeLoggerDestroy()
Definition: debug-py.cc:43
setup.name
name
Definition: setup.in.py:179
dynamicgraph::Entity::Entity
Entity(const std::string &name)
convert-dg-to-py.hh
dynamicgraph::PoolStorage::getEntityMap
const Entities & getEntityMap() const
exposeOldAPI
void exposeOldAPI()
Definition: dynamic-graph-py.cc:228
dynamicgraph::python::PythonSignalContainer::rmSignal
void rmSignal(const std::string &name)
Definition: signal-wrapper.cc:16
dynamicgraph::VERBOSITY_ALL
VERBOSITY_ALL
PythonEntity::signalDeregistration
void signalDeregistration(const std::string &name)
Definition: dynamic-graph-py.cc:83
dynamicgraph::PoolStorage::Entities
std::map< std::string, Entity * > Entities
dynamicgraph::PoolStorage::getInstance
static PoolStorage * getInstance()
dynamicgraph::python::debug::realTimeLoggerInstance
void realTimeLoggerInstance()
Definition: debug-py.cc:47
dynamicgraph::python::pool::getEntityList
bp::list getEntityList()
Get list of entities.
Definition: pool-py.cc:26
dynamicgraph::VERBOSITY_ERROR
VERBOSITY_ERROR
dynamicgraph::Entity::getSignal
SignalBase< sigtime_t > & getSignal(const std::string &signalName)
dynamicgraph::VERBOSITY_NONE
VERBOSITY_NONE
dynamicgraph::Entity::getTimeSample
double getTimeSample()
MapOfEntities
dg::PoolStorage::Entities MapOfEntities
Definition: dynamic-graph-py.cc:56
dynamicgraph::DebugTrace::openFile
static void openFile(const char *filename=DEBUG_FILENAME_DEFAULT)
dynamicgraph::python::plug
void plug(SignalBase< sigtime_t > *signalOut, SignalBase< sigtime_t > *signalIn)
plug a signal into another one.
Definition: dynamic-graph-py.cc:35
getEntityMap
MapOfEntities * getEntityMap()
Definition: dynamic-graph-py.cc:64
reference_existing_object
bp::return_value_policy< bp::reference_existing_object > reference_existing_object
Definition: dynamic-graph-py.cc:54
dynamicgraph::Entity::getStreamPrintPeriod
double getStreamPrintPeriod()
SignalBase< sigtime_t >
geometry.hpp
dynamicgraph::Entity::signalDeregistration
void signalDeregistration(const std::string &name)
command.h
dynamicgraph::python::pool::writeGraph
void writeGraph(const char *filename)
Definition: pool-py.cc:15
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
dynamicgraph::Entity::setLoggerVerbosityLevel
void setLoggerVerbosityLevel(LoggerVerbosity lv)
dynamicgraph::Entity::getLoggerVerbosityLevel
LoggerVerbosity getLoggerVerbosityLevel()


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Oct 27 2023 02:16:36