View Single Post
Old 01-22-2011, 07:55 AM   #66
beckywc
Addict
beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.beckywc ought to be getting tired of karma fortunes by now.
 
beckywc's Avatar
 
Posts: 272
Karma: 1050426
Join Date: Feb 2010
Location: California
Device: iPad Mini w/Retina, Kindle 3, Kindle Fire HDX 8.9, & Asus Transformer
Quote:
Originally Posted by pchrist7 View Post
Hi beckywc.
With great help from chaley, once again, I think I have what you want.
In exchange I want your help to create a howto for the sticky thread about custom columns
Please look closely at the instructions, grammar, punctuations etc.
I want this to be as precise as possible.
Also, do you understand the Documentation part in the Template function ?
Please suggest corrections, clarifications.
So please try the following instructions, to the letter, to see if I got it all right:
And of course, please post all comments to this thread.


Howto:

Do you have a Kindle ?
Did you ever want Calibre to show "Read Status" of your books in a column ?
Automagically ?

Then the following might for you as well.

You must be running Calibre 0.7.40 or newer to use this.
Also you must use "Fetch annotations" from you Kindle.

The following example is based on the following Kindler requirements:
"What I want is a custom column to show the "Last Opened" Date if Last location read > 95%.
If the book shows >0% but < 95%, I want the column to show "Reading"
And if the book is 0% or unopened the column should show "Not Read".


How-to "Fetch annotations" from you Kindle:
Start Calibre,
Connect your Kindle, wait a bit,
Click an the arrow just to the right of the "Send to Device" icon,
choose "Fetch Annotations"
and the Kindle "Book status" is added to the Calibre book details, in the comments.

This is just an example of what I could with my K3:

25-12-2010
Last Page Read: Location 7365 (68%)
Location 4603 • Bookmark
Location 4942 • Bookmark
Location 4980 • Bookmark
Location 5984 • Bookmark
Location 7363 • Bookmark


The best/fastest way, but hardest way, to get the needed information into a custom column, is to use Python Template functions.
Sounds scary, but don't worry. Copy/paste is your helper

Simply do as follows:

1) go to Preferences -> Advanced -> Template Functions.

2) in the Function box type: kindle_read_status

3) in the Arg count box type: 4

4) in the Documentation box, paste the following:
Code:
Check if the associated field contains a kindle annotation for percent read. If not, return no_page_read_str. If so, then compare that percent against is_read_pct. If the val is larger, then return date, if the val is >0 and < is_read_pct return is_reading_str. Otherwise return is_not_read_str. 

One usage: {comments:kindle_read_status(95,Not Read,Reading)}
( use the above when defining your custom column )
95 goes into variable is_read_pct
Not Read goes into variable no_page_read_str
Reading goes into variable is_reading_str

Feel free to change the %, the test strings to suit your needs.
Do this in the custom column definition.
5) In the Program Code box, paste the following:
Code:
def evaluate(self, formatter, kwargs, mi, locals, val, is_read_pct,  
				is_reading_str, no_page_read_str):
	try:
		test_val = int(is_read_pct)
	except:
		return 'is_read_pct is not a number'

	import re
	mg = re.match('.*\s(\d+[-/]\d+[-/]\d\d\d\d).*?Last Page Read: Location \d+ \((\d+)%\)', val, re.I + re.DOTALL);
	if mg is None:
		return no_page_read_str
	date = mg.group(1)
	pct = mg.group(2)
	try:
		f = int(pct)
		if f > test_val:
			return date
		elif f > 0:
			return is_reading_str + ': ' + pct + '%'
	except:
		pass
	return no_page_read_str
6) press the Create button, then the Apply button.

7) Still in Preferences, Choose Add your own columns

8) Choose Add custom column bar at the bottom

9) enter the following into your custom column:
Lookup name: read_status
Column Heading: Read Status
Column type: Column built from other columns
Template:
Code:
{comments:kindle_read_status(95,Reading,Not Read)}
10) Choose Apply

11) Click Ok to the Restart Warning

12) Shutdown Calibre

13) Start Calibre

If you followed instructions minutely, you should have a new column showing the "Read Status" for your books.

If you like this functionality, please remember to send some Karma to chaley

https://www.mobileread.com/forums/rep....php?p=1351864

He (?) has been absolutely fantastic and helpfull with this.

If this fails, please try one more time before posting to this thread.
This has been tested by several Kindlers succesfully.
Enjoy

Edit: Using the code formatting of the Template function documentation, looses all line breaks during copy/paste. Any suggestions ?
Also, need help with the "chaley karma" link
Quote:
Originally Posted by pchrist7 View Post
Well, I DID promise to do it, right ?


In a unix shell script you use #!/usr/bin/ksh -x as first line in script, or any where else in script use set -x
Then you'll see, line by line, how the scripts is executed, what value is assigned to variables, comparisons, etc.
I was more looking for a "python -x " thingy that could do something similar.
Forgive my ignorance, but sometimes wishes DO come true. ( I heard )


Thanks. Will give it a try sometime.
Still working, but now from home


Code:
def myfunc(self, formatter, kwargs, mi, locals, val, is_read_pct, is_read_str, 
				is_not_read_str, no_page_read_str):
	try:
		test_val = int(is_read_pct)
	except:
		return 'is_read_pct is not a number'
	print 'here'
	import re
	mg = re.match('.*last page read: location \d+ \((\d+)%\)', val, re.I + re.DOTALL);
	if mg is None:
		return no_page_read_str
	print 'here2'
	try:
		f = int(mg.group(1))
		if f >= test_val:
			return is_read_str
		return is_not_read_str
	except:
		pass
	return no_page_read_str

def evaluate(self, *args):

	import linecache, inspect, sys

	def traceit(frame, event, arg):
		if event == 'line':
			lineno = frame.f_lineno
			if '__file__' in frame.f_globals:
				filename = frame.f_globals['__file__']
				if (filename.endswith('.pyc') or
					filename.endswith('.pyo')):
					filename = filename[:-1]
				name = frame.f_globals['__name__']
				line = linecache.getline(filename, lineno)
			else:
				name = '[unknown]'
				try:
					src = inspect.getsourcelines(frame)
					line = src[lineno]
				except IOError:
					line = 'Unknown code named [%s].  VM instruction #%d' % \
						(frame.f_code.co_name, frame.f_lasti)
			print '%s:%s: %s' % (name, lineno, line.rstrip())
		return traceit

	sys.settrace(traceit)
	try:
		rv = self.myfunc(*args)
	except:
		rv = 'EXCEPTION'
	sys.settrace(None)
	return rv
Acknowledgment: code for the traceit function from http://www.friday.com/bbum/2005/10/2...hon-execution/
Will give it a couple of test runs tomorrow
BTW: You have any tips for my latest added "edit" comments ?
Hoping beckywc will try my howto during the weekend.
Did 3 test runs, in 3 different Calibre libs.
My main lib with 1k books works fine, no performance penalty I can detect.
Have a nice weekend

Edit: I forgot to mention that I really like your thinking :
return is_reading_str + ': ' + pct + '%'

Thats is NEAT !
Never thought of that myself.
Let's see if bekywc and others choke, or love it :-)[/QUOTE]

I get EXCEPTION for reading in the column though your first one worked.

Last edited by beckywc; 01-22-2011 at 07:58 AM.
beckywc is offline   Reply With Quote