Fix adding the same plugin twice to a running calibre would not cause a code reload.

This commit is contained in:
Kovid Goyal 2011-03-27 17:13:56 -06:00
parent 157155d774
commit ed1e75cc82
3 changed files with 19 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import os, zipfile, posixpath, importlib, threading, re, imp, sys
from collections import OrderedDict from collections import OrderedDict
from functools import partial from functools import partial
from calibre import as_unicode
from calibre.customize import (Plugin, numeric_version, platform, from calibre.customize import (Plugin, numeric_version, platform,
InvalidPlugin, PluginNotFound) InvalidPlugin, PluginNotFound)
@ -160,27 +161,31 @@ class PluginLoader(object):
try: try:
ans = None ans = None
m = importlib.import_module( plugin_module = 'calibre_plugins.%s'%plugin_name
'calibre_plugins.%s'%plugin_name) m = sys.modules.get(plugin_module, None)
if m is not None:
reload(m)
else:
m = importlib.import_module(plugin_module)
for obj in m.__dict__.itervalues(): for obj in m.__dict__.itervalues():
if isinstance(obj, type) and issubclass(obj, Plugin) and \ if isinstance(obj, type) and issubclass(obj, Plugin) and \
obj.name != 'Trivial Plugin': obj.name != 'Trivial Plugin':
ans = obj ans = obj
break break
if ans is None: if ans is None:
raise InvalidPlugin('No plugin class found in %r:%r'%( raise InvalidPlugin('No plugin class found in %s:%s'%(
path_to_zip_file, plugin_name)) as_unicode(path_to_zip_file), plugin_name))
if ans.minimum_calibre_version > numeric_version: if ans.minimum_calibre_version > numeric_version:
raise InvalidPlugin( raise InvalidPlugin(
'The plugin at %r needs a version of calibre >= %r' % 'The plugin at %s needs a version of calibre >= %s' %
(path_to_zip_file, '.'.join(map(str, (as_unicode(path_to_zip_file), '.'.join(map(unicode,
ans.minimum_calibre_version)))) ans.minimum_calibre_version))))
if platform not in ans.supported_platforms: if platform not in ans.supported_platforms:
raise InvalidPlugin( raise InvalidPlugin(
'The plugin at %r cannot be used on %s' % 'The plugin at %s cannot be used on %s' %
(path_to_zip_file, platform)) (as_unicode(path_to_zip_file), platform))
return ans return ans
except: except:

View File

@ -19,7 +19,7 @@ class HelloWorld(FileTypePlugin):
version = (1, 0, 0) # The version number of this plugin version = (1, 0, 0) # The version number of this plugin
file_types = set(['epub', 'mobi']) # The file types that this plugin will be applied to file_types = set(['epub', 'mobi']) # The file types that this plugin will be applied to
on_postprocess = True # Run this plugin after conversion is complete on_postprocess = True # Run this plugin after conversion is complete
minimum_calibre_version = (0, 7, 52) minimum_calibre_version = (0, 7, 53)
def run(self, path_to_ebook): def run(self, path_to_ebook):
from calibre.ebooks.metadata.meta import get_metadata, set_metadata from calibre.ebooks.metadata.meta import get_metadata, set_metadata

View File

@ -25,7 +25,7 @@ class InterfacePluginDemo(InterfaceActionBase):
supported_platforms = ['windows', 'osx', 'linux'] supported_platforms = ['windows', 'osx', 'linux']
author = 'Kovid Goyal' author = 'Kovid Goyal'
version = (1, 0, 0) version = (1, 0, 0)
minimum_calibre_version = (0, 7, 52) minimum_calibre_version = (0, 7, 53)
#: This field defines the GUI plugin class that contains all the code #: This field defines the GUI plugin class that contains all the code
#: that actually does something. Its format is module_path:class_name #: that actually does something. Its format is module_path:class_name
@ -57,6 +57,10 @@ class InterfacePluginDemo(InterfaceActionBase):
The base class implementation of this method raises NotImplementedError The base class implementation of this method raises NotImplementedError
so by default no user configuration is possible. so by default no user configuration is possible.
''' '''
# It is important to put this import statement here rather than at the
# top of the module as importing the config class will also cause the
# GUI libraries to be loaded, which we do not want when using calibre
# from the command line
from calibre_plugins.interface_demo.config import ConfigWidget from calibre_plugins.interface_demo.config import ConfigWidget
return ConfigWidget() return ConfigWidget()