Add a load_resource method to the InterfaceAction class to facilitate loading of resources from plugin ZIP files

This commit is contained in:
Kovid Goyal 2010-12-15 08:36:20 -07:00
parent dae4402949
commit 0b57b1ae82
2 changed files with 26 additions and 0 deletions

View File

@ -6,6 +6,7 @@ __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from functools import partial
from zipfile import ZipFile
from PyQt4.Qt import QToolButton, QAction, QIcon, QObject
@ -108,6 +109,30 @@ class InterfaceAction(QObject):
setattr(self, attr, action)
return action
def load_resource(self, name):
'''
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_resource('images/icon.png'))
icon = QIcon(pixmap)
:param name: Path to resource in zip file using / as separator
'''
if self.plugin_path is None:
raise ValueError('This plugin was not loaded from a ZIP file')
with ZipFile(self.plugin_path, 'r') as zf:
for candidate in zf.namelist():
if candidate == name:
return zf.read(name)
raise ValueError('The name %r was not found in the plugin zip'
' file'%name)
def genesis(self):
'''
Setup this plugin. Only called once during initialization. self.gui is

View File

@ -103,6 +103,7 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
acmap = OrderedDict()
for action in interface_actions():
ac = action.load_actual_plugin(self)
ac.plugin_path = action.plugin_path
if ac.name in acmap:
if ac.priority >= acmap[ac.name].priority:
acmap[ac.name] = ac