Skip to content
Permalink
Browse files
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/qgis@13943 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jul 20, 2010
1 parent 1734caa commit ac8383e03215864055417f85c7f6f1457bc7280e
Showing with 41 additions and 30 deletions.
  1. +8 −20 python/utils.py
  2. +30 −10 src/python/qgspythonutilsimpl.cpp
  3. +3 −0 src/python/qgspythonutilsimpl.h
@@ -60,6 +60,9 @@ def initInterface(pointer):
#######################
# PLUGINS

# list of plugin paths. it gets filled in by the QGIS python library
plugin_paths = []

# dictionary of plugins
plugins = {}

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

def updateAvailablePlugins():
from qgis.core import QgsApplication
plugindirs = [
unicode(QgsApplication.pkgDataPath()) + "/python/plugins",
unicode(QgsApplication.qgisSettingsDirPath()) + "/python/plugins"
]
env = os.environ.get("QGIS_PLUGINPATH")
if env:
plugindirs.extend(env.split(";"))

""" go thrgouh the plugin_paths list and find out what plugins are available """
# merge the lists
plugins = []
for pluginpath in plugindirs:
newplugins = findPlugins(pluginpath)
if len(newplugins) > 0:
if pluginpath not in sys.path:
sys.path.append(pluginpath)
for p in newplugins:
if p not in plugins:
plugins.append(p)
for pluginpath in plugin_paths:
for p in findPlugins(pluginpath):
if p not in plugins:
plugins.append(p)

global available_plugins
available_plugins = plugins

# update list on start
updateAvailablePlugins()


def pluginMetadata(packageName, fct):
""" fetch metadata from a plugin """
@@ -59,17 +59,18 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
runString( "import sys" ); // import sys module (for display / exception hooks)
runString( "import os" ); // import os module (for user paths)

// construct a list of plugin paths
// plugin dirs passed in QGIS_PLUGINPATH env. variable have highest priority (usually empty)
// locally installed plugins have priority over the system plugins
QStringList pluginpaths = extraPluginsPaths();
pluginpaths << homePluginsPath() << pluginsPath();

// expect that bindings are installed locally, so add the path to modules
// also add path to plugins
#ifdef Q_OS_WIN
runString( "oldhome=None\n" );
runString( "if os.environ.has_key('HOME'): oldhome=os.environ['HOME']\n" );
runString( "os.environ['HOME']=os.environ['USERPROFILE']" );
#endif
runString( "sys.path = [\"" + pythonPath() + "\", os.path.expanduser(\"~/.qgis/python\"), os.path.expanduser(\"~/.qgis/python/plugins\"), \"" + pluginsPath() + "\" ] + sys.path" );
#ifdef Q_OS_WIN
runString( "if oldhome: os.environ['HOME']=oldhome\n" );
#endif
QStringList newpaths;
newpaths << pythonPath() << homePythonPath();
newpaths += pluginpaths;
runString( "sys.path = [\"" + newpaths.join("\", \"") + "\"] + sys.path" );

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

// tell the utils script where to look for the plugins
runString( "qgis.utils.plugin_paths = [\"" + pluginpaths.join( "\",\"" ) + "\"]" );

// initialize 'iface' object
runString( "qgis.utils.initInterface(" + QString::number(( unsigned long ) interface ) + ")" );
}
@@ -390,14 +394,30 @@ QString QgsPythonUtilsImpl::pluginsPath()

QString QgsPythonUtilsImpl::homePythonPath()
{
return QgsApplication::qgisSettingsDirPath() + "/python";
return QgsApplication::qgisSettingsDirPath() + "python";
}

QString QgsPythonUtilsImpl::homePluginsPath()
{
return homePythonPath() + "/plugins";
}

QStringList QgsPythonUtilsImpl::extraPluginsPaths()
{
const char* cpaths = getenv( "QGIS_PLUGINPATH" );
if (cpaths == NULL)
return QStringList();

QString paths = QString::fromLocal8Bit( cpaths );
if ( paths.contains( ';' ) ) // keep windows users happy
return paths.split( ';' );
else if ( paths.contains( ':' ) ) // keep unix users happy
return paths.split( ':' );
else
return QStringList( paths ); // just one path
}


QStringList QgsPythonUtilsImpl::pluginList()
{
runString( "qgis.utils.updateAvailablePlugins()" );
@@ -79,6 +79,9 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
//! return current path for home directory python plugins
QString homePluginsPath();

//! return a list of extra plugins paths passed with QGIS_PLUGINPATH environment variable
QStringList extraPluginsPaths();

//! return list of all available python plugins
QStringList pluginList();

0 comments on commit ac8383e

Please sign in to comment.