mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
JsonUtils & improvements (#476)
* add JsonUtils * update * repair * update * ident * Schema correct msg other adjusts * fix effDel, ExceptionLog, cleanup * fix travis qt5.2 * not so funny * use Qthread interrupt instead abort bool * update services
This commit is contained in:
@@ -78,9 +78,8 @@ void Effect::registerHyperionExtensionModule()
|
||||
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
|
||||
}
|
||||
|
||||
Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const QString & script, const QString & name, const QJsonObject & args, const QString & origin, unsigned smoothCfg)
|
||||
Effect::Effect(int priority, int timeout, const QString & script, const QString & name, const QJsonObject & args, const QString & origin, unsigned smoothCfg)
|
||||
: QThread()
|
||||
, _mainThreadState(mainThreadState)
|
||||
, _priority(priority)
|
||||
, _timeout(timeout)
|
||||
, _script(script)
|
||||
@@ -89,7 +88,6 @@ Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const
|
||||
, _args(args)
|
||||
, _endTime(-1)
|
||||
, _interpreterThreadState(nullptr)
|
||||
, _abortRequested(false)
|
||||
, _imageProcessor(ImageProcessorFactory::getInstance().newImageProcessor())
|
||||
, _colors()
|
||||
, _origin(origin)
|
||||
@@ -99,15 +97,15 @@ Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const
|
||||
_colors.resize(_imageProcessor->getLedCount());
|
||||
_colors.fill(ColorRgb::BLACK);
|
||||
|
||||
_log = Logger::getInstance("EFFECTENGINE");
|
||||
|
||||
// disable the black border detector for effects
|
||||
_imageProcessor->enableBlackBorderDetector(false);
|
||||
|
||||
|
||||
// init effect image for image based effects, size is based on led layout
|
||||
_image.fill(Qt::black);
|
||||
_painter = new QPainter(&_image);
|
||||
|
||||
// connect the finished signal
|
||||
connect(this, SIGNAL(finished()), this, SLOT(effectFinished()));
|
||||
Q_INIT_RESOURCE(EffectEngine);
|
||||
}
|
||||
|
||||
@@ -120,7 +118,7 @@ Effect::~Effect()
|
||||
void Effect::run()
|
||||
{
|
||||
// switch to the main thread state and acquire the GIL
|
||||
PyEval_RestoreThread(_mainThreadState);
|
||||
PyEval_AcquireLock();
|
||||
|
||||
// Initialize a new thread state
|
||||
_interpreterThreadState = Py_NewInterpreter();
|
||||
@@ -158,13 +156,109 @@ void Effect::run()
|
||||
}
|
||||
else
|
||||
{
|
||||
Error(Logger::getInstance("EFFECTENGINE"), "Unable to open script file %s.", QSTRING_CSTR(_script));
|
||||
Error(_log, "Unable to open script file %s.", QSTRING_CSTR(_script));
|
||||
}
|
||||
file.close();
|
||||
|
||||
if (!python_code.isEmpty())
|
||||
{
|
||||
PyRun_SimpleString(python_code.constData());
|
||||
PyObject *main_module = PyImport_ImportModule("__main__"); // New Reference
|
||||
PyObject *main_dict = PyModule_GetDict(main_module); // Borrowed reference
|
||||
Py_INCREF(main_dict); // Incref "main_dict" to use it in PyRun_String(), because PyModule_GetDict() has decref "main_dict"
|
||||
Py_DECREF(main_module); // // release "main_module" when done
|
||||
PyObject *result = PyRun_String(python_code.constData(), Py_file_input, main_dict, main_dict); // New Reference
|
||||
|
||||
if (!result)
|
||||
{
|
||||
if (PyErr_Occurred()) // Nothing needs to be done for a borrowed reference
|
||||
{
|
||||
Error(_log,"###### PYTHON EXCEPTION ######");
|
||||
Error(_log,"## In effect '%s'", QSTRING_CSTR(_name));
|
||||
/* Objects all initialized to NULL for Py_XDECREF */
|
||||
PyObject *errorType = NULL, *errorValue = NULL, *errorTraceback = NULL;
|
||||
|
||||
PyErr_Fetch(&errorType, &errorValue, &errorTraceback); // New Reference or NULL
|
||||
PyErr_NormalizeException(&errorType, &errorValue, &errorTraceback);
|
||||
|
||||
// Extract exception message from "errorValue"
|
||||
if(errorValue)
|
||||
{
|
||||
QString message;
|
||||
if(PyObject_HasAttrString(errorValue, "__class__"))
|
||||
{
|
||||
PyObject *classPtr = PyObject_GetAttrString(errorValue, "__class__"); // New Reference
|
||||
PyObject *class_name = NULL; /* Object "class_name" initialized to NULL for Py_XDECREF */
|
||||
class_name = PyObject_GetAttrString(classPtr, "__name__"); // New Reference or NULL
|
||||
|
||||
if(class_name && PyString_Check(class_name))
|
||||
message.append(PyString_AsString(class_name));
|
||||
|
||||
Py_DECREF(classPtr); // release "classPtr" when done
|
||||
Py_XDECREF(class_name); // Use Py_XDECREF() to ignore NULL references
|
||||
}
|
||||
|
||||
// Object "class_name" initialized to NULL for Py_XDECREF
|
||||
PyObject *valueString = NULL;
|
||||
valueString = PyObject_Str(errorValue); // New Reference or NULL
|
||||
|
||||
if(valueString && PyString_Check(valueString))
|
||||
{
|
||||
if(!message.isEmpty())
|
||||
message.append(": ");
|
||||
|
||||
message.append(PyString_AsString(valueString));
|
||||
}
|
||||
Py_XDECREF(valueString); // Use Py_XDECREF() to ignore NULL references
|
||||
|
||||
Error(_log, "## %s", QSTRING_CSTR(message));
|
||||
}
|
||||
|
||||
// Extract exception message from "errorTraceback"
|
||||
if(errorTraceback)
|
||||
{
|
||||
// Object "tracebackList" initialized to NULL for Py_XDECREF
|
||||
PyObject *tracebackModule = NULL, *methodName = NULL, *tracebackList = NULL;
|
||||
QString tracebackMsg;
|
||||
|
||||
tracebackModule = PyImport_ImportModule("traceback"); // New Reference or NULL
|
||||
methodName = PyString_FromString("format_exception"); // New Reference or NULL
|
||||
tracebackList = PyObject_CallMethodObjArgs(tracebackModule, methodName, errorType, errorValue, errorTraceback, NULL); // New Reference or NULL
|
||||
|
||||
if(tracebackList)
|
||||
{
|
||||
PyObject* iterator = PyObject_GetIter(tracebackList); // New Reference
|
||||
|
||||
PyObject* item;
|
||||
while( (item = PyIter_Next(iterator)) ) // New Reference
|
||||
{
|
||||
Error(_log, "## %s",QSTRING_CSTR(QString(PyString_AsString(item)).trimmed()));
|
||||
Py_DECREF(item); // release "item" when done
|
||||
}
|
||||
Py_DECREF(iterator); // release "iterator" when done
|
||||
}
|
||||
|
||||
// Use Py_XDECREF() to ignore NULL references
|
||||
Py_XDECREF(tracebackModule);
|
||||
Py_XDECREF(methodName);
|
||||
Py_XDECREF(tracebackList);
|
||||
|
||||
// Give the exception back to python and print it to stderr in case anyone else wants it.
|
||||
Py_XINCREF(errorType);
|
||||
Py_XINCREF(errorValue);
|
||||
Py_XINCREF(errorTraceback);
|
||||
|
||||
PyErr_Restore(errorType, errorValue, errorTraceback);
|
||||
//PyErr_PrintEx(0); // Remove this line to switch off stderr output
|
||||
}
|
||||
Error(_log,"###### EXCEPTION END ######");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Py_DECREF(result); // release "result" when done
|
||||
}
|
||||
|
||||
Py_DECREF(main_dict); // release "main_dict" when done
|
||||
}
|
||||
|
||||
// Clean up the thread state
|
||||
@@ -173,28 +267,8 @@ void Effect::run()
|
||||
PyEval_ReleaseLock();
|
||||
}
|
||||
|
||||
int Effect::getPriority() const
|
||||
{
|
||||
return _priority;
|
||||
}
|
||||
|
||||
bool Effect::isAbortRequested() const
|
||||
{
|
||||
return _abortRequested;
|
||||
}
|
||||
|
||||
void Effect::abort()
|
||||
{
|
||||
_abortRequested = true;
|
||||
}
|
||||
|
||||
void Effect::effectFinished()
|
||||
{
|
||||
emit effectFinished(this);
|
||||
}
|
||||
|
||||
PyObject *Effect::json2python(const QJsonValue &jsonData) const
|
||||
{
|
||||
{
|
||||
switch (jsonData.type())
|
||||
{
|
||||
case QJsonValue::Null:
|
||||
@@ -251,7 +325,7 @@ PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
|
||||
Effect * effect = getEffect();
|
||||
|
||||
// check if we have aborted already
|
||||
if (effect->_abortRequested)
|
||||
if (effect->isInterruptionRequested())
|
||||
{
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
@@ -333,7 +407,7 @@ PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
|
||||
Effect * effect = getEffect();
|
||||
|
||||
// check if we have aborted already
|
||||
if (effect->_abortRequested)
|
||||
if (effect->isInterruptionRequested())
|
||||
{
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
@@ -398,10 +472,10 @@ PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
|
||||
// Test if the effect has reached it end time
|
||||
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
|
||||
{
|
||||
effect->_abortRequested = true;
|
||||
effect->requestInterruption();
|
||||
}
|
||||
|
||||
return Py_BuildValue("i", effect->_abortRequested ? 1 : 0);
|
||||
return Py_BuildValue("i", effect->isInterruptionRequested() ? 1 : 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -453,7 +527,7 @@ PyObject* Effect::wrapImageShow(PyObject *self, PyObject *args)
|
||||
binaryImage.append((char) qBlue(scanline[j]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
memcpy(image.memptr(), binaryImage.data(), binaryImage.size());
|
||||
std::vector<ColorRgb> v = effect->_colors.toStdVector();
|
||||
effect->_imageProcessor->process(image, v);
|
||||
@@ -513,7 +587,7 @@ PyObject* Effect::wrapImageLinearGradient(PyObject *self, PyObject *args)
|
||||
|
||||
gradient.setSpread(static_cast<QGradient::Spread>(spread));
|
||||
effect->_painter->fillRect(myQRect, gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -580,7 +654,7 @@ PyObject* Effect::wrapImageConicalGradient(PyObject *self, PyObject *args)
|
||||
}
|
||||
|
||||
effect->_painter->fillRect(myQRect, gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -616,7 +690,7 @@ PyObject* Effect::wrapImageRadialGradient(PyObject *self, PyObject *args)
|
||||
if ( argCount == 12 && PyArg_ParseTuple(args, "iiiiiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &focalX, &focalY, &focalRadius, &bytearray, &spread) )
|
||||
{
|
||||
argsOK = true;
|
||||
}
|
||||
}
|
||||
if ( argCount == 9 && PyArg_ParseTuple(args, "iiiiiiiOi", &startX, &startY, &width, &height, ¢erX, ¢erY, &radius, &bytearray, &spread) )
|
||||
{
|
||||
argsOK = true;
|
||||
@@ -635,7 +709,7 @@ PyObject* Effect::wrapImageRadialGradient(PyObject *self, PyObject *args)
|
||||
focalY = centerY;
|
||||
focalRadius = radius;
|
||||
}
|
||||
|
||||
|
||||
if (argsOK)
|
||||
{
|
||||
if (PyByteArray_Check(bytearray))
|
||||
@@ -661,7 +735,7 @@ PyObject* Effect::wrapImageRadialGradient(PyObject *self, PyObject *args)
|
||||
|
||||
gradient.setSpread(static_cast<QGradient::Spread>(spread));
|
||||
effect->_painter->fillRect(myQRect, gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -742,7 +816,7 @@ PyObject* Effect::wrapImageDrawPie(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
PyObject * bytearray = nullptr;
|
||||
|
||||
|
||||
QString brush;
|
||||
int argCount = PyTuple_Size(args);
|
||||
int radius, centerX, centerY;
|
||||
@@ -804,7 +878,7 @@ PyObject* Effect::wrapImageDrawPie(PyObject *self, PyObject *args)
|
||||
));
|
||||
}
|
||||
painter->setBrush(gradient);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
else
|
||||
@@ -908,7 +982,7 @@ PyObject* Effect::wrapImageDrawLine(PyObject *self, PyObject *args)
|
||||
painter->setPen(newPen);
|
||||
painter->drawLine(startX, startY, endX, endY);
|
||||
painter->setPen(oldPen);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
return nullptr;
|
||||
@@ -943,7 +1017,7 @@ PyObject* Effect::wrapImageDrawPoint(PyObject *self, PyObject *args)
|
||||
painter->setPen(newPen);
|
||||
painter->drawPoint(x, y);
|
||||
painter->setPen(oldPen);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
return nullptr;
|
||||
@@ -983,7 +1057,7 @@ PyObject* Effect::wrapImageDrawRect(PyObject *self, PyObject *args)
|
||||
painter->setPen(newPen);
|
||||
painter->drawRect(startX, startY, width, height);
|
||||
painter->setPen(oldPen);
|
||||
|
||||
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
return nullptr;
|
||||
@@ -1028,7 +1102,7 @@ PyObject* Effect::wrapImageSave(PyObject *self, PyObject *args)
|
||||
QImage img(effect->_image.copy());
|
||||
effect->_imageStack.append(img);
|
||||
|
||||
return Py_BuildValue("i", effect->_imageStack.size()-1);
|
||||
return Py_BuildValue("i", effect->_imageStack.size()-1);
|
||||
}
|
||||
|
||||
PyObject* Effect::wrapImageMinSize(PyObject *self, PyObject *args)
|
||||
@@ -1045,7 +1119,7 @@ PyObject* Effect::wrapImageMinSize(PyObject *self, PyObject *args)
|
||||
if (width<w || height<h)
|
||||
{
|
||||
delete effect->_painter;
|
||||
|
||||
|
||||
effect->_image = effect->_image.scaled(qMax(width,w),qMax(height,h), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
||||
effect->_imageSize = effect->_image.size();
|
||||
effect->_painter = new QPainter(&(effect->_image));
|
||||
@@ -1070,10 +1144,10 @@ PyObject* Effect::wrapImageHeight(PyObject *self, PyObject *args)
|
||||
PyObject* Effect::wrapImageCRotate(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
|
||||
|
||||
int argCount = PyTuple_Size(args);
|
||||
int angle;
|
||||
|
||||
|
||||
if ( argCount == 1 && PyArg_ParseTuple(args, "i", &angle ) )
|
||||
{
|
||||
angle = qMax(qMin(angle,360),0);
|
||||
@@ -1086,16 +1160,16 @@ PyObject* Effect::wrapImageCRotate(PyObject *self, PyObject *args)
|
||||
PyObject* Effect::wrapImageCOffset(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
|
||||
|
||||
int offsetX = 0;
|
||||
int offsetY = 0;
|
||||
int argCount = PyTuple_Size(args);
|
||||
|
||||
|
||||
if ( argCount == 2 )
|
||||
{
|
||||
PyArg_ParseTuple(args, "ii", &offsetX, &offsetY );
|
||||
}
|
||||
|
||||
|
||||
effect->_painter->translate(QPoint(offsetX,offsetY));
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
@@ -1103,10 +1177,10 @@ PyObject* Effect::wrapImageCOffset(PyObject *self, PyObject *args)
|
||||
PyObject* Effect::wrapImageCShear(PyObject *self, PyObject *args)
|
||||
{
|
||||
Effect * effect = getEffect();
|
||||
|
||||
|
||||
int sh,sv;
|
||||
int argCount = PyTuple_Size(args);
|
||||
|
||||
|
||||
if ( argCount == 2 && PyArg_ParseTuple(args, "ii", &sh, &sv ))
|
||||
{
|
||||
effect->_painter->shear(sh,sv);
|
||||
|
Reference in New Issue
Block a user