提交 37d6549979b5afc3c8fd381d16d22474eeae6d29

作者 LJH 李佳桓
1 个父辈 3d849844

add

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