Skip to content

Commit

Permalink
Add global python startup script support in addition to per-user scri…
Browse files Browse the repository at this point in the history
…pts (fix #6297)
  • Loading branch information
alexbruy committed Nov 3, 2016
1 parent 777fc86 commit 434bbea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/python/qgspythonutilsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QMessageBox>
#include <QStringList>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>

#if (PY_VERSION_HEX < 0x03000000)
Expand Down Expand Up @@ -209,6 +210,19 @@ bool QgsPythonUtilsImpl::checkQgisUser()
return true;
}

void QgsPythonUtilsImpl::doGlobalImports()
{
QString startupPath = QStandardPaths::locate( QStandardPaths::AppDataLocation, "global_startup.py" );
//runString( "if os.path.exists(" + startuppath + "): from global_startup import *\n" );
if ( !startupPath.isEmpty() )
{
runString( "import importlib.util" );
runString( QString( "spec = importlib.util.spec_from_file_location('global_startup','%1')" ).arg( startupPath ) );
runString( "module = importlib.util.module_from_spec(spec)" );
runString( "spec.loader.exec_module(module)" );
}
}

void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
{
init();
Expand All @@ -224,6 +238,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
exitPython();
return;
}
doGlobalImports();
finish();
}

Expand All @@ -249,6 +264,7 @@ void QgsPythonUtilsImpl::initServerPython( QgsServerInterface* interface )
// This is the other main difference with initInterface() for desktop plugins
runString( "qgis.utils.initServerInterface(" + QString::number(( unsigned long ) interface ) + ')' );

doGlobalImports();
finish();
}

Expand Down
3 changes: 3 additions & 0 deletions src/python/qgspythonutilsimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
//@return true if qgis.user could be imported
bool checkQgisUser();

//! import global Python code
void doGlobalImports();

//! cleanup Python context
void finish();

Expand Down

0 comments on commit 434bbea

Please sign in to comment.