Skip to content

Commit 373e591

Browse files
committed
drop old user startup script in favour of the new system
User script should be placed into $HOME/.local/share/QGIS. Additionally global scripts from /usr/local/share and /usr/share will be loaded if present
1 parent 434bbea commit 373e591

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

python/user.py

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,20 @@ bool QgsPythonUtilsImpl::checkQgisUser()
210210
return true;
211211
}
212212

213-
void QgsPythonUtilsImpl::doGlobalImports()
213+
void QgsPythonUtilsImpl::doCustomImports()
214214
{
215-
QString startupPath = QStandardPaths::locate( QStandardPaths::AppDataLocation, "global_startup.py" );
216-
//runString( "if os.path.exists(" + startuppath + "): from global_startup import *\n" );
217-
if ( !startupPath.isEmpty() )
215+
QStringList startupPaths = QStandardPaths::locateAll( QStandardPaths::AppDataLocation, "startup.py" );
216+
if ( startupPaths.isEmpty() )
218217
{
219-
runString( "import importlib.util" );
220-
runString( QString( "spec = importlib.util.spec_from_file_location('global_startup','%1')" ).arg( startupPath ) );
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 ) );
221227
runString( "module = importlib.util.module_from_spec(spec)" );
222228
runString( "spec.loader.exec_module(module)" );
223229
}
@@ -238,7 +244,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
238244
exitPython();
239245
return;
240246
}
241-
doGlobalImports();
247+
doCustomImports();
242248
finish();
243249
}
244250

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

267-
doGlobalImports();
273+
doCustomImports();
268274
finish();
269275
}
270276

src/python/qgspythonutilsimpl.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,12 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
126126
//@return true if qgis.user could be imported
127127
bool checkQgisUser();
128128

129-
//! import global Python code
130-
void doGlobalImports();
129+
//! import custom user and global Python code (startup scripts)
130+
void doCustomImports();
131131

132132
//! cleanup Python context
133133
void finish();
134134

135-
136135
void installErrorHook();
137136

138137
void uninstallErrorHook();
@@ -152,5 +151,4 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
152151
bool mPythonEnabled;
153152
};
154153

155-
156154
#endif

0 commit comments

Comments
 (0)