Skip to content

Commit 7d11220

Browse files
author
wonder
committed
Manage python plugins loaded state in python utils.py module.
This allows seamless loading/unloading of plugins by plugin installer. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12552 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent df933c3 commit 7d11220

7 files changed

+97
-40
lines changed

python/utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def uninstallConsoleHooks():
9191
# dictionary of plugins
9292
plugins = {}
9393

94+
# list of active (started) plugins
95+
active_plugins = []
96+
9497
def pluginMetadata(packageName, fct):
9598
""" fetch metadata from a plugin """
9699
try:
@@ -125,7 +128,9 @@ def loadPlugin(packageName):
125128

126129
def startPlugin(packageName):
127130
""" initialize the plugin """
128-
global plugins, iface
131+
global plugins, active_plugins, iface
132+
133+
if packageName in active_plugins: return False
129134

130135
package = sys.modules[packageName]
131136

@@ -147,20 +152,32 @@ def startPlugin(packageName):
147152
showException(sys.exc_type, sys.exc_value, sys.exc_traceback, msg)
148153
return False
149154

155+
# add to active plugins
156+
active_plugins.append(packageName)
157+
150158
return True
151159

152160

153161
def unloadPlugin(packageName):
154162
""" unload and delete plugin! """
155-
global plugins
163+
global plugins, active_plugins
156164

157165
if not plugins.has_key(packageName): return False
166+
if packageName not in active_plugins: return False
158167

159168
try:
160169
plugins[packageName].unload()
161170
del plugins[packageName]
171+
active_plugins.remove(packageName)
162172
return True
163173
except Exception, e:
164174
msg = QCoreApplication.translate("Python", "Error while unloading plugin %1").arg(packageName)
165175
showException(sys.exc_type, sys.exc_value, sys.exc_traceback, msg)
166176
return False
177+
178+
def isPluginLoaded(packageName):
179+
""" find out whether a plugin is active (i.e. has been started) """
180+
global plugins, active_plugins
181+
182+
if not plugins.has_key(packageName): return False
183+
return (packageName in active_plugins)

src/app/qgspluginmetadata.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
#include "qgspluginmetadata.h"
2222
QgsPluginMetadata::QgsPluginMetadata( QString _libraryPath,
2323
QString _name,
24-
QgisPlugin * _plugin,
25-
bool _python ):
24+
QgisPlugin * _plugin ):
2625
m_name( _name ),
2726
libraryPath( _libraryPath ),
28-
m_plugin( _plugin ),
29-
m_python( _python )
27+
m_plugin( _plugin )
3028
{
3129

3230
}
@@ -45,8 +43,3 @@ QgisPlugin *QgsPluginMetadata::plugin()
4543
{
4644
return m_plugin;
4745
}
48-
49-
bool QgsPluginMetadata::isPython()
50-
{
51-
return m_python;
52-
}

src/app/qgspluginmetadata.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ class QgisPlugin;
3030
class QgsPluginMetadata
3131
{
3232
public:
33-
QgsPluginMetadata( QString _libraryPath, QString _name, QgisPlugin *_plugin, bool _python = false );
33+
QgsPluginMetadata( QString _libraryPath, QString _name, QgisPlugin *_plugin );
3434
QString name();
3535
QString library();
3636
QgisPlugin *plugin();
37-
bool isPython();
3837
private:
3938
QString m_name;
4039
QString libraryPath;
4140
QgisPlugin *m_plugin;
42-
bool m_python;
4341
};
4442
#endif //QGSPLUGINMETADATA_H
4543

src/app/qgspluginregistry.cpp

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,30 @@ void QgsPluginRegistry::setPythonUtils( QgsPythonUtils* pythonUtils )
6868
bool QgsPluginRegistry::isLoaded( QString key )
6969
{
7070
QMap<QString, QgsPluginMetadata>::iterator it = mPlugins.find( key );
71-
return ( it != mPlugins.end() );
71+
if ( it != mPlugins.end() ) // found a c++ plugin?
72+
return true;
73+
74+
if ( mPythonUtils && mPythonUtils->isEnabled() )
75+
{
76+
return mPythonUtils->isPluginLoaded( key );
77+
}
78+
79+
return false;
7280
}
7381

7482
QString QgsPluginRegistry::library( QString key )
7583
{
7684
QMap<QString, QgsPluginMetadata>::iterator it = mPlugins.find( key );
77-
if ( it == mPlugins.end() )
78-
return QString();
85+
if ( it != mPlugins.end() )
86+
return it->library();
7987

80-
return it->library();
88+
if ( mPythonUtils && mPythonUtils->isEnabled() )
89+
{
90+
if ( mPythonUtils->isPluginLoaded( key ) )
91+
return key;
92+
}
93+
94+
return QString();
8195
}
8296

8397
QgisPlugin *QgsPluginRegistry::plugin( QString key )
@@ -86,15 +100,19 @@ QgisPlugin *QgsPluginRegistry::plugin( QString key )
86100
if ( it == mPlugins.end() )
87101
return NULL;
88102

103+
// note: not used by python plugins
104+
89105
return it->plugin();
90106
}
91107

92108
bool QgsPluginRegistry::isPythonPlugin( QString key )
93109
{
94-
QMap<QString, QgsPluginMetadata>::iterator it = mPlugins.find( key );
95-
if ( it == mPlugins.end() )
96-
return false;
97-
return it->isPython();
110+
if ( mPythonUtils && mPythonUtils->isEnabled() )
111+
{
112+
if ( mPythonUtils->isPluginLoaded( key ) )
113+
return true;
114+
}
115+
return false;
98116
}
99117

