Skip to content

Commit

Permalink
Merge pull request #3035 from alexbruy/global-startup-script
Browse files Browse the repository at this point in the history
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 a382ec7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
5 changes: 0 additions & 5 deletions python/user.py
Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions src/python/qgspythonutilsimpl.cpp
Expand Up @@ -32,6 +32,7 @@
#include <QMessageBox>
#include <QStringList>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>

#if (PY_VERSION_HEX < 0x03000000)
Expand Down Expand Up @@ -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();
Expand All @@ -224,6 +244,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
exitPython();
return;
}
doCustomImports();
finish();
}

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

Expand Down
5 changes: 3 additions & 2 deletions src/python/qgspythonutilsimpl.h
Expand Up @@ -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();
Expand All @@ -149,5 +151,4 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
bool mPythonEnabled;
};


#endif

0 comments on commit a382ec7

Please sign in to comment.