Skip to content

Commit be9bdeb

Browse files
committedNov 9, 2011
[FEATURE] Allow plugin metadata to be in metadata.txt
Until now plugins' metadata were always retrieved from __init__.py by calling python methods. Reading metadata from a text file has the advantage of not requiring to load the plugin code and can be done by plugin repository. Metadata in metadata.txt is preferred to the methods in __init__.py - if the text file is present, it is used to fetch the values. From QGIS 2.0 the metadata from __init__.py will not be accepted - the metadata.txt file will be required. Plugin metadata should be in INI file format, recognized by python's ConfigParser module and by Qt's QSettings class. All currently used metadata should be in [general] section. Example use: [general] name=PostGIS manager description=Manage your PostGIS database version=Version 0.5.15 icon=icons/postgis_elephant.png qgisMinimumVersion=1.0.0
1 parent b9d26e4 commit be9bdeb

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed
 

‎python/utils.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import glob
1212
import os.path
1313
import re
14-
14+
import ConfigParser
1515

1616
#######################
1717
# ERROR HANDLING
@@ -72,29 +72,55 @@ def initInterface(pointer):
7272
# list of plugins in plugin directory and home plugin directory
7373
available_plugins = []
7474

75+
# dictionary of plugins providing metadata in a text file (metadata.txt)
76+
# key = plugin package name, value = config parser instance
77+
plugins_metadata_parser = {}
78+
7579
def findPlugins(path):
80+
""" for internal use: return list of plugins in given path """
7681
plugins = []
7782
for plugin in glob.glob(path + "/*"):
7883
if os.path.isdir(plugin) and os.path.exists(os.path.join(plugin, '__init__.py')):
7984
plugins.append( os.path.basename(plugin) )
8085
return plugins
8186

87+
def _checkMetadataFile(pluginpath, plugin):
88+
""" Check whether there exists a metadata.txt file.
89+
That is now a preferred way to store plugin's metadata """
90+
metadataFile = os.path.join(pluginpath, plugin, 'metadata.txt')
91+
if not os.path.exists(metadataFile):
92+
return None
93+
cp = ConfigParser.ConfigParser()
94+
res = cp.read(metadataFile)
95+
if len(res) == 0:
96+
return None # reading of metadata file failed
97+
return cp
98+
8299
def updateAvailablePlugins():
83100
""" go thrgouh the plugin_paths list and find out what plugins are available """
84101
# merge the lists
85102
plugins = []
103+
metadata_parser = {}
86104
for pluginpath in plugin_paths:
87105
for p in findPlugins(pluginpath):
88106
if p not in plugins:
89107
plugins.append(p)
108+
cp = _checkMetadataFile(pluginpath, p)
109+
if cp: metadata_parser[p] = cp
90110

91111
global available_plugins
92112
available_plugins = plugins
113+
global plugins_metadata_parser
114+
plugins_metadata_parser = metadata_parser
93115

94116

95117
def pluginMetadata(packageName, fct):
96118
""" fetch metadata from a plugin """
97119
try:
120+
# try to use values from metadata.txt if available
121+
if plugins_metadata_parser.has_key(packageName):
122+
return plugins_metadata_parser[packageName].get('general', fct)
123+
# otherwise fall back to old method, using __init__.py
98124
package = sys.modules[packageName]
99125
return getattr(package, fct)()
100126
except:

0 commit comments

Comments
 (0)
Please sign in to comment.