Skip to content

Commit 50734ac

Browse files
author
wonder
committed
Better handling of python path and plugins paths.
Additionally, plugin paths in QGIS_PLUGINPATH can be separated by either semicolon or colon. git-svn-id: http://svn.osgeo.org/qgis/trunk@13943 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent f8e22c1 commit 50734ac

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

python/utils.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def initInterface(pointer):
6060
#######################
6161
# PLUGINS
6262

63+
# list of plugin paths. it gets filled in by the QGIS python library
64+
plugin_paths = []
65+
6366
# dictionary of plugins
6467
plugins = {}
6568

@@ -77,32 +80,17 @@ def findPlugins(path):
7780
return plugins
7881

7982
def updateAvailablePlugins():
80-
from qgis.core import QgsApplication
81-
plugindirs = [
82-
unicode(QgsApplication.pkgDataPath()) + "/python/plugins",
83-
unicode(QgsApplication.qgisSettingsDirPath()) + "/python/plugins"
84-
]
85-
env = os.environ.get("QGIS_PLUGINPATH")
86-
if env:
87-
plugindirs.extend(env.split(";"))
88-
83+
""" go thrgouh the plugin_paths list and find out what plugins are available """
8984
# merge the lists
9085
plugins = []
91-
for pluginpath in plugindirs:
92-
newplugins = findPlugins(pluginpath)
93-
if len(newplugins) > 0:
94-
if pluginpath not in sys.path:
95-
sys.path.append(pluginpath)
96-
for p in newplugins:
97-
if p not in plugins:
98-
plugins.append(p)
86+
for pluginpath in plugin_paths:
87+
for p in findPlugins(pluginpath):
88+
if p not in plugins:
89+
plugins.append(p)
9990

10091
global available_plugins
10192
available_plugins = plugins
10293

103-
# update list on start
104-
updateAvailablePlugins()
105-
10694

10795
def pluginMetadata(packageName, fct):
10896
""" fetch metadata from a plugin """

src/python/qgspythonutilsimpl.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
5959
runString( "import sys" ); // import sys module (for display / exception hooks)
6060
runString( "import os" ); // import os module (for user paths)
6161

62+
// construct a list of plugin paths
63+
// plugin dirs passed in QGIS_PLUGINPATH env. variable have highest priority (usually empty)
64+
// locally installed plugins have priority over the system plugins
65+
QStringList pluginpaths = extraPluginsPaths();
66+
pluginpaths << homePluginsPath() << pluginsPath();
67+
6268
// expect that bindings are installed locally, so add the path to modules
6369
// also add path to plugins
64-
#ifdef Q_OS_WIN
65-
runString( "oldhome=None\n" );
66-
runString( "if os.environ.has_key('HOME'): oldhome=os.environ['HOME']\n" );
67-
runString( "os.environ['HOME']=os.environ['USERPROFILE']" );
68-
#endif
69-
runString( "sys.path = [\"" + pythonPath() + "\", os.path.expanduser(\"~/.qgis/python\"), os.path.expanduser(\"~/.qgis/python/plugins\"), \"" + pluginsPath() + "\" ] + sys.path" );
70-
#ifdef Q_OS_WIN
71-
runString( "if oldhome: os.environ['HOME']=oldhome\n" );
72-
#endif
70+
QStringList newpaths;
71+
newpaths << pythonPath() << homePythonPath();
72+
newpaths += pluginpaths;
73+
runString( "sys.path = [\"" + newpaths.join("\", \"") + "\"] + sys.path" );
7374

7475
// import SIP
7576
if ( !runString( "from sip import wrapinstance, unwrapinstance",
@@ -103,6 +104,9 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
103104
return;
104105
}
105106

107+
// tell the utils script where to look for the plugins
108+
runString( "qgis.utils.plugin_paths = [\"" + pluginpaths.join( "\",\"" ) + "\"]" );
109+
106110
// initialize 'iface' object
107111
runString( "qgis.utils.initInterface(" + QString::number(( unsigned long ) interface ) + ")" );
108112
}
@@ -390,14 +394,30 @@ QString QgsPythonUtilsImpl::pluginsPath()
390394

391395
QString QgsPythonUtilsImpl::homePythonPath()
392396
{
393-
return QgsApplication::qgisSettingsDirPath() + "/python";
397+
return QgsApplication::qgisSettingsDirPath() + "python";
394398
}
395399

396400
QString QgsPythonUtilsImpl::homePluginsPath()
397401
{
398402
return homePythonPath() + "/plugins";
399403
}
400404

405+
QStringList QgsPythonUtilsImpl::extraPluginsPaths()
406+
{
407+
const char* cpaths = getenv( "QGIS_PLUGINPATH" );
408+
if (cpaths == NULL)
409+
return QStringList();
410+
411+
QString paths = QString::fromLocal8Bit( cpaths );
412+
if ( paths.contains( ';' ) ) // keep windows users happy
413+
return paths.split( ';' );
414+
else if ( paths.contains( ':' ) ) // keep unix users happy
415+
return paths.split( ':' );
416+
else
417+
return QStringList( paths ); // just one path
418+
}
419+
420+
401421
QStringList QgsPythonUtilsImpl::pluginList()
402422
{
403423
runString( "qgis.utils.updateAvailablePlugins()" );

src/python/qgspythonutilsimpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
7979
//! return current path for home directory python plugins
8080
QString homePluginsPath();
8181

82+
//! return a list of extra plugins paths passed with QGIS_PLUGINPATH environment variable
83+
QStringList extraPluginsPaths();
84+
8285
//! return list of all available python plugins
8386
QStringList pluginList();
8487

0 commit comments

Comments
 (0)