Fix validation exception and exception when empty fields are subscripted in templates

This commit is contained in:
Charles Haley 2010-09-20 08:32:16 +01:00
parent 1c63e368fb
commit 792fccbcad
2 changed files with 19 additions and 4 deletions

View File

@ -6,12 +6,24 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__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'),
'<p>'+_('The template %s is invalid:')%tmpl + \

View File

@ -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,