Commit efd545ef authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

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
{
/// \ingroup error
///
/// \brief Generic error class.
class WRAP_DLLAPI ExceptionPython : public ExceptionAbstract {
public:
enum ErrorCodeEnum
{
GENERIC
,VALUE_PARSING
,VECTOR_PARSING
,MATRIX_PARSING
,CLASS_INCONSISTENT
};
enum ErrorCodeEnum { GENERIC, VALUE_PARSING, VECTOR_PARSING, MATRIX_PARSING, CLASS_INCONSISTENT };
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
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
......@@ -22,37 +22,35 @@
#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
{
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 );
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);
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 );
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
......@@ -63,14 +61,14 @@ namespace dynamicgraph {
private:
/// The Pythone thread state
PyThreadState *_pyState;
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 python
} // namespace dynamicgraph
#endif // DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
......@@ -21,56 +21,47 @@
#include <dynamic-graph/signal.h>
#include <dynamic-graph/signal-caster.h>
namespace dynamicgraph {
using ::dynamicgraph::SignalBase;
namespace python {
namespace convert {
using ::dynamicgraph::SignalBase;
namespace python {
namespace convert {
void fillMatrixRow(Matrix& m, unsigned iRow, PyObject* sequence)
{
void fillMatrixRow(Matrix& m, unsigned iRow, PyObject* sequence) {
if (PySequence_Size(sequence) != (int)m.cols()) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"lines of matrix have different sizes.");
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "lines of matrix have different sizes.");
}
for (int iCol=0; iCol < m.cols(); iCol++) {
for (int iCol = 0; iCol < m.cols(); iCol++) {
PyObject* pyDouble = PySequence_GetItem(sequence, iCol);
if (PyFloat_Check(pyDouble))
m(iRow, iCol) = PyFloat_AsDouble(pyDouble);
else if(PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble)+0.0;
else if (PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
"a floating point number.");
}
}
void fillMatrixRow(Eigen::Matrix4d& m, unsigned iRow, PyObject* sequence)
{
}
void fillMatrixRow(Eigen::Matrix4d& m, unsigned iRow, PyObject* sequence) {
if (PySequence_Size(sequence) != (int)m.cols()) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"lines of matrix have different sizes.");
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "lines of matrix have different sizes.");
}
for (int iCol=0; iCol < m.cols(); iCol++) {
for (int iCol = 0; iCol < m.cols(); iCol++) {
PyObject* pyDouble = PySequence_GetItem(sequence, iCol);
if (PyFloat_Check(pyDouble))
m(iRow, iCol) = PyFloat_AsDouble(pyDouble);
else if(PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble)+0.0;
else if (PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
"a floating point number.");
}
}
}
command::Value pythonToValue(PyObject* pyObject,
const command::Value::Type& valueType)
{
command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& valueType) {
using command::Value;
bool bvalue;
unsigned uvalue;
......@@ -87,31 +78,28 @@ namespace dynamicgraph {
Py_ssize_t nRows;
switch (valueType) {
case (Value::BOOL) :
case (Value::BOOL):
if (!PyBool_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"bool");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "bool");
}
bvalue = PyObject_IsTrue(pyObject);
return Value(bvalue);
break;
case (Value::UNSIGNED) :
case (Value::UNSIGNED):
if (!PyInt_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"unsigned int");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "unsigned int");
}
uvalue = (unsigned int)PyInt_AsUnsignedLongMask(pyObject);
return Value(uvalue);
break;
case (Value::INT) :
case (Value::INT):
if (!PyInt_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"int");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "int");
}
ivalue = (int)PyInt_AS_LONG(pyObject);
return Value(ivalue);
break;
case (Value::FLOAT) :
case (Value::FLOAT):
if (PyFloat_Check(pyObject)) {
fvalue = (float)PyFloat_AsDouble(pyObject);
return Value(fvalue);
......@@ -119,11 +107,10 @@ namespace dynamicgraph {
fvalue = (float)PyInt_AS_LONG(pyObject);
return Value(fvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"float");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "float");
}
break;
case (Value::DOUBLE) :
case (Value::DOUBLE):
if (PyFloat_Check(pyObject)) {
dvalue = PyFloat_AsDouble(pyObject);
return Value(dvalue);
......@@ -131,32 +118,29 @@ namespace dynamicgraph {
dvalue = (double)PyInt_AS_LONG(pyObject);
return Value(dvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"double");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "double");
}
break;
case (Value::STRING) :
case (Value::STRING):
if (!PyString_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"string");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "string");
}
svalue = PyString_AsString(pyObject);
return Value(svalue);
break;
case (Value::VECTOR) :
case (Value::VECTOR):
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"vector");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "vector");
}
size = PySequence_Size(pyObject);
v.resize(size);
for (Py_ssize_t i=0; i<size; i++) {
for (Py_ssize_t i = 0; i < size; i++) {
PyObject* pyDouble = PySequence_GetItem(pyObject, i);
if (PyFloat_Check(pyDouble))
v(i) = PyFloat_AsDouble(pyDouble);
else if(PyInt_Check(pyDouble))
v(i) = (int)PyInt_AS_LONG(pyDouble)+0.0;
else if (PyInt_Check(pyDouble))
v(i) = (int)PyInt_AS_LONG(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::VECTOR_PARSING,
"element of vector should be a floating "
......@@ -164,11 +148,10 @@ namespace dynamicgraph {
}
return Value(v);
break;
case (Value::MATRIX) :
case (Value::MATRIX):
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"matrix");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "matrix");
}
nRows = PySequence_Size(pyObject);
if (nRows == 0) {
......@@ -176,29 +159,26 @@ namespace dynamicgraph {
}
row = PySequence_GetItem(pyObject, 0);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix");
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix");
}
nCols = PySequence_Size(row);
m.resize((unsigned int)nRows, (unsigned int)nCols);
fillMatrixRow(m, 0, row);
for (Py_ssize_t iRow=1; iRow<nRows; iRow++) {
for (Py_ssize_t iRow = 1; iRow < nRows; iRow++) {
row = PySequence_GetItem(pyObject, iRow);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix");
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix");
}
fillMatrixRow(m, static_cast<unsigned> (iRow), row);
fillMatrixRow(m, static_cast<unsigned>(iRow), row);
}
return Value(m);
break;
case (Value::MATRIX4D) :
case (Value::MATRIX4D):
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"matrix4d");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "matrix4d");
}
nRows = PySequence_Size(pyObject);
if (nRows == 0) {
......@@ -206,71 +186,64 @@ namespace dynamicgraph {
}
row = PySequence_GetItem(pyObject, 0);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix4d");
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix4d");
}
nCols = PySequence_Size(row);
m4.resize(nRows, nCols);
fillMatrixRow(m4, 0, row);
for (Py_ssize_t iRow=1; iRow<nRows; iRow++) {
for (Py_ssize_t iRow = 1; iRow < nRows; iRow++) {
row = PySequence_GetItem(pyObject, iRow);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix");
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix");
}
fillMatrixRow(m4, static_cast<unsigned> (iRow), row);
fillMatrixRow(m4, static_cast<unsigned>(iRow), row);
}
return Value(m4);
break;
default:
std::cerr << "Only int, double and string are supported."
<< std::endl;
std::cerr << "Only int, double and string are supported." << std::endl;
}
return Value();
}
}
PyObject* vectorToPython(const Vector& vector)
{
PyObject* vectorToPython(const Vector& vector) {
PyObject* tuple = PyTuple_New(vector.size());
for (int index = 0; index < vector.size() ; index++) {
for (int index = 0; index < vector.size(); index++) {
PyObject* pyDouble = PyFloat_FromDouble(vector(index));
PyTuple_SET_ITEM(tuple, index, pyDouble);
}
return tuple;
}
}
PyObject* matrixToPython(const Matrix& matrix)
{
PyObject* matrixToPython(const Matrix& matrix) {
PyObject* tuple = PyTuple_New(matrix.rows());
for (int iRow = 0; iRow < matrix.rows(); iRow++) {
PyObject* row = PyTuple_New(matrix.cols());
for (int iCol=0; iCol < matrix.cols(); iCol++) {
for (int iCol = 0; iCol < matrix.cols(); iCol++) {
PyObject* pyDouble = PyFloat_FromDouble(matrix(iRow, iCol));
PyTuple_SET_ITEM(row, iCol, pyDouble);
}
PyTuple_SET_ITEM(tuple, iRow, row);
}
return tuple;
}
}
PyObject* matrix4dToPython(const Eigen::Matrix4d& matrix)
{
PyObject* matrix4dToPython(const Eigen::Matrix4d& matrix) {
PyObject* tuple = PyTuple_New(matrix.rows());
for (int iRow = 0; iRow < matrix.rows(); iRow++) {
PyObject* row = PyTuple_New(matrix.cols());
for (int iCol=0; iCol < matrix.cols(); iCol++) {
for (int iCol = 0; iCol < matrix.cols(); iCol++) {
PyObject* pyDouble = PyFloat_FromDouble(matrix(iRow, iCol));
PyTuple_SET_ITEM(row, iCol, pyDouble);
}
PyTuple_SET_ITEM(tuple, iRow, row);
}
return tuple;
}
}
PyObject* valueToPython(const command::Value& value)
{
PyObject* valueToPython(const command::Value& value) {
using command::Value;
bool boolValue;
unsigned unsignedValue;
......@@ -281,44 +254,43 @@ namespace dynamicgraph {
Vector vectorValue;
Matrix matrixValue;
Eigen::Matrix4d matrix4dValue;
switch(value.type()) {
case (Value::BOOL) :
switch (value.type()) {
case (Value::BOOL):
boolValue = value.value();
if (boolValue) {
return PyBool_FromLong(1);
}
return PyBool_FromLong(0);
case (Value::UNSIGNED) :
case (Value::UNSIGNED):
unsignedValue = value.value();
return Py_BuildValue("I", unsignedValue);
case (Value::INT) :
case (Value::INT):
intValue = value.value();
return Py_BuildValue("i", intValue);
case (Value::FLOAT) :
case (Value::FLOAT):
floatValue = value.value();
return Py_BuildValue("f", floatValue);
case (Value::DOUBLE) :
case (Value::DOUBLE):
doubleValue = value.value();
return Py_BuildValue("d", doubleValue);
case (Value::STRING) :
stringValue = (std::string) value.value();
case (Value::STRING):
stringValue = (std::string)value.value();
return Py_BuildValue("s", stringValue.c_str());
case (Value::VECTOR) :
case (Value::VECTOR):
vectorValue = value.value();
return vectorToPython(vectorValue);
case (Value::MATRIX) :
case (Value::MATRIX):
matrixValue = value.value();
return matrixToPython(matrixValue);
case (Value::MATRIX4D) :
case (Value::MATRIX4D):
matrix4dValue = value.value();
return matrix4dToPython(matrix4dValue);
default:
return Py_BuildValue("");
}
return Py_BuildValue("");
}
}
} // namespace dynamicgraph
} // namespace python
} // namespace convert
} // namespace python
} // namespace dynamicgraph
......@@ -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)
{
PyObject* addLoggerFileOutputStream(PyObject* /*self*/, PyObject* args) {
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
std::string sfilename(filename);
try {
std::ofstream *aofs = new std::ofstream;
std::ofstream* aofs = new std::ofstream;
ofstreamShrPtr ofs_shrptr = boost::shared_ptr<std::ofstream>(aofs);
aofs->open(filename,
std::ofstream::out);
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 ("");
mapOfFiles_[sfilename] = ofs_shrptr;
}
CATCH_ALL_EXCEPTIONS();
return Py_BuildValue("");
}
PyObject* closeLoggerFileOutputStream (PyObject* /*self*/, PyObject* /*args */)
{
PyObject* closeLoggerFileOutputStream(PyObject* /*self*/, PyObject* /*args */) {
try {
for (std::map<std::string,ofstreamShrPtr>::iterator