Skip to content

Commit

Permalink
delays PYQGIS_STARTUP logs until qgis module is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
ostr00000 authored and nyalldawson committed Sep 29, 2024
1 parent 98de62c commit 02236e6
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions src/python/qgspythonutilsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,55 @@ bool QgsPythonUtilsImpl::checkSystemImports()
exec(
compile(
"""
def run_startup_script(script_path):
script_executed = False
if not script_path:
class StartupScriptRunner:
def __init__(self):
self.info_messages: list[str] = []
self.warning_messages: list[str] = []
def run_startup_script(self, script_path: pathlib.Path | str | None) -> bool:
script_executed = False
if not script_path:
return script_executed
p1 = pathlib.Path(script_path)
if p1.exists():
self.info_messages.append(f"Executed startup script: {p1}")
code = compile(p1.read_text(), p1, 'exec')
exec(code, globals())
script_executed = True
p2 = pathlib.Path('%1') / script_path
if p2.exists() and p2 != p1:
self.info_messages.append(f"Executed startup script: {p2}")
code = compile(p2.read_text(), p2, 'exec')
exec(code, globals())
script_executed = True
if not script_executed:
self.warning_messages.append(
f"Startup script not executed - neither {p1} nor {p2} exist!"
)
return script_executed
from qgis.core import Qgis, QgsMessageLog
p1 = pathlib.Path(script_path)
if p1.exists():
QgsMessageLog.logMessage(f"Running {p1}", "QGIS", Qgis.MessageLevel.Info)
code = compile(p1.read_text(), p1, 'exec')
exec(code, globals())
script_executed = True
p2 = pathlib.Path('%1') / script_path
if p2.exists() and p2 != p1:
QgsMessageLog.logMessage(f"Running {p2}", "QGIS", Qgis.MessageLevel.Info)
code = compile(p2.read_text(), p2, 'exec')
exec(code, globals())
script_executed = True
if not script_executed:
QgsMessageLog.logMessage(
f"Startup script not executed - neither {p1} nor {p2} exist!",
"QGIS",
Qgis.MessageLevel.Warning,
)
return script_executed
def log_messages(self):
from qgis.core import Qgis, QgsMessageLog
for msg in self.info_messages:
QgsMessageLog.logMessage(msg, "QGIS", Qgis.MessageLevel.Info)
for msg in self.warning_messages:
QgsMessageLog.logMessage(msg, "QGIS", Qgis.MessageLevel.Warning)
_ssr = StartupScriptRunner()
""",
'QgsPythonUtilsImpl::checkSystemImports [run_startup_script]',
'exec',
),
globals(),
)
)"""" ).arg( pythonPath() ), QObject::tr( "Couldn't create run_startup_script." ), true );
runString( QStringLiteral( "is_startup_script_executed = run_startup_script(pyqgstart)" ) );
runString( QStringLiteral( "is_startup_script_executed = _ssr.run_startup_script(pyqgstart)" ) );

#ifdef Q_OS_WIN
runString( "oldhome=None" );
Expand Down Expand Up @@ -184,6 +196,9 @@ def run_startup_script(script_path):
runString( "if oldhome: os.environ['HOME']=oldhome\n" );
#endif

// now, after successful import of `qgis` module, we can show logs from `StartupScriptRunner`
runString( QStringLiteral( "_ssr.log_messages()" ));

return true;
}

Expand Down

0 comments on commit 02236e6

Please sign in to comment.