utils.py
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import sys
import traceback
import glob
import os.path
import configparser
import warnings
import codecs
import time
#######################
# PLUGINS
# list of plugin paths. it gets filled in by the QGIS python library
plugin_paths = []
# dictionary of plugins
plugins = {}
available_plugins = []
plugins_metadata_parser = {}
def findPlugins(path):
""" 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
metadataFile = os.path.join(plugin, 'metadata.txt')
if not os.path.exists(metadataFile):
continue
cp = configparser.ConfigParser()
try:
with codecs.open(metadataFile, "r", "utf8") as f:
cp.read_file(f)
except:
cp = None
pluginName = os.path.basename(plugin)
yield (pluginName, cp)
def metadataParser():
"""Used by other modules to access the local parser object"""
return plugins_metadata_parser
def updateAvailablePlugins():
""" 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):
if parser is None:
continue
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
def pluginMetadata(packageName, fct):
""" fetch metadata from a plugin - use values from metadata.txt """
try:
return plugins_metadata_parser[packageName].get('general', fct)
except Exception:
return "__error__"
def loadPlugin(packageName):
""" load plugin's package """
try:
__import__(packageName)
return True
except:
pass # continue...
# snake in the grass, we know it's there
sys.path_importer_cache.clear()
# retry
try:
__import__(packageName)
return True
except:
print("Couldn't load plugin '%s'" % (packageName))
return False
def _unloadPluginModules(packageName):
""" unload plugin package with all its modules (files) """
global _plugin_modules
mods = _plugin_modules[packageName]
for mod in mods:
if not mod in sys.modules:
continue
# try removing path
if hasattr(sys.modules[mod], '__path__'):
for path in sys.modules[mod].__path__:
try:
sys.path.remove(path)
except ValueError:
# Discard if path is not there
pass
# try to remove the module from python
try:
del sys.modules[mod]
except:
print("Error when removing module:\n%s" % traceback.format_exc())
# remove the plugin entry
del _plugin_modules[packageName]
#######################
# SERVER PLUGINS
#
# 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
def initServerInterface(serverIface_):
#from qgis.server import DmpServerInterface
#from sip import wrapinstance
#sys.excepthook = sys.__excepthook__
global serverIface
serverIface = serverIface_
def startServerPlugin(packageName):
""" initialize the plugin """
global server_plugins, server_active_plugins, serverIface
if packageName in server_active_plugins:
return False
if packageName not in sys.modules:
return False
package = sys.modules[packageName]
errMsg = "Couldn't load server plugin: %s" %(packageName)
# create an instance of the plugin
try:
server_plugins[packageName] = package.serverClassFactory(serverIface)
except:
_unloadPluginModules(packageName)
print("%s ,due to an error when calling its serverClassFactory() method." %(errMsg))
return False
# add to active plugins
server_active_plugins.append(packageName)
return True