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[源代码]

add(x [, y]*) –返回其参数的和。如果有参数不是数字,则抛出异常。此函数通常可以用+ 运算符替换。

ceiling(x)

class calibre.utils.formatter_functions.BuiltinCeiling[源代码]

ceiling(x) – 返回大于或等于x的最小整数。如果x不是数字,则引发异常。

divide(x, y)

class calibre.utils.formatter_functions.BuiltinDivide[源代码]

divide(x, y) – 返回x / y。如果x或y都不是数字,则抛出异常。此函数通常可以用 / 运算符替换。

floor(x)

class calibre.utils.formatter_functions.BuiltinFloor[源代码]

floor(x) – 返回小于或等于x的最大整数。如果x不是数字,则引发异常。

fractional_part(x)

class calibre.utils.formatter_functions.BuiltinFractionalPart[源代码]

fractional_part(x) – 返回小数点后的值。例如,fractional_part(3.14)返回0.14。如果x不是数字,则引发异常。

multiply(x [, y]*)

class calibre.utils.formatter_functions.BuiltinMultiply[源代码]

multiply(x [, y]*) –返回其参数的乘积。如果任何参数都不是数字,则抛出异常。此函数通常可以用*运算符替换。

round(x)

class calibre.utils.formatter_functions.BuiltinRound[源代码]

round(x) – 返回最接近x的整数。如果x不是数字,则引发异常。

subtract(x, y)

class calibre.utils.formatter_functions.BuiltinSubtract[源代码]

subtract(x, y) – 返回x - y。如果x或y不是数字,则抛出异常。此函数通常可以用-运算符替换。

Boolean

and(value[, value]*)

class calibre.utils.formatter_functions.BuiltinAnd[源代码]

and(value[, value]*) – 如果所有的值不为空则返回字符串 “1”, 否则返回空字符串. 这个函数和test或first_non_empty一起运行. 你可以拥有多个值。在许多情况下,&&运算符可以替换此函数。

not(value)

class calibre.utils.formatter_functions.BuiltinNot[源代码]

not(value) – 如果值为空,则返回字符串“1”,否则返回空字符串。此函数与test或first_non_empty配合使用效果很好。在许多情况下!运算符可以替代此函数。

or(value [, value]*)

class calibre.utils.formatter_functions.BuiltinOr[源代码]

or(value [, value]*) – 如果值为空,则返回字符串“1”,否则返回空字符串。此函数与test或first_non_empty配合使用效果很好。您可以有任意数量的值。在许多情况下,||运算符可以替换此函数。

Date functions

date_arithmetic(date, calc_spec, fmt)

class calibre.utils.formatter_functions.BuiltinDateArithmetic[源代码]

date_arithmetic(date, calc_spec, fmt) –使用“calc_spec”从“date”计算新日期。返回根据可选“fmt”格式化的新日期:如果未提供,则结果将为iso格式。calc_spec是通过串联成对的“vW”(valueWhat)形成的字符串,其中“v”可能是负数,W是以下字母之一: s: 将“v”秒添加到“date”,m: 将“v”分钟添加到“date”,h: 将“v”小时添加到“date”,d: 将“v”天添加到“date”,w: 将“v”周添加到“date”,y: 将“v”年添加到“date”,其中一年是365天。 示例:“1s3d-1m”将增加1秒,增加3天,并从“date”中减去1分钟。

days_between(date1, date2)

class calibre.utils.formatter_functions.BuiltinDaysBetween[源代码]

days_between(date1, date2) – 返回Date1和Date2之间的天数。如果Date1大于Date2,则该数字为正数,否则为负数。如果Date1或Date2不是日期,则函数返回空字符串。

today()

class calibre.utils.formatter_functions.BuiltinToday[源代码]

today() – 返回日期字符串。该值被设计用于format_date或days_between,但可以像任何其他字符串一样操作。日期为ISO格式。

Formatting values

finish_formatting(val, fmt, prefix, suffix)

class calibre.utils.formatter_functions.BuiltinFinishFormatting[源代码]

