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.
This commit is contained in:
Charles Haley 2022-01-17 11:50:16 +00:00
parent 8423f761af
commit 32b6f9943a

View File

@ -113,9 +113,9 @@ class Rule: # {{{
def ondevice_condition(self, col, action, val): def ondevice_condition(self, col, action, val):
if action == 'is set': if action == 'is set':
return "test(ondevice(), '1', '')" return "ondevice()"
if action == 'is not set': if action == 'is not set':
return "test(ondevice(), '', '1')" return "!ondevice()"
def bool_condition(self, col, action, val): def bool_condition(self, col, action, val):
test = {'is true': '0, 0, 1', test = {'is true': '0, 0, 1',
@ -128,9 +128,9 @@ class Rule: # {{{
def number_condition(self, col, action, val): def number_condition(self, col, action, val):
if action == 'is set': if action == 'is set':
return "test(field('%s'), '1', '')"%col return f"${col}"
if action == 'is not set': if action == 'is not set':
return "test(field('%s'), '', '1')"%col return f"!${col}"
lt, eq, gt = { lt, eq, gt = {
'eq': ('', '1', ''), 'eq': ('', '1', ''),
'lt': ('1', '', ''), 'lt': ('1', '', ''),
@ -139,13 +139,13 @@ class Rule: # {{{
if col == 'size': if col == 'size':
return f"cmp(booksize(), {val}, '{lt}', '{eq}', '{gt}')" return f"cmp(booksize(), {val}, '{lt}', '{eq}', '{gt}')"
else: 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): def rating_condition(self, col, action, val):
if action == 'is set': if action == 'is set':
return "test(field('%s'), '1', '')"%col return f"${col}"
if action == 'is not set': if action == 'is not set':
return "test(field('%s'), '', '1')"%col return f"!${col}"
lt, eq, gt = { lt, eq, gt = {
'eq': ('', '1', ''), 'eq': ('', '1', ''),
'lt': ('1', '', ''), 'lt': ('1', '', ''),
@ -175,26 +175,25 @@ class Rule: # {{{
"format_date(today(), 'yyyy-MM-dd')), '1', '', ''), '')") "format_date(today(), 'yyyy-MM-dd')), '1', '', ''), '')")
%(col, val, col)) %(col, val, col))
if action == 'is set': if action == 'is set':
return ("test(field('%s'), '1', '')"%(col)) return (f"${col}")
if action == 'is not set': if action == 'is not set':
return ("test(field('%s'), '', '1')"%(col)) return (f"!${col}")
if action == 'is today': 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 = { lt, eq, gt = {
'eq': ('', '1', ''), 'eq': ('', '1', ''),
'lt': ('1', '', ''), 'lt': ('1', '', ''),
'gt': ('', '', '1') 'gt': ('', '', '1')
}[action] }[action]
return ("strcmp(format_date(raw_field('%s'), 'yyyy-MM-dd'), '%s', '%s', '%s', '%s')" % return (f"strcmp(format_date(raw_field('{col}'), 'yyyy-MM-dd'), '{val}', '{lt}', '{eq}', '{gt}')")
(col, val, lt, eq, gt))
def multiple_condition(self, col, action, val, sep): def multiple_condition(self, col, action, val, sep):
if not sep or sep == '|': if not sep or sep == '|':
sep = ',' sep = ','
if action == 'is set': if action == 'is set':
return "test(field('%s'), '1', '')"%col return f"${col}"
if action == 'is not set': if action == 'is not set':
return "test(field('%s'), '', '1')"%col return f"!${col}"
if action == 'has': if action == 'has':
return "str_in_list(field('%s'), '%s', \"%s\", '1', '')"%(col, sep, val) return "str_in_list(field('%s'), '%s', \"%s\", '1', '')"%(col, sep, val)
if action == 'does not have': if action == 'does not have':
@ -206,9 +205,9 @@ class Rule: # {{{
def text_condition(self, col, action, val): def text_condition(self, col, action, val):
if action == 'is set': if action == 'is set':
return "test(field('%s'), '1', '')"%col return f"${col}"
if action == 'is not set': if action == 'is not set':
return "test(field('%s'), '', '1')"%col return f"!${col}"
if action == 'is': if action == 'is':
return "strcmp(field('%s'), \"%s\", '', '1', '')"%(col, val) return "strcmp(field('%s'), \"%s\", '', '1', '')"%(col, val)
if action == 'is not': if action == 'is not':