2010-01-07 14:59:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-10-04 19:33:47 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
utils.py
|
|
|
|
---------------------
|
|
|
|
Date : November 2009
|
|
|
|
Copyright : (C) 2009 by Martin Dobias
|
2012-10-08 00:29:13 +02:00
|
|
|
Email : wonder dot sk at gmail dot com
|
2012-10-04 19:33:47 +02:00
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Martin Dobias'
|
|
|
|
__date__ = 'November 2009'
|
|
|
|
__copyright__ = '(C) 2009, Martin Dobias'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
"""
|
|
|
|
QGIS utilities module
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2013-05-29 22:08:16 +10:00
|
|
|
from PyQt4.QtCore import QCoreApplication, QLocale
|
2015-09-28 17:13:41 +10:00
|
|
|
from PyQt4.QtGui import QPushButton
|
2014-11-29 23:21:38 +10:00
|
|
|
from qgis.core import QGis, QgsExpression, QgsMessageLog, qgsfunction
|
2015-09-28 17:13:41 +10:00
|
|
|
from qgis.gui import QgsMessageBar
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
2010-01-07 14:59:27 +00:00
|
|
|
import glob
|
|
|
|
import os.path
|
2011-11-08 18:54:23 -03:00
|
|
|
import ConfigParser
|
2013-01-24 03:08:20 +01:00
|
|
|
import warnings
|
2013-06-09 10:56:35 +02:00
|
|
|
import codecs
|
2014-09-18 09:38:52 +10:00
|
|
|
import time
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
# ######################
|
2009-11-07 12:04:42 +00:00
|
|
|
# ERROR HANDLING
|
|
|
|
|
2013-01-24 03:08:20 +01:00
|
|
|
warnings.simplefilter('default')
|
2013-01-26 14:16:39 +01:00
|
|
|
warnings.filterwarnings("ignore", "the sets module is deprecated")
|
2013-01-24 03:08:20 +01:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
|
2013-01-24 03:08:20 +01:00
|
|
|
def showWarning(message, category, filename, lineno, file=None, line=None):
|
2014-11-29 23:18:18 +10:00
|
|
|
stk = ""
|
|
|
|
for s in traceback.format_stack()[:-2]:
|
|
|
|
stk += s.decode('utf-8', 'replace')
|
|
|
|
QgsMessageLog.logMessage(
|
2015-08-22 14:29:41 +02:00
|
|
|
"warning:%s\ntraceback:%s" % (warnings.formatwarning(message, category, filename, lineno), stk),
|
2014-11-29 23:18:18 +10:00
|
|
|
QCoreApplication.translate("Python", "Python warning")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-01-24 03:08:20 +01:00
|
|
|
warnings.showwarning = showWarning
|
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
|
2015-09-28 17:13:41 +10:00
|
|
|
def showException(type, value, tb, msg, messagebar=False):
|
2014-11-29 23:18:18 +10:00
|
|
|
lst = traceback.format_exception(type, value, tb)
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
if msg is None:
|
2014-11-29 23:18:18 +10:00
|
|
|
msg = QCoreApplication.translate('Python', 'An error has occured while executing Python code:')
|
|
|
|
txt = '<font color="red">%s</font><br><br><pre>' % msg
|
|
|
|
for s in lst:
|
|
|
|
txt += s.decode('utf-8', 'replace')
|
|
|
|
txt += '</pre><br>%s<br>%s<br><br>' % (QCoreApplication.translate('Python', 'Python version:'), sys.version)
|
|
|
|
txt += '<br>%s<br>%s %s, %s<br><br>' % (
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
QCoreApplication.translate('Python', 'QGIS version:'), QGis.QGIS_VERSION, QGis.QGIS_RELEASE_NAME,
|
|
|
|
QGis.QGIS_DEV_VERSION)
|
2014-11-29 23:18:18 +10:00
|
|
|
txt += '%s %s' % (QCoreApplication.translate('Python', 'Python path:'), str(sys.path))
|
|
|
|
txt = txt.replace('\n', '<br>')
|
|
|
|
txt = txt.replace(' ', ' ') # preserve whitespaces for nicer output
|
|
|
|
|
|
|
|
from qgis.core import QgsMessageOutput
|
|
|
|
|
2015-09-28 17:13:41 +10:00
|
|
|
title = QCoreApplication.translate('Python', 'Python error')
|
|
|
|
logmessage = ''
|
|
|
|
for s in lst:
|
|
|
|
logmessage += s.decode('utf-8', 'replace')
|
|
|
|
|
|
|
|
QgsMessageLog.logMessage(logmessage, title)
|
|
|
|
|
|
|
|
if messagebar and iface:
|
|
|
|
widget = iface.messageBar().createMessage(title, msg + " See message log (Python Error) for more details.")
|
|
|
|
button = QPushButton("View message log", pressed=iface.openMessageLog)
|
|
|
|
widget.layout().addWidget(button)
|
|
|
|
iface.messageBar().pushWidget(widget, QgsMessageBar.WARNING)
|
|
|
|
else:
|
|
|
|
msg = QgsMessageOutput.createMessageOutput()
|
|
|
|
msg.setTitle()
|
|
|
|
msg.setMessage(txt, QgsMessageOutput.MessageHtml)
|
|
|
|
msg.showMessage()
|
2014-11-29 23:18:18 +10:00
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
|
|
|
|
def qgis_excepthook(type, value, tb):
|
2015-09-28 17:13:41 +10:00
|
|
|
showException(type, value, tb, None, messagebar=True)
|
2014-11-29 23:18:18 +10:00
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
|
|
|
|
def installErrorHook():
|
2014-11-29 23:18:18 +10:00
|
|
|
sys.excepthook = qgis_excepthook
|
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
|
|
|
|
def uninstallErrorHook():
|
2014-11-29 23:18:18 +10:00
|
|
|
sys.excepthook = sys.__excepthook__
|
2009-11-07 12:04:42 +00:00
|
|
|
|
|
|
|
# install error hook() on module load
|
|
|
|
installErrorHook()
|
|
|
|
|
|
|
|
# initialize 'iface' object
|
|
|
|
iface = None
|
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
def initInterface(pointer):
|
2014-11-29 23:18:18 +10:00
|
|
|
from qgis.gui import QgisInterface
|
|
|
|
from sip import wrapinstance
|
|
|
|
|
|
|
|
global iface
|
|
|
|
iface = wrapinstance(pointer, QgisInterface)
|
2009-11-07 12:04:42 +00:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# PLUGINS
|
|
|
|
|
2010-07-20 13:06:49 +00:00
|
|
|
# list of plugin paths. it gets filled in by the QGIS python library
|
|
|
|
plugin_paths = []
|
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
# dictionary of plugins
|
|
|
|
plugins = {}
|
|
|
|
|
2014-09-18 09:38:52 +10:00
|
|
|
plugin_times = {}
|
|
|
|
|
2009-12-20 22:12:16 +00:00
|
|
|
# list of active (started) plugins
|
|
|
|
active_plugins = []
|
|
|
|
|
2010-01-07 14:59:27 +00:00
|
|
|
# list of plugins in plugin directory and home plugin directory
|
|
|
|
available_plugins = []
|
|
|
|
|
2011-11-08 18:54:23 -03:00
|
|
|
# dictionary of plugins providing metadata in a text file (metadata.txt)
|
|
|
|
# key = plugin package name, value = config parser instance
|
|
|
|
plugins_metadata_parser = {}
|
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
|
2010-01-09 20:50:14 +00:00
|
|
|
def findPlugins(path):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" for internal use: return list of plugins in given path """
|
|
|
|
for plugin in glob.glob(path + "/*"):
|
|
|
|
if not os.path.isdir(plugin):
|
|
|
|
continue
|
|
|
|
if not os.path.exists(os.path.join(plugin, '__init__.py')):
|
|
|
|
continue
|
2013-03-19 00:06:48 +01:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
metadataFile = os.path.join(plugin, 'metadata.txt')
|
|
|
|
if not os.path.exists(metadataFile):
|
|
|
|
continue
|
2013-03-19 00:06:48 +01:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
cp = ConfigParser.ConfigParser()
|
2013-06-09 10:56:35 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
try:
|
|
|
|
cp.readfp(codecs.open(metadataFile, "r", "utf8"))
|
|
|
|
except:
|
|
|
|
cp = None
|
2013-03-19 00:06:48 +01:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
pluginName = os.path.basename(plugin)
|
|
|
|
yield (pluginName, cp)
|
2010-01-07 14:59:27 +00:00
|
|
|
|
2011-11-08 18:54:23 -03:00
|
|
|
|
2010-01-07 14:59:27 +00:00
|
|
|
def updateAvailablePlugins():
|
2014-11-29 23:18:18 +10:00
|
|
|
""" Go through the plugin_paths list and find out what plugins are available. """
|
|
|
|
# merge the lists
|
|
|
|
plugins = []
|
|
|
|
metadata_parser = {}
|
|
|
|
for pluginpath in plugin_paths:
|
|
|
|
for pluginName, parser in findPlugins(pluginpath):
|
2015-08-22 14:29:41 +02:00
|
|
|
if parser is None:
|
|
|
|
continue
|
2014-11-29 23:18:18 +10:00
|
|
|
if pluginName not in plugins:
|
|
|
|
plugins.append(pluginName)
|
|
|
|
metadata_parser[pluginName] = parser
|
|
|
|
|
|
|
|
global available_plugins
|
|
|
|
available_plugins = plugins
|
|
|
|
global plugins_metadata_parser
|
|
|
|
plugins_metadata_parser = metadata_parser
|
2010-01-07 14:59:27 +00:00
|
|
|
|
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
def pluginMetadata(packageName, fct):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" fetch metadata from a plugin - use values from metadata.txt """
|
|
|
|
try:
|
|
|
|
return plugins_metadata_parser[packageName].get('general', fct)
|
|
|
|
except Exception:
|
|
|
|
return "__error__"
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2013-03-19 00:06:48 +01:00
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
def loadPlugin(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" load plugin's package """
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
try:
|
|
|
|
__import__(packageName)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
pass # continue...
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
# snake in the grass, we know it's there
|
|
|
|
sys.path_importer_cache.clear()
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
# retry
|
|
|
|
try:
|
|
|
|
__import__(packageName)
|
|
|
|
return True
|
|
|
|
except:
|
2015-09-28 17:13:41 +10:00
|
|
|
msgTemplate = QCoreApplication.translate("Python", "Couldn't load plugin '%s'")
|
|
|
|
msg = msgTemplate % packageName
|
|
|
|
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
|
2014-11-29 23:18:18 +10:00
|
|
|
return False
|
2009-11-07 12:04:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def startPlugin(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" initialize the plugin """
|
|
|
|
global plugins, active_plugins, iface, plugin_times
|
2009-12-20 22:12:16 +00:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
if packageName in active_plugins:
|
|
|
|
return False
|
|
|
|
if packageName not in sys.modules:
|
|
|
|
return False
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
package = sys.modules[packageName]
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
errMsg = QCoreApplication.translate("Python", "Couldn't load plugin %s") % packageName
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
start = time.clock()
|
|
|
|
# create an instance of the plugin
|
|
|
|
try:
|
|
|
|
plugins[packageName] = package.classFactory(iface)
|
|
|
|
except:
|
|
|
|
_unloadPluginModules(packageName)
|
|
|
|
msg = QCoreApplication.translate("Python", "%s due to an error when calling its classFactory() method") % errMsg
|
2015-09-28 17:13:41 +10:00
|
|
|
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
|
2014-11-29 23:18:18 +10:00
|
|
|
return False
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
# initGui
|
|
|
|
try:
|
|
|
|
plugins[packageName].initGui()
|
|
|
|
except:
|
|
|
|
del plugins[packageName]
|
|
|
|
_unloadPluginModules(packageName)
|
|
|
|
msg = QCoreApplication.translate("Python", "%s due to an error when calling its initGui() method") % errMsg
|
2015-09-28 17:13:41 +10:00
|
|
|
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
|
2014-11-29 23:18:18 +10:00
|
|
|
return False
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
# add to active plugins
|
|
|
|
active_plugins.append(packageName)
|
|
|
|
end = time.clock()
|
|
|
|
plugin_times[packageName] = "{0:02f}s".format(end - start)
|
2009-12-20 22:12:16 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
return True
|
2009-11-07 12:04:42 +00:00
|
|
|
|
|
|
|
|
2010-03-12 07:58:48 +00:00
|
|
|
def canUninstallPlugin(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" confirm that the plugin can be uninstalled """
|
|
|
|
global plugins, active_plugins
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
if packageName not in plugins:
|
|
|
|
return False
|
|
|
|
if packageName not in active_plugins:
|
|
|
|
return False
|
2014-11-29 23:18:18 +10:00
|
|
|
|
|
|
|
try:
|
|
|
|
metadata = plugins[packageName]
|
|
|
|
if "canBeUninstalled" not in dir(metadata):
|
|
|
|
return True
|
|
|
|
return bool(metadata.canBeUninstalled())
|
|
|
|
except:
|
|
|
|
msg = "Error calling " + packageName + ".canBeUninstalled"
|
2015-09-28 17:13:41 +10:00
|
|
|
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
|
2014-11-29 23:18:18 +10:00
|
|
|
return True
|
2010-03-12 07:58:48 +00:00
|
|
|
|
|
|
|
|
2009-11-07 12:04:42 +00:00
|
|
|
def unloadPlugin(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" unload and delete plugin! """
|
|
|
|
global plugins, active_plugins
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
if packageName not in plugins:
|
|
|
|
return False
|
|
|
|
if packageName not in active_plugins:
|
|
|
|
return False
|
2009-11-07 12:04:42 +00:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
try:
|
|
|
|
plugins[packageName].unload()
|
|
|
|
del plugins[packageName]
|
|
|
|
active_plugins.remove(packageName)
|
|
|
|
_unloadPluginModules(packageName)
|
|
|
|
return True
|
2015-08-22 14:29:41 +02:00
|
|
|
except Exception as e:
|
2014-11-29 23:18:18 +10:00
|
|
|
msg = QCoreApplication.translate("Python", "Error while unloading plugin %s") % packageName
|
2015-09-28 17:13:41 +10:00
|
|
|
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg, messagebar=True)
|
2014-11-29 23:18:18 +10:00
|
|
|
return False
|
2009-12-20 22:12:16 +00:00
|
|
|
|
2010-01-07 14:59:27 +00:00
|
|
|
|
|
|
|
def _unloadPluginModules(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" unload plugin package with all its modules (files) """
|
|
|
|
global _plugin_modules
|
|
|
|
mods = _plugin_modules[packageName]
|
|
|
|
|
|
|
|
for mod in mods:
|
|
|
|
# if it looks like a Qt resource file, try to do a cleanup
|
|
|
|
# otherwise we might experience a segfault next time the plugin is loaded
|
|
|
|
# because Qt will try to access invalid plugin resource data
|
|
|
|
try:
|
|
|
|
if hasattr(sys.modules[mod], 'qCleanupResources'):
|
|
|
|
sys.modules[mod].qCleanupResources()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
# try to remove the module from python
|
|
|
|
try:
|
|
|
|
del sys.modules[mod]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
# remove the plugin entry
|
|
|
|
del _plugin_modules[packageName]
|
2010-01-07 14:59:27 +00:00
|
|
|
|
|
|
|
|
2009-12-20 22:12:16 +00:00
|
|
|
def isPluginLoaded(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" find out whether a plugin is active (i.e. has been started) """
|
|
|
|
global plugins, active_plugins
|
2009-12-20 22:12:16 +00:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
if packageName not in plugins:
|
|
|
|
return False
|
2014-11-29 23:18:18 +10:00
|
|
|
return (packageName in active_plugins)
|
2010-01-07 14:59:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def reloadPlugin(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" unload and start again a plugin """
|
|
|
|
global active_plugins
|
|
|
|
if packageName not in active_plugins:
|
|
|
|
return # it's not active
|
|
|
|
|
|
|
|
unloadPlugin(packageName)
|
|
|
|
loadPlugin(packageName)
|
|
|
|
startPlugin(packageName)
|
|
|
|
|
|
|
|
|
|
|
|
def showPluginHelp(packageName=None, filename="index", section=""):
|
|
|
|
""" show a help in the user's html browser. The help file should be named index-ll_CC.html or index-ll.html"""
|
|
|
|
try:
|
|
|
|
source = ""
|
|
|
|
if packageName is None:
|
|
|
|
import inspect
|
|
|
|
|
|
|
|
source = inspect.currentframe().f_back.f_code.co_filename
|
|
|
|
else:
|
|
|
|
source = sys.modules[packageName].__file__
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
path = os.path.dirname(source)
|
|
|
|
locale = str(QLocale().name())
|
|
|
|
helpfile = os.path.join(path, filename + "-" + locale + ".html")
|
|
|
|
if not os.path.exists(helpfile):
|
|
|
|
helpfile = os.path.join(path, filename + "-" + locale.split("_")[0] + ".html")
|
|
|
|
if not os.path.exists(helpfile):
|
|
|
|
helpfile = os.path.join(path, filename + "-en.html")
|
|
|
|
if not os.path.exists(helpfile):
|
|
|
|
helpfile = os.path.join(path, filename + "-en_US.html")
|
|
|
|
if not os.path.exists(helpfile):
|
|
|
|
helpfile = os.path.join(path, filename + ".html")
|
|
|
|
if os.path.exists(helpfile):
|
|
|
|
url = "file://" + helpfile
|
|
|
|
if section != "":
|
|
|
|
url = url + "#" + section
|
|
|
|
iface.openURL(url, False)
|
2010-03-20 09:23:38 +00:00
|
|
|
|
|
|
|
|
2010-12-12 19:19:54 +00:00
|
|
|
def pluginDirectory(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" return directory where the plugin resides. Plugin must be loaded already """
|
|
|
|
return os.path.dirname(sys.modules[packageName].__file__)
|
|
|
|
|
2010-12-12 19:19:54 +00:00
|
|
|
|
2012-08-21 08:49:02 +02:00
|
|
|
def reloadProjectMacros():
|
2014-11-29 23:18:18 +10:00
|
|
|
# unload old macros
|
|
|
|
unloadProjectMacros()
|
2012-08-21 08:49:02 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
from qgis.core import QgsProject
|
2012-08-21 08:49:02 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
code, ok = QgsProject.instance().readEntry("Macros", "/pythonCode")
|
|
|
|
if not ok or not code or code == '':
|
|
|
|
return
|
2012-08-21 08:49:02 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
# create a new empty python module
|
|
|
|
import imp
|
|
|
|
|
|
|
|
mod = imp.new_module("proj_macros_mod")
|
|
|
|
|
|
|
|
# set the module code and store it sys.modules
|
2015-08-22 14:29:41 +02:00
|
|
|
exec(unicode(code), mod.__dict__)
|
2014-11-29 23:18:18 +10:00
|
|
|
sys.modules["proj_macros_mod"] = mod
|
|
|
|
|
|
|
|
# load new macros
|
|
|
|
openProjectMacro()
|
2012-08-21 08:49:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def unloadProjectMacros():
|
2014-11-29 23:18:18 +10:00
|
|
|
if "proj_macros_mod" not in sys.modules:
|
|
|
|
return
|
|
|
|
# unload old macros
|
|
|
|
closeProjectMacro()
|
|
|
|
# destroy the reference to the module
|
|
|
|
del sys.modules["proj_macros_mod"]
|
2012-08-21 08:49:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def openProjectMacro():
|
2014-11-29 23:18:18 +10:00
|
|
|
if "proj_macros_mod" not in sys.modules:
|
|
|
|
return
|
|
|
|
mod = sys.modules["proj_macros_mod"]
|
|
|
|
if hasattr(mod, 'openProject'):
|
|
|
|
mod.openProject()
|
|
|
|
|
2012-08-21 08:49:02 +02:00
|
|
|
|
|
|
|
def saveProjectMacro():
|
2014-11-29 23:18:18 +10:00
|
|
|
if "proj_macros_mod" not in sys.modules:
|
|
|
|
return
|
|
|
|
mod = sys.modules["proj_macros_mod"]
|
|
|
|
if hasattr(mod, 'saveProject'):
|
|
|
|
mod.saveProject()
|
|
|
|
|
2012-08-21 08:49:02 +02:00
|
|
|
|
|
|
|
def closeProjectMacro():
|
2014-11-29 23:18:18 +10:00
|
|
|
if "proj_macros_mod" not in sys.modules:
|
|
|
|
return
|
|
|
|
mod = sys.modules["proj_macros_mod"]
|
|
|
|
if hasattr(mod, 'closeProject'):
|
|
|
|
mod.closeProject()
|
2012-08-21 08:49:02 +02:00
|
|
|
|
|
|
|
|
2014-10-09 15:05:11 +02:00
|
|
|
#######################
|
|
|
|
# SERVER PLUGINS
|
|
|
|
#
|
|
|
|
# TODO: move into server_utils.py ?
|
|
|
|
|
|
|
|
# list of plugin paths. it gets filled in by the QGIS python library
|
|
|
|
server_plugin_paths = []
|
|
|
|
|
|
|
|
# dictionary of plugins
|
|
|
|
server_plugins = {}
|
|
|
|
|
|
|
|
# list of active (started) plugins
|
|
|
|
server_active_plugins = []
|
|
|
|
|
|
|
|
|
|
|
|
# initialize 'serverIface' object
|
|
|
|
serverIface = None
|
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
|
2014-10-09 15:05:11 +02:00
|
|
|
def initServerInterface(pointer):
|
2014-11-29 23:18:18 +10:00
|
|
|
from qgis.server import QgsServerInterface
|
|
|
|
from sip import wrapinstance
|
2014-10-09 15:05:11 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
global serverIface
|
|
|
|
serverIface = wrapinstance(pointer, QgsServerInterface)
|
2014-10-09 15:05:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
def startServerPlugin(packageName):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" initialize the plugin """
|
|
|
|
global server_plugins, server_active_plugins, serverIface
|
2014-10-09 15:05:11 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
if packageName in server_active_plugins:
|
|
|
|
return False
|
|
|
|
if packageName not in sys.modules:
|
|
|
|
return False
|
2014-10-09 15:05:11 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
package = sys.modules[packageName]
|
2014-10-09 15:05:11 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
errMsg = QCoreApplication.translate("Python", "Couldn't load server plugin %s") % packageName
|
2014-10-09 15:05:11 +02:00
|
|
|
|
2014-11-29 23:18:18 +10:00
|
|
|
# create an instance of the plugin
|
|
|
|
try:
|
|
|
|
server_plugins[packageName] = package.serverClassFactory(serverIface)
|
|
|
|
except:
|
|
|
|
_unloadPluginModules(packageName)
|
|
|
|
msg = QCoreApplication.translate("Python",
|
|
|
|
"%s due to an error when calling its serverClassFactory() method") % errMsg
|
2015-08-22 14:29:41 +02:00
|
|
|
showException(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], msg)
|
2014-11-29 23:18:18 +10:00
|
|
|
return False
|
|
|
|
|
|
|
|
# add to active plugins
|
|
|
|
server_active_plugins.append(packageName)
|
|
|
|
return True
|
2014-10-09 15:05:11 +02:00
|
|
|
|
|
|
|
|
2010-01-07 14:59:27 +00:00
|
|
|
#######################
|
|
|
|
# IMPORT wrapper
|
|
|
|
|
|
|
|
import __builtin__
|
|
|
|
|
|
|
|
_builtin_import = __builtin__.__import__
|
2014-11-29 23:18:18 +10:00
|
|
|
_plugin_modules = {}
|
|
|
|
|
2010-01-07 14:59:27 +00:00
|
|
|
|
|
|
|
def _import(name, globals={}, locals={}, fromlist=[], level=-1):
|
2014-11-29 23:18:18 +10:00
|
|
|
""" wrapper around builtin import that keeps track of loaded plugin modules """
|
|
|
|
mod = _builtin_import(name, globals, locals, fromlist, level)
|
|
|
|
|
|
|
|
if mod and '__file__' in mod.__dict__:
|
|
|
|
module_name = mod.__name__
|
|
|
|
package_name = module_name.split('.')[0]
|
|
|
|
# check whether the module belongs to one of our plugins
|
|
|
|
if package_name in available_plugins:
|
|
|
|
if package_name not in _plugin_modules:
|
|
|
|
_plugin_modules[package_name] = set()
|
|
|
|
_plugin_modules[package_name].add(module_name)
|
|
|
|
# check the fromlist for additional modules (from X import Y,Z)
|
|
|
|
if fromlist:
|
|
|
|
for fromitem in fromlist:
|
|
|
|
frmod = module_name + "." + fromitem
|
|
|
|
if frmod in sys.modules:
|
|
|
|
_plugin_modules[package_name].add(frmod)
|
|
|
|
|
|
|
|
return mod
|
|
|
|
|
2010-01-07 14:59:27 +00:00
|
|
|
|
|
|
|
__builtin__.__import__ = _import
|