Improvements to editing templates and formatter exception reporting. Largest improvement is adding a value preview box to the template editor.

This commit is contained in:
Charles Haley 2011-05-29 11:30:04 +01:00
parent faffa55cf1
commit 393cfe78cd
8 changed files with 92 additions and 56 deletions

View File

@ -41,27 +41,24 @@ field_metadata = FieldMetadata()
class SafeFormat(TemplateFormatter):
def get_value(self, key, args, kwargs):
try:
key = key.lower()
if key != 'title_sort' and key not in TOP_LEVEL_IDENTIFIERS:
key = field_metadata.search_term_to_field_key(key)
b = self.book.get_user_metadata(key, False)
if b and b['datatype'] == 'int' and self.book.get(key, 0) == 0:
v = ''
elif b and b['datatype'] == 'float' and self.book.get(key, 0.0) == 0.0:
v = ''
else:
v = self.book.format_field(key, series_with_index=False)[1]
if v is None:
return ''
if v == '':
return ''
return v
except:
if DEBUG:
traceback.print_exc()
return key
def get_value(self, orig_key, args, kwargs):
key = orig_key.lower()
if key != 'title_sort' and key not in TOP_LEVEL_IDENTIFIERS:
key = field_metadata.search_term_to_field_key(key)
if key is None or key not in self.book.all_field_keys():
raise ValueError(_('Value: unknown field ') + orig_key)
b = self.book.get_user_metadata(key, False)
if b and b['datatype'] == 'int' and self.book.get(key, 0) == 0:
v = ''
elif b and b['datatype'] == 'float' and self.book.get(key, 0.0) == 0.0:
v = ''
else:
v = self.book.format_field(key, series_with_index=False)[1]
if v is None:
return ''
if v == '':
return ''
return v
composite_formatter = SafeFormat()

View File

@ -11,6 +11,7 @@ from PyQt4.Qt import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter,
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
from calibre.utils.formatter_functions import formatter_functions
from calibre.ebooks.metadata.book.base import composite_formatter
class ParenPosition:
@ -194,10 +195,13 @@ class TemplateHighlighter(QSyntaxHighlighter):
class TemplateDialog(QDialog, Ui_TemplateDialog):
def __init__(self, parent, text):
def __init__(self, parent, text, mi):
QDialog.__init__(self, parent)
Ui_TemplateDialog.__init__(self)
self.setupUi(self)
self.mi = mi
# Remove help icon on title bar
icon = self.windowIcon()
self.setWindowFlags(self.windowFlags()&(~Qt.WindowContextHelpButtonHint))
@ -233,12 +237,16 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.function.addItems(func_names)
self.function.setCurrentIndex(0)
self.function.currentIndexChanged[str].connect(self.function_changed)
self.textbox_changed()
def textbox_changed(self):
cur_text = unicode(self.textbox.toPlainText())
if self.last_text != cur_text:
self.last_text = cur_text
self.highlighter.regenerate_paren_positions()
self.template_value.setText(
composite_formatter.safe_format(cur_text, self.mi,
_('EXCEPTION: '), self.mi))
def text_cursor_changed(self):
cursor = self.textbox.textCursor()

View File

@ -23,19 +23,39 @@
<item>
<widget class="QPlainTextEdit" name="textbox"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel">
<property name="text">
<string>Template value:</string>
</property>
<property name="buddy">
<cstring>template_value</cstring>
</property>
<property name="toolTip">
<string>The value the of the template using the current book in the library view</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="template_value">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Function &amp;name:</string>
@ -45,10 +65,10 @@
</property>
</widget>
</item>
<item row="0" column="1">
<item row="2" column="1">
<widget class="QComboBox" name="function"/>
</item>
<item row="1" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;Documentation:</string>
@ -61,7 +81,7 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Python &amp;code:</string>
@ -74,7 +94,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="3" column="1">
<widget class="QPlainTextEdit" name="documentation">
<property name="maximumSize">
<size>
@ -84,7 +104,7 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="4" column="1">
<widget class="QPlainTextEdit" name="source_code"/>
</item>
</layout>

