Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 02-18-2011, 05:34 PM   #1
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
Can I hold a reference to .model() and .db in a plugin?

To access the data model and database/caches from a plugin, it appears you use self.gui.library_view.model() and self.gui.library_view.model().db respectively.

For coding purposes I usually want to hold a shortcut reference to that such as: self.db = self.gui.library_view.model().db to make the code more readable when doing a bunch of db calls.

Would it be valid to set this reference just once for your InterfaceAction class in initialization_complete()? Currently I re-set it in each connected event in response to a user action. The reason I do this currently is sheer ignorance from not knowing if other code in Calibre at some point would change the object reference returned from either .model() or .model().db.
kiwidude is offline   Reply With Quote
Old 02-18-2011, 05:47 PM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,738
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by kiwidude View Post
To access the data model and database/caches from a plugin, it appears you use self.gui.library_view.model() and self.gui.library_view.model().db respectively.

For coding purposes I usually want to hold a shortcut reference to that such as: self.db = self.gui.library_view.model().db to make the code more readable when doing a bunch of db calls.

Would it be valid to set this reference just once for your InterfaceAction class in initialization_complete()? Currently I re-set it in each connected event in response to a user action. The reason I do this currently is sheer ignorance from not knowing if other code in Calibre at some point would change the object reference returned from either .model() or .model().db.
No, you can't cache it. The value of db changes if libraries change. Holding the old one could lead to disaster, writing the former library while (potentially) using data from the new library.
chaley is offline   Reply With Quote
Old 02-18-2011, 05:51 PM   #3
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
Quote:
Originally Posted by chaley View Post
No. The value of db changes if libraries change. Holding the old one could lead to disaster, writing the former library while (potentially) using data from the new library.
Phew, cool. That makes sense now. I just looked at the repeated self.db = calls in all those action methods and wondered if I was being too conservative.

However that does raise another issue I hadn't considered with my id caches I mentioned in the notification thread. How do I know that the library has been switched in my plugin so I need to invalidate my caches?
kiwidude is offline   Reply With Quote
Old 02-18-2011, 05:55 PM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,850
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Implement the library_moved method in your plugin
kovidgoyal is offline   Reply With Quote
Old 02-18-2011, 05:55 PM   #5
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,738
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by kiwidude View Post
However that does raise another issue I hadn't considered with my id caches I mentioned in the notification thread. How do I know that the library has been switched in my plugin so I need to invalidate my caches?
When the library is changed, the 'library_changed' method in actions is called. Assuming that your plugin defines an action, you supply that method.
chaley is offline   Reply With Quote
Old 02-18-2011, 05:58 PM   #6
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,738
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by kovidgoyal View Post
Implement the library_moved method in your plugin
@kovid,

The comment says that the method is named 'library_moved', but the code contains the method 'library_changed'. I suspect that the comment should be changed.
chaley is offline   Reply With Quote
Old 02-18-2011, 06:00 PM   #7
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,850
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Done.
kovidgoyal is offline   Reply With Quote
Old 02-18-2011, 06:05 PM   #8
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
Durn it, I remember seeing that function months ago in the API and wondering what the use case was for it.

I guess that "could" be used to set a self.db in response to my original question on this post.

Thanks guys.
kiwidude is offline   Reply With Quote
Old 02-18-2011, 06:10 PM   #9
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,738
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by kiwidude View Post
I guess that "could" be used to set a self.db in response to my original question on this post.
Nope. Check_library will change the value of db (closing and re-opening) without calling change_library.

You can cache the value of self.gui.library_view.model(). I can't think of any instance where the library_view is replaced.
chaley is offline   Reply With Quote
Old 02-18-2011, 06:21 PM   #10
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
Quote:
Originally Posted by chaley View Post
Nope. Check_library will change the value of db (closing and re-opening) without calling change_library.

You can cache the value of self.gui.library_view.model(). I can't think of any instance where the library_view is replaced.
Hehe, ok that is good to know. I was trying to hold a .db reference within my cache class but in hindsight that is clearly a very bad idea. Good to know these things *before* releasing a plugin and trashing databases
kiwidude is offline   Reply With Quote
Old 02-18-2011, 06:21 PM   #11
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,850
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Quote:
Originally Posted by chaley View Post
Nope. Check_library will change the value of db (closing and re-opening) without calling change_library.
check_library was refactored a while ago to call library_changed after it completes (see actions/choose_library.py).

However, I would encourage you to not hold references to the db unnecessarily. Calling self.gui.library_view.model().db is fast enough that as long as you dont have it in an inner loop you wont see any practical performance implications.
kovidgoyal is offline   Reply With Quote
Old 02-18-2011, 06:52 PM   #12
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
Quote:
Originally Posted by kovidgoyal View Post
check_library was refactored a while ago to call library_changed after it completes (see actions/choose_library.py).

However, I would encourage you to not hold references to the db unnecessarily. Calling self.gui.library_view.model().db is fast enough that as long as you dont have it in an inner loop you wont see any practical performance implications.
It wasn't so much for performance reasons as just passing to that class the least scope it needed to know about - an object design choice. Now I'll just pass self.gui from the action class instead and forget about trying to be too clever
kiwidude is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New Plugin Type Idea: Library Plugin cgranade Plugins 3 09-15-2010 12:11 PM
Unutterably Silly Hold your breath and hold down 'x'. (Dumb game.) frabjous Lounge 82 11-29-2009 04:17 AM
Reference Books... Do you or don't you? Stitchawl Lounge 12 10-31-2009 03:31 PM
PDF Reference joblack PDF 5 06-12-2009 08:39 AM
E-book for Reference QFT Which one should I buy? 8 10-17-2008 10:56 PM


All times are GMT -4. The time now is 04:15 AM.


MobileRead.com is a privately owned, operated and funded community.