Skip to content

Commit f844e2f

Browse files
committedApr 15, 2012
GdalTools: display a friendly message when python-gdal is missing
1 parent be8ec1b commit f844e2f

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed
 

‎python/plugins/GdalTools/GdalTools.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,40 @@
2525
# Initialize Qt resources from file resources_rc.py
2626
import resources_rc
2727

28-
# Import required modules and if missing show the right module's name
29-
req_mods = { "osgeo": "osgeo [python-gdal]" }
28+
# are all dependecies satisfied?
29+
valid = True
3030

31+
# Import required modules
32+
req_mods = { "osgeo": "osgeo [python-gdal]" }
3133
try:
3234
from osgeo import gdal
3335
from osgeo import ogr
3436
except ImportError, e:
35-
error_str = e.args[0]
36-
error_mod = error_str.replace( "No module named ", "" )
37-
if req_mods.has_key( error_mod ):
38-
error_str = error_str.replace( error_mod, req_mods[error_mod] )
39-
raise ImportError( error_str )
37+
valid = False
38+
39+
# if the plugin is shipped with QGis catch the exception and
40+
# display an error message
41+
import os.path
42+
qgisUserPluginPath = os.path.abspath( os.path.join( str( QgsApplication.qgisSettingsDirPath() ), "python") )
43+
if not os.path.dirname(__file__).startswith( qgisUserPluginPath ):
44+
title = QCoreApplication.translate( "GdalTools", "Plugin error" )
45+
message = QCoreApplication.translate( "GdalTools", u'Unable to load %1 plugin. \nThe required "%2" module is missing. \nInstall it and try again.' )
46+
import qgis.utils
47+
QMessageBox.warning( qgis.utils.iface.mainWindow(), title, message.arg( "GdalTools" ).arg( req_mods["osgeo"] ) )
48+
else:
49+
# if a module is missing show a more friendly module's name
50+
error_str = e.args[0]
51+
error_mod = error_str.replace( "No module named ", "" )
52+
if req_mods.has_key( error_mod ):
53+
error_str = error_str.replace( error_mod, req_mods[error_mod] )
54+
raise ImportError( error_str )
4055

4156

4257
class GdalTools:
4358

4459
def __init__( self, iface ):
60+
if not valid: return
61+
4562
# Save reference to the QGIS interface
4663
self.iface = iface
4764
try:
@@ -72,6 +89,7 @@ def __init__( self, iface ):
7289
QCoreApplication.installTranslator( self.translator )
7390

7491
def initGui( self ):
92+
if not valid: return
7593
if int( self.QgisVersion ) < 1:
7694
QMessageBox.warning( self.iface.getMainWindow(), "Gdal Tools",
7795
QCoreApplication.translate( "GdalTools", "Quantum GIS version detected: " ) +unicode( self.QgisVersion )+".xx\n"
@@ -258,6 +276,7 @@ def initGui( self ):
258276
menu_bar.insertMenu( lastAction, self.menu )
259277

260278
def unload( self ):
279+
if not valid: return
261280
pass
262281

263282
def doBuildVRT( self ):

0 commit comments

Comments
 (0)
Please sign in to comment.