Commit 403a73bf authored by Jan Möbius's avatar Jan Möbius

Merge branch 'Python_command_line_scripting' into 'master'

Python command line scripting

See merge request !129
parents de76a6c5 a4b815b3
......@@ -1549,6 +1549,7 @@ QList<int> Core::objectList (QString _selection, QStringList _types)
foreach (QString s, _types)
if (!s.isEmpty ())
ids = typeId (s);
if (_selection == "source")
selection = PluginFunctions::SOURCE_OBJECTS;
else if (_selection == "target")
......
......@@ -1058,6 +1058,12 @@ private slots:
*/
QWidget *getToolbox(QString _pluginName, QString _toolboxName);
/** \brief expand or collapse a toolbox
*
* @param _pluginName To which plugin does the toolbox belong
* @param _toolboxName Name of the toolbox
* @param activate Expand or collapse?
*/
void activateToolbox(QString _pluginName, QString _toolboxName, bool activate);
private :
......@@ -1318,7 +1324,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
......
......@@ -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);
......
......@@ -56,6 +56,9 @@
#include "Core.hh"
#include <QUiLoader>
#ifdef PYTHON_ENABLED
#include <PythonInterpreter/PythonInterpreter.hh>
#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
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment