From ed4709f9ac827aa0171e65f324712baca08bb4b4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 15 Dec 2010 09:02:35 -0700 Subject: [PATCH] Refactor ZIP plugin resource loading for greater efficiency and add method to Plugin class as well for use in non GUI plugins --- src/calibre/customize/__init__.py | 28 ++++++++++++++++++++++++++++ src/calibre/gui2/actions/__init__.py | 18 +++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/calibre/customize/__init__.py b/src/calibre/customize/__init__.py index b4e9b6c448..a76cb71acd 100644 --- a/src/calibre/customize/__init__.py +++ b/src/calibre/customize/__init__.py @@ -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. diff --git a/src/calibre/gui2/actions/__init__.py b/src/calibre/gui2/actions/__init__.py index 4ab15f6099..c88203593b 100644 --- a/src/calibre/gui2/actions/__init__.py +++ b/src/calibre/gui2/actions/__init__.py @@ -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):