Commit efd545ef authored by Guilhem Saurel's avatar Guilhem Saurel

format

parent 646ab3b0
......@@ -15,6 +15,6 @@
// dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_PYTHON_API_HH
# define DYNAMIC_GRAPH_PYTHON_API_HH
# include <dynamic-graph/python/config.hh>
#endif //DYNAMIC_GRAPH_PYTHON_API_HH
#define DYNAMIC_GRAPH_PYTHON_API_HH
#include <dynamic-graph/python/config.hh>
#endif // DYNAMIC_GRAPH_PYTHON_API_HH
......@@ -16,59 +16,45 @@
// dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
# define DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
# include <string>
#define DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
#include <string>
# include <dynamic-graph/fwd.hh>
# include <dynamic-graph/exception-abstract.h>
#include <dynamic-graph/fwd.hh>
#include <dynamic-graph/exception-abstract.h>
// Depending on whether one is building or using the
// library define DLLAPI to import or export.
# if defined (WIN32)
# if defined (wrap_EXPORTS)
# define WRAP_DLLAPI __declspec(dllexport)
# else
# define WRAP_DLLAPI __declspec(dllimport)
# endif
# else
# define WRAP_DLLAPI
# endif
#if defined(WIN32)
#if defined(wrap_EXPORTS)
#define WRAP_DLLAPI __declspec(dllexport)
#else
#define WRAP_DLLAPI __declspec(dllimport)
#endif
#else
#define WRAP_DLLAPI
#endif
namespace dynamicgraph {
namespace python {
namespace python {
/// \ingroup error
///
/// \brief Generic error class.
class WRAP_DLLAPI ExceptionPython : public ExceptionAbstract
{
public:
enum ErrorCodeEnum
{
GENERIC
,VALUE_PARSING
,VECTOR_PARSING
,MATRIX_PARSING
,CLASS_INCONSISTENT
};
/// \ingroup error
///
/// \brief Generic error class.
class WRAP_DLLAPI ExceptionPython : public ExceptionAbstract {
public:
enum ErrorCodeEnum { GENERIC, VALUE_PARSING, VECTOR_PARSING, MATRIX_PARSING, CLASS_INCONSISTENT };
static const std::string EXCEPTION_NAME;
static const std::string EXCEPTION_NAME;
explicit ExceptionPython (const ExceptionPython::ErrorCodeEnum& errcode,
const std::string & msg = "");
explicit ExceptionPython(const ExceptionPython::ErrorCodeEnum& errcode, const std::string& msg = "");
ExceptionPython (const ExceptionPython::ErrorCodeEnum& errcode,
const std::string& msg, const char* format, ...);
ExceptionPython(const ExceptionPython::ErrorCodeEnum& errcode, const std::string& msg, const char* format, ...);
virtual ~ExceptionPython () throw ()
{}
virtual ~ExceptionPython() throw() {}
virtual const std::string& getExceptionName () const
{
return ExceptionPython::EXCEPTION_NAME;
}
};
} // end of namespace python
} // end of namespace dynamicgraph
virtual const std::string& getExceptionName() const { return ExceptionPython::EXCEPTION_NAME; }
};
} // end of namespace python
} // end of namespace dynamicgraph
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
......@@ -22,55 +22,53 @@
#include "dynamic-graph/python/deprecated.hh"
#ifndef DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
# define DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
#define DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
#include "dynamic-graph/python/api.hh"
namespace dynamicgraph {
namespace python {
///
/// This class implements a basis python interpreter.
///
/// String sent to method python are interpreted by an onboard python
/// interpreter.
class DYNAMIC_GRAPH_PYTHON_DLLAPI Interpreter
{
public:
Interpreter();
~Interpreter();
/// \brief Method to start python interperter.
/// \param command string to execute
/// Method deprecated, you *SHOULD* handle error messages.
DYNAMIC_GRAPH_PYTHON_DEPRECATED std::string python( const std::string& command );
namespace python {
///
/// This class implements a basis python interpreter.
///
/// String sent to method python are interpreted by an onboard python
/// interpreter.
class DYNAMIC_GRAPH_PYTHON_DLLAPI Interpreter {
public:
Interpreter();
~Interpreter();
/// \brief Method to start python interperter.
/// \param command string to execute
/// Method deprecated, you *SHOULD* handle error messages.
DYNAMIC_GRAPH_PYTHON_DEPRECATED std::string python(const std::string& command);
/// \brief Method to start python interperter.
/// \param command string to execute, result, stdout, stderr strings
void python( const std::string& command , std::string& result,
std::string& out, std::string& err);
/// \brief Method to start python interperter.
/// \param command string to execute, result, stdout, stderr strings
void python(const std::string& command, std::string& result, std::string& out, std::string& err);
/// \brief Method to exectue a python script.
/// \param filename the filename
void runPythonFile( std::string filename );
void runPythonFile( std::string filename, std::string& err);
void runMain( void );
/// \brief Method to exectue a python script.
/// \param filename the filename
void runPythonFile(std::string filename);
void runPythonFile(std::string filename, std::string& err);
void runMain(void);
/// \brief Process input stream to send relevant blocks to python
/// \param stream input stream
std::string processStream(std::istream& stream, std::ostream& os);
/// \brief Process input stream to send relevant blocks to python
/// \param stream input stream
std::string processStream(std::istream& stream, std::ostream& os);
/// \brief Return a pointer to the dictionary of global variables
PyObject* globals();
/// \brief Return a pointer to the dictionary of global variables
PyObject* globals();
private:
/// The Pythone thread state
PyThreadState *_pyState;
/// Pointer to the dictionary of global variables
PyObject* globals_;
/// Pointer to the dictionary of local variables
PyObject* locals_;
PyObject* mainmod_;
PyObject* traceback_format_exception_;
};
} // namespace python
} // namespace dynamicgraph
#endif // DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
private:
/// The Pythone thread state
PyThreadState* _pyState;
/// Pointer to the dictionary of global variables
PyObject* globals_;
/// Pointer to the dictionary of local variables
PyObject* locals_;
PyObject* mainmod_;
PyObject* traceback_format_exception_;
};
} // namespace python
} // namespace dynamicgraph
#endif // DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
This diff is collapsed.
......@@ -19,16 +19,15 @@
#include <dynamic-graph/python/exception-python.hh>
namespace dynamicgraph {
namespace python {
namespace convert {
namespace python {
namespace convert {
command::Value pythonToValue(PyObject* pyObject,
const command::Value::Type& valueType);
PyObject* vectorToPython(const Vector& vector);
PyObject* matrixToPython(const ::dynamicgraph::Matrix& matrix);
PyObject* matrix4dToPython(const Eigen::Matrix4d& matrix);
PyObject* valueToPython(const ::dynamicgraph::command::Value& value);
command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& valueType);
PyObject* vectorToPython(const Vector& vector);
PyObject* matrixToPython(const ::dynamicgraph::Matrix& matrix);
PyObject* matrix4dToPython(const Eigen::Matrix4d& matrix);
PyObject* valueToPython(const ::dynamicgraph::command::Value& value);
} // namespace dynamicgraph
} // namespace python
}
} // namespace convert
} // namespace python
} // namespace dynamicgraph
......@@ -19,77 +19,72 @@
typedef boost::shared_ptr<std::ofstream> ofstreamShrPtr;
namespace dynamicgraph {
namespace python {
namespace python {
extern PyObject* dgpyError;
namespace debug {
extern PyObject* dgpyError;
namespace debug {
std::map<std::string, ofstreamShrPtr > mapOfFiles_;
std::map<std::string, ofstreamShrPtr> mapOfFiles_;
PyObject* addLoggerFileOutputStream (PyObject* /*self*/, PyObject* args)
{
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
std::string sfilename(filename);
try {
std::ofstream *aofs = new std::ofstream;
ofstreamShrPtr ofs_shrptr = boost::shared_ptr<std::ofstream>(aofs);
aofs->open(filename,
std::ofstream::out);
dynamicgraph::RealTimeLogger::instance();
dgADD_OSTREAM_TO_RTLOG(*aofs);
dgRTLOG() << "Added " << filename << " as an output stream \n";
mapOfFiles_[sfilename]= ofs_shrptr;
} CATCH_ALL_EXCEPTIONS();
return Py_BuildValue ("");
}
PyObject* addLoggerFileOutputStream(PyObject* /*self*/, PyObject* args) {
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
std::string sfilename(filename);
try {
std::ofstream* aofs = new std::ofstream;
ofstreamShrPtr ofs_shrptr = boost::shared_ptr<std::ofstream>(aofs);
aofs->open(filename, std::ofstream::out);
dynamicgraph::RealTimeLogger::instance();
dgADD_OSTREAM_TO_RTLOG(*aofs);
dgRTLOG() << "Added " << filename << " as an output stream \n";
mapOfFiles_[sfilename] = ofs_shrptr;
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
PyObject* closeLoggerFileOutputStream (PyObject* /*self*/, PyObject* /*args */)
{
try {
for (std::map<std::string,ofstreamShrPtr>::iterator
it=mapOfFiles_.begin();
it!=mapOfFiles_.end(); ++it)
{
it->second->close();
}
} CATCH_ALL_EXCEPTIONS();
return Py_BuildValue ("");
}
PyObject* closeLoggerFileOutputStream(PyObject* /*self*/, PyObject* /*args */) {
try {
for (std::map<std::string, ofstreamShrPtr>::iterator it = mapOfFiles_.begin(); it != mapOfFiles_.end(); ++it) {
it->second->close();
}
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
PyObject* addLoggerCoutOutputStream (PyObject* /*self*/, PyObject* /*args*/)
{
try {
dgADD_OSTREAM_TO_RTLOG(std::cout);
} CATCH_ALL_EXCEPTIONS();
return Py_BuildValue ("");
}
PyObject* addLoggerCoutOutputStream(PyObject* /*self*/, PyObject* /*args*/) {
try {
dgADD_OSTREAM_TO_RTLOG(std::cout);
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
PyObject* realTimeLoggerDestroy (PyObject* /*self*/, PyObject* /*args*/)
{
try {
RealTimeLogger::destroy();
} CATCH_ALL_EXCEPTIONS();
return Py_BuildValue ("");
}
PyObject* realTimeLoggerDestroy(PyObject* /*self*/, PyObject* /*args*/) {
try {
RealTimeLogger::destroy();
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
PyObject* realTimeLoggerSpinOnce (PyObject* /*self*/, PyObject* /*args*/)
{
try {
RealTimeLogger::instance().spinOnce();
} CATCH_ALL_EXCEPTIONS();
return Py_BuildValue ("");
}
PyObject* realTimeLoggerSpinOnce(PyObject* /*self*/, PyObject* /*args*/) {
try {
RealTimeLogger::instance().spinOnce();
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
PyObject* realTimeLoggerInstance (PyObject* /*self*/, PyObject* /*args*/)
{
try {
RealTimeLogger::instance();
} CATCH_ALL_EXCEPTIONS();
return Py_BuildValue ("");
}
PyObject* realTimeLoggerInstance(PyObject* /*self*/, PyObject* /*args*/) {
try {
RealTimeLogger::instance();
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
} // python
} // namespace debug
} // dynamicgraph
} // namespace debug
} // namespace python
} // namespace dynamicgraph
This diff is collapsed.
This diff is collapsed.
......@@ -24,49 +24,43 @@
#include <cstdio>
namespace dynamicgraph {
namespace python {
namespace python {
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
const std::string ExceptionPython::EXCEPTION_NAME = "Python";
const std::string ExceptionPython::EXCEPTION_NAME = "Python";
ExceptionPython::
ExceptionPython ( const ExceptionPython::ErrorCodeEnum& errcode,
const std::string & msg )
:ExceptionAbstract(errcode,msg)
{
dgDEBUGF( 15,"Created with message <%s>.",msg.c_str ());
dgDEBUG( 1) <<"Created with message <%s>."<<msg<<std::endl;
}
ExceptionPython::ExceptionPython(const ExceptionPython::ErrorCodeEnum& errcode, const std::string& msg)
: ExceptionAbstract(errcode, msg) {
dgDEBUGF(15, "Created with message <%s>.", msg.c_str());
dgDEBUG(1) << "Created with message <%s>." << msg << std::endl;
}
ExceptionPython::
ExceptionPython ( const ExceptionPython::ErrorCodeEnum& errcode,
const std::string & msg,const char* format, ... )
:ExceptionAbstract(errcode,msg)
{
va_list args;
va_start(args,format);
ExceptionPython::ExceptionPython(const ExceptionPython::ErrorCodeEnum& errcode, const std::string& msg,
const char* format, ...)
: ExceptionAbstract(errcode, msg) {
va_list args;
va_start(args, format);
const unsigned int SIZE = 256;
char buffer[SIZE];
vsnprintf(buffer,SIZE,format,args);
const unsigned int SIZE = 256;
char buffer[SIZE];
vsnprintf(buffer, SIZE, format, args);
dgDEBUG(15) <<"Created "<<" with message <"
<<msg<<"> and buffer <"<<buffer<<">. "<<std::endl;
dgDEBUG(15) << "Created "
<< " with message <" << msg << "> and buffer <" << buffer << ">. " << std::endl;
message += buffer;
message += buffer;
va_end(args);
va_end(args);
dgDEBUG(1) << "Throw exception " << EXCEPTION_NAME << "[#" << errcode<<"]: "
<<"<"<< message << ">."<<std::endl;
dgDEBUG(1) << "Throw exception " << EXCEPTION_NAME << "[#" << errcode << "]: "
<< "<" << message << ">." << std::endl;
}
}
} // namespace dynamicgraph
} // namespace python
} // namespace python
} // namespace dynamicgraph
/*
* Local variables:
......
......@@ -14,23 +14,23 @@
// with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_PYTHON_EXCEPTION
# define DYNAMIC_GRAPH_PYTHON_EXCEPTION
#define DYNAMIC_GRAPH_PYTHON_EXCEPTION
/// \brief Catch all exceptions which may be sent when C++ code is
/// called.
# define CATCH_ALL_EXCEPTIONS() \
catch (const std::exception& exc) \
{ \
PyErr_SetString(dgpyError, exc.what()); \
return NULL; \
} \
catch (const char* s) \
{ \
PyErr_SetString(dgpyError, s); \
return NULL; \
} catch (...) { \
PyErr_SetString(dgpyError, "Unknown exception"); \
return NULL; \
} struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
#define CATCH_ALL_EXCEPTIONS() \
catch (const std::exception& exc) { \
PyErr_SetString(dgpyError, exc.what()); \
return NULL; \
} \
catch (const char* s) { \
PyErr_SetString(dgpyError, s); \
return NULL; \
} \
catch (...) { \
PyErr_SetString(dgpyError, "Unknown exception"); \
return NULL; \
} \
struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION
......@@ -21,38 +21,31 @@ using dynamicgraph::Entity;
using dynamicgraph::ExceptionAbstract;
namespace dynamicgraph {
namespace python {
namespace factory {
/**
\brief Get name of entity
*/
PyObject* getEntityClassList(PyObject* /*self*/, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
std::vector <std::string> classNames;
dynamicgraph::FactoryStorage::getInstance()
->listEntities(classNames);
Py_ssize_t classNumber = classNames.size();
// Build a tuple object
PyObject* classTuple = PyTuple_New(classNumber);
for (Py_ssize_t iEntity = 0;
iEntity < (Py_ssize_t)classNames.size(); ++iEntity)
{
PyObject* className =
Py_BuildValue("s", classNames[iEntity].c_str());
PyTuple_SetItem(classTuple, iEntity, className);
}
return Py_BuildValue("O", classTuple);
}
} // namespace factory
} // namespace python
} // namespace dynamicgraph
namespace python {
namespace factory {
/**
\brief Get name of entity
*/
PyObject* getEntityClassList(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "")) return NULL;
std::vector<std::string> classNames;
dynamicgraph::FactoryStorage::getInstance()->listEntities(classNames);
Py_ssize_t classNumber = classNames.size();
// Build a tuple object
PyObject* classTuple = PyTuple_New(classNumber);
for (Py_ssize_t iEntity = 0; iEntity < (Py_ssize_t)classNames.size(); ++iEntity) {
PyObject* className = Py_BuildValue("s", classNames[iEntity].c_str());
PyTuple_SetItem(classTuple, iEntity, className);
}
return Py_BuildValue("O", classTuple);
}
} // namespace factory
} // namespace python
} // namespace dynamicgraph
This diff is collapsed.
......@@ -20,58 +20,50 @@
#include "exception.hh"
namespace dynamicgraph {
namespace python {
namespace python {
extern PyObject* dgpyError;
namespace pool {
extern PyObject* dgpyError;
namespace pool {
PyObject* writeGraph (PyObject* /*self*/, PyObject* args)
{
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
try {
PoolStorage::getInstance()->writeGraph (filename);
} CATCH_ALL_EXCEPTIONS();
return Py_BuildValue ("");
}
PyObject* writeGraph(PyObject* /*self*/, PyObject* args) {
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
try {
PoolStorage::getInstance()->writeGraph(filename);
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
/**
\brief Get list of entities
*/
PyObject* getEntityList(PyObject* /*self*/, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
/**
\brief Get list of entities
*/
PyObject* getEntityList(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "")) return NULL;
std::vector <std::string> entityNames;
try {
const PoolStorage::Entities & listOfEntities=
dynamicgraph::PoolStorage::getInstance()
->getEntityMap();
std::vector<std::string> entityNames;
try {
const PoolStorage::Entities& listOfEntities = dynamicgraph::PoolStorage::getInstance()->getEntityMap();
Py_ssize_t classNumber = listOfEntities.size();
// Build a tuple object
PyObject* classTuple = PyTuple_New(classNumber);
Py_ssize_t classNumber = listOfEntities.size();
// Build a tuple object
PyObject* classTuple = PyTuple_New(classNumber);
Py_ssize_t iEntity = 0;
for (PoolStorage::Entities::const_iterator entity_it =
listOfEntities.begin();
entity_it != listOfEntities.end();
++entity_it)
{
const std::string & aname = entity_it->second->getName();
Py_ssize_t iEntity = 0;
for (PoolStorage::Entities::const_iterator entity_it = listOfEntities.begin(); entity_it != listOfEntities.end();
++entity_it) {
const std::string& aname = entity_it->second->getName();
PyObject* className =
Py_BuildValue("s", aname.c_str());
PyTuple_SetItem(classTuple, iEntity, className);
iEntity++;
}
return Py_BuildValue("O", classTuple);
} CATCH_ALL_EXCEPTIONS();
return NULL;
}
PyObject* className = Py_BuildValue("s", aname.c_str());
PyTuple_SetItem(classTuple, iEntity, className);
iEntity++;
}
return Py_BuildValue("O", classTuple);
}
CATCH_ALL_EXCEPTIONS();
return NULL;
}
} // python
} // namespace pool
} // dynamicgraph
} // namespace pool
} // namespace python
} // namespace dynamicgraph
This diff is collapsed.
......@@ -20,28 +20,23 @@
#include <dynamic-graph/signal-caster.h>
namespace dynamicgraph {
namespace python {
namespace python {
namespace signalCaster {
PyObject* getSignalTypeList(PyObject* /*self*/, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
std::vector<std::string> typeList =
dynamicgraph::SignalCaster::getInstance()->listTypenames();
Py_ssize_t typeNumber = typeList.size();
// Build a tuple object
PyObject* typeTuple = PyTuple_New(typeNumber);
namespace signalCaster {
PyObject* getSignalTypeList(PyObject* /*self*/, PyObject* args) {
if (!PyArg_ParseTuple(args, "")) return NULL;
std::vector<std::string> typeList = dynamicgraph::SignalCaster::getInstance()->listTypenames();
Py_ssize_t typeNumber = typeList.size();
// Build a tuple object
PyObject* typeTuple = PyTuple_New(typeNumber);
for (Py_ssize_t iType = 0; iType < typeNumber; ++iType)
{
PyObject* className =
Py_BuildValue("s", typeList[iType].c_str());
PyTuple_SetItem(typeTuple, iType, className);
}
for (Py_ssize_t iType = 0; iType < typeNumber; ++iType) {
PyObject* className = Py_BuildValue("s", typeList[iType].c_str());
PyTuple_SetItem(typeTuple, iType, className);
}
return Py_BuildValue("O", typeTuple);
}
} //namespace signalCaster
} // namespace dynamicgraph
} // namespace python
return Py_BuildValue("O", typeTuple);
}