Refactor ZIP plugin resource loading for greater efficiency and add method to Plugin class as well for use in non GUI plugins

This commit is contained in:
Kovid Goyal 2010-12-15 09:02:35 -07:00
parent 0b57b1ae82
commit ed4709f9ac
2 changed files with 39 additions and 7 deletions

View File

@ -80,6 +80,34 @@ class Plugin(object): # {{{
''' '''
pass pass
def load_resources(self, names):
'''
If this plugin comes in a ZIP file (user added plugin), this method
will allow you to load resources from the ZIP file.
For example to load an image::
pixmap = QPixmap()
pixmap.loadFromData(self.load_resources(['images/icon.png']).itervalues().next())
icon = QIcon(pixmap)
:param names: List of paths to resources in the zip file using / as separator
:return: A dictionary of the form ``{name : file_contents}``. Any names
that were not found in the zip file will not be present in the
dictionary.
'''
if self.plugin_path is None:
raise ValueError('This plugin was not loaded from a ZIP file')
ans = {}
with zipfile.ZipFile(self.plugin_path, 'r') as zf:
for candidate in zf.namelist():
if candidate in names:
ans[candidate] = zf.read(candidate)
return ans
def customization_help(self, gui=False): def customization_help(self, gui=False):
''' '''
Return a string giving help on how to customize this plugin. Return a string giving help on how to customize this plugin.

View File

@ -109,7 +109,7 @@ class InterfaceAction(QObject):
setattr(self, attr, action) setattr(self, attr, action)
return action return action
def load_resource(self, name): def load_resources(self, names):
''' '''
If this plugin comes in a ZIP file (user added plugin), this method If this plugin comes in a ZIP file (user added plugin), this method
will allow you to load resources from the ZIP file. will allow you to load resources from the ZIP file.
@ -117,20 +117,24 @@ class InterfaceAction(QObject):
For example to load an image:: For example to load an image::
pixmap = QPixmap() pixmap = QPixmap()
pixmap.loadFromData(self.load_resource('images/icon.png')) pixmap.loadFromData(self.load_resources(['images/icon.png']).itervalues().next())
icon = QIcon(pixmap) icon = QIcon(pixmap)
:param name: Path to resource in zip file using / as separator :param names: List of paths to resources in the zip file using / as separator
:return: A dictionary of the form ``{name : file_contents}``. Any names
that were not found in the zip file will not be present in the
dictionary.
''' '''
if self.plugin_path is None: if self.plugin_path is None:
raise ValueError('This plugin was not loaded from a ZIP file') raise ValueError('This plugin was not loaded from a ZIP file')
ans = {}
with ZipFile(self.plugin_path, 'r') as zf: with ZipFile(self.plugin_path, 'r') as zf:
for candidate in zf.namelist(): for candidate in zf.namelist():
if candidate == name: if candidate in names:
return zf.read(name) ans[candidate] = zf.read(candidate)
raise ValueError('The name %r was not found in the plugin zip' return ans
' file'%name)
def genesis(self): def genesis(self):