View Single Post
Old 08-11-2016, 04:41 PM   #8
readin
Enthusiast
readin is on a distinguished road
 
Posts: 25
Karma: 50
Join Date: Apr 2016
Device: calibre
Quote:
Originally Posted by chaley View Post
We are getting very technical here.
True, but you're very good at explaining!

Here's what I have now:

1) An advanced rule for coloring books if they don't have an ebook format:

Code:
program:
	contains(approximate_formats(), 'EPUB|MOBI|PDF', '', 'red')
2) An advanced rule for composing icons which calls three functions:

Code:
program:
	strcat(
		icon_if_val('#own', 'p', 'book'),
		':',
		icon_if_set('series', 'series'),
		':',
		icon_if_fmt('LNK', 'folder')
	)
This function checks if a field has a specified value:

Code:
name: icon_if_val
arg count: 3
doc: icon_if_val(field, val, icon) -- if 'field' contains 'val', returns icon filename ('icon' + '.png'), else returns 'blank.png'
program code:
def evaluate(self, formatter, kwargs, mi, locals, field, val, icon):
	import re
	f = mi.get(field)
	# if 'field' is an array (like tags), convert it to a CSV list:
	if isinstance(f, list):
		f = ','.join(f)
	if re.search(val, f):
		return icon + '.png'
	return 'blank.png'
This function checks if a field has any value (is not empty):

Code:
name: icon_if_set
arg count: 2
doc: icon_if_set(field, icon) -- if 'field' value is set, returns icon filename ('icon' + '.png:'), else returns 'blank.png'
program code:
def evaluate(self, formatter, kwargs, mi, locals, field, icon):
	if mi.get(field):
		return icon + '.png'
	return 'blank.png'
This function checks if a book has a specified format:

Code:
name: icon_if_fmt
arg count: 2
doc: icon_if_fmt(format, icon) -- if format is found, returns icon filename ('icon' + '.png'), else returns 'blank.png'
program code:
def evaluate(self, formatter, kwargs, mi, locals, format, icon):
	import re
	fmt_data = mi._proxy_metadata.db_approx_formats
	fmt_data = ','.join(v.upper() for v in fmt_data)
	if re.search(format, fmt_data):
		return icon + '.png'
	return 'blank.png'
As you correctly guessed, the field I'm sending to the icon_if_val() function is like tags. But I used your second example anyway, which checks if the field is an array, in case I want to expand my use of that function in the future.

Everything seems to be working perfectly. Thanks so much for all your help!
readin is offline   Reply With Quote