diff --git a/src/calibre/customize/conversion.py b/src/calibre/customize/conversion.py index ee8656f0ca..50bceb4def 100644 --- a/src/calibre/customize/conversion.py +++ b/src/calibre/customize/conversion.py @@ -91,6 +91,37 @@ class DummyReporter(object): def __call__(self, percent, msg=''): pass +def gui_configuration_widget(name, parent, get_option_by_name, + get_option_help, db, book_id, for_output=True): + import importlib + + def widget_factory(cls): + return cls(parent, get_option_by_name, + get_option_help, db, book_id) + + if for_output: + try: + output_widget = importlib.import_module( + 'calibre.gui2.convert.'+name) + pw = output_widget.PluginWidget + pw.ICON = I('back.png') + pw.HELP = _('Options specific to the output format.') + return widget_factory(pw) + except ImportError: + pass + else: + try: + input_widget = importlib.import_module( + 'calibre.gui2.convert.'+name) + pw = input_widget.PluginWidget + pw.ICON = I('forward.png') + pw.HELP = _('Options specific to the input format.') + return widget_factory(pw) + except ImportError: + pass + return None + + class InputFormatPlugin(Plugin): ''' InputFormatPlugins are responsible for converting a document into @@ -225,6 +256,17 @@ class InputFormatPlugin(Plugin): ''' pass + def gui_configuration_widget(self, parent, get_option_by_name, + get_option_help, db, book_id): + ''' + Called to create the widget used for configuring this plugin in the + calibre GUI. The widget must be an instance of the PluginWidget class. + See the builting input plugins for examples. + ''' + name = self.name.lower().replace(' ', '_') + return gui_configuration_widget(name, parent, get_option_by_name, + get_option_help, db, book_id, for_output=False) + class OutputFormatPlugin(Plugin): ''' @@ -308,4 +350,16 @@ class OutputFormatPlugin(Plugin): ''' pass + def gui_configuration_widget(self, parent, get_option_by_name, + get_option_help, db, book_id): + ''' + Called to create the widget used for configuring this plugin in the + calibre GUI. The widget must be an instance of the PluginWidget class. + See the builtin output plugins for examples. + ''' + name = self.name.lower().replace(' ', '_') + return gui_configuration_widget(name, parent, get_option_by_name, + get_option_help, db, book_id, for_output=True) + + diff --git a/src/calibre/gui2/convert/bulk.py b/src/calibre/gui2/convert/bulk.py index 5324a83865..3a65a4617e 100644 --- a/src/calibre/gui2/convert/bulk.py +++ b/src/calibre/gui2/convert/bulk.py @@ -4,7 +4,7 @@ __license__ = 'GPL 3' __copyright__ = '2009, John Schember ' __docformat__ = 'restructuredtext en' -import shutil, importlib +import shutil from PyQt4.Qt import QString, SIGNAL @@ -86,17 +86,9 @@ class BulkConfig(Config): sd = widget_factory(StructureDetectionWidget) toc = widget_factory(TOCWidget) - output_widget = None - name = self.plumber.output_plugin.name.lower().replace(' ', '_') - try: - output_widget = importlib.import_module( - 'calibre.gui2.convert.'+name) - pw = output_widget.PluginWidget - pw.ICON = I('back.png') - pw.HELP = _('Options specific to the output format.') - output_widget = widget_factory(pw) - except ImportError: - pass + output_widget = self.plumber.output_plugin.gui_configuration_widget( + self.stack, self.plumber.get_option_by_name, + self.plumber.get_option_help, self.db) while True: c = self.stack.currentWidget() diff --git a/src/calibre/gui2/convert/single.py b/src/calibre/gui2/convert/single.py index 9160c820bd..4d13ce371b 100644 --- a/src/calibre/gui2/convert/single.py +++ b/src/calibre/gui2/convert/single.py @@ -6,7 +6,7 @@ __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import cPickle, shutil, importlib +import cPickle, shutil from PyQt4.Qt import QString, SIGNAL, QAbstractListModel, Qt, QVariant, QFont @@ -187,29 +187,12 @@ class Config(ResizableDialog, Ui_Dialog): toc = widget_factory(TOCWidget) debug = widget_factory(DebugWidget) - output_widget = None - name = self.plumber.output_plugin.name.lower().replace(' ', '_') - try: - output_widget = importlib.import_module( - 'calibre.gui2.convert.'+name) - pw = output_widget.PluginWidget - pw.ICON = I('back.png') - pw.HELP = _('Options specific to the output format.') - output_widget = widget_factory(pw) - except ImportError: - pass - input_widget = None - name = self.plumber.input_plugin.name.lower().replace(' ', '_') - try: - input_widget = importlib.import_module( - 'calibre.gui2.convert.'+name) - pw = input_widget.PluginWidget - pw.ICON = I('forward.png') - pw.HELP = _('Options specific to the input format.') - input_widget = widget_factory(pw) - except ImportError: - pass - + output_widget = self.plumber.output_plugin.gui_configuration_widget( + self.stack, self.plumber.get_option_by_name, + self.plumber.get_option_help, self.db, self.book_id) + input_widget = self.plumber.input_plugin.gui_configuration_widget( + self.stack, self.plumber.get_option_by_name, + self.plumber.get_option_help, self.db, self.book_id) while True: c = self.stack.currentWidget() if not c: break