finish_formatting(val, fmt, prefix, suffix) – 将格式、前缀和后缀应用于一个值,就像在模板中使用`{Series_index:05.2f-x-}‘作用一样。例如,以下程序产生与上述模板相同的输出:程序:finish_formatting(field(“series_index”), “05.2f”, “ - “, “ - “)

format_date(val, format_string)

class calibre.utils.formatter_functions.BuiltinFormatDate[源代码]

format_date(val, format_string) – 使用format_string格式化该值(必须为日期),并返回一个字符串。 格式代码为: d:以带前导零(1到31)的数字表示的天 dd:以带前导零(01到31)的数字表示的天 ddd:本地化的缩写日名(例如,“ 周一”到“ 周日” ”)。 dddd:本地化的长名称(例如,“星期一”至“星期日”)。 M:月份,不带前导零(1到12)的数字。 MM:月份的数字,前导零(01到12) MMM:本地化月份的缩写名称(例如,“1月”到“ 12月”)。 MMMM:本地化的长月份名称(例如,“ 一月”到“ 十二月”)。 yy:年份为两位数字(00至99)。 yyyy:年份为四位数。 h:无前导0的小时(0至11或0至23,取决于am / pm) hh:无前导0的小时(00至11或00至23,取决于am / pm) m:分钟无前导0(0到59) mm:无前导0(00到59)的分钟 s:无前导0(0到59)的秒 ss:有前导0(00到59)的秒 ap:使用12个小时的时钟代替24个小时的时钟,用“ ap”替换为am或pm AP的本地化字符串:使用12个小时的时钟而不是24小时时钟,用“ AP”替换为AM或PM iso:带时间和时区的日期。 to_number必须是唯一的格式:日期为浮点数 from_number[:fmt]:如果存在,则使用fmt格式化时间戳,否则为iso

format_date_field(field_name, format_string)

class calibre.utils.formatter_functions.BuiltinFormatDateField[源代码]

format_date_field(field_name, format_string) – 设置字段‘field_name’中的值的格式,该字段必须是日期字段的查找名称,无论是标准的还是自定义的。有关格式代码,请参阅‘FORMAT_DATE’。此函数比FORMAT_DATE快得多,应在设置字段(列)中的值的格式时使用。它不能用于计算的日期或字符串变量中的日期。示例:FORMAT_DATE_FIELD(‘pubdate’,‘yyyy.MM.dd’)

format_number(v, template)

class calibre.utils.formatter_functions.BuiltinFormatNumber[源代码]

format_number(v, template) –使用Python格式模板(例如“ {0:5.2f}”或“ {0:,d}”或“ $ {0:5,.2f}”)格式化数字v。 模板的field_name部分必须为0(zero)(即以上示例中的“ {0:”)。 有关更多示例,请参见模板语言和Python文档。 如果模板只包含一个格式,则可以省略开头的“ {0:”和结尾的“}”。 如果无法格式化,则返回空字符串。

human_readable(v)

class calibre.utils.formatter_functions.BuiltinHumanReadable[源代码]

human_readable(v) –返回代表数字v的字符串,以KB、MB、GB等为单位。

rating_to_stars(value, use_half_stars)

class calibre.utils.formatter_functions.BuiltinRatingToStars[源代码]

rating_to_stars(value, use_half_stars) – 以星形字符字符串形式返回评级。该值是一个介于0和5之间的数字。如果希望支持非整数评级的自定义评级栏目使用半星字符,请将use_half_stars设置为1,例如2.5。

Get values from metadata

annotation_count()

class calibre.utils.formatter_functions.BuiltinAnnotationCount[源代码]

annotation_count() —返回属于前书籍的所有类型的标注总数。此功能仅在GUI中工作。

approximate_formats()

class calibre.utils.formatter_functions.BuiltinApproximateFormats[源代码]

approximate_formats() – 返回一个逗号分隔的格式列表,其中一个点与书籍关联。不能保证这个列表是正确的,尽管它可能是正确的。这个函数可以在模板程序模式下调用,使用模板 “{:’approximate_formats()’}”。请注意,如在EPUB中格式名称总是大写的。这个函数只在图形界面中工作。如果要在保存到硬盘或发送到设备模板中使用这些值,则要自定义一个“基于其它栏目创建栏目”,使用该栏目的模板中的函数,并在保存/发送模板中使用该栏目的值

author_sorts(val_separator)

class calibre.utils.formatter_functions.BuiltinAuthorSorts[源代码]

author_sorts(val_separator) – 返回一个字符串,其中包含书籍作者的排序作者值列表。排序是author元数据中的排序(不同于书籍中的author_sort)。返回的列表的格式为 排序作者1 分隔符 排序作者2等。此列表中的排序作者值与书籍作者的顺序相同。如果您希望val_paricator周围有空格,则将它们包括在分隔符字符串中

booksize()

class calibre.utils.formatter_functions.BuiltinBooksize[源代码]

booksize() – 返回字段的大小。这个函数只在图形界面中工作。如果要在保存到硬盘或发送到设备模板中使用这些值,则必须定制“基于其它栏目创建栏目”,使用该栏目的模板中的函数,并在保存/发送模板中使用该栏目的值

connected_device_name(storage_location)

class calibre.utils.formatter_functions.BuiltinConnectedDeviceName[源代码]

connected_device_name(storage_location) – 如果设备已连接,则返回设备名称,否则返回空字符串。设备上的每个存储位置可以有不同的名称。位置名称为’main’、’carda’和’cardb’。此函数仅在GUI中工作。

connected_device_uuid(storage_location)

class calibre.utils.formatter_functions.BuiltinConnectedDeviceUUID[源代码]

connected_device_uuid(storage_location) – 如果设备已连接,则返回设备uuid(唯一id),否则返回空字符串。设备上的每个存储位置可以有不同的uuid。位置名称为’main’、’carda’和’cardb’。此函数仅在GUI中工作。

current_library_name()

class calibre.utils.formatter_functions.BuiltinCurrentLibraryName[源代码]

current_library_name() – 返回当前 calibre 书库路径上的最后一个名称。这个函数可以在模板程序模式下被调用,使用模板”{:’current_library_name()’}”。

current_library_path()

class calibre.utils.formatter_functions.BuiltinCurrentLibraryPath[源代码]

current_library_path() – 返回当前 calibre 书库的路径。这个函数可以在模板程序模式下被调用,使用模板”{:’current_library_path()’}”。

current_virtual_library_name()

class calibre.utils.formatter_functions.BuiltinCurrentVirtualLibraryName[源代码]

current_virtual_library_name() –返回当前虚拟书库的名称(如果有),否则返回空字符串。书库名称大小写敏感。示例:”program: current_virtual_library_name()”.

field(lookup_name)

class calibre.utils.formatter_functions.BuiltinField[源代码]

field(lookup_name) – 通过 lookup_name 返回元数据字段名称

formats_modtimes(date_format)

class calibre.utils.formatter_functions.BuiltinFormatsModtimes[源代码]

formats_modtimes(date_format) – 返回逗号分隔的项目列表,这些项目以冒号分隔,表示书籍格式的修改时间。date_format参数指定如何将日期格式化。有关详情,请参阅format_date函数。您可以使用select函数获取特定格式的修改时间。请注意,格式名称始终为大写,就像在EPUB中一样。

formats_paths()

class calibre.utils.formatter_functions.BuiltinFormatsPaths[源代码]

formats_paths() – 返回逗号分隔的项目列表,这些项目以冒号分隔,表示书籍格式的完整路径。您可以使用SELECT函数获取特定格式的路径。请注意,格式名称始终为大写,就像在EPUB中一样。

formats_sizes()

class calibre.utils.formatter_functions.BuiltinFormatsSizes[源代码]

formats_sizes() – 返回以逗号分隔的项目列表,这些项目以冒号分隔,表示书籍格式的大小(以字节为单位)。您可以使用SELECT函数获取特定格式的大小。请注意,格式名称始终为大写,就像在EPUB中一样。

has_cover()

class calibre.utils.formatter_functions.BuiltinHasCover[源代码]

has_cover() –如果书有封面,则返回“是”,否则返回空字符串

is_marked()

class calibre.utils.formatter_functions.BuiltinIsMarked[源代码]

is_marked() –检查书籍在calibre中是否“标记”。如果是,则返回标记的值,“true”或逗号分隔的标记名称列表。如果书籍未标记,则返回 ‘’ 。

language_codes(lang_strings)

class calibre.utils.formatter_functions.BuiltinLanguageCodes[源代码]

language_codes(lang_strings) – 返回lang_strings中传递的字符串的语言代码。字符串必须使用当前区域设置的语言。Lang_strings是逗号分隔的列表。

language_strings(lang_codes, localize)

class calibre.utils.formatter_functions.BuiltinLanguageStrings[源代码]

language_strings(lang_codes, localize) – 返回lang_codes中传递的语言代码的字符串。如果localize为零,则返回英文字符串。如果localize不为零,则以当前区域设置的语言返回字符串。Lang_codes是逗号分隔的列表。

ondevice()

class calibre.utils.formatter_functions.BuiltinOndevice[源代码]

ondevice() – 如果设置了设备则返回“是”,否则返回空字符串。这个函数只在图形界面中工作。如果要在保存到硬盘或发送到设备模板中使用这些值,则必须定制“基于其它栏目创建栏目”,使用该栏目的模板中的函数,并在保存/发送模板中使用该栏目的值

raw_field(lookup_name [, optional_default])

class calibre.utils.formatter_functions.BuiltinRawField[源代码]

raw_field(lookup_name [, optional_default]) –返回由lookup_name命名的元数据字段,不应用任何格式。如果字段未定义 (‘None’),则计算并返回可选的第二个参数’default’。

raw_list(lookup_name, separator)

class calibre.utils.formatter_functions.BuiltinRawList[源代码]

raw_list(lookup_name, separator) – 返回按 lookup_name命名的元数据列表,不应用任何格式或排序,并用分隔符分隔每项。

series_sort()

class calibre.utils.formatter_functions.BuiltinSeriesSort[源代码]

series_sort() – 返回丛书排序值

user_categories()

class calibre.utils.formatter_functions.BuiltinUserCategories[源代码]

user_categories() – 返回由逗号分隔的包含这本书’自定义分类’的列表。这个函数只在图形界面中工作。如果要在保存到硬盘或发送到设备模板中使用这些值,则需自定义一个“基于其它栏目创建栏目”,使用该栏目的模板中的函数,并在保存/发送模板中使用该栏目的值

virtual_libraries()

class calibre.utils.formatter_functions.BuiltinVirtualLibraries[源代码]

virtual_libraries() – 返回包含这本书的以逗号分隔的虚拟书库列表。此功能仅在GUI中有效。如果要在保存到硬盘或发送到设备模板中使用这些值,则必须自定义“基于其它栏目创建栏目”,使用该栏目模板中的函数,并在保存/发送模板中使用该栏目的值

If-then-else

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

class calibre.utils.formatter_functions.BuiltinContains[源代码]

contains(val, pattern, text if match, text if not match) – 检查val是否包含正则表达式“ pattern”的匹配项。如果找到匹配项则返回”文本,如果匹配”,否则,返回”文本,如果不匹配”

field_exists(field_name)

class calibre.utils.formatter_functions.BuiltinFieldExists[源代码]

field_exists(field_name) – 检查是否一个字段 (栏目) 名称为 field_name 存在,存在返回 ‘1’ 否则返回 ‘’.

ifempty(val, text if empty)

class calibre.utils.formatter_functions.BuiltinIfempty[源代码]

ifempty(val, text if empty) –如果val非空值则返回val,否则返回文本。

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

class calibre.utils.formatter_functions.BuiltinTest[源代码]

test(val, text if not empty, text if empty) – 如果值不为空则返回“文本不为空”,否则返回“空文本”

Iterating over values

first_non_empty(value[, value]*)

class calibre.utils.formatter_functions.BuiltinFirstNonEmpty[源代码]

first_non_empty(value[, value]*) – 返回第一个非空的值。如果所有值都为空,则返回空值。如果需要,您可以拥有任意多个值。

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

class calibre.utils.formatter_functions.BuiltinLookup[源代码]

lookup(val, [pattern, field,]+ else_field) – 与switch类似,只不过参数是字段(元数据)名称,而不是文本。将获取并使用相应字段的值。请注意,因为复合栏目是字段,所以您可以在一个复合字段中使用此函数来使用其他一些复合字段的值。这在构建可变保存路径时非常有用

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

class calibre.utils.formatter_functions.BuiltinSwitch[源代码]

switch(val, [pattern, value,]+ else_value) – 对于每个“pattern,value”配对,检查“val”是否与正则表达式“pattern”匹配,如果匹配,则返回该“value”。如果没有匹配的规则,则返回“else_value”。如果需要,可以有任意多个“pattern,value”配对

switch_if([test_expression, value_expression,]+ else_expression)

class calibre.utils.formatter_functions.BuiltinSwitchIf[源代码]

switch_if([test_expression, value_expression,]+ else_expression) – 对于每个“test_expression,value_expression”对,检查test_express是否为True(非空),如果为True,则返回value_express的结果。如果没有test_expression为True,则返回else_expression的结果。您可以有任意数量的“test_expression,value_expression”对。

List lookup

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

class calibre.utils.formatter_functions.BuiltinIdentifierInList[源代码]

identifier_in_list(val, id_name [,found_val, not_found_val]) – 将 val 视为以逗号分隔的标识符列表。 标识符的格式为“id_name:value”。 id_name 参数是要搜索的 id_name 文本,可以是“id_name”或“id_name:regexp”。 如果有任何标识符与 id_name 匹配,则第一种情况匹配。 如果 id_name 与标识符匹配并且正则表达式与标识符的值匹配,则第二种情况匹配。 如果提供了found_val和not_found_val,则如果存在匹配则返回found_val,否则返回not_found_val。 如果未提供found_val和not_found_val,则如果存在匹配则返回identifier:value对,否则返回空字符串。

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

class calibre.utils.formatter_functions.BuiltinInList[源代码]

in_list(val, separator, [ pattern, found_val, ]+ not_found_val) –将val视为由分隔符分隔的项目列表,如果规则与任何列表值匹配,则返回found_val。如果规则与列表值不匹配,则返回not_found_val。规则和found_value对可以根据需要重复多次。该规则为按顺序检查。返回第一个匹配的found_val。别名: in_list(), list_contains()

list_item(val, index, separator)

class calibre.utils.formatter_functions.BuiltinListitem[源代码]

list_item(val, index, separator) – 将该值解释为由`分隔符`分隔的项目列表,返回第`index`项。第一个项目是零号。最后一项可以使用`List_Item(-1,分隔符)`返回。如果该项不在列表中,则返回空值。分隔符的含义与COUNT函数中的相同。

select(val, key)

class calibre.utils.formatter_functions.BuiltinSelect[源代码]

select(val, key) –将值解释为以逗号分隔的项目列表,其中项目为”id:value”。找到id等于 key的配对,并返回相应的值。如果未找到匹配项,则返回空字符串。

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

class calibre.utils.formatter_functions.BuiltinStrInList[源代码]

str_in_list(val, separator, [string, found_val, ]+ not_found_val)–将val视为由分隔符分隔的项目列表,如果字符串与任何列表值匹配,则返回found_val。如果字符串与列表值不匹配,则返回not_found_val。比较完全匹配(不包含),不区分大小写。字符串和found_value对可以根据需要重复多次。该规则为按顺序检查。返回第一个匹配的found_val。

List manipulation

count(val, separator)

class calibre.utils.formatter_functions.BuiltinCount[源代码]

count(val, separator) –将该值解释为由`分隔符`分隔的项列表,返回列表中的项数。大多数列表使用逗号作为分隔符,但作者使用&号。示例: {tags:count(,)}, {authors:count(&)}。别名: count(), list_count()

list_count_matching(list, pattern, separator)

class calibre.utils.formatter_functions.BuiltinListCountMatching[源代码]

list_count_matching(list, pattern, separator) –将“list”解释为由分隔符separator分隔的项目列表,返回列表中与正则表达式“pattern”匹配的项目数。别名: list_count_matching(), count_matching()

list_difference(list1, list2, separator)

class calibre.utils.formatter_functions.BuiltinListDifference[源代码]

list_difference(list1, list2, separator) – 使用不区分大小写的比较从list1中删除在list2中找到的任何项,从而返回一个列表。list1和list2中的项用分隔符分隔,返回的列表中的项也是如此。

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

class calibre.utils.formatter_functions.BuiltinListEquals[源代码]

list_equals(list1, sep1, list2, sep2, yes_val, no_val) – 如果list1和list2包含相同的项,则返回yes_val,否则返回no_val。这些项目是通过使用适当的分隔符(Sep1或Sep2)拆分每个列表来确定的。列表中项目的顺序无关紧要。该比较不区分大小写。

list_intersection(list1, list2, separator)

class calibre.utils.formatter_functions.BuiltinListIntersection[源代码]

list_intersection(list1, list2, separator) – 使用不区分大小写的比较从list1中删除在list2中找不到的任何项,从而返回一个列表。list1和list2中的项用分隔符分隔,返回的列表中的项也是如此。

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

class calibre.utils.formatter_functions.BuiltinListJoin[源代码]

list_join(with_separator, list1, separator1 [, list2, separator2]*) –返回通过使用with_separaator连接源列表lists (list1, 等) 中的结果列表中的项而生成的列表。 每个源列表list[123…] 中的项目由关联的分隔符separator[123…]分隔。 列表可以不包含值。 它可以是一个类似于出版商这样的单一值的字段,实际上是一个单项列表。 使用不区分大小写的比较来删除重复项。 项目将按照它们在源列表中出现的顺序返回。 如果列表中的项目仅字母大小写不同,则使用最后一个。 所有分隔符可以是多个字符。 例子:

program:

list_join(‘#@#’, $authors, ‘&’, $tags, ‘,’)

您可以对先前调用list_join的结果使用list_join,如下所示:

program:

a = list_join(‘#@#’, $authors, ‘&’, $tags, ‘,’); b = list_join(‘#@#’, a, ‘#@#’, $#genre, ‘,’, $#people, ‘&’)

可以使用表达式生成列表。例如,假设您想要作者和#genre(类型)的项目,但将genre改为单词“Genre:”,后跟该类型的第一个字母,即,类型“Fiction”变为“Genre: F”。以下操作将实现这一点:
program:

list_join(‘#@#’, $authors, ‘&’, list_re($#genre, ‘,’, ‘^(.).*$’, ‘Genre: 1’), ‘,’)

list_re(src_list, separator, include_re, opt_replace)

class calibre.utils.formatter_functions.BuiltinListRe[源代码]

list_re(src_list, separator, include_re, opt_replace) – 通过首先使用分隔符将src_list分成多个项来构造列表。对于列表中的每一项,检查它是否与include_re匹配。如果是,则将其添加到要返回的列表中。如果opt_place不是空字符串,在将项目添加到返回列表之前先应用替换。

list_re_group(src_list, separator, include_re, search_re[, group_1_template]+)

class calibre.utils.formatter_functions.BuiltinListReGroup[源代码]

list_re_group(src_list, separator, include_re, search_re[, group_1_template]+) – 除替换项之外的类似列表list_re不是可选的。当对结果列表进行替换时,它使用re_group(list_item,search_re,group_template,…)。

list_remove_duplicates(list, separator)

class calibre.utils.formatter_functions.BuiltinListRemoveDuplicates[源代码]

list_remove_duplicates(list, separator) –返回通过删除源列表中的重复项而生成的列表。如果项目仅大小写不同,则返回最后一个项目。源列表中的项和返回列表中的项目都用分隔符分隔。

list_sort(list, direction, separator)

class calibre.utils.formatter_functions.BuiltinListSort[源代码]

list_sort(list, direction, separator) – 返回使用不区分大小写的排序的列表。如果排序方向为零,则列表按升序排序,否则按降序排序。列表项与返回列表中的项一样,用分隔符分隔。

list_union(list1, list2, separator)

class calibre.utils.formatter_functions.BuiltinListUnion[源代码]

list_union(list1, list2, separator) – 通过合并list1和list2中的项,在不区分大小写的比较后,删除其中重复项,返回一个列表。如果项的大小写不同,则使用list1中的项。list1和list2中的项用分隔符分隔,返回的列表中的项也是如此。别名: list_union(), merge_lists()

range(start, stop, step, limit)

class calibre.utils.formatter_functions.BuiltinRange[源代码]

range(start, stop, step, limit) –返回通过在参数start、stop和step指定的范围内循环生成的数字列表,最大长度为limit。 产生的第一个值是“start”。 后续值next_v为current_v+step。step为正。当next_v<stop时,则循环继续,否则当next_v>stop时停止。 生成一个空列表,如果启动未通过测试:start>=stop 。此时step为正。 limit设置列表的最大长度,默认值为1000。参数start、step和limit是可选的。 调用 range() 同时带有一个指定参数stop或两个指定参数start和stop或三个指定参数start、stop和step或四个指定参数start、stop、step和limit。 示例: 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[源代码]

subitems(val, start_index, end_index) – 此函数用于拆分诸如题材等类型的项目列表。它将该值解释为逗号分隔的项列表,其中每个项都是句点分隔的列表。返回一个新列表,该列表首先找到所有用句点分隔的项目,然后将每个这样的项目从start_index提取到end_index组件,然后将结果一起返回。句点分隔列表中的第一个组件的索引为零。如果索引为负数,则从列表末尾开始计数。作为一种特殊情况,假定end_index为零是列表的长度。 使用基本模板模式并假定#genre值为“ A.B.C”的示例: {#genre:subitems(0,1)} 返回结果 “A”。 {#genre:subitems(0,2)} 返回结果 “A.B”。{#genre:subitems(1,0)} 返回结果 “B.C”。假设#genre值为”A.B.C, D.E.F”, {#genre:subitems(0,1)}返回“A,D”。{#genre:subitems(0,2)}返回“A.B,D.E”

sublist(val, start_index, end_index, separator)

class calibre.utils.formatter_functions.BuiltinSublist[源代码]

sublist(val, start_index, end_index, separator) –将该值解释为以`分隔符`分隔的项目列表,将从`start_index`生成的新列表返回给`end_index`项目。第一个项目是零号。如果索引为负数,则从列表末尾开始计数。作为一种特殊情况,假定end_index为零是列表的长度。 示例: 使用基本模板模式,并假定标签栏目(用逗号分隔)包含“ A,B,C”,{tags:sublist(0,1,\,)}返回“ A”。 {tags:sublist(0,1,\,)}返回“ C”。

{tags:sublist(-1,0,\,)} 返回“ A,B”。

Other

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

class calibre.utils.formatter_functions.BuiltinArguments[源代码]

arguments(id[=expression] [, id[=expression]]*) –在保存的模板中用于检索调用中传递的参数。它同时声明和初始化局部变量,实际上是参数。变量是位置变量;它们在同一位置获得调用中给定的参数值。如果调用中未提供相应的参数,则参数将为该变量分配所提供的默认值。如果没有默认值,则将变量设置为空字符串。

assign(id, val)

class calibre.utils.formatter_functions.BuiltinAssign[源代码]

assign(id, val) – 将val分配给id,然后返回的val. id必须是标识符,而不是表达式。此函数通常可以用=运算符替换。

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

class calibre.utils.formatter_functions.BuiltinGlobals[源代码]

globals(id[=expression] [, id[=expression]]*)–检索可以传递到格式化程序中的“全局变量”。它使用传入的全局变量的名称来声明和初始化局部变量。如果传入的全局变量中没有提供相应的变量,那么它将为该变量分配所提供的默认值。如果没有默认值,则将变量设置为空字符串。

Recursion

eval(template)

class calibre.utils.formatter_functions.BuiltinEval[源代码]

eval(template) – 评估模板,传递局部变量(那些“赋值”给的变量)而不是书籍元数据。这允许使用模板处理器从局部变量构造复杂的结果。由于{ and }字符是特殊字符,因此必须使用[[表示{字符,and]]表示}字符;它们会自动转换。还要注意,在使用模板程序模式时,不能在此函数的参数中使用前缀和后缀(`| prefix | suffix`语法)。

template(x)

class calibre.utils.formatter_functions.BuiltinTemplate[源代码]

template(x) – x作为模板进行评估。评估是在其自己的上下文中完成的,这意味着变量不会在调用方和模板评估之间共享。因为{ and }字符是特殊的,所以必须使用[[表示{字符,使用]]表示}字符; 它们会自动转换。例如,template (‘[[title_sort]]’)将评估模板{title_sort} 并返回其值。 还要注意,在使用模板程序模式时,不能在此函数的参数中使用前缀和后缀(|prefix|suffix 语法)。

Relational

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

class calibre.utils.formatter_functions.BuiltinFirstMatchingCmp[源代码]

first_matching_cmp(val, [cmp1, result1,]+, else_result) – 依次比较“ val <cmpN”,如第一次比较满足(即val小于cmpN)则返回“resultN”。 如果没有一次比较满足,则返回else_result。 示例: first_matching_cmp(10,5,”small”,10,”middle”,15,”large”,”giant”) 返回”large”。 相同例子中如果第一个值为16,则返回”giant”。

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

class calibre.utils.formatter_functions.BuiltinStrcmp[源代码]

strcmp(x, y, lt, eq, gt) – 作为字符串比较 x 和 y,不区分大小写。当 x < y 时返回 lt,x = y 时返回 eq,否则返回 gt。在许多情况下,词法比较运算符(>、<、==等)可以替换此函数。

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

class calibre.utils.formatter_functions.BuiltinStrcmpcase[源代码]

strcmpcase(x, y, lt, eq, gt) –将x和y作为字符串进行区分大小写敏感的比较。如果x<y,返回lt。如果x==y,则返回eq。否则返回gt。 注意:这不是calibre使用的默认行为,例如,在词法比较运算符 (==, >, <, 等.)中。此函数可能导致意外结果,最好尽可能使用 strcmp() 。

String case changes

capitalize(val)

class calibre.utils.formatter_functions.BuiltinCapitalize[源代码]

capitalize(val) – 返回句首大写的 val

lowercase(val)

class calibre.utils.formatter_functions.BuiltinLowercase[源代码]

lowercase(val) – 返回小写的 val

titlecase(val)

class calibre.utils.formatter_functions.BuiltinTitlecase[源代码]

titlecase(val) – 返回词首大写的 val

uppercase(val)

class calibre.utils.formatter_functions.BuiltinUppercase[源代码]

uppercase(val) – 返回大写的 val

String manipulation

character(character_name)

class calibre.utils.formatter_functions.BuiltinCharacter[源代码]

character(character_name) –返回由character_name命名的字符。例如,字符(’newline’)返回换行符(’n’)。支持的字符名为’newline’, ‘return’, ‘tab’, 和 ‘backslash’.。

re(val, pattern, replacement)

class calibre.utils.formatter_functions.BuiltinRe[源代码]

re(val, pattern, replacement) –应用正则表达式后返回val。所有`pattern`实例都将以`replacement`替换。与所有calibre中的使用相同,这些是与Python兼容的正则表达式。

re_group(val, pattern [, template_for_group]*)

class calibre.utils.formatter_functions.BuiltinReGroup[源代码]

re_group(val, pattern [, template_for_group]*) – 返回一个字符串,该字符串是通过将正则表达式应用于val,并将每个匹配的实例替换为通过将每个匹配的组替换为相应模板返回的值计算的字符串而生成的字符串。 该组的原始匹配值可用$表示。 在模板程序模式下,与模板和eval函数一样,您可以使用[[ for { and ]] for }.下面的示例表示在模板程序模式下查找具有多个单词的序列,并将第一个单词大写: {series:’re_group($, “(S* )(.*)”, “[[$:uppercase()]]”, “[[$]]”)’}

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

class calibre.utils.formatter_functions.BuiltinShorten[源代码]

shorten(val, left chars, middle text, right chars) – 返回val的缩写版本,由val开头的“左字符”字符组成,后跟“中间文本”,然后是字符串末尾的“右字符”字符。左字符`和`右字符`必须是整数。例如,假设书名为”Ancient English Laws in the Times of Ivanhoe”,您希望它最多只能容纳15个字符。如果使用{title:Shorten(9,-,5)},则结果为`Ancient E-nhoe。如果该字段的长度小于左字符+右字符+`中间文本`的长度,则该字段将被原封不动地使用。例如,标题‘The Dome`不会改变。