View File

@ -21,6 +21,10 @@ class TemplateLineEditor(QLineEdit):
def __init__(self, parent):
QLineEdit.__init__(self, parent)
self.tags = None
self.mi = None
def set_mi(self, mi):
self.mi = mi
def set_tags(self, tags):
self.tags = tags
@ -37,7 +41,7 @@ class TemplateLineEditor(QLineEdit):
menu.exec_(event.globalPos())
def open_editor(self):
t = TemplateDialog(self, self.text())
t = TemplateDialog(self, self.text(), self.mi)
t.setWindowTitle(_('Edit template'))
if t.exec_():
self.setText(t.textbox.toPlainText())

View File

@ -418,8 +418,9 @@ class CcTemplateDelegate(QStyledItemDelegate): # {{{
def createEditor(self, parent, option, index):
m = index.model()
mi = m.db.get_metadata(index.row(), index_is_id=False)
text = m.custom_columns[m.column_map[index.column()]]['display']['composite_template']
editor = TemplateDialog(parent, text)
editor = TemplateDialog(parent, text, mi)
editor.setWindowTitle(_("Edit template"))
editor.textbox.setTabChangesFocus(False)
editor.textbox.setTabStopWidth(20)

View File

@ -205,11 +205,21 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
choices.insert(0, '')
self.column_color_count = db.column_color_count+1
tags = db.all_tags()
mi=None
try:
idx = gui.library_view.currentIndex().row()
if idx:
mi = db.get_metadata(idx, index_is_id=False)
except:
pass
for i in range(1, self.column_color_count):
r('column_color_name_'+str(i), db.prefs, choices=choices)
r('column_color_template_'+str(i), db.prefs)
tpl = getattr(self, 'opt_column_color_template_'+str(i))
tpl.set_tags(tags)
tpl.set_mi(mi)
toolbutton = getattr(self, 'opt_column_color_wizard_'+str(i))
toolbutton.clicked.connect(tpl.tag_wizard)
all_colors = [unicode(s) for s in list(QColor.colorNames())]

View File

@ -98,7 +98,10 @@ class _Parser(object):
cls = funcs['assign']
return cls.eval_(self.parent, self.parent.kwargs,
self.parent.book, self.parent.locals, id, self.expr())
return self.parent.locals.get(id, _('unknown id ') + id)
val = self.parent.locals.get(id, None)
if val is None:
self.error(_('Unknown identifier ') + id)
return val
# We have a function.
# Check if it is a known one. We do this here so error reporting is
# better, as it can identify the tokens near the problem.
@ -317,8 +320,8 @@ class TemplateFormatter(string.Formatter):
try:
ans = self.vformat(fmt, [], kwargs).strip()
except Exception as e:
if DEBUG:
traceback.print_exc()
# if DEBUG:
# traceback.print_exc()
ans = error_value + ' ' + e.message
return ans

View File

@ -63,20 +63,13 @@ class FormatterFunction(object):
raise NotImplementedError()
def eval_(self, formatter, kwargs, mi, locals, *args):
try:
ret = self.evaluate(formatter, kwargs, mi, locals, *args)
if isinstance(ret, (str, unicode)):
return ret
if isinstance(ret, (int, float, bool)):
return unicode(ret)
if isinstance(ret, list):
return ','.join(list)
except:
traceback.print_exc()
exc_type, exc_value, exc_traceback = sys.exc_info()
info = ': '.join(traceback.format_exception(exc_type, exc_value,
exc_traceback)[-2:]).replace('\n', '')
return _('Exception ') + info
ret = self.evaluate(formatter, kwargs, mi, locals, *args)
if isinstance(ret, (str, unicode)):
return ret
if isinstance(ret, (int, float, bool)):
return unicode(ret)
if isinstance(ret, list):
return ','.join(list)
all_builtin_functions = []
class BuiltinFormatterFunction(FormatterFunction):