From fe65b989ce1e56fbe66e559ecd6ca970b0097a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20M=C3=B6bius?= Date: Tue, 5 Feb 2019 14:29:07 +0100 Subject: [PATCH] Introduced openflipper command line python scripts with extension ofp --- Core/Core.hh | 10 ++++++++ Core/openFunctions.cc | 23 +++++++++++++---- Core/scripting.cc | 35 ++++++++++++++++++++++++++ PythonInterpreter/PythonInterpreter.cc | 2 ++ 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/Core/Core.hh b/Core/Core.hh index 76047527..906e6ce8 100644 --- a/Core/Core.hh +++ b/Core/Core.hh @@ -1318,7 +1318,17 @@ private slots: void showReducedMenuBar(bool reduced); + /** \brief Open the given file and execute its contents as a python script + * + * @param _filename Name of the python script + */ + void executePythonScriptFile(QString _filename); + /** \brief execute the given string as a python script + * + * @param _script Python script + */ + void executePythonScript(QString _script); private : /// Core scripting engine diff --git a/Core/openFunctions.cc b/Core/openFunctions.cc index 8ffd58a8..85806a59 100644 --- a/Core/openFunctions.cc +++ b/Core/openFunctions.cc @@ -149,10 +149,14 @@ void Core::slotExecuteAfterStartup() { QStringList scriptFiles = scriptDir.entryList(QDir::Files,QDir::Name); // Execute all files ending with ofs - for ( int i = 0 ; i < scriptFiles.size(); ++i ) + for ( int i = 0 ; i < scriptFiles.size(); ++i ) { if ( scriptFiles[i].endsWith("ofs",Qt::CaseInsensitive) ) emit executeFileScript(scriptDir.path() + "/" + scriptFiles[i]); + if ( scriptFiles[i].endsWith("ofp",Qt::CaseInsensitive) ) + executePythonScriptFile(scriptDir.path() + "/" + scriptFiles[i]); + } + // Clear scripting window afterexecuting the coresubdir scripts bool ok = false; slotCall( "scripting" ,"clearEditor()",ok); @@ -165,7 +169,7 @@ void Core::slotExecuteAfterStartup() { // Skip scripts here as they will be handled by a different function QString tmp = commandLineFileNames_[i].first; - if ( tmp.endsWith("ofs",Qt::CaseInsensitive) ) { + if ( tmp.endsWith(".ofs",Qt::CaseInsensitive) || tmp.endsWith(".ofp",Qt::CaseInsensitive) ) { commandLineScriptNames_.push_back(commandLineFileNames_[i].first); continue; } @@ -185,8 +189,14 @@ void Core::slotExecuteAfterStartup() { // If we have scripting support, execute the scripts given at the commandline. if ( scriptingSupport ) - for ( uint i = 0 ; i < commandLineScriptNames_.size() ; ++i ) { - emit executeFileScript(commandLineScriptNames_[i]); + for ( unsigned int i = 0 ; i < commandLineScriptNames_.size() ; ++i ) { + + if ( commandLineScriptNames_[i].endsWith("ofs",Qt::CaseInsensitive) ) + emit executeFileScript(commandLineScriptNames_[i]); + + if ( commandLineScriptNames_[i].endsWith("ofp",Qt::CaseInsensitive) ) + executePythonScriptFile(commandLineScriptNames_[i]); + } // If we don't have a gui and we are not under remote control, @@ -249,8 +259,11 @@ int Core::loadObject ( QString _filename ) { return -2; } else if (_filename.endsWith(".ofs",Qt::CaseInsensitive)) { - emit log(LOGINFO ,tr("Starting script execution of %1.").arg( _filename)) ; + emit log(LOGINFO ,tr("Starting script execution of OpenFlipper Script %1.").arg( _filename)) ; emit executeFileScript(_filename); + } else if (_filename.endsWith(".ofp",Qt::CaseInsensitive)) { + emit log(LOGINFO ,tr("Starting script execution of Python script %1.").arg( _filename)) ; + executePythonScriptFile(_filename); } else { QFileInfo fi(_filename); diff --git a/Core/scripting.cc b/Core/scripting.cc index 31a11f7b..672a7af7 100644 --- a/Core/scripting.cc +++ b/Core/scripting.cc @@ -56,6 +56,9 @@ #include "Core.hh" #include +#ifdef PYTHON_ENABLED + #include +#endif //== IMPLEMENTATION ========================================================== @@ -410,3 +413,35 @@ QScriptValue helpFunction(QScriptContext *context, QScriptEngine *engine) return engine->undefinedValue(); } + +void Core::executePythonScriptFile(QString _filename){ + QFile file(_filename); + + if (!file.exists()) { + emit scriptLog("Unable to load file " + _filename + " as python script. File not found!"); + return; + } + + file.open(QIODevice::ReadOnly| QFile::Text); + + QTextStream in(&file); + + QString script = in.readAll(); + + std::cerr << "Script : " << std::endl; + std::cerr << script.toStdString() << std::endl; + + executePythonScript(script); + +} + + +void Core::executePythonScript(QString _script) { +#ifdef PYTHON_ENABLED + PythonInterpreter* interpreter = PythonInterpreter::getInstance(); + interpreter->runScript(_script); +#else + emit scriptLog("Python scripting Error! No build in python support. Unable to execute script. Build OpenFlipper with Python support to enable Python based scripting."); +#endif +} + diff --git a/PythonInterpreter/PythonInterpreter.cc b/PythonInterpreter/PythonInterpreter.cc index 3d88b86d..776ac464 100644 --- a/PythonInterpreter/PythonInterpreter.cc +++ b/PythonInterpreter/PythonInterpreter.cc @@ -311,6 +311,8 @@ PYBIND11_EMBEDDED_MODULE(openflipper, m) { core.def("updateView", &Core::updateView); core.def("clearAll", &Core::clearAll); + core.def("fullscreen", &Core::fullscreen); + core.def("writeVersionNumbers", &Core::writeVersionNumbers); -- GitLab