strcat(a [, b]*)

class calibre.utils.formatter_functions.BuiltinStrcat[源代码]

strcat(a [, b]*) –可以接受任意数量的参数。返回通过串联所有参数形成的字符串

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

class calibre.utils.formatter_functions.BuiltinStrcatMax[源代码]

strcat_max(max, string1 [, prefix2, string2]*) – 返回通过串联参数形成的字符串。返回的值初始化为string1。只要得到的字符串长度小于’max’,`Prefix,string`字符串对就会添加到值的末尾。即使String1比max长,也会返回String1。可以根据需要传递任意多个“prefix,string”对。

strlen(a)

class calibre.utils.formatter_functions.BuiltinStrlen[源代码]

strlen(a) – 返回作为参数传递的字符串的长度

substr(str, start, end)

class calibre.utils.formatter_functions.BuiltinSubstr[源代码]

substr(str, start, end) – 返回str的开始字符到结束字符。str中的第一个字符是第零个字符。如果end为负数,则表示许多字符从右边开始计数。如果end为零,则表示最后一个字符。例如,substr(’12345’,1,0)返回’2345’,substr(’12345’,1,-1)返回’234’。

swap_around_articles(val, separator)

class calibre.utils.formatter_functions.BuiltinSwapAroundArticles[源代码]

