Skip to content
Permalink
Browse files

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

Add global python startup script support in addition to per-user scripts
  • Loading branch information
alexbruy committed Nov 3, 2016
2 parents 777fc86 + 373e591 commit a382ec77e09cb97b2a160f488a23dbeae54e40ce
Showing with 25 additions and 7 deletions.
  1. +0 −5 python/user.py
  2. +22 −0 src/python/qgspythonutilsimpl.cpp
  3. +3 −2 src/python/qgspythonutilsimpl.h
@@ -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)

@@ -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();
}

@@ -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

0 comments on commit a382ec7

Please sign in to comment.
You can’t perform that action at this time.