From 6c26b9debc72060cfc10f1239d96311c1691ed08 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 29 May 2011 10:04:34 +0100 Subject: [PATCH] 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()