swap_around_articles(val, separator) –返回的val与冠词一起移到末尾。该值可以是一个列表,在这种情况下,将处理列表的每个成员。如果值是列表,则必须提供列表值分隔符。如果没有提供分隔符,则将该值视为单个值,而不是列表。

swap_around_comma(val)

class calibre.utils.formatter_functions.BuiltinSwapAroundComma[源代码]

swap_around_comma(val) – 如果给定值的形式为“B,A”,则返回“A、B”。这对于将’姓、名’格式的名称转换为’名、姓’非常有用。如果没有逗号,则函数返回未更改的val

to_hex(val)

class calibre.utils.formatter_functions.BuiltinToHex[源代码]

to_hex(val) –返回以十六进制编码的字符串。这在构造calibre URL时很有用。

transliterate(a)

class calibre.utils.formatter_functions.BuiltinTransliterate[源代码]

transliterate(a) – 返回一个拉丁字母表中的字符串,该字符串是通过近似源字符串中的单词的声音而形成的。例如,如果源是”Фёдор Миха́йлович Достоевский” ,则函数返回 “Fiodor Mikhailovich Dostoievskii”。

Template database functions

book_values(column, query, sep, use_vl)

class calibre.utils.formatter_functions.BuiltinBookValues[源代码]

book_values(column, query, sep, use_vl) –返回通过搜索“query”找到的书籍中“column”栏目中包含的值列表,用“sep”分隔。如果use_vl为0(零),则忽略虚拟书库。此功能只能在GUI中使用。

