Merge pull request #3035 from alexbruy/global-startup-script

Add global python startup script support in addition to per-user scripts
This commit is contained in:
Alexander Bruy 2016-11-03 12:40:09 +02:00 committed by GitHub
commit a382ec77e0
3 changed files with 25 additions and 7 deletions

View File

@ -55,14 +55,9 @@ def load_user_expressions(path):
userpythonhome = os.path.join(QgsApplication.qgisSettingsDirPath(), "python")
expressionspath = os.path.join(userpythonhome, "expressions")
startuppy = os.path.join(userpythonhome, "startup.py")
sys.path.append(userpythonhome)
# exec startup script
if os.path.exists(startuppy):
exec(compile(open(startuppy).read(), startuppy, 'exec'), locals(), globals())
if not os.path.exists(expressionspath):
os.makedirs(expressionspath)

View File

@ -32,6 +32,7 @@
#include <QMessageBox>
#include <QStringList>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>
#if (PY_VERSION_HEX < 0x03000000)
@ -209,6 +210,25 @@ bool QgsPythonUtilsImpl::checkQgisUser()
return true;
}
void QgsPythonUtilsImpl::doCustomImports()
{
QStringList startupPaths = QStandardPaths::locateAll( QStandardPaths::AppDataLocation, "startup.py" );
if ( startupPaths.isEmpty() )
{
return;
}
runString( "import importlib.util" );
QStringList::const_iterator iter = startupPaths.constBegin();
for ( ; iter != startupPaths.constEnd(); ++iter )
{
runString( QString( "spec = importlib.util.spec_from_file_location('startup','%1')" ).arg( *iter ) );
runString( "module = importlib.util.module_from_spec(spec)" );
runString( "spec.loader.exec_module(module)" );
}
}
void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
{
init();
@ -224,6 +244,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
exitPython();
return;
}
doCustomImports();
finish();
}
@ -249,6 +270,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 ) + ')' );
doCustomImports();
finish();
}

View File

@ -126,10 +126,12 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
//@return true if qgis.user could be imported
bool checkQgisUser();
//! import custom user and global Python code (startup scripts)
void doCustomImports();
//! cleanup Python context
void finish();
void installErrorHook();
void uninstallErrorHook();
@ -149,5 +151,4 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
bool mPythonEnabled;
};
#endif