From 6c26b9debc72060cfc10f1239d96311c1691ed08 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 10:04:34 +0100 Subject: [PATCH 1/9] Add the ondevice() function to the template language --- src/calibre/library/database2.py | 1 + src/calibre/manual/template_lang.rst | 3 ++- src/calibre/utils/formatter_functions.py | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 819ac2cd24..df465c919e 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -860,6 +860,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): mi.uuid = row[fm['uuid']] mi.title_sort = row[fm['sort']] mi.book_size = row[fm['size']] + mi.ondevice_col= row[fm['ondevice']] mi.last_modified = row[fm['last_modified']] formats = row[fm['formats']] if not formats: diff --git a/src/calibre/manual/template_lang.rst b/src/calibre/manual/template_lang.rst index 059376565d..28c9855ce4 100644 --- a/src/calibre/manual/template_lang.rst +++ b/src/calibre/manual/template_lang.rst @@ -255,6 +255,7 @@ The following functions are available in addition to those described in single-f * ``not(value)`` -- returns the string "1" if the value is empty, otherwise returns the empty string. This function works well with test or first_non_empty. You can have as many values as you want. * ``merge_lists(list1, list2, separator)`` -- return a list made by merging the items in list1 and list2, removing duplicate items using a case-insensitive compare. If items differ in case, the one in list1 is used. The items in list1 and list2 are separated by separator, as are the items in the returned list. * ``multiply(x, y)`` -- returns x * y. Throws an exception if either x or y are not numbers. + * ``ondevice()`` -- return the string "Yes" if ondevice is set, otherwise return the empty string * ``or(value, value, ...)`` -- returns the string "1" if any value is not empty, otherwise returns the empty string. This function works well with test or first_non_empty. You can have as many values as you want. * ``print(a, b, ...)`` -- prints the arguments to standard output. Unless you start calibre from the command line (``calibre-debug -g``), the output will go to a black hole. * ``raw_field(name)`` -- returns the metadata field named by name without applying any formatting. @@ -277,7 +278,7 @@ Function classification summary: * Relational: ``cmp`` , ``strcmp`` for strings * String case changes: ``lowercase``, ``uppercase``, ``titlecase``, ``capitalize`` * String manipulation: ``re``, ``shorten``, ``substr`` - * Other: ``assign``, ``booksize``, ``print``, ``format_date``, + * Other: ``assign``, ``booksize``, ``format_date``, ``ondevice`` ``print`` .. _general_mode: diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 2f15d5d592..761da2c8e2 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -568,7 +568,7 @@ class BuiltinCapitalize(BuiltinFormatterFunction): class BuiltinBooksize(BuiltinFormatterFunction): name = 'booksize' arg_count = 0 - doc = _('booksize() -- return value of the field capitalized') + doc = _('booksize() -- return value of the size field') def evaluate(self, formatter, kwargs, mi, locals): if mi.book_size is not None: @@ -578,6 +578,21 @@ class BuiltinBooksize(BuiltinFormatterFunction): pass return '' +class BuiltinOndevice(BuiltinFormatterFunction): + name = 'ondevice' + arg_count = 0 + doc = _('ondevice() -- return Yes if ondevice is set, otherwise return ' + 'the empty string') + + def evaluate(self, formatter, kwargs, mi, locals): + print mi.ondevice_col + if mi.ondevice_col: + try: + return _('Yes') + except: + pass + return '' + class BuiltinFirstNonEmpty(BuiltinFormatterFunction): name = 'first_non_empty' arg_count = -1 @@ -687,6 +702,7 @@ builtin_lowercase = BuiltinLowercase() builtin_merge_lists = BuiltinMergeLists() builtin_multiply = BuiltinMultiply() builtin_not = BuiltinNot() +builtin_ondevice = BuiltinOndevice() builtin_or = BuiltinOr() builtin_print = BuiltinPrint() builtin_raw_field = BuiltinRaw_field() From faffa55cf1299cc1beaf257f0aea765db2d37fb5 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 10:05:47 +0100 Subject: [PATCH 2/9] take out print statement --- src/calibre/utils/formatter_functions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 761da2c8e2..bf597d5b9c 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -585,7 +585,6 @@ class BuiltinOndevice(BuiltinFormatterFunction): 'the empty string') def evaluate(self, formatter, kwargs, mi, locals): - print mi.ondevice_col if mi.ondevice_col: try: return _('Yes') From 393cfe78cdca8f96b4e85d40e612087af86076f0 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 11:30:04 +0100 Subject: [PATCH 3/9] Improvements to editing templates and formatter exception reporting. Largest improvement is adding a value preview box to the template editor. --- src/calibre/ebooks/metadata/book/base.py | 39 +++++++-------- src/calibre/gui2/dialogs/template_dialog.py | 10 +++- src/calibre/gui2/dialogs/template_dialog.ui | 50 +++++++++++++------ .../gui2/dialogs/template_line_editor.py | 6 ++- src/calibre/gui2/library/delegates.py | 3 +- src/calibre/gui2/preferences/look_feel.py | 10 ++++ src/calibre/utils/formatter.py | 9 ++-- src/calibre/utils/formatter_functions.py | 21 +++----- 8 files changed, 92 insertions(+), 56 deletions(-) diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index 5dc3f25dfb..179a96e578 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -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() diff --git a/src/calibre/gui2/dialogs/template_dialog.py b/src/calibre/gui2/dialogs/template_dialog.py index ca55bb0e66..083dacbf00 100644 --- a/src/calibre/gui2/dialogs/template_dialog.py +++ b/src/calibre/gui2/dialogs/template_dialog.py @@ -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() diff --git a/src/calibre/gui2/dialogs/template_dialog.ui b/src/calibre/gui2/dialogs/template_dialog.ui index dd8fb7bd88..d36cbbd3d4 100644 --- a/src/calibre/gui2/dialogs/template_dialog.ui +++ b/src/calibre/gui2/dialogs/template_dialog.ui @@ -23,19 +23,39 @@ - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - + + + Template value: + + + template_value + + + The value the of the template using the current book in the library view + + + + + + + true + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + Function &name: @@ -45,10 +65,10 @@ - + - + &Documentation: @@ -61,7 +81,7 @@ - + Python &code: @@ -74,7 +94,7 @@ - + @@ -84,7 +104,7 @@ - + diff --git a/src/calibre/gui2/dialogs/template_line_editor.py b/src/calibre/gui2/dialogs/template_line_editor.py index 98b74b391d..a724b5b072 100644 --- a/src/calibre/gui2/dialogs/template_line_editor.py +++ b/src/calibre/gui2/dialogs/template_line_editor.py @@ -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()) diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index 6990a76b21..94c3deb403 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -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) diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index b6e6de1902..fcdd56fd5f 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -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())] diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index fccd0015c1..695355330e 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -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 diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index bf597d5b9c..d7b6e63f5e 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -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): From 52da86dbb6fa48d61ea852436082b6c7b8607e82 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 13:17:07 +0100 Subject: [PATCH 4/9] Added an else box to the tag color wizard. Also added more extensive tooltips. --- .../gui2/dialogs/template_line_editor.py | 61 ++++++++++++++++--- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/src/calibre/gui2/dialogs/template_line_editor.py b/src/calibre/gui2/dialogs/template_line_editor.py index a724b5b072..d466cecc55 100644 --- a/src/calibre/gui2/dialogs/template_line_editor.py +++ b/src/calibre/gui2/dialogs/template_line_editor.py @@ -69,10 +69,38 @@ class TagWizard(QDialog): self.setLayout(l) l.setColumnStretch(0, 1) l.setColumnMinimumWidth(0, 300) - l.addWidget(QLabel(_('Tags (more than one per box permitted)')), 0, 0, 1, 1) - l.addWidget(QLabel(_('Color')), 0, 1, 1, 1) + h = QLabel(_('Tags (see the popup help for more information)')) + h.setToolTip('

' + + _('You can enter more than one tag per box, separated by commas. ' + 'The comparison ignores letter case.
' + 'A tag value can be a regular expression. ' + 'When using regular expressions, note that the wizard ' + 'puts anchors (^ and $) around the expression, so you ' + 'must ensure your expression matches from the beginning ' + 'to the end of the tag.
' + 'Regular expression examples:') + '

') + l.addWidget(h , 0, 0, 1, 1) + c = QLabel(_('Color if tag found')) + c.setToolTip('

' + + _('At least one of the two color boxes must have a value. Leave ' + 'one color box empty if you want the template to use the next ' + 'line in this wizard. If both boxes are filled in, the rest of ' + 'the lines in this wizard will be ignored.') + '

') + l.addWidget(c, 0, 1, 1, 1) + c = QLabel(_('Color if tag not found')) + c.setToolTip('

' + + _('This box is usually filled in only on the last test. If it is ' + 'filled in before the last test, then the color for tag found box ' + 'must be empty or all the rest of the tests will be ignored.') + '

') + l.addWidget(c, 0, 2, 1, 1) self.tagboxes = [] self.colorboxes = [] + self.nfcolorboxes = [] self.colors = [unicode(s) for s in list(QColor.colorNames())] self.colors.insert(0, '') for i in range(0, 10): @@ -85,15 +113,25 @@ class TagWizard(QDialog): cb.addItems(self.colors) self.colorboxes.append(cb) l.addWidget(cb, i+1, 1, 1, 1) + cb = QComboBox(self) + cb.addItems(self.colors) + self.nfcolorboxes.append(cb) + l.addWidget(cb, i+1, 2, 1, 1) if txt: lines = txt.split('\n')[3:] i = 0 for line in lines: if line.startswith('#'): - t,c = line[1:].split(':|:') + vals = line[1:].split(':|:') + if len(vals) == 2: + t, c = vals + nc = '' + else: + t,c,nc = vals try: self.colorboxes[i].setCurrentIndex(self.colorboxes[i].findText(c)) + self.nfcolorboxes[i].setCurrentIndex(self.nfcolorboxes[i].findText(nc)) self.tagboxes[i].setText(t) except: pass @@ -109,28 +147,35 @@ class TagWizard(QDialog): res = ("program:\n#tag wizard -- do not directly edit\n" " t = field('tags');\n first_non_empty(\n") lines = [] - for tb, cb in zip(self.tagboxes, self.colorboxes): + for tb, cb, nfcb in zip(self.tagboxes, self.colorboxes, self.nfcolorboxes): tags = [t.strip() for t in unicode(tb.text()).split(',') if t.strip()] tags = '$|^'.join(tags) c = unicode(cb.currentText()).strip() - if not tags or not c: + nfc = unicode(nfcb.currentText()).strip() + if not tags or not (c or nfc): continue if c not in self.colors: error_dialog(self, _('Invalid color'), _('The color {0} is not valid').format(c), show=True, show_copy_button=False) return False - lines.append(" in_list(t, ',', '^{0}$', '{1}', '')".format(tags, c)) + if nfc not in self.colors: + error_dialog(self, _('Invalid color'), + _('The color {0} is not valid').format(nfc), + show=True, show_copy_button=False) + return False + lines.append(" in_list(t, ',', '^{0}$', '{1}', '{2}')".format(tags, c, nfc)) res += ',\n'.join(lines) res += ')\n' self.template = res res = '' - for tb, cb in zip(self.tagboxes, self.colorboxes): + for tb, cb, nfcb in zip(self.tagboxes, self.colorboxes, self.nfcolorboxes): t = unicode(tb.text()).strip() if t.endswith(','): t = t[:-1] c = unicode(cb.currentText()).strip() + nfc = unicode(nfcb.currentText()).strip() if t and c: - res += '#' + t + ':|:' + c + '\n' + res += '#' + t + ':|:' + c + ':|:' + nfc + '\n' self.template += res self.accept() From 67d570de1c99867e69796cac4710a409bfaf34c2 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 13:39:35 +0100 Subject: [PATCH 5/9] Permit empty keys in templates. Simplifies using functions that don't require a value --- src/calibre/ebooks/metadata/book/base.py | 2 ++ src/calibre/library/save_to_disk.py | 2 ++ src/calibre/utils/formatter.py | 2 ++ 3 files changed, 6 insertions(+) diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index 179a96e578..f98bebe1dc 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -42,6 +42,8 @@ field_metadata = FieldMetadata() class SafeFormat(TemplateFormatter): def get_value(self, orig_key, args, kwargs): + if not orig_key: + return '' key = orig_key.lower() if key != 'title_sort' and key not in TOP_LEVEL_IDENTIFIERS: key = field_metadata.search_term_to_field_key(key) diff --git a/src/calibre/library/save_to_disk.py b/src/calibre/library/save_to_disk.py index dc83b44c01..5f49833564 100644 --- a/src/calibre/library/save_to_disk.py +++ b/src/calibre/library/save_to_disk.py @@ -134,6 +134,8 @@ class SafeFormat(TemplateFormatter): ''' def get_value(self, key, args, kwargs): + if key == '': + return '' try: key = key.lower() try: diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index 695355330e..ebf47db854 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -342,6 +342,8 @@ class EvalFormatter(TemplateFormatter): A template formatter that uses a simple dict instead of an mi instance ''' def get_value(self, key, args, kwargs): + if key == '': + return '' key = key.lower() return kwargs.get(key, _('No such variable ') + key) From 0d57a3fb18f03574a242789cce9189e33aa2e54e Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 14:09:01 +0100 Subject: [PATCH 6/9] Correct some problems with setting the ondevice column values when the device is disconnected. --- src/calibre/gui2/device.py | 3 ++- src/calibre/gui2/library/models.py | 2 +- src/calibre/utils/formatter_functions.py | 5 +---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index 3977a6bca1..dd9d7aaa50 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -1294,7 +1294,8 @@ class DeviceMixin(object): # {{{ self.book_db_uuid_path_map = None return - if not hasattr(self, 'db_book_uuid_cache'): + if not self.device_manager.is_device_connected or \ + not hasattr(self, 'db_book_uuid_cache'): return loc if self.book_db_id_cache is None: diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 554b104c34..793f2d353b 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -125,7 +125,7 @@ class BooksModel(QAbstractTableModel): # {{{ def refresh_ondevice(self): self.db.refresh_ondevice() - self.resort() + self.refresh(reset=False) self.research() def set_book_on_device_func(self, func): diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index d7b6e63f5e..76faf04941 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -579,10 +579,7 @@ class BuiltinOndevice(BuiltinFormatterFunction): def evaluate(self, formatter, kwargs, mi, locals): if mi.ondevice_col: - try: - return _('Yes') - except: - pass + return _('Yes') return '' class BuiltinFirstNonEmpty(BuiltinFormatterFunction): From 322e6d9ac44e9304cccfedda1d69a654f9805673 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 15:09:56 +0100 Subject: [PATCH 7/9] Add an RE box to the color tags wizard. Add the necessary function to the template language. Fix problem with passing mi to the template editor. --- src/calibre/ebooks/metadata/book/base.py | 2 +- .../gui2/dialogs/template_line_editor.py | 67 +++++++++++++------ src/calibre/gui2/preferences/look_feel.py | 3 +- src/calibre/utils/formatter_functions.py | 22 +++++- 4 files changed, 70 insertions(+), 24 deletions(-) diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index f98bebe1dc..3e2201f6a4 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -47,7 +47,7 @@ class SafeFormat(TemplateFormatter): 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(): + if key is None or (self.book and 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: diff --git a/src/calibre/gui2/dialogs/template_line_editor.py b/src/calibre/gui2/dialogs/template_line_editor.py index d466cecc55..3d199b156c 100644 --- a/src/calibre/gui2/dialogs/template_line_editor.py +++ b/src/calibre/gui2/dialogs/template_line_editor.py @@ -5,7 +5,7 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -from PyQt4.Qt import (QLineEdit, QDialog, QGridLayout, QLabel, +from PyQt4.Qt import (QLineEdit, QDialog, QGridLayout, QLabel, QCheckBox, QDialogButtonBox, QColor, QComboBox, QIcon) from calibre.gui2.dialogs.template_dialog import TemplateDialog @@ -73,8 +73,8 @@ class TagWizard(QDialog): h.setToolTip('

' + _('You can enter more than one tag per box, separated by commas. ' 'The comparison ignores letter case.
' - 'A tag value can be a regular expression. ' - 'When using regular expressions, note that the wizard ' + 'A tag value can be a regular expression. Check the box to turn ' + 'them on. When using regular expressions, note that the wizard ' 'puts anchors (^ and $) around the expression, so you ' 'must ensure your expression matches from the beginning ' 'to the end of the tag.
' @@ -85,22 +85,29 @@ class TagWizard(QDialog): '

  • .*mystery.* matches any tag containing ' 'the word "mystery"
  • ') + '

    ') l.addWidget(h , 0, 0, 1, 1) + + c = QLabel(_('is RE')) + c.setToolTip('

    ' + + _('Check this box if the tag box contains regular expressions') + '

    ') + l.addWidget(c, 0, 1, 1, 1) + c = QLabel(_('Color if tag found')) c.setToolTip('

    ' + _('At least one of the two color boxes must have a value. Leave ' 'one color box empty if you want the template to use the next ' 'line in this wizard. If both boxes are filled in, the rest of ' 'the lines in this wizard will be ignored.') + '

    ') - l.addWidget(c, 0, 1, 1, 1) + l.addWidget(c, 0, 2, 1, 1) c = QLabel(_('Color if tag not found')) c.setToolTip('

    ' + _('This box is usually filled in only on the last test. If it is ' 'filled in before the last test, then the color for tag found box ' 'must be empty or all the rest of the tests will be ignored.') + '

    ') - l.addWidget(c, 0, 2, 1, 1) + l.addWidget(c, 0, 3, 1, 1) self.tagboxes = [] self.colorboxes = [] self.nfcolorboxes = [] + self.reboxes = [] self.colors = [unicode(s) for s in list(QColor.colorNames())] self.colors.insert(0, '') for i in range(0, 10): @@ -109,14 +116,20 @@ class TagWizard(QDialog): tb.update_items_cache(self.tags) self.tagboxes.append(tb) l.addWidget(tb, i+1, 0, 1, 1) - cb = QComboBox(self) - cb.addItems(self.colors) - self.colorboxes.append(cb) - l.addWidget(cb, i+1, 1, 1, 1) - cb = QComboBox(self) - cb.addItems(self.colors) - self.nfcolorboxes.append(cb) - l.addWidget(cb, i+1, 2, 1, 1) + + w = QCheckBox(self) + self.reboxes.append(w) + l.addWidget(w, i+1, 1, 1, 1) + + w = QComboBox(self) + w.addItems(self.colors) + self.colorboxes.append(w) + l.addWidget(w, i+1, 2, 1, 1) + + w = QComboBox(self) + w.addItems(self.colors) + self.nfcolorboxes.append(w) + l.addWidget(w, i+1, 3, 1, 1) if txt: lines = txt.split('\n')[3:] @@ -127,18 +140,20 @@ class TagWizard(QDialog): if len(vals) == 2: t, c = vals nc = '' + re = False else: - t,c,nc = vals + t,c,nc,re = vals try: self.colorboxes[i].setCurrentIndex(self.colorboxes[i].findText(c)) self.nfcolorboxes[i].setCurrentIndex(self.nfcolorboxes[i].findText(nc)) self.tagboxes[i].setText(t) + self.reboxes[i].setChecked(re == '2') except: pass i += 1 bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, parent=self) - l.addWidget(bb, 100, 1, 1, 1) + l.addWidget(bb, 100, 2, 1, 2) bb.accepted.connect(self.accepted) bb.rejected.connect(self.reject) self.template = '' @@ -147,11 +162,16 @@ class TagWizard(QDialog): res = ("program:\n#tag wizard -- do not directly edit\n" " t = field('tags');\n first_non_empty(\n") lines = [] - for tb, cb, nfcb in zip(self.tagboxes, self.colorboxes, self.nfcolorboxes): + for tb, cb, nfcb, reb in zip(self.tagboxes, self.colorboxes, + self.nfcolorboxes, self.reboxes): tags = [t.strip() for t in unicode(tb.text()).split(',') if t.strip()] - tags = '$|^'.join(tags) c = unicode(cb.currentText()).strip() nfc = unicode(nfcb.currentText()).strip() + re = reb.checkState() + if re == 2: + tags = '$|^'.join(tags) + else: + tags = ','.join(tags) if not tags or not (c or nfc): continue if c not in self.colors: @@ -164,18 +184,25 @@ class TagWizard(QDialog): _('The color {0} is not valid').format(nfc), show=True, show_copy_button=False) return False - lines.append(" in_list(t, ',', '^{0}$', '{1}', '{2}')".format(tags, c, nfc)) + if re == 2: + lines.append(" in_list(t, ',', '^{0}$', '{1}', '{2}')".\ + format(tags, c, nfc)) + else: + lines.append(" str_in_list(t, ',', '{0}', '{1}', '{2}')".\ + format(tags, c, nfc)) res += ',\n'.join(lines) res += ')\n' self.template = res res = '' - for tb, cb, nfcb in zip(self.tagboxes, self.colorboxes, self.nfcolorboxes): + for tb, cb, nfcb, reb in zip(self.tagboxes, self.colorboxes, + self.nfcolorboxes, self.reboxes): t = unicode(tb.text()).strip() if t.endswith(','): t = t[:-1] c = unicode(cb.currentText()).strip() nfc = unicode(nfcb.currentText()).strip() + re = unicode(reb.checkState()) if t and c: - res += '#' + t + ':|:' + c + ':|:' + nfc + '\n' + res += '#' + t + ':|:' + c + ':|:' + nfc + ':|:' + re + '\n' self.template += res self.accept() diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index fcdd56fd5f..37e4588b9b 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -209,8 +209,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): mi=None try: idx = gui.library_view.currentIndex().row() - if idx: - mi = db.get_metadata(idx, index_is_id=False) + mi = db.get_metadata(idx, index_is_id=False) except: pass diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 76faf04941..7d5dbe3e0e 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -8,7 +8,7 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import inspect, re, traceback, sys +import inspect, re, traceback from calibre.utils.titlecase import titlecase from calibre.utils.icu import capitalize, strcmp, sort_key @@ -336,6 +336,25 @@ class BuiltinInList(BuiltinFormatterFunction): return fv return nfv +class BuiltinStrInList(BuiltinFormatterFunction): + name = 'str_in_list' + arg_count = 5 + doc = _('str_in_list(val, separator, string, found_val, not_found_val) -- ' + 'treat val as a list of items separated by separator, ' + 'comparing the string against each value in the list. If the ' + 'string matches a value, return found_val, otherwise return ' + 'not_found_val. If the string contains separators, then it is ' + 'also treated as a list and each value is checked.') + + def evaluate(self, formatter, kwargs, mi, locals, val, sep, str, fv, nfv): + l = [v.strip() for v in val.split(sep) if v.strip()] + c = [v.strip() for v in str.split(sep) if v.strip()] + for v in l: + for t in c: + if strcmp(t, v) == 0: + return fv + return nfv + class BuiltinRe(BuiltinFormatterFunction): name = 're' arg_count = 3 @@ -700,6 +719,7 @@ builtin_select = BuiltinSelect() builtin_shorten = BuiltinShorten() builtin_strcat = BuiltinStrcat() builtin_strcmp = BuiltinStrcmp() +builtin_str_in_list = BuiltinStrInList() builtin_subitems = BuiltinSubitems() builtin_sublist = BuiltinSublist() builtin_substr = BuiltinSubstr() From b9b6286209dcf3e3355f6696cdfb0218e612c017 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 15:18:19 +0100 Subject: [PATCH 8/9] ... --- src/calibre/manual/template_lang.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/calibre/manual/template_lang.rst b/src/calibre/manual/template_lang.rst index 28c9855ce4..ec398b5d28 100644 --- a/src/calibre/manual/template_lang.rst +++ b/src/calibre/manual/template_lang.rst @@ -130,6 +130,7 @@ The functions available are: * ``switch(pattern, value, pattern, value, ..., else_value)`` -- for each ``pattern, value`` pair, checks if the field matches the regular expression ``pattern`` and if so, returns that ``value``. If no ``pattern`` matches, then ``else_value`` is returned. You can have as many ``pattern, value`` pairs as you want. * ``lookup(pattern, field, pattern, field, ..., else_field)`` -- like switch, except the arguments are field (metadata) names, not text. The value of the appropriate field will be fetched and used. Note that because composite columns are fields, you can use this function in one composite field to use the value of some other composite field. This is extremely useful when constructing variable save paths (more later). * ``select(key)`` -- interpret the field as a comma-separated list of items, with the items being of the form "id:value". Find the pair with the id equal to key, and return the corresponding value. This function is particularly useful for extracting a value such as an isbn from the set of identifiers for a book. + * ``str_in_list(val, separator, string, found_val, not_found_val)`` -- treat val as a list of items separated by separator, comparing the string against each value in the list. If the string matches a value, return found_val, otherwise return not_found_val. If the string contains separators, then it is also treated as a list and each value is checked. * ``subitems(val, start_index, end_index)`` -- This function is used to break apart lists of tag-like hierarchical items such as genres. It interprets the value as a comma-separated list of tag-like items, where each item is a period-separated list. Returns a new list made by first finding all the period-separated tag-like items, then for each such item extracting the components from `start_index` to `end_index`, then combining the results back together. The first component in a period-separated list has an index of zero. If an index is negative, then it counts from the end of the list. As a special case, an end_index of zero is assumed to be the length of the list. Examples:: Assuming a #genre column containing "A.B.C": @@ -272,10 +273,10 @@ Function classification summary: * Boolean: ``and``, ``or``, ``not``. The function ``if_empty`` is similar to ``and`` called with one argument. * If-then-else: ``contains``, ``test`` * Iterating over values: ``first_non_empty``, ``lookup``, ``switch`` - * List lookup: ``in_list``, ``list_item``, ``select``, + * List lookup: ``in_list``, ``list_item``, ``select``, ``str_in_list`` * List manipulation: ``count``, ``merge_lists``, ``sublist``, ``subitems`` * Recursion: ``eval``, ``template`` - * Relational: ``cmp`` , ``strcmp`` for strings + * Relational: ``cmp`` (for numbers), ``strcmp`` (for strings) * String case changes: ``lowercase``, ``uppercase``, ``titlecase``, ``capitalize`` * String manipulation: ``re``, ``shorten``, ``substr`` * Other: ``assign``, ``booksize``, ``format_date``, ``ondevice`` ``print`` From 87bd2bd5716640f114acae839e7c1c598c477298 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 15:46:17 +0100 Subject: [PATCH 9/9] Make formatter functions that use regexps ignore case --- src/calibre/utils/formatter_functions.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 7d5dbe3e0e..32822e1d72 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -269,7 +269,7 @@ class BuiltinLookup(BuiltinFormatterFunction): while i < len(args): if i + 1 >= len(args): return formatter.vformat('{' + args[i].strip() + '}', [], kwargs) - if re.search(args[i], val): + if re.search(args[i], val, flags=re.I): return formatter.vformat('{'+args[i+1].strip() + '}', [], kwargs) i += 2 @@ -295,7 +295,7 @@ class BuiltinContains(BuiltinFormatterFunction): def evaluate(self, formatter, kwargs, mi, locals, val, test, value_if_present, value_if_not): - if re.search(test, val): + if re.search(test, val, flags=re.I): return value_if_present else: return value_if_not @@ -316,7 +316,7 @@ class BuiltinSwitch(BuiltinFormatterFunction): while i < len(args): if i + 1 >= len(args): return args[i] - if re.search(args[i], val): + if re.search(args[i], val, flags=re.I): return args[i+1] i += 2 @@ -332,7 +332,7 @@ class BuiltinInList(BuiltinFormatterFunction): def evaluate(self, formatter, kwargs, mi, locals, val, sep, pat, fv, nfv): l = [v.strip() for v in val.split(sep) if v.strip()] for v in l: - if re.search(pat, v): + if re.search(pat, v, flags=re.I): return fv return nfv @@ -364,7 +364,7 @@ class BuiltinRe(BuiltinFormatterFunction): 'python-compatible regular expressions') def evaluate(self, formatter, kwargs, mi, locals, val, pattern, replacement): - return re.sub(pattern, replacement, val) + return re.sub(pattern, replacement, val, flags=re.I) class BuiltinIfempty(BuiltinFormatterFunction): name = 'ifempty'