mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
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:
parent
0b57b1ae82
commit
ed4709f9ac
@ -80,6 +80,34 @@ class Plugin(object): # {{{
|
||||
'''
|
||||
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):
|
||||
'''
|
||||
Return a string giving help on how to customize this plugin.
|
||||
|
@ -109,7 +109,7 @@ class InterfaceAction(QObject):
|
||||
setattr(self, attr, 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
|
||||
will allow you to load resources from the ZIP file.
|
||||
@ -117,20 +117,24 @@ class InterfaceAction(QObject):
|
||||
For example to load an image::
|
||||
|
||||
pixmap = QPixmap()
|
||||
pixmap.loadFromData(self.load_resource('images/icon.png'))
|
||||
pixmap.loadFromData(self.load_resources(['images/icon.png']).itervalues().next())
|
||||
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:
|
||||
raise ValueError('This plugin was not loaded from a ZIP file')
|
||||
ans = {}
|
||||
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)
|
||||
if candidate in names:
|
||||
ans[candidate] = zf.read(candidate)
|
||||
return ans
|
||||
|
||||
|
||||
def genesis(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user