Reference for all built-in template language functionsĀ¶

Here, we document all the built-in functions available in the calibre template language. Every function is implemented as a class in python and you can click the source links to see the source code, in case the documentation is insufficient. The functions are arranged in logical groups by type.

ArithmeticĀ¶

add(x [, y]*)Ā¶

class calibre.utils.formatter_functions.BuiltinAdd[source]Ā¶

add(x [, y]*) ā€“ returns the sum of its arguments. Throws an exception if an argument is not a number. This function can often be replaced with the + operator.

ceiling(x)Ā¶

class calibre.utils.formatter_functions.BuiltinCeiling[source]Ā¶

ceiling(x) ā€“ returns the smallest integer greater than or equal to x. Throws an exception if x is not a number.

divide(x, y)Ā¶

class calibre.utils.formatter_functions.BuiltinDivide[source]Ā¶

divide(x, y) ā€“ returns x / y. Throws an exception if either x or y are not numbers. This function can often be replaced with the / operator.

floor(x)Ā¶

class calibre.utils.formatter_functions.BuiltinFloor[source]Ā¶

floor(x) ā€“ returns the largest integer less than or equal to x. Throws an exception if x is not a number.

fractional_part(x)Ā¶

class calibre.utils.formatter_functions.BuiltinFractionalPart[source]Ā¶

fractional_part(x) ā€“ returns the value after the decimal point. For example, fractional_part(3.14) returns 0.14. Throws an exception if x is not a number.

mod(x)Ā¶

class calibre.utils.formatter_functions.BuiltinMod[source]Ā¶

mod(x) ā€“ returns floor(remainder of x / y). Throws an exception if either x or y is not a number.

multiply(x [, y]*)Ā¶

class calibre.utils.formatter_functions.BuiltinMultiply[source]Ā¶

multiply(x [, y]*) ā€“ returns the product of its arguments. Throws an exception if any argument is not a number. This function can often be replaced with the * operator.

round(x)Ā¶

class calibre.utils.formatter_functions.BuiltinRound[source]Ā¶

round(x) ā€“ returns the nearest integer to x. Throws an exception if x is not a number.

subtract(x, y)Ā¶

class calibre.utils.formatter_functions.BuiltinSubtract[source]Ā¶

subtract(x, y) ā€“ returns x - y. Throws an exception if either x or y are not numbers. This function can often be replaced with the - operator.

BooleanĀ¶

and(value [, value]*)Ā¶

class calibre.utils.formatter_functions.BuiltinAnd[source]Ā¶

and(value [, value]*) ā€“ returns the string ā€œ1ā€ if all values are 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. In many cases the && operator can replace this function.

not(value)Ā¶

class calibre.utils.formatter_functions.BuiltinNot[source]Ā¶

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. In many cases the ! operator can replace this function.

or(value [, value]*)Ā¶

class calibre.utils.formatter_functions.BuiltinOr[source]Ā¶

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. In many cases the || operator can replace this function.

Date functionsĀ¶

date_arithmetic(date, calc_spec, fmt)Ā¶

class calibre.utils.formatter_functions.BuiltinDateArithmetic[source]Ā¶

date_arithmetic(date, calc_spec, fmt) ā€“ Calculate a new date from ā€˜dateā€™ using ā€˜calc_specā€™. Return the new date formatted according to optional ā€˜fmtā€™: if not supplied then the result will be in iso format. The calc_spec is a string formed by concatenating pairs of ā€˜vWā€™ (valueWhat) where ā€˜vā€™ is a possibly-negative number and W is one of the following letters: s: add ā€˜vā€™ seconds to ā€˜dateā€™ m: add ā€˜vā€™ minutes to ā€˜dateā€™ h: add ā€˜vā€™ hours to ā€˜dateā€™ d: add ā€˜vā€™ days to ā€˜dateā€™ w: add ā€˜vā€™ weeks to ā€˜dateā€™ y: add ā€˜vā€™ years to ā€˜dateā€™, where a year is 365 days. Example: ā€˜1s3d-1mā€™ will add 1 second, add 3 days, and subtract 1 minute from ā€˜dateā€™.

days_between(date1, date2)Ā¶

class calibre.utils.formatter_functions.BuiltinDaysBetween[source]Ā¶

days_between(date1, date2) ā€“ return the number of days between date1 and date2. The number is positive if date1 is greater than date2, otherwise negative. If either date1 or date2 are not dates, the function returns the empty string.

today()Ā¶

class calibre.utils.formatter_functions.BuiltinToday[source]Ā¶

today() ā€“ return a date string for today. This value is designed for use in format_date or days_between, but can be manipulated like any other string. The date is in ISO format.

Formatting valuesĀ¶

finish_formatting(val, fmt, prefix, suffix)Ā¶

class calibre.utils.formatter_functions.BuiltinFinishFormatting[source]Ā¶

finish_formatting(val, fmt, prefix, suffix) ā€“ apply the format, prefix, and suffix to a value in the same way as done in a template like {series_index:05.2f| - |- }. For example, the following program produces the same output as the above template: program: finish_formatting(field(ā€œseries_indexā€), ā€œ05.2fā€, ā€œ - ā€œ, ā€œ - ā€œ)

format_date(val, format_string)Ā¶

class calibre.utils.formatter_functions.BuiltinFormatDate[source]Ā¶

format_date(val, format_string) ā€“ format the value, which must be a date, using the format_string, returning a string. The formatting codes are: d : the day as number without a leading zero (1 to 31) dd : the day as number with a leading zero (01 to 31) ddd : the abbreviated localized day name (e.g. ā€œMonā€ to ā€œSunā€). dddd : the long localized day name (e.g. ā€œMondayā€ to ā€œSundayā€). M : the month as number without a leading zero (1 to 12). MM : the month as number with a leading zero (01 to 12) MMM : the abbreviated localized month name (e.g. ā€œJanā€ to ā€œDecā€). MMMM : the long localized month name (e.g. ā€œJanuaryā€ to ā€œDecemberā€). yy : the year as two digit number (00 to 99). yyyy : the year as four digit number. h : the hours without a leading 0 (0 to 11 or 0 to 23, depending on am/pm) hh : the hours with a leading 0 (00 to 11 or 00 to 23, depending on am/pm) m : the minutes without a leading 0 (0 to 59) mm : the minutes with a leading 0 (00 to 59) s : the seconds without a leading 0 (0 to 59) ss : the seconds with a leading 0 (00 to 59) ap : use a 12-hour clock instead of a 24-hour clock, with ā€œapā€ replaced by the localized string for am or pm AP : use a 12-hour clock instead of a 24-hour clock, with ā€œAPā€ replaced by the localized string for AM or PM iso : the date with time and timezone. Must be the only format present to_number: the date as a floating point number from_number[:fmt]: format the timestamp using fmt if present otherwise iso

