From 4645138a67193537ba3e91fc3e4942017d72e1de Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Thu, 16 Sep 2010 16:03:07 +0100 Subject: [PATCH] 1) Re-enable syntactic validation of save templates. 2) fix row numbering on send_to_device preferences ui template. --- src/calibre/gui2/preferences/save_template.py | 37 +++++++++++-------- src/calibre/gui2/preferences/sending.ui | 2 +- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/calibre/gui2/preferences/save_template.py b/src/calibre/gui2/preferences/save_template.py index 26dc02f259..0dbee5bf21 100644 --- a/src/calibre/gui2/preferences/save_template.py +++ b/src/calibre/gui2/preferences/save_template.py @@ -8,8 +8,10 @@ __docformat__ = 'restructuredtext en' 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 +from calibre.library.save_to_disk import FORMAT_ARG_DESCS, preprocess_template,\ + safe_format class SaveTemplate(QWidget, Ui_Form): @@ -24,8 +26,11 @@ class SaveTemplate(QWidget, Ui_Form): variables = sorted(FORMAT_ARG_DESCS.keys()) rows = [] for var in variables: - rows.append(u'%s%s'% + rows.append(u'%s %s'% (var, FORMAT_ARG_DESCS[var])) + rows.append(u'%s  %s'%( + _('Any custom field'), + _('The lookup name of any custom field. These names begin with "#")'))) table = u'%s
'%(u'\n'.join(rows)) self.template_variables.setText(table) @@ -39,21 +44,21 @@ class SaveTemplate(QWidget, Ui_Form): self.changed_signal.emit() def validate(self): - # TODO: NEWMETA: I haven't figured out how to get the custom columns - # into here, so for the moment make all templates valid. + ''' + Do a syntax check on the format string. Doing a semantic check + (verifying that the fields exist) is not useful in the presence of + custom fields, because they may or may not exist. + ''' + tmpl = preprocess_template(self.opt_template.text()) + fa = {} + try: + safe_format(tmpl, fa) + except Exception, err: + error_dialog(self, _('Invalid template'), + '

'+_('The template %s is invalid:')%tmpl + \ + '
'+str(err), show=True) + return False return True -# tmpl = preprocess_template(self.opt_template.text()) -# fa = {} -# for x in FORMAT_ARG_DESCS.keys(): -# fa[x]='random long string' -# try: -# tmpl.format(**fa) -# except Exception, err: -# error_dialog(self, _('Invalid template'), -# '

'+_('The template %s is invalid:')%tmpl + \ -# '
'+str(err), show=True) -# return False -# return True def set_value(self, val): self.opt_template.set_value(val) diff --git a/src/calibre/gui2/preferences/sending.ui b/src/calibre/gui2/preferences/sending.ui index b9d1d1e1d2..75b1899a3a 100644 --- a/src/calibre/gui2/preferences/sending.ui +++ b/src/calibre/gui2/preferences/sending.ui @@ -103,7 +103,7 @@ - +