2011-01-13 03:26:00 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file uavobjectgeneratorpython.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief produce python code for uavobjects
|
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uavobjectgeneratorpython.h"
|
|
|
|
using namespace std;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool UAVObjectGeneratorPython::generate(UAVObjectParser *parser, QString templatepath, QString outputpath)
|
|
|
|
{
|
2011-03-03 03:22:50 +01:00
|
|
|
// Load template and setup output directory
|
2013-05-19 16:37:30 +02:00
|
|
|
pythonCodePath = QDir(templatepath + QString("flight/modules/FlightPlan/lib"));
|
|
|
|
pythonOutputPath = QDir(outputpath + QString("python"));
|
2011-01-22 18:38:43 +01:00
|
|
|
pythonOutputPath.mkpath(pythonOutputPath.absolutePath());
|
2013-05-19 16:37:30 +02:00
|
|
|
pythonCodeTemplate = readFile(pythonCodePath.absoluteFilePath("uavobject.pyt.template"));
|
2011-01-13 03:26:00 +01:00
|
|
|
if (pythonCodeTemplate.isEmpty()) {
|
|
|
|
std::cerr << "Problem reading python templates" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-03 03:22:50 +01:00
|
|
|
// Process each object
|
2011-01-13 03:26:00 +01:00
|
|
|
for (int objidx = 0; objidx < parser->getNumObjects(); ++objidx) {
|
2013-05-19 16:37:30 +02:00
|
|
|
ObjectInfo *info = parser->getObjectByIndex(objidx);
|
2011-01-13 03:26:00 +01:00
|
|
|
process_object(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true; // if we come here everything should be fine
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the python object files
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
bool UAVObjectGeneratorPython::process_object(ObjectInfo *info)
|
2011-01-13 03:26:00 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (info == NULL) {
|
2011-01-13 03:26:00 +01:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
// Prepare output strings
|
|
|
|
QString outCode = pythonCodeTemplate;
|
|
|
|
|
|
|
|
// Replace common tags
|
|
|
|
replaceCommonTags(outCode, info);
|
|
|
|
|
2011-03-03 03:22:50 +01:00
|
|
|
// Replace the ($DATAFIELDS) tag
|
|
|
|
QString datafields;
|
2013-05-19 16:37:30 +02:00
|
|
|
for (int n = 0; n < info->fields.length(); ++n) {
|
2011-03-03 03:22:50 +01:00
|
|
|
// Class header
|
|
|
|
datafields.append(QString("# Field %1 definition\n").arg(info->fields[n]->name));
|
|
|
|
datafields.append(QString("class %1Field(UAVObjectField):\n").arg(info->fields[n]->name));
|
|
|
|
// Only for enum types
|
2013-05-19 16:37:30 +02:00
|
|
|
if (info->fields[n]->type == FIELDTYPE_ENUM) {
|
2013-04-30 02:02:36 +02:00
|
|
|
datafields.append(QString(" # Enumeration options\n"));
|
2011-01-13 03:26:00 +01:00
|
|
|
// Go through each option
|
|
|
|
QStringList options = info->fields[n]->options;
|
2011-03-03 03:22:50 +01:00
|
|
|
for (int m = 0; m < options.length(); ++m) {
|
|
|
|
QString name = options[m].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), "");
|
2013-05-19 16:37:30 +02:00
|
|
|
if (name[0].isDigit()) {
|
2011-03-03 03:22:50 +01:00
|
|
|
name = QString("N%1").arg(name);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-30 02:02:36 +02:00
|
|
|
datafields.append(QString(" %1 = %2\n").arg(name).arg(m));
|
2011-03-03 03:22:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Generate element names (only if field has more than one element)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (info->fields[n]->numElements > 1 && !info->fields[n]->defaultElementNames) {
|
2013-04-30 02:02:36 +02:00
|
|
|
datafields.append(QString(" # Array element names\n"));
|
2011-03-03 03:22:50 +01:00
|
|
|
// Go through the element names
|
|
|
|
QStringList elemNames = info->fields[n]->elementNames;
|
2013-05-19 16:37:30 +02:00
|
|
|
for (int m = 0; m < elemNames.length(); ++m) {
|
2011-03-03 03:22:50 +01:00
|
|
|
QString name = elemNames[m].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), "");
|
2013-05-19 16:37:30 +02:00
|
|
|
if (name[0].isDigit()) {
|
2011-03-03 03:22:50 +01:00
|
|
|
name = QString("N%1").arg(name);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-30 02:02:36 +02:00
|
|
|
datafields.append(QString(" %1 = %2\n").arg(name).arg(m));
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-03 03:22:50 +01:00
|
|
|
// Constructor
|
2013-04-30 02:02:36 +02:00
|
|
|
datafields.append(QString(" def __init__(self):\n"));
|
|
|
|
datafields.append(QString(" UAVObjectField.__init__(self, %1, %2)\n\n").arg(info->fields[n]->type).arg(info->fields[n]->numElements));
|
2011-03-03 03:22:50 +01:00
|
|
|
}
|
|
|
|
outCode.replace(QString("$(DATAFIELDS)"), datafields);
|
|
|
|
|
|
|
|
// Replace the $(DATAFIELDINIT) tag
|
|
|
|
QString fields;
|
2013-05-19 16:37:30 +02:00
|
|
|
for (int n = 0; n < info->fields.length(); ++n) {
|
2013-04-30 02:02:36 +02:00
|
|
|
fields.append(QString(" self.%1 = %1Field()\n").arg(info->fields[n]->name));
|
|
|
|
fields.append(QString(" self.addField(self.%1)\n").arg(info->fields[n]->name));
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2011-03-03 03:22:50 +01:00
|
|
|
outCode.replace(QString("$(DATAFIELDINIT)"), fields);
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
// Write the Python code
|
2013-05-19 16:37:30 +02:00
|
|
|
bool res = writeFileIfDiffrent(pythonOutputPath.absolutePath() + "/" + info->namelc + ".py", outCode);
|
2011-01-13 03:26:00 +01:00
|
|
|
if (!res) {
|
|
|
|
cout << "Error: Could not write Python output files" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|