Bug #1928548: Error opening template functions preference. Part of a fix, ensuring that the exception doesn't leave the ghost dialog.

This commit is contained in:
Charles Haley 2021-05-16 11:45:50 +01:00
parent ed62171495
commit 54ea660722
3 changed files with 20 additions and 6 deletions

View File

@ -59,7 +59,8 @@ class ConfigWidgetInterface(object):
def initialize(self):
'''
Should set all config values to their initial values (the values
stored in the config files).
stored in the config files). A "return" statement is optional. Return
False if the dialog is not to be shown.
'''
raise NotImplementedError()

View File

@ -310,7 +310,9 @@ class Preferences(QDialog):
def show_plugin(self, plugin):
self.showing_widget = plugin.create_widget(self.scroll_area)
self.showing_widget.genesis(self.gui)
self.showing_widget.initialize()
x = self.showing_widget.initialize()
if isinstance(x, bool) and not x:
return
self.set_tooltips_for_labels()
self.scroll_area.setWidget(self.showing_widget)
self.stack.setCurrentIndex(1)

View File

@ -5,7 +5,7 @@
import copy, json, traceback
from qt.core import QDialogButtonBox, QDialog
from calibre.gui2 import error_dialog, warning_dialog
from calibre.gui2 import error_dialog, warning_dialog, question_dialog
from calibre.gui2.dialogs.template_dialog import TemplateDialog
from calibre.gui2.preferences import ConfigWidgetBase, test_widget
from calibre.gui2.preferences.template_functions_ui import Ui_Form
@ -149,9 +149,20 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.builtins = formatter_functions().get_builtins_and_aliases()
self.st_funcs = {}
try:
for v in self.db.prefs.get('user_template_functions', []):
if not function_pref_is_python(v):
self.st_funcs.update({function_pref_name(v):compile_user_function(*v)})
except:
ans = question_dialog(self, _('Template functions'),
_('The template functions saved in the library are corrupt. '
"Do you want to delete them? Answering 'Yes' will delete all "
"the functions."), det_msg=traceback.format_exc(),
show_copy_button=True)
if ans:
print('remove the preference')
self.db.prefs['user_template_functions'] = []
return False
self.build_function_names_box()
self.function_name.currentIndexChanged[native_string_type].connect(self.function_index_changed)