Skip to content

Commit a382ec7

Browse files
authoredNov 3, 2016
Merge pull request #3035 from alexbruy/global-startup-script
Add global python startup script support in addition to per-user scripts
2 parents 777fc86 + 373e591 commit a382ec7

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed
 

‎python/user.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,9 @@ def load_user_expressions(path):
5555

5656
userpythonhome = os.path.join(QgsApplication.qgisSettingsDirPath(), "python")
5757
expressionspath = os.path.join(userpythonhome, "expressions")
58-
startuppy = os.path.join(userpythonhome, "startup.py")
5958

6059
sys.path.append(userpythonhome)
6160

62-
# exec startup script
63-
if os.path.exists(startuppy):
64-
exec(compile(open(startuppy).read(), startuppy, 'exec'), locals(), globals())
65-
6661
if not os.path.exists(expressionspath):
6762
os.makedirs(expressionspath)
6863

‎src/python/qgspythonutilsimpl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <QMessageBox>
3333
#include <QStringList>
3434
#include <QDir>
35+
#include <QStandardPaths>
3536
#include <QDebug>
3637

3738
#if (PY_VERSION_HEX < 0x03000000)
@@ -209,6 +210,25 @@ bool QgsPythonUtilsImpl::checkQgisUser()
209210
return true;
210211
}
211212

213+
void QgsPythonUtilsImpl::doCustomImports()
214+
{
215+
QStringList startupPaths = QStandardPaths::locateAll( QStandardPaths::AppDataLocation, "startup.py" );
216+
if ( startupPaths.isEmpty() )
217+
{
218+
return;
219+
}
220+
221+
runString( "import importlib.util" );
222+
223+
QStringList::const_iterator iter = startupPaths.constBegin();
224+
for ( ; iter != startupPaths.constEnd(); ++iter )
225+
{
226+
runString( QString( "spec = importlib.util.spec_from_file_location('startup','%1')" ).arg( *iter ) );
227+
runString( "module = importlib.util.module_from_spec(spec)" );
228+
runString( "spec.loader.exec_module(module)" );
229+
}
230+
}
231+
212232
void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
213233
{
214234
init();
@@ -224,6 +244,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
224244
exitPython();
225245
return;
226246
}
247+
doCustomImports();
227248
finish();
228249
}
229250

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

273+
doCustomImports();
252274
finish();
253275
}
254276

‎src/python/qgspythonutilsimpl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
126126
//@return true if qgis.user could be imported
127127
bool checkQgisUser();
128128

129+
//! import custom user and global Python code (startup scripts)
130+
void doCustomImports();
131+
129132
//! cleanup Python context
130133
void finish();
131134

132-
133135
void installErrorHook();
134136

135137
void uninstallErrorHook();
@@ -149,5 +151,4 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
149151
bool mPythonEnabled;
150152
};
151153

152-
153154
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.