100118
void QgsPluginRegistry::addPlugin( QString key, QgsPluginMetadata metadata )
@@ -104,16 +122,24 @@ void QgsPluginRegistry::addPlugin( QString key, QgsPluginMetadata metadata )
104122

105123
void QgsPluginRegistry::dump()
106124
{
107-
QgsDebugMsg( "PLUGINS IN REGISTRY: key -> (name, library, isPython)" );
125+
QgsDebugMsg( "PLUGINS IN REGISTRY: key -> (name, library)" );
108126
for ( QMap<QString, QgsPluginMetadata>::iterator it = mPlugins.begin();
109127
it != mPlugins.end();
110128
it++ )
111129
{
112-
QgsDebugMsg( QString( "PLUGIN: %1 -> (%2, %3, %4)" )
130+
QgsDebugMsg( QString( "PLUGIN: %1 -> (%2, %3)" )
113131
.arg( it.key() )
114132
.arg( it->name() )
115-
.arg( it->library() )
116-
.arg( it->isPython() ) );
133+
.arg( it->library() ) );
134+
}
135+
136+
if ( mPythonUtils && mPythonUtils->isEnabled() )
137+
{
138+
QgsDebugMsg( "PYTHON PLUGINS IN REGISTRY:" );
139+
foreach( QString pluginName, mPythonUtils->listActivePlugins() )
140+
{
141+
QgsDebugMsg( pluginName );
142+
}
117143
}
118144
}
119145

@@ -126,6 +152,8 @@ void QgsPluginRegistry::removePlugin( QString key )
126152
{
127153
mPlugins.erase( it );
128154
}
155+
156+
// python plugins are removed when unloaded
129157
}
130158

131159
void QgsPluginRegistry::unloadAll()
@@ -134,19 +162,17 @@ void QgsPluginRegistry::unloadAll()
134162
it != mPlugins.end();
135163
it++ )
136164
{
137-
if ( isPythonPlugin( it.key() ) )
138-
{
139-
if ( mPythonUtils )
140-
mPythonUtils->unloadPlugin( it->library() );
141-
else
142-
QgsDebugMsg( "warning: python utils is NULL" );
143-
}
165+
if ( it->plugin() )
166+
it->plugin()->unload();
144167
else
168+
QgsDebugMsg( "warning: plugin is NULL:" + it.key() );
169+
}
170+
171+
if ( mPythonUtils && mPythonUtils->isEnabled() )
172+
{
173+
foreach( QString pluginName, mPythonUtils->listActivePlugins() )
145174
{
146-
if ( it->plugin() )
147-
it->plugin()->unload();
148-
else
149-
QgsDebugMsg( "warning: plugin is NULL:" + it.key() );
175+
mPythonUtils->unloadPlugin( pluginName );
150176
}
151177
}
152178
}
@@ -222,9 +248,6 @@ void QgsPluginRegistry::loadPythonPlugin( QString packageName )
222248

223249
QString pluginName = mPythonUtils->getPluginMetadata( packageName, "name" );
224250

225-
// add to plugin registry
226-
addPlugin( packageName, QgsPluginMetadata( packageName, pluginName, NULL, true ) );
227-
228251
// add to settings
229252
settings.setValue( "/PythonPlugins/" + packageName, true );
230253
std::cout << "Loaded : " << pluginName.toLocal8Bit().constData() << " (package: "

src/python/qgspythonutils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ class PYTHON_EXPORT QgsPythonUtils
7676
//! return list of all available python plugins
7777
virtual QStringList pluginList() = 0;
7878

79+
//! return whether the plugin is loaded (active)
80+
virtual bool isPluginLoaded( QString packageName ) = 0;
81+
82+
//! return a list of active plugins
83+
virtual QStringList listActivePlugins() = 0;
84+
7985
//! load python plugin (import)
8086
virtual bool loadPlugin( QString packageName ) = 0;
8187

src/python/qgspythonutilsimpl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,17 @@ bool QgsPythonUtilsImpl::unloadPlugin( QString packageName )
475475
evalString( "qgis.utils.unloadPlugin('" + packageName + "')", output );
476476
return ( output == "True" );
477477
}
478+
479+
bool QgsPythonUtilsImpl::isPluginLoaded( QString packageName )
480+
{
481+
QString output;
482+
evalString( "qgis.utils.isPluginLoaded('" + packageName + "')", output );
483+
return ( output == "True" );
484+
}
485+
486+
QStringList QgsPythonUtilsImpl::listActivePlugins()
487+
{
488+
QString output;
489+
evalString( "'\\n'.join(qgis.utils.active_plugins)", output );
490+
return output.split( QChar( '\n' ) );
491+
}

src/python/qgspythonutilsimpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ class QgsPythonUtilsImpl : public QgsPythonUtils
9898
//! return list of all available python plugins
9999
QStringList pluginList();
100100

101+
//! return whether the plugin is loaded (active)
102+
virtual bool isPluginLoaded( QString packageName );
103+
104+
//! return a list of active plugins
105+
virtual QStringList listActivePlugins();
106+
101107
//! load python plugin (import)
102108
bool loadPlugin( QString packageName );
103109

0 commit comments

Comments
 (0)