extra_file_modtime(file_name, format_spec)

class calibre.utils.formatter_functions.BuiltinExtraFileModtime[源代码]

extra_file_modtime(file_name, format_spec) —-返回书籍 ‘data/’文件夹中额外文件’file_name’ 的修改时间(如果存在),否则为-1.0。modtime根据’format_string’进行格式化(请参阅format_date())。如果’format_string’为空,则返回modtime格式为自epoch以来的浮点数格式的秒数。epoch的设定值取决于操作系统。此功能只能在GUI中使用。

extra_file_names(sep [, pattern])

class calibre.utils.formatter_functions.BuiltinExtraFileNames[源代码]

extra_file_names(sep [, pattern]) – 回以分隔符-分隔的书籍的 ‘data/’文件夹中额外文件的列表。 如果提供了可选参数“pattern”(一个正则表达式),会先筛选出与规则匹配的文件的列表。规则匹配不区分大小写。此功能只能在GUI中使用。

extra_file_size(file_name)

class calibre.utils.formatter_functions.BuiltinExtraFileSize[源代码]

extra_file_size(file_name) –返回书籍的 ‘data/’ 文件夹中的额外文件 ‘file_name’的大小(以字节为单位),否则为-1。此函数只能在GUI中使用。

get_note(field_name, field_value, plain_text)

