mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
faffa55cf1
commit
393cfe78cd
@ -41,27 +41,24 @@ field_metadata = FieldMetadata()
|
|||||||
|
|
||||||
class SafeFormat(TemplateFormatter):
|
class SafeFormat(TemplateFormatter):
|
||||||
|
|
||||||
def get_value(self, key, args, kwargs):
|
def get_value(self, orig_key, args, kwargs):
|
||||||
try:
|
key = orig_key.lower()
|
||||||
key = key.lower()
|
if key != 'title_sort' and key not in TOP_LEVEL_IDENTIFIERS:
|
||||||
if key != 'title_sort' and key not in TOP_LEVEL_IDENTIFIERS:
|
key = field_metadata.search_term_to_field_key(key)
|
||||||
key = field_metadata.search_term_to_field_key(key)
|
if key is None or key not in self.book.all_field_keys():
|
||||||
b = self.book.get_user_metadata(key, False)
|
raise ValueError(_('Value: unknown field ') + orig_key)
|
||||||
if b and b['datatype'] == 'int' and self.book.get(key, 0) == 0:
|
b = self.book.get_user_metadata(key, False)
|
||||||
v = ''
|
if b and b['datatype'] == 'int' and self.book.get(key, 0) == 0:
|
||||||
elif b and b['datatype'] == 'float' and self.book.get(key, 0.0) == 0.0:
|
v = ''
|
||||||
v = ''
|
elif b and b['datatype'] == 'float' and self.book.get(key, 0.0) == 0.0:
|
||||||
else:
|
v = ''
|
||||||
v = self.book.format_field(key, series_with_index=False)[1]
|
else:
|
||||||
if v is None:
|
v = self.book.format_field(key, series_with_index=False)[1]
|
||||||
return ''
|
if v is None:
|
||||||
if v == '':
|
return ''
|
||||||
return ''
|
if v == '':
|
||||||
return v
|
return ''
|
||||||
except:
|
return v
|
||||||
if DEBUG:
|
|
||||||
traceback.print_exc()
|
|
||||||
return key
|
|
||||||
|
|
||||||
composite_formatter = SafeFormat()
|
composite_formatter = SafeFormat()
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ from PyQt4.Qt import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter,
|
|||||||
|
|
||||||
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
|
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
|
||||||
from calibre.utils.formatter_functions import formatter_functions
|
from calibre.utils.formatter_functions import formatter_functions
|
||||||
|
from calibre.ebooks.metadata.book.base import composite_formatter
|
||||||
|
|
||||||
class ParenPosition:
|
class ParenPosition:
|
||||||
|
|
||||||
@ -194,10 +195,13 @@ class TemplateHighlighter(QSyntaxHighlighter):
|
|||||||
|
|
||||||
class TemplateDialog(QDialog, Ui_TemplateDialog):
|
class TemplateDialog(QDialog, Ui_TemplateDialog):
|
||||||
|
|
||||||
def __init__(self, parent, text):
|
def __init__(self, parent, text, mi):
|
||||||
QDialog.__init__(self, parent)
|
QDialog.__init__(self, parent)
|
||||||
Ui_TemplateDialog.__init__(self)
|
Ui_TemplateDialog.__init__(self)
|
||||||
self.setupUi(self)
|
self.setupUi(self)
|
||||||
|
|
||||||
|
self.mi = mi
|
||||||
|
|
||||||
# Remove help icon on title bar
|
# Remove help icon on title bar
|
||||||
icon = self.windowIcon()
|
icon = self.windowIcon()
|
||||||
self.setWindowFlags(self.windowFlags()&(~Qt.WindowContextHelpButtonHint))
|
self.setWindowFlags(self.windowFlags()&(~Qt.WindowContextHelpButtonHint))
|
||||||
@ -233,12 +237,16 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
|
|||||||
self.function.addItems(func_names)
|
self.function.addItems(func_names)
|
||||||
self.function.setCurrentIndex(0)
|
self.function.setCurrentIndex(0)
|
||||||
self.function.currentIndexChanged[str].connect(self.function_changed)
|
self.function.currentIndexChanged[str].connect(self.function_changed)
|
||||||
|
self.textbox_changed()
|
||||||
|
|
||||||
def textbox_changed(self):
|
def textbox_changed(self):
|
||||||
cur_text = unicode(self.textbox.toPlainText())
|
cur_text = unicode(self.textbox.toPlainText())
|
||||||
if self.last_text != cur_text:
|
if self.last_text != cur_text:
|
||||||
self.last_text = cur_text
|
self.last_text = cur_text
|
||||||
self.highlighter.regenerate_paren_positions()
|
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):
|
def text_cursor_changed(self):
|
||||||
cursor = self.textbox.textCursor()
|
cursor = self.textbox.textCursor()
|
||||||
|
@ -23,19 +23,39 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QPlainTextEdit" name="textbox"/>
|
<widget class="QPlainTextEdit" name="textbox"/>
|
||||||
</item>
|
</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>
|
<item>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
<item row="0" column="0">
|
<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">
|
<widget class="QLabel" name="label">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Function &name:</string>
|
<string>Function &name:</string>
|
||||||
@ -45,10 +65,10 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="1">
|
<item row="2" column="1">
|
||||||
<widget class="QComboBox" name="function"/>
|
<widget class="QComboBox" name="function"/>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="3" column="0">
|
||||||
<widget class="QLabel" name="label_2">
|
<widget class="QLabel" name="label_2">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Documentation:</string>
|
<string>&Documentation:</string>
|
||||||
@ -61,7 +81,7 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="0">
|
<item row="4" column="0">
|
||||||
<widget class="QLabel" name="label_3">
|
<widget class="QLabel" name="label_3">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Python &code:</string>
|
<string>Python &code:</string>
|
||||||
@ -74,7 +94,7 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="1">
|
<item row="3" column="1">
|
||||||
<widget class="QPlainTextEdit" name="documentation">
|
<widget class="QPlainTextEdit" name="documentation">
|
||||||
<property name="maximumSize">
|
<property name="maximumSize">
|
||||||
<size>
|
<size>
|
||||||
@ -84,7 +104,7 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="1">
|
<item row="4" column="1">
|
||||||
<widget class="QPlainTextEdit" name="source_code"/>
|
<widget class="QPlainTextEdit" name="source_code"/>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
@ -21,6 +21,10 @@ class TemplateLineEditor(QLineEdit):
|
|||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
QLineEdit.__init__(self, parent)
|
QLineEdit.__init__(self, parent)
|
||||||
self.tags = None
|
self.tags = None
|
||||||
|
self.mi = None
|
||||||
|
|
||||||
|
def set_mi(self, mi):
|
||||||
|
self.mi = mi
|
||||||
|
|
||||||
def set_tags(self, tags):
|
def set_tags(self, tags):
|
||||||
self.tags = tags
|
self.tags = tags
|
||||||
@ -37,7 +41,7 @@ class TemplateLineEditor(QLineEdit):
|
|||||||
menu.exec_(event.globalPos())
|
menu.exec_(event.globalPos())
|
||||||
|
|
||||||
def open_editor(self):
|
def open_editor(self):
|
||||||
t = TemplateDialog(self, self.text())
|
t = TemplateDialog(self, self.text(), self.mi)
|
||||||
t.setWindowTitle(_('Edit template'))
|
t.setWindowTitle(_('Edit template'))
|
||||||
if t.exec_():
|
if t.exec_():
|
||||||
self.setText(t.textbox.toPlainText())
|
self.setText(t.textbox.toPlainText())
|
||||||
|
@ -418,8 +418,9 @@ class CcTemplateDelegate(QStyledItemDelegate): # {{{
|
|||||||
|
|
||||||
def createEditor(self, parent, option, index):
|
def createEditor(self, parent, option, index):
|
||||||
m = index.model()
|
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']
|
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.setWindowTitle(_("Edit template"))
|
||||||
editor.textbox.setTabChangesFocus(False)
|
editor.textbox.setTabChangesFocus(False)
|
||||||
editor.textbox.setTabStopWidth(20)
|
editor.textbox.setTabStopWidth(20)
|
||||||
|
@ -205,11 +205,21 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
choices.insert(0, '')
|
choices.insert(0, '')
|
||||||
self.column_color_count = db.column_color_count+1
|
self.column_color_count = db.column_color_count+1
|
||||||
tags = db.all_tags()
|
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):
|
for i in range(1, self.column_color_count):
|
||||||
r('column_color_name_'+str(i), db.prefs, choices=choices)
|
r('column_color_name_'+str(i), db.prefs, choices=choices)
|
||||||
r('column_color_template_'+str(i), db.prefs)
|
r('column_color_template_'+str(i), db.prefs)
|
||||||
tpl = getattr(self, 'opt_column_color_template_'+str(i))
|
tpl = getattr(self, 'opt_column_color_template_'+str(i))
|
||||||
tpl.set_tags(tags)
|
tpl.set_tags(tags)
|
||||||
|
tpl.set_mi(mi)
|
||||||
toolbutton = getattr(self, 'opt_column_color_wizard_'+str(i))
|
toolbutton = getattr(self, 'opt_column_color_wizard_'+str(i))
|
||||||
toolbutton.clicked.connect(tpl.tag_wizard)
|
toolbutton.clicked.connect(tpl.tag_wizard)
|
||||||
all_colors = [unicode(s) for s in list(QColor.colorNames())]
|
all_colors = [unicode(s) for s in list(QColor.colorNames())]
|
||||||
|
@ -98,7 +98,10 @@ class _Parser(object):
|
|||||||
cls = funcs['assign']
|
cls = funcs['assign']
|
||||||
return cls.eval_(self.parent, self.parent.kwargs,
|
return cls.eval_(self.parent, self.parent.kwargs,
|
||||||
self.parent.book, self.parent.locals, id, self.expr())
|
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.
|
# We have a function.
|
||||||
# Check if it is a known one. We do this here so error reporting is
|
# 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.
|
# better, as it can identify the tokens near the problem.
|
||||||
@ -317,8 +320,8 @@ class TemplateFormatter(string.Formatter):
|
|||||||
try:
|
try:
|
||||||
ans = self.vformat(fmt, [], kwargs).strip()
|
ans = self.vformat(fmt, [], kwargs).strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if DEBUG:
|
# if DEBUG:
|
||||||
traceback.print_exc()
|
# traceback.print_exc()
|
||||||
ans = error_value + ' ' + e.message
|
ans = error_value + ' ' + e.message
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
@ -63,20 +63,13 @@ class FormatterFunction(object):
|
|||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def eval_(self, formatter, kwargs, mi, locals, *args):
|
def eval_(self, formatter, kwargs, mi, locals, *args):
|
||||||
try:
|
ret = self.evaluate(formatter, kwargs, mi, locals, *args)
|
||||||
ret = self.evaluate(formatter, kwargs, mi, locals, *args)
|
if isinstance(ret, (str, unicode)):
|
||||||
if isinstance(ret, (str, unicode)):
|
return ret
|
||||||
return ret
|
if isinstance(ret, (int, float, bool)):
|
||||||
if isinstance(ret, (int, float, bool)):
|
return unicode(ret)
|
||||||
return unicode(ret)
|
if isinstance(ret, list):
|
||||||
if isinstance(ret, list):
|
return ','.join(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
|
|
||||||
|
|
||||||
all_builtin_functions = []
|
all_builtin_functions = []
|
||||||
class BuiltinFormatterFunction(FormatterFunction):
|
class BuiltinFormatterFunction(FormatterFunction):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user