format_date_field(field_name, format_string)Ā¶

class calibre.utils.formatter_functions.BuiltinFormatDateField[source]Ā¶

format_date_field(field_name, format_string) ā€“ format the value in the field ā€˜field_nameā€™, which must be the lookup name of date field, either standard or custom. See ā€˜format_dateā€™ for the formatting codes. This function is much faster than format_date and should be used when you are formatting the value in a field (column). It canā€™t be used for computed dates or dates in string variables. Example: format_date_field(ā€˜pubdateā€™, ā€˜yyyy.MM.ddā€™)

format_number(v, template)Ā¶

class calibre.utils.formatter_functions.BuiltinFormatNumber[source]Ā¶

format_number(v, template) ā€“ format the number v using a Python formatting template such as ā€œ{0:5.2f}ā€ or ā€œ{0:,d}ā€ or ā€œ${0:5,.2f}ā€. The field_name part of the template must be a 0 (zero) (the ā€œ{0:ā€ in the above examples). See the template language and Python documentation for more examples. You can leave off the leading ā€œ{0:ā€ and trailing ā€œ}ā€ if the template contains only a format. Returns the empty string if formatting fails.

human_readable(v)Ā¶

class calibre.utils.formatter_functions.BuiltinHumanReadable[source]Ā¶

human_readable(v) ā€“ return a string representing the number v in KB, MB, GB, etc.

rating_to_stars(value, use_half_stars)Ā¶

class calibre.utils.formatter_functions.BuiltinRatingToStars[source]Ā¶

rating_to_stars(value, use_half_stars) ā€“ Returns the rating as string of star characters. The value is a number between 0 and 5. Set use_half_stars to 1 if you want half star characters for custom ratings columns that support non-integer ratings, for example 2.5.

urls_from_identifiers(identifiers, sort_results)Ā¶

class calibre.utils.formatter_functions.BuiltinUrlsFromIdentifiers[source]Ā¶

urls_from_identifiers(identifiers, sort_results) ā€“ given a comma-separated list of identifiers, where an identifier is a colon-separated pair of values (name:id_value), returns a comma-separated list of HTML URLs generated from the identifiers. The list not sorted if sort_results is 0 (character or number), otherwise it is sorted alphabetically by the identifier name. The URLs are generated in the same way as the built-in identifiers column when shown in Book details.

Get values from metadataĀ¶

annotation_count()Ā¶

class calibre.utils.formatter_functions.BuiltinAnnotationCount[source]Ā¶

annotation_count() ā€“ return the total number of annotations of all types attached to the current book. This function works only in the GUI.

approximate_formats()Ā¶

class calibre.utils.formatter_functions.BuiltinApproximateFormats[source]Ā¶

approximate_formats() ā€“ return a comma-separated list of formats that at one point were associated with the book. There is no guarantee that this list is correct, although it probably is. This function can be called in template program mode using the template ā€œ{:ā€™approximate_formats()ā€™}ā€. Note that format names are always uppercase, as in EPUB. This function works only in the GUI. If you want to use these values in save-to-disk or send-to-device templates then you must make a custom ā€œColumn built from other columnsā€, use the function in that columnā€™s template, and use that columnā€™s value in your save/send templates

author_sorts(val_separator)Ā¶

class calibre.utils.formatter_functions.BuiltinAuthorSorts[source]Ā¶

author_sorts(val_separator) ā€“ returns a string containing a list of authorā€™s sort values for the authors of the book. The sort is the one in the author metadata (different from the author_sort in books). The returned list has the form author sort 1 val_separator author sort 2 etc. The author sort values in this list are in the same order as the authors of the book. If you want spaces around val_separator then include them in the separator string

booksize()Ā¶

class calibre.utils.formatter_functions.BuiltinBooksize[source]Ā¶

booksize() ā€“ return value of the size field. This function works only in the GUI. If you want to use this value in save-to-disk or send-to-device templates then you must make a custom ā€œColumn built from other columnsā€, use the function in that columnā€™s template, and use that columnā€™s value in your save/send templates

connected_device_name(storage_location)Ā¶

class calibre.utils.formatter_functions.BuiltinConnectedDeviceName[source]Ā¶

connected_device_name(storage_location) ā€“ if a device is connected then return the device name, otherwise return the empty string. Each storage location on a device can have a different name. The location names are ā€˜mainā€™, ā€˜cardaā€™ and ā€˜cardbā€™. This function works only in the GUI.

connected_device_uuid(storage_location)Ā¶

class calibre.utils.formatter_functions.BuiltinConnectedDeviceUUID[source]Ā¶

connected_device_uuid(storage_location) ā€“ if a device is connected then return the device uuid (unique id), otherwise return the empty string. Each storage location on a device has a different uuid. The location names are ā€˜mainā€™, ā€˜cardaā€™ and ā€˜cardbā€™. This function works only in the GUI.

current_library_name()Ā¶

class calibre.utils.formatter_functions.BuiltinCurrentLibraryName[source]Ā¶

current_library_name() ā€“ return the last name on the path to the current calibre library. This function can be called in template program mode using the template ā€œ{:ā€™current_library_name()ā€™}ā€.

current_library_path()Ā¶

class calibre.utils.formatter_functions.BuiltinCurrentLibraryPath[source]Ā¶

current_library_path() ā€“ return the path to the current calibre library. This function can be called in template program mode using the template ā€œ{:ā€™current_library_path()ā€™}ā€.

current_virtual_library_name()Ā¶

class calibre.utils.formatter_functions.BuiltinCurrentVirtualLibraryName[source]Ā¶

current_virtual_library_name() ā€“ return the name of the current virtual library if there is one, otherwise the empty string. Library name case is preserved. Example: ā€œprogram: current_virtual_library_name()ā€.

field(lookup_name)Ā¶

class calibre.utils.formatter_functions.BuiltinField[source]Ā¶

field(lookup_name) ā€“ returns the metadata field named by lookup_name

formats_modtimes(date_format)Ā¶

class calibre.utils.formatter_functions.BuiltinFormatsModtimes[source]Ā¶

formats_modtimes(date_format) ā€“ return a comma-separated list of colon-separated items representing modification times for the formats of a book. The date_format parameter specifies how the date is to be formatted. See the format_date function for details. You can use the select function to get the mod time for a specific format. Note that format names are always uppercase, as in EPUB.

formats_paths()Ā¶

class calibre.utils.formatter_functions.BuiltinFormatsPaths[source]Ā¶

formats_paths() ā€“ return a comma-separated list of colon-separated items representing full path to the formats of a book. You can use the select function to get the path for a specific format. Note that format names are always uppercase, as in EPUB.

formats_sizes()Ā¶

class calibre.utils.formatter_functions.BuiltinFormatsSizes[source]Ā¶

formats_sizes() ā€“ return a comma-separated list of colon-separated items representing sizes in bytes of the formats of a book. You can use the select function to get the size for a specific format. Note that format names are always uppercase, as in EPUB.

has_cover()Ā¶

class calibre.utils.formatter_functions.BuiltinHasCover[source]Ā¶

has_cover() ā€“ return Yes if the book has a cover, otherwise return the empty string

is_marked()Ā¶

class calibre.utils.formatter_functions.BuiltinIsMarked[source]Ā¶

is_marked() ā€“ check whether the book is ā€˜markedā€™ in calibre. If it is then return the value of the mark, either ā€˜trueā€™ or the comma-separated list of named marks. Returns ā€˜ā€™ if the book is not marked.

language_codes(lang_strings)Ā¶

class calibre.utils.formatter_functions.BuiltinLanguageCodes[source]Ā¶

language_codes(lang_strings) ā€“ return the language codes for the strings passed in lang_strings. The strings must be in the language of the current locale. Lang_strings is a comma-separated list.

language_strings(lang_codes, localize)Ā¶

class calibre.utils.formatter_functions.BuiltinLanguageStrings[source]Ā¶

language_strings(lang_codes, localize) ā€“ return the strings for the language codes passed in lang_codes. If localize is zero, return the strings in English. If localize is not zero, return the strings in the language of the current locale. Lang_codes is a comma-separated list.

ondevice()Ā¶

class calibre.utils.formatter_functions.BuiltinOndevice[source]Ā¶

ondevice() ā€“ return Yes if ondevice is set, otherwise return the empty string. This function works only in the GUI. If you want to use this value in save-to-disk or send-to-device templates then you must make a custom ā€œColumn built from other columnsā€, use the function in that columnā€™s template, and use that columnā€™s value in your save/send templates

raw_field(lookup_name [, optional_default])Ā¶

class calibre.utils.formatter_functions.BuiltinRawField[source]Ā¶

raw_field(lookup_name [, optional_default]) ā€“ returns the metadata field named by lookup_name without applying any formatting. It evaluates and returns the optional second argument ā€˜defaultā€™ if the field is undefined (ā€˜Noneā€™).

raw_list(lookup_name, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinRawList[source]Ā¶

raw_list(lookup_name, separator) ā€“ returns the metadata list named by lookup_name without applying any formatting or sorting and with items separated by separator.

series_sort()Ā¶

class calibre.utils.formatter_functions.BuiltinSeriesSort[source]Ā¶

series_sort() ā€“ return the series sort value

user_categories()Ā¶

class calibre.utils.formatter_functions.BuiltinUserCategories[source]Ā¶

user_categories() ā€“ return a comma-separated list of the user categories that contain this book. This function works only in the GUI. If you want to use these values in save-to-disk or send-to-device templates then you must make a custom ā€œColumn built from other columnsā€, use the function in that columnā€™s template, and use that columnā€™s value in your save/send templates

virtual_libraries()Ā¶

class calibre.utils.formatter_functions.BuiltinVirtualLibraries[source]Ā¶

virtual_libraries() ā€“ return a comma-separated list of Virtual libraries that contain this book. This function works only in the GUI. If you want to use these values in save-to-disk or send-to-device templates then you must make a custom ā€œColumn built from other columnsā€, use the function in that columnā€™s template, and use that columnā€™s value in your save/send templates

If-then-elseĀ¶

check_yes_no(field_name, is_undefined, is_false, is_true)Ā¶

class calibre.utils.formatter_functions.BuiltinCheckYesNo[source]Ā¶

check_yes_no(field_name, is_undefined, is_false, is_true) ā€“ checks the value of the yes/no field named by the lookup key field_name for a value specified by the parameters, returning ā€œyesā€ if a match is found, otherwise returning an empty string. Set the parameter is_undefined, is_false, or is_true to 1 (the number) to check that condition, otherwise set it to 0. Example: check_yes_no(ā€œ#boolā€, 1, 0, 1) returns ā€œyesā€ if the yes/no field ā€œ#boolā€ is either undefined (neither True nor False) or True. More than one of is_undefined, is_false, or is_true can be set to 1. This function is usually used by the test() or is_empty() functions.

contains(val, pattern, text if match, text if not match)Ā¶

class calibre.utils.formatter_functions.BuiltinContains[source]Ā¶

contains(val, pattern, text if match, text if not match) ā€“ checks if val contains matches for the regular expression pattern. Returns text if match if matches are found, otherwise it returns text if no match

field_exists(field_name)Ā¶

class calibre.utils.formatter_functions.BuiltinFieldExists[source]Ā¶

field_exists(field_name) ā€“ checks if a field (column) named field_name exists, returning ā€˜1ā€™ if so and ā€˜ā€™ if not.

ifempty(val, text if empty)Ā¶

class calibre.utils.formatter_functions.BuiltinIfempty[source]Ā¶

ifempty(val, text if empty) ā€“ return val if val is not empty, otherwise return text if empty

test(val, text if not empty, text if empty)Ā¶

class calibre.utils.formatter_functions.BuiltinTest[source]Ā¶

test(val, text if not empty, text if empty) ā€“ return text if not empty if val is not empty, otherwise return text if empty

Iterating over valuesĀ¶

first_non_empty(value [, value]*)Ā¶

class calibre.utils.formatter_functions.BuiltinFirstNonEmpty[source]Ā¶

first_non_empty(value [, value]*) ā€“ returns the first value that is not empty. If all values are empty, then the empty string is returned. You can have as many values as you want.

lookup(val, [pattern, field,]+ else_field)Ā¶

class calibre.utils.formatter_functions.BuiltinLookup[source]Ā¶

lookup(val, [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

switch(val, [pattern, value,]+ else_value)Ā¶

class calibre.utils.formatter_functions.BuiltinSwitch[source]Ā¶

switch(val, [pattern, value,]+ else_value) ā€“ for each pattern, value pair, checks if val 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

switch_if([test_expression, value_expression,]+ else_expression)Ā¶

class calibre.utils.formatter_functions.BuiltinSwitchIf[source]Ā¶

switch_if([test_expression, value_expression,]+ else_expression) ā€“ for each ā€œtest_expression, value_expressionā€ pair, checks if test_expression is True (non-empty) and if so returns the result of value_expression. If no test_expression is True then the result of else_expression is returned. You can have as many ā€œtest_expression, value_expressionā€ pairs as you want.

List lookupĀ¶

identifier_in_list(val, id_name [, found_val, not_found_val])Ā¶

class calibre.utils.formatter_functions.BuiltinIdentifierInList[source]Ā¶

identifier_in_list(val, id_name [, found_val, not_found_val]) ā€“ treat val as a list of identifiers separated by commas. An identifier has the format ā€œid_name:valueā€. The id_name parameter is the id_name text to search for, either ā€œid_nameā€ or ā€œid_name:regexpā€. The first case matches if there is any identifier matching that id_name. The second case matches if id_name matches an identifier and the regexp matches the identifierā€™s value. If found_val and not_found_val are provided then if there is a match then return found_val, otherwise return not_found_val. If found_val and not_found_val are not provided then if there is a match then return the identifier:value pair, otherwise the empty string.

in_list(val, separator, [ pattern, found_val, ]+ not_found_val)Ā¶

class calibre.utils.formatter_functions.BuiltinInList[source]Ā¶

in_list(val, separator, [ pattern, found_val, ]+ not_found_val) ā€“ treating val as a list of items separated by separator, if the pattern matches any of the list values then return found_val.If the pattern matches no list value then return not_found_val. The pattern and found_value pairs can be repeated as many times as desired. The patterns are checked in order. The found_val for the first match is returned. Aliases: in_list(), list_contains()

list_item(val, index, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinListitem[source]Ā¶

list_item(val, index, separator) ā€“ interpret the value as a list of items separated by separator, returning the index`th item. The first item is number zero. The last item can be returned using `list_item(-1,separator). If the item is not in the list, then the empty value is returned. The separator has the same meaning as in the count function.

select(val, key)Ā¶

class calibre.utils.formatter_functions.BuiltinSelect[source]Ā¶

select(val, key) ā€“ interpret the value as a comma-separated list of items, with the items being ā€œid:valueā€. Find the pair with the id equal to key, and return the corresponding value. Returns the empty string if no match is found.

str_in_list(val, separator, [string, found_val, ]+ not_found_val)Ā¶

class calibre.utils.formatter_functions.BuiltinStrInList[source]Ā¶

str_in_list(val, separator, [string, found_val, ]+ not_found_val) ā€“ treating val as a list of items separated by separator, if the string matches any of the list values then return found_val.If the string matches no list value then return not_found_val. The comparison is exact match (not contains) and is case insensitive. The string and found_value pairs can be repeated as many times as desired. The patterns are checked in order. The found_val for the first match is returned.

List manipulationĀ¶

count(val, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinCount[source]Ā¶

count(val, separator) ā€“ interprets the value as a list of items separated by separator, returning the number of items in the list. Most lists use a comma as the separator, but authors uses an ampersand. Examples: {tags:count(,)}, {authors:count(&)}. Aliases: count(), list_count()

list_count_matching(list, pattern, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinListCountMatching[source]Ā¶

list_count_matching(list, pattern, separator) ā€“ interprets ā€˜listā€™ as a list of items separated by ā€˜separatorā€™, returning the number of items in the list that match the regular expression ā€˜patternā€™. Aliases: list_count_matching(), count_matching()

list_difference(list1, list2, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinListDifference[source]Ā¶

list_difference(list1, list2, separator) ā€“ return a list made by removing from list1 any item found in list2, using a case-insensitive comparison. The items in list1 and list2 are separated by separator, as are the items in the returned list.

list_equals(list1, sep1, list2, sep2, yes_val, no_val)Ā¶

class calibre.utils.formatter_functions.BuiltinListEquals[source]Ā¶

list_equals(list1, sep1, list2, sep2, yes_val, no_val) ā€“ return yes_val if list1 and list2 contain the same items, otherwise return no_val. The items are determined by splitting each list using the appropriate separator character (sep1 or sep2). The order of items in the lists is not relevant. The comparison is case insensitive.

list_intersection(list1, list2, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinListIntersection[source]Ā¶

list_intersection(list1, list2, separator) ā€“ return a list made by removing from list1 any item not found in list2, using a case-insensitive comparison. The items in list1 and list2 are separated by separator, as are the items in the returned list.

list_join(with_separator, list1, separator1 [, list2, separator2]*)Ā¶

class calibre.utils.formatter_functions.BuiltinListJoin[source]Ā¶

list_join(with_separator, list1, separator1 [, list2, separator2]*) ā€“ return a list made by joining the items in the source lists (list1, etc) using with_separator between the items in the result list. Items in each source list[123ā€¦] are separated by the associated separator[123ā€¦]. A list can contain zero values. It can be a field like publisher that is single-valued, effectively a one-item list. Duplicates are removed using a case-insensitive comparison. Items are returned in the order they appear in the source lists. If items on lists differ only in letter case then the last is used. All separators can be more than one character. Example:

program:

list_join(ā€˜#@#ā€™, $authors, ā€˜&ā€™, $tags, ā€˜,ā€™)

You can use list_join on the results of previous calls to list_join as follows:

program:

a = list_join(ā€˜#@#ā€™, $authors, ā€˜&ā€™, $tags, ā€˜,ā€™); b = list_join(ā€˜#@#ā€™, a, ā€˜#@#ā€™, $#genre, ā€˜,ā€™, $#people, ā€˜&ā€™)

You can use expressions to generate a list. For example, assume you want items for authors and #genre, but with the genre changed to the word ā€˜Genre: ā€˜ followed by the first letter of the genre, i.e. the genre ā€˜Fictionā€™ becomes ā€˜Genre: Fā€™. The following will do that:
program:

list_join(ā€˜#@#ā€™, $authors, ā€˜&ā€™, list_re($#genre, ā€˜,ā€™, ā€˜^(.).*$ā€™, ā€˜Genre: 1ā€™), ā€˜,ā€™)

list_re(src_list, separator, include_re, opt_replace)Ā¶

class calibre.utils.formatter_functions.BuiltinListRe[source]Ā¶

list_re(src_list, separator, include_re, opt_replace) ā€“ Construct a list by first separating src_list into items using the separator character. For each item in the list, check if it matches include_re. If it does, then add it to the list to be returned. If opt_replace is not the empty string, then apply the replacement before adding the item to the returned list.

list_re_group(src_list, separator, include_re, search_re [, group_template]+)Ā¶

class calibre.utils.formatter_functions.BuiltinListReGroup[source]Ā¶

list_re_group(src_list, separator, include_re, search_re [, group_template]+) ā€“ Like list_re except replacements are not optional. It uses re_group(list_item, search_re, group_template, ā€¦) when doing the replacements on the resulting list.

list_remove_duplicates(list, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinListRemoveDuplicates[source]Ā¶

list_remove_duplicates(list, separator) ā€“ return a list made by removing duplicate items in the source list. If items differ only in case, the last of them is returned. The items in source list are separated by separator, as are the items in the returned list.

list_sort(list, direction, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinListSort[source]Ā¶

list_sort(list, direction, separator) ā€“ return list sorted using a case-insensitive sort. If direction is zero, the list is sorted ascending, otherwise descending. The list items are separated by separator, as are the items in the returned list.

list_split(list_val, sep, id_prefix)Ā¶

class calibre.utils.formatter_functions.BuiltinListSplit[source]Ā¶

list_split(list_val, sep, id_prefix) ā€“ splits the list_val into separate values using ā€˜sepā€™, then assigns the values to variables named ā€˜id_prefix_Nā€™ where N is the position of the value in the list. The first item has position 0 (zero). The function returns the last element in the list. Example: split(ā€˜one:two:fooā€™, ā€˜:ā€™, ā€˜varā€™) is equivalent to var_0 = ā€˜oneā€™; var_1 = ā€˜twoā€™; var_2 = ā€˜fooā€™.

list_union(list1, list2, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinListUnion[source]Ā¶

list_union(list1, list2, separator) ā€“ return a list made by merging the items in list1 and list2, removing duplicate items using a case-insensitive comparison. 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. Aliases: list_union(), merge_lists()

range(start, stop, step, limit)Ā¶

class calibre.utils.formatter_functions.BuiltinRange[source]Ā¶

range(start, stop, step, limit) ā€“ returns a list of numbers generated by looping over the range specified by the parameters start, stop, and step, with a maximum length of limit. The first value produced is ā€˜startā€™. Subsequent values next_v are current_v+step. The loop continues while next_v < stop assuming step is positive, otherwise while next_v > stop. An empty list is produced if start fails the test: start>=stop if step is positive. The limit sets the maximum length of the list and has a default of 1000. The parameters start, step, and limit are optional. Calling range() with one argument specifies stop. Two arguments specify start and stop. Three arguments specify start, stop, and step. Four arguments specify start, stop, step and limit. Examples: range(5) -> ā€˜0,1,2,3,4ā€™. range(0,5) -> ā€˜0,1,2,3,4ā€™. range(-1,5) -> ā€˜-1,0,1,2,3,4ā€™. range(1,5) -> ā€˜1,2,3,4ā€™. range(1,5,2) -> ā€˜1,3ā€™. range(1,5,2,5) -> ā€˜1,3ā€™. range(1,5,2,1) -> error(limit exceeded).

subitems(val, start_index, end_index)Ā¶

class calibre.utils.formatter_functions.BuiltinSubitems[source]Ā¶

subitems(val, start_index, end_index) ā€“ This function is used to break apart lists of items such as genres. It interprets the value as a comma-separated list of items, where each item is a period-separated list. Returns a new list made by first finding all the period-separated items, then for each such item extracting the start_index to the end_index components, 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. Example using basic template mode and assuming a #genre value of ā€œA.B.Cā€: {#genre:subitems(0,1)} returns ā€œAā€. {#genre:subitems(0,2)} returns ā€œA.Bā€. {#genre:subitems(1,0)} returns ā€œB.Cā€. Assuming a #genre value of ā€œA.B.C, D.E.Fā€, {#genre:subitems(0,1)} returns ā€œA, Dā€. {#genre:subitems(0,2)} returns ā€œA.B, D.Eā€

sublist(val, start_index, end_index, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinSublist[source]Ā¶

sublist(val, start_index, end_index, separator) ā€“ interpret the value as a list of items separated by separator, returning a new list made from the start_index to the end_index item. The first item is number 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 using basic template mode and assuming that the tags column (which is comma-separated) contains ā€œA, B, Cā€: {tags:sublist(0,1,\,)} returns ā€œAā€. {tags:sublist(-1,0,\,)} returns ā€œCā€. {tags:sublist(0,-1,\,)} returns ā€œA, Bā€.

OtherĀ¶

arguments(id[=expression] [, id[=expression]]*)Ā¶

class calibre.utils.formatter_functions.BuiltinArguments[source]Ā¶

arguments(id[=expression] [, id[=expression]]*) ā€“ Used in a stored template to retrieve the arguments passed in the call. It both declares and initializes local variables, effectively parameters. The variables are positional; they get the value of the parameter given in the call in the same position. If the corresponding parameter is not provided in the call then arguments assigns that variable the provided default value. If there is no default value then the variable is set to the empty string.

assign(id, val)Ā¶

class calibre.utils.formatter_functions.BuiltinAssign[source]Ā¶

assign(id, val) ā€“ assigns val to id, then returns val. id must be an identifier, not an expression. This function can often be replaced with the = operator.

globals(id[=expression] [, id[=expression]]*)Ā¶

class calibre.utils.formatter_functions.BuiltinGlobals[source]Ā¶

globals(id[=expression] [, id[=expression]]*) ā€“ Retrieves ā€œglobal variablesā€ that can be passed into the formatter. It both declares and initializes local variables with the names of the global variables passed in. If the corresponding variable is not provided in the passed-in globals then it assigns that variable the provided default value. If there is no default value then the variable is set to the empty string.

RecursionĀ¶

eval(template)Ā¶

class calibre.utils.formatter_functions.BuiltinEval[source]Ā¶

eval(template) ā€“ evaluates the template, passing the local variables (those ā€˜assignā€™ed to) instead of the book metadata. This permits using the template processor to construct complex results from local variables. Because the { and } characters are special, you must use [[ for the { character and ]] for the } character; they are converted automatically. Note also that prefixes and suffixes (the |prefix|suffix syntax) cannot be used in the argument to this function when using template program mode.

template(x)Ā¶

class calibre.utils.formatter_functions.BuiltinTemplate[source]Ā¶

template(x) ā€“ evaluates x as a template. The evaluation is done in its own context, meaning that variables are not shared between the caller and the template evaluation. Because the { and } characters are special, you must use [[ for the { character and ]] for the } character; they are converted automatically. For example, template(ā€˜[[title_sort]]ā€™) will evaluate the template {title_sort} and return its value. Note also that prefixes and suffixes (the |prefix|suffix syntax) cannot be used in the argument to this function when using template program mode.

RelationalĀ¶

cmp(x, y, lt, eq, gt)Ā¶

class calibre.utils.formatter_functions.BuiltinCmp[source]Ā¶

cmp(x, y, lt, eq, gt) ā€“ compares x and y after converting both to numbers. Returns lt if x < y. Returns eq if x == y. Otherwise returns gt. In many cases the numeric comparison operators (>#, <#, ==# etc) can replace this function.

first_matching_cmp(val, [cmp1, result1,]+, else_result)Ā¶

class calibre.utils.formatter_functions.BuiltinFirstMatchingCmp[source]Ā¶

first_matching_cmp(val, [cmp1, result1,]+, else_result) ā€“ compares ā€œval < cmpNā€ in sequence, returning resultN for the first comparison that succeeds. Returns else_result if no comparison succeeds. Example: first_matching_cmp(10,5,ā€smallā€,10,ā€middleā€,15,ā€largeā€,ā€giantā€) returns ā€œlargeā€. The same example with a first value of 16 returns ā€œgiantā€.

strcmp(x, y, lt, eq, gt)Ā¶

class calibre.utils.formatter_functions.BuiltinStrcmp[source]Ā¶

strcmp(x, y, lt, eq, gt) ā€“ does a case-insensitive comparison of x and y as strings. Returns lt if x < y. Returns eq if x == y. Otherwise returns gt. In many cases the lexical comparison operators (>, <, == etc) can replace this function.

strcmpcase(x, y, lt, eq, gt)Ā¶

class calibre.utils.formatter_functions.BuiltinStrcmpcase[source]Ā¶

strcmpcase(x, y, lt, eq, gt) ā€“ does a case-sensitive comparison of x and y as strings. Returns lt if x < y. Returns eq if x == y. Otherwise returns gt. Note: This is NOT the default behavior used by calibre, for example, in the lexical comparison operators (==, >, <, etc.). This function could cause unexpected results, preferably use strcmp() whenever possible.

String case changesĀ¶

capitalize(val)Ā¶

class calibre.utils.formatter_functions.BuiltinCapitalize[source]Ā¶

capitalize(val) ā€“ return val capitalized

lowercase(val)Ā¶

class calibre.utils.formatter_functions.BuiltinLowercase[source]Ā¶

lowercase(val) ā€“ return val in lower case

titlecase(val)Ā¶

class calibre.utils.formatter_functions.BuiltinTitlecase[source]Ā¶

titlecase(val) ā€“ return val in title case

uppercase(val)Ā¶

class calibre.utils.formatter_functions.BuiltinUppercase[source]Ā¶

uppercase(val) ā€“ return val in upper case

String manipulationĀ¶

character(character_name)Ā¶

class calibre.utils.formatter_functions.BuiltinCharacter[source]Ā¶

character(character_name) ā€“ returns the character named by character_name. For example, character(ā€˜newlineā€™) returns a newline character (ā€™nā€™). The supported character names are ā€˜newlineā€™, ā€˜returnā€™, ā€˜tabā€™, and ā€˜backslashā€™.

re(val, pattern, replacement)Ā¶

class calibre.utils.formatter_functions.BuiltinRe[source]Ā¶

re(val, pattern, replacement) ā€“ return val after applying the regular expression. All instances of pattern are replaced with replacement. As in all of calibre, these are Python-compatible regular expressions

re_group(val, pattern [, template_for_group]*)Ā¶

class calibre.utils.formatter_functions.BuiltinReGroup[source]Ā¶

re_group(val, pattern [, template_for_group]*) ā€“ return a string made by applying the regular expression pattern to the val and replacing each matched instance with the string computed by replacing each matched group by the value returned by the corresponding template. The original matched value for the group is available as $. In template program mode, like for the template and the eval functions, you use [[ for { and ]] for }. The following example in template program mode looks for series with more than one word and uppercases the first word: {series:ā€™re_group($, ā€œ(S* )(.*)ā€, ā€œ[[$:uppercase()]]ā€, ā€œ[[$]]ā€)ā€™}

shorten(val, left chars, middle text, right chars)Ā¶

class calibre.utils.formatter_functions.BuiltinShorten[source]Ā¶

shorten(val, left chars, middle text, right chars) ā€“ Return a shortened version of val, consisting of left chars characters from the beginning of val, followed by middle text, followed by right chars characters from the end of the string. Left chars and right chars must be integers. For example, assume the title of the book is Ancient English Laws in the Times of Ivanhoe, and you want it to fit in a space of at most 15 characters. If you use {title:shorten(9,-,5)}, the result will be Ancient E-anhoe. If the fieldā€™s length is less than left chars + right chars + the length of middle text, then the field will be used intact. For example, the title The Dome would not be changed.

strcat(a [, b]*)Ā¶

class calibre.utils.formatter_functions.BuiltinStrcat[source]Ā¶

strcat(a [, b]*) ā€“ can take any number of arguments. Returns the string formed by concatenating all the arguments

strcat_max(max, string1 [, prefix2, string2]*)Ā¶

class calibre.utils.formatter_functions.BuiltinStrcatMax[source]Ā¶

strcat_max(max, string1 [, prefix2, string2]*) ā€“ Returns a string formed by concatenating the arguments. The returned value is initialized to string1. Prefix, string pairs are added to the end of the value as long as the resulting string length is less than max. String1 is returned even if string1 is longer than max. You can pass as many prefix, string pairs as you wish.

strlen(a)Ā¶

class calibre.utils.formatter_functions.BuiltinStrlen[source]Ā¶

strlen(a) ā€“ Returns the length of the string passed as the argument

substr(str, start, end)Ā¶

class calibre.utils.formatter_functions.BuiltinSubstr[source]Ā¶

substr(str, start, end) ā€“ returns the startā€™th through the endā€™th characters of str. The first character in str is the zeroā€™th character. If end is negative, then it indicates that many characters counting from the right. If end is zero, then it indicates the last character. For example, substr(ā€˜12345ā€™, 1, 0) returns ā€˜2345ā€™, and substr(ā€˜12345ā€™, 1, -1) returns ā€˜234ā€™.

swap_around_articles(val, separator)Ā¶

class calibre.utils.formatter_functions.BuiltinSwapAroundArticles[source]Ā¶

swap_around_articles(val, separator) ā€“ returns the val with articles moved to the end. The value can be a list, in which case each member of the list is processed. If the value is a list then you must provide the list value separator. If no separator is provided then the value is treated as being a single value, not a list.

swap_around_comma(val)Ā¶

class calibre.utils.formatter_functions.BuiltinSwapAroundComma[source]Ā¶

swap_around_comma(val) ā€“ given a value of the form ā€œB, Aā€, return ā€œA Bā€. This is most useful for converting names in LN, FN format to FN LN. If there is no comma, the function returns val unchanged

to_hex(val)Ā¶

class calibre.utils.formatter_functions.BuiltinToHex[source]Ā¶

to_hex(val) ā€“ returns the string encoded in hex. This is useful when constructing calibre URLs.

transliterate(a)Ā¶

class calibre.utils.formatter_functions.BuiltinTransliterate[source]Ā¶

transliterate(a) ā€“ Returns a string in a latin alphabet formed by approximating the sound of the words in the source string. For example, if the source is ā€œŠ¤Ń‘Š“Š¾Ń€ ŠœŠøхŠ°ĢŠ¹Š»Š¾Š²Šøч Š”Š¾ŃŃ‚Š¾ŠµŠ²ŃŠŗŠøŠ¹ā€ the function returns ā€œFiodor Mikhailovich Dostoievskiiā€.

Template database functionsĀ¶

book_count(query, use_vl)Ā¶

class calibre.utils.formatter_functions.BuiltinBookCount[source]Ā¶

book_count(query, use_vl) ā€“ returns the count of books found by searching for query. If use_vl is 0 (zero) then virtual libraries are ignored. This function can be used only in the GUI.

book_values(column, query, sep, use_vl)Ā¶

class calibre.utils.formatter_functions.BuiltinBookValues[source]Ā¶

book_values(column, query, sep, use_vl) ā€“ returns a list of the values contained in the column ā€œcolumnā€, separated by ā€œsepā€, in the books found by searching for ā€œqueryā€. If use_vl is 0 (zero) then virtual libraries are ignored. This function can be used only in the GUI.

extra_file_modtime(file_name, format_string)Ā¶

class calibre.utils.formatter_functions.BuiltinExtraFileModtime[source]Ā¶

extra_file_modtime(file_name, format_string) ā€“ returns the modification time of the extra file ā€˜file_nameā€™ in the bookā€™s ā€˜data/ā€™ folder if it exists, otherwise -1.0. The modtime is formatted according to ā€˜format_stringā€™ (see format_date()). If ā€˜format_stringā€™ is empty, returns the modtime as the floating point number of seconds since the epoch. The epoch is OS dependent. This function can be used only in the GUI.

extra_file_names(sep [, pattern])Ā¶

class calibre.utils.formatter_functions.BuiltinExtraFileNames[source]Ā¶

extra_file_names(sep [, pattern]) ā€“ returns a sep-separated list of extra files in the bookā€™s ā€˜data/ā€™ folder. If the optional parameter ā€˜patternā€™, a regular expression, is supplied then the list is filtered to files that match pattern. The pattern match is case insensitive. This function can be used only in the GUI.

extra_file_size(file_name)Ā¶

class calibre.utils.formatter_functions.BuiltinExtraFileSize[source]Ā¶

extra_file_size(file_name) ā€“ returns the size in bytes of the extra file ā€˜file_nameā€™ in the bookā€™s ā€˜data/ā€™ folder if it exists, otherwise -1.This function can be used only in the GUI.

get_note(field_name, field_value, plain_text)Ā¶

class calibre.utils.formatter_functions.BuiltinGetNote[source]Ā¶

get_note(field_name, field_value, plain_text) ā€“ fetch the note for field ā€˜field_nameā€™ with value ā€˜field_valueā€™. If ā€˜plain_textā€™ is empty, return the noteā€™s HTML. If ā€˜plain_textā€™ is non-empty, return the noteā€™s plain text. If the note doesnā€™t exist, return ā€˜ā€™ in both cases. Example: get_note(ā€˜tagsā€™, ā€˜Fictionā€™, ā€˜ā€™) returns the HTML of the note attached to the tag ā€˜Fictionā€™.

has_extra_files([pattern])Ā¶

class calibre.utils.formatter_functions.BuiltinHasExtraFiles[source]Ā¶

has_extra_files([pattern]) ā€“ returns the count of extra files, otherwise ā€˜ā€™ (the empty string). If the optional parameter ā€˜patternā€™ (a regular expression) is supplied then the list is filtered to files that match pattern before the files are counted. The pattern match is case insensitive. This function can be used only in the GUI.

has_note(field_name, field_value)Ā¶

class calibre.utils.formatter_functions.BuiltinHasNote[source]Ā¶

has_note(field_name, field_value) ā€“ return ā€˜1ā€™ if the value ā€˜field_valueā€™ in the field ā€˜field_nameā€™ has an attached note, ā€˜ā€™ otherwise. Example: has_note(ā€˜tagsā€™, ā€˜Fictionā€™) returns ā€˜1ā€™ if the tag ā€˜fictionā€™ has an attached note, ā€˜ā€™ otherwise.

otherĀ¶

set_globals(id[=expression] [, id[=expression]]*)Ā¶

class calibre.utils.formatter_functions.BuiltinSetGlobals[source]Ā¶

set_globals(id[=expression] [, id[=expression]]*) ā€“ Sets ā€œglobal variablesā€ that can be passed into the formatter. The globals are given the name of the id passed in. The value of the id is used unless an expression is provided.

API of the Metadata objectsĀ¶

The python implementation of the template functions is passed in a Metadata object. Knowing itā€™s API is useful if you want to define your own template functions.

class calibre.ebooks.metadata.book.base.Metadata(title, authors=('Unknown',), other=None, template_cache=None, formatter=None)[source]Ā¶

A class representing all the metadata for a book. The various standard metadata fields are available as attributes of this object. You can also stick arbitrary attributes onto this object.

Metadata from custom columns should be accessed via the get() method, passing in the lookup name for the column, for example: ā€œ#mytagsā€.

Use the is_null() method to test if a field is null.

This object also has functions to format fields into strings.

The list of standard metadata fields grows with time is in STANDARD_METADATA_FIELDS.

Please keep the method based API of this class to a minimum. Every method becomes a reserved field name.

is_null(field)[source]Ā¶

Return True if the value of field is null in this object. ā€˜nullā€™ means it is unknown or evaluates to False. So a title of _(ā€˜Unknownā€™) is null or a language of ā€˜undā€™ is null.

Be careful with numeric fields since this will return True for zero as well as None.

Also returns True if the field does not exist.

deepcopy(class_generator=<function Metadata.<lambda>>)[source]Ā¶

Do not use this method unless you know what you are doing, if you want to create a simple clone of this object, use deepcopy_metadata() instead. Class_generator must be a function that returns an instance of Metadata or a subclass of it.

get_identifiers()[source]Ā¶

Return a copy of the identifiers dictionary. The dict is small, and the penalty for using a reference where a copy is needed is large. Also, we donā€™t want any manipulations of the returned dict to show up in the book.

set_identifiers(identifiers)[source]Ā¶

Set all identifiers. Note that if you previously set ISBN, calling this method will delete it.

set_identifier(typ, val)[source]Ā¶

If val is empty, deletes identifier of type typ

standard_field_keys()[source]Ā¶

return a list of all possible keys, even if this book doesnā€™t have them

custom_field_keys()[source]Ā¶

return a list of the custom fields in this book

all_field_keys()[source]Ā¶

All field keys known by this instance, even if their value is None

metadata_for_field(key)[source]Ā¶

return metadata describing a standard or custom field.

all_non_none_fields()[source]Ā¶

Return a dictionary containing all non-None metadata fields, including the custom ones.

get_standard_metadata(field, make_copy)[source]Ā¶

return field metadata from the field if it is there. Otherwise return None. field is the key name, not the label. Return a copy if requested, just in case the user wants to change values in the dict.

get_all_standard_metadata(make_copy)[source]Ā¶

return a dict containing all the standard field metadata associated with the book.

get_all_user_metadata(make_copy)[source]Ā¶

return a dict containing all the custom field metadata associated with the book.

get_user_metadata(field, make_copy)[source]Ā¶

return field metadata from the object if it is there. Otherwise return None. field is the key name, not the label. Return a copy if requested, just in case the user wants to change values in the dict.

set_all_user_metadata(metadata)[source]Ā¶

store custom field metadata into the object. Field is the key name not the label

set_user_metadata(field, metadata)[source]Ā¶

store custom field metadata for one column into the object. Field is the key name not the label

remove_stale_user_metadata(other_mi)[source]Ā¶

Remove user metadata keys (custom column keys) if they donā€™t exist in ā€˜other_miā€™, which must be a metadata object

template_to_attribute(other, ops)[source]Ā¶

Takes a list [(src,dest), (src,dest)], evaluates the template in the context of other, then copies the result to self[dest]. This is on a best-efforts basis. Some assignments can make no sense.

smart_update(other, replace_metadata=False)[source]Ā¶

Merge the information in other into self. In case of conflicts, the information in other takes precedence, unless the information in other is NULL.

format_field(key, series_with_index=True)[source]Ā¶

Returns the tuple (display_name, formatted_value)

to_html()[source]Ā¶

A HTML representation of this object.

calibre.ebooks.metadata.book.base.STANDARD_METADATA_FIELDSĀ¶

The set of standard metadata fields.


'''
All fields must have a NULL value represented as None for simple types,
an empty list/dictionary for complex types and (None, None) for cover_data
'''

SOCIAL_METADATA_FIELDS = frozenset((
    'tags',             # Ordered list
    'rating',           # A floating point number between 0 and 10
    'comments',         # A simple HTML enabled string
    'series',           # A simple string
    'series_index',     # A floating point number
    # Of the form { scheme1:value1, scheme2:value2}
    # For example: {'isbn':'123456789', 'doi':'xxxx', ... }
    'identifiers',
))

'''
The list of names that convert to identifiers when in get and set.
'''

TOP_LEVEL_IDENTIFIERS = frozenset((
    'isbn',
))

PUBLICATION_METADATA_FIELDS = frozenset((
    'title',            # title must never be None. Should be _('Unknown')
    # Pseudo field that can be set, but if not set is auto generated
    # from title and languages
    'title_sort',
    'authors',          # Ordered list. Must never be None, can be [_('Unknown')]
    'author_sort_map',  # Map of sort strings for each author
    # Pseudo field that can be set, but if not set is auto generated
    # from authors and languages
    'author_sort',
    'book_producer',
    'timestamp',        # Dates and times must be timezone aware
    'pubdate',
    'last_modified',
    'rights',
    # So far only known publication type is periodical:calibre
    # If None, means book
    'publication_type',
    'uuid',             # A UUID usually of type 4
    'languages',        # ordered list of languages in this publication
    'publisher',        # Simple string, no special semantics
    # Absolute path to image file encoded in filesystem_encoding
    'cover',
    # Of the form (format, data) where format is, e.g. 'jpeg', 'png', 'gif'...
    'cover_data',
    # Either thumbnail data, or an object with the attribute
    # image_path which is the path to an image file, encoded
    # in filesystem_encoding
    'thumbnail',
))

BOOK_STRUCTURE_FIELDS = frozenset((
    # These are used by code, Null values are None.
    'toc', 'spine', 'guide', 'manifest',
))

USER_METADATA_FIELDS = frozenset((
    # A dict of dicts similar to field_metadata. Each field description dict
    # also contains a value field with the key #value#.
    'user_metadata',
))

DEVICE_METADATA_FIELDS = frozenset((
    'device_collections',   # Ordered list of strings
    'lpath',                # Unicode, / separated
    'size',                 # In bytes
    'mime',                 # Mimetype of the book file being represented
))

CALIBRE_METADATA_FIELDS = frozenset((
    'application_id',   # An application id, currently set to the db_id.
    'db_id',            # the calibre primary key of the item.
    'formats',          # list of formats (extensions) for this book
    # a dict of user category names, where the value is a list of item names
    # from the book that are in that category
    'user_categories',
    # a dict of items to associated hyperlink
    'link_maps',
))

ALL_METADATA_FIELDS =      SOCIAL_METADATA_FIELDS.union(
                           PUBLICATION_METADATA_FIELDS).union(
                           BOOK_STRUCTURE_FIELDS).union(
                           USER_METADATA_FIELDS).union(
                           DEVICE_METADATA_FIELDS).union(
                           CALIBRE_METADATA_FIELDS)

# All fields except custom fields
STANDARD_METADATA_FIELDS = SOCIAL_METADATA_FIELDS.union(
                           PUBLICATION_METADATA_FIELDS).union(
                           BOOK_STRUCTURE_FIELDS).union(
                           DEVICE_METADATA_FIELDS).union(
                           CALIBRE_METADATA_FIELDS)

# Metadata fields that smart update must do special processing to copy.
SC_FIELDS_NOT_COPIED =     frozenset(('title', 'title_sort', 'authors',
                                      'author_sort', 'author_sort_map',
                                      'cover_data', 'tags', 'languages',
                                      'identifiers'))

# Metadata fields that smart update should copy only if the source is not None
SC_FIELDS_COPY_NOT_NULL =  frozenset(('device_collections', 'lpath', 'size', 'comments', 'thumbnail'))

# Metadata fields that smart update should copy without special handling
SC_COPYABLE_FIELDS =       SOCIAL_METADATA_FIELDS.union(
                           PUBLICATION_METADATA_FIELDS).union(
                           BOOK_STRUCTURE_FIELDS).union(
                           DEVICE_METADATA_FIELDS).union(
                           CALIBRE_METADATA_FIELDS) - \
                           SC_FIELDS_NOT_COPIED.union(
                           SC_FIELDS_COPY_NOT_NULL)

SERIALIZABLE_FIELDS =      SOCIAL_METADATA_FIELDS.union(
                           USER_METADATA_FIELDS).union(
                           PUBLICATION_METADATA_FIELDS).union(
                           CALIBRE_METADATA_FIELDS).union(
                           DEVICE_METADATA_FIELDS) - \
                           frozenset(('device_collections', 'formats',
                               'cover_data'))
# these are rebuilt when needed