class calibre.utils.formatter_functions.BuiltinGetNote[源代码]

get_note(field_name, field_value, plain_text) – 取取值为’field_value’的字段’field_name’的注释。如果’plain_text’为空,则返回注释的的HTML。如果’plain_text’非空,则返回注释的纯文本。如果注释不存在,则在两种情况下都返回 ‘’。示例:get_note(‘tags’, ‘Fiction’, ‘’) 返回附加到标签’Fiction’的注释的HTML。

has_extra_files([pattern])

class calibre.utils.formatter_functions.BuiltinHasExtraFiles[源代码]

has_extra_files([pattern]) –返回额外文件的计数,否则为”(空字符串)。如果提供了可选参数“pattern”(正则表达式),则在对文件进行计数之前,会先筛选出与规则匹配的文件的列表。规则匹配不区分大小写。此功能只能在GUI中使用。

has_note(field_name, field_value)

class calibre.utils.formatter_functions.BuiltinHasNote[源代码]

has_note(field_name, field_value) – 如果在’field_name’字段中’field_value’的值附有注释,则返回 ‘1’ ,否则返回’’ 。例如:has_note(‘tags’, ‘Fiction’) ,如果标签’fiction’带有附加注释,则返回’1’,否则返回’’。

other

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

class calibre.utils.formatter_functions.BuiltinSetGlobals[源代码]

set_globals(id[=expression] [, id[=expression]]*) –设置可以传递到格式化程序中的“全局变量”。全局变量被赋予传入的id的名称。除非提供了表达式,否则将使用id的值。

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=('未知',), other=None, template_cache=None, formatter=None)[源代码]

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)[源代码]

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>>)[源代码]

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()[源代码]

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)[源代码]

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

set_identifier(typ, val)[源代码]

If val is empty, deletes identifier of type typ

standard_field_keys()[源代码]

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

custom_field_keys()[源代码]

return a list of the custom fields in this book

all_field_keys()[源代码]

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

metadata_for_field(key)[源代码]

return metadata describing a standard or custom field.

all_non_none_fields()[源代码]

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

get_standard_metadata(field, make_copy)[源代码]

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)[源代码]

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

get_all_user_metadata(make_copy)[源代码]

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

get_user_metadata(field, make_copy)[源代码]

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)[源代码]

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

set_user_metadata(field, metadata)[源代码]

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

remove_stale_user_metadata(other_mi)[源代码]

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)[源代码]

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)[源代码]

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)[源代码]

Returns the tuple (display_name, formatted_value)

to_html()[源代码]

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