Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexbruy committed Nov 3, 2016
1 parent 434bbea commit 373e591
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 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: 14 additions & 8 deletions src/python/qgspythonutilsimpl.cpp
Expand Up @@ -210,14 +210,20 @@ bool QgsPythonUtilsImpl::checkQgisUser()
return true;
}

void QgsPythonUtilsImpl::doGlobalImports()
void QgsPythonUtilsImpl::doCustomImports()
{
QString startupPath = QStandardPaths::locate( QStandardPaths::AppDataLocation, "global_startup.py" );
//runString( "if os.path.exists(" + startuppath + "): from global_startup import *\n" );
if ( !startupPath.isEmpty() )
QStringList startupPaths = QStandardPaths::locateAll( QStandardPaths::AppDataLocation, "startup.py" );
if ( startupPaths.isEmpty() )
{
runString( "import importlib.util" );
runString( QString( "spec = importlib.util.spec_from_file_location('global_startup','%1')" ).arg( startupPath ) );
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)" );
}
Expand All @@ -238,7 +244,7 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
exitPython();
return;
}
doGlobalImports();
doCustomImports();
finish();
}

Expand All @@ -264,7 +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 ) + ')' );

doGlobalImports();
doCustomImports();
finish();
}

Expand Down
6 changes: 2 additions & 4 deletions src/python/qgspythonutilsimpl.h
Expand Up @@ -126,13 +126,12 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
//@return true if qgis.user could be imported
bool checkQgisUser();

//! import global Python code
void doGlobalImports();
//! import custom user and global Python code (startup scripts)
void doCustomImports();

//! cleanup Python context
void finish();


void installErrorHook();

void uninstallErrorHook();
Expand All @@ -152,5 +151,4 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
bool mPythonEnabled;
};


#endif

0 comments on commit 373e591

Please sign in to comment.