From 792fccbcad06c995b93ce9b97539d359e17740b5 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Mon, 20 Sep 2010 08:32:16 +0100 Subject: [PATCH] Fix validation exception and exception when empty fields are subscripted in templates --- src/calibre/gui2/preferences/save_template.py | 18 +++++++++++++++--- src/calibre/library/save_to_disk.py | 5 ++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/calibre/gui2/preferences/save_template.py b/src/calibre/gui2/preferences/save_template.py index 0dbee5bf21..0f48893b69 100644 --- a/src/calibre/gui2/preferences/save_template.py +++ b/src/calibre/gui2/preferences/save_template.py @@ -6,12 +6,24 @@ __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' +import string + from PyQt4.Qt import QWidget, pyqtSignal from calibre.gui2 import error_dialog from calibre.gui2.preferences.save_template_ui import Ui_Form -from calibre.library.save_to_disk import FORMAT_ARG_DESCS, preprocess_template,\ - safe_format +from calibre.library.save_to_disk import FORMAT_ARG_DESCS, preprocess_template + +class ValidateFormat(string.Formatter): + ''' + Provides a format function that substitutes '' for any missing value + ''' + def get_value(self, key, args, kwargs): + return 'this is some text that should be long enough' + +validate_formatter = ValidateFormat() +def validate_format(x, format_args): + return validate_formatter.vformat(x, [], format_args).strip() class SaveTemplate(QWidget, Ui_Form): @@ -52,7 +64,7 @@ class SaveTemplate(QWidget, Ui_Form): tmpl = preprocess_template(self.opt_template.text()) fa = {} try: - safe_format(tmpl, fa) + validate_format(tmpl, fa) except Exception, err: error_dialog(self, _('Invalid template'), '

'+_('The template %s is invalid:')%tmpl + \ diff --git a/src/calibre/library/save_to_disk.py b/src/calibre/library/save_to_disk.py index 71850abcd5..d5300d93e9 100644 --- a/src/calibre/library/save_to_disk.py +++ b/src/calibre/library/save_to_disk.py @@ -113,7 +113,10 @@ class SafeFormat(string.Formatter): safe_formatter = SafeFormat() def safe_format(x, format_args): - ans = safe_formatter.vformat(x, [], format_args).strip() + try: + ans = safe_formatter.vformat(x, [], format_args).strip() + except: + ans = '' return re.sub(r'\s+', ' ', ans) def get_components(template, mi, id, timefmt='%b %Y', length=250,