From 32b6f9943af9edacfe7efeac35d4637e681f8a20 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Mon, 17 Jan 2022 11:50:16 +0000 Subject: [PATCH] 1) Performance improvements for icon etc rules. 2) Make comparisons of undefined floats and ints consistent with template language conditionals, where undefined is assumed to be zero. --- src/calibre/library/coloring.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/calibre/library/coloring.py b/src/calibre/library/coloring.py index 63c256a23e..65c2e90888 100644 --- a/src/calibre/library/coloring.py +++ b/src/calibre/library/coloring.py @@ -113,9 +113,9 @@ class Rule: # {{{ def ondevice_condition(self, col, action, val): if action == 'is set': - return "test(ondevice(), '1', '')" + return "ondevice()" if action == 'is not set': - return "test(ondevice(), '', '1')" + return "!ondevice()" def bool_condition(self, col, action, val): test = {'is true': '0, 0, 1', @@ -128,9 +128,9 @@ class Rule: # {{{ def number_condition(self, col, action, val): if action == 'is set': - return "test(field('%s'), '1', '')"%col + return f"${col}" if action == 'is not set': - return "test(field('%s'), '', '1')"%col + return f"!${col}" lt, eq, gt = { 'eq': ('', '1', ''), 'lt': ('1', '', ''), @@ -139,13 +139,13 @@ class Rule: # {{{ if col == 'size': return f"cmp(booksize(), {val}, '{lt}', '{eq}', '{gt}')" else: - return f"cmp(raw_field('{col}'), {val}, '{lt}', '{eq}', '{gt}')" + return f"cmp(raw_field('{col}', 0), {val}, '{lt}', '{eq}', '{gt}')" def rating_condition(self, col, action, val): if action == 'is set': - return "test(field('%s'), '1', '')"%col + return f"${col}" if action == 'is not set': - return "test(field('%s'), '', '1')"%col + return f"!${col}" lt, eq, gt = { 'eq': ('', '1', ''), 'lt': ('1', '', ''), @@ -175,26 +175,25 @@ class Rule: # {{{ "format_date(today(), 'yyyy-MM-dd')), '1', '', ''), '')") %(col, val, col)) if action == 'is set': - return ("test(field('%s'), '1', '')"%(col)) + return (f"${col}") if action == 'is not set': - return ("test(field('%s'), '', '1')"%(col)) + return (f"!${col}") if action == 'is today': - return f"strcmp(format_date(raw_field('{col}'), 'yyyy-MM-dd'), 'format_date(today(), 'yyyy-MM-dd')', '', '1', '')" + return f"substr(format_date(raw_field('{col}'), 'iso'), 0, 10) == substr(today(), 0, 10)" lt, eq, gt = { 'eq': ('', '1', ''), 'lt': ('1', '', ''), 'gt': ('', '', '1') }[action] - return ("strcmp(format_date(raw_field('%s'), 'yyyy-MM-dd'), '%s', '%s', '%s', '%s')" % - (col, val, lt, eq, gt)) + return (f"strcmp(format_date(raw_field('{col}'), 'yyyy-MM-dd'), '{val}', '{lt}', '{eq}', '{gt}')") def multiple_condition(self, col, action, val, sep): if not sep or sep == '|': sep = ',' if action == 'is set': - return "test(field('%s'), '1', '')"%col + return f"${col}" if action == 'is not set': - return "test(field('%s'), '', '1')"%col + return f"!${col}" if action == 'has': return "str_in_list(field('%s'), '%s', \"%s\", '1', '')"%(col, sep, val) if action == 'does not have': @@ -206,9 +205,9 @@ class Rule: # {{{ def text_condition(self, col, action, val): if action == 'is set': - return "test(field('%s'), '1', '')"%col + return f"${col}" if action == 'is not set': - return "test(field('%s'), '', '1')"%col + return f"!${col}" if action == 'is': return "strcmp(field('%s'), \"%s\", '', '1', '')"%(col, val) if action == 'is not':