Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 02-18-2011, 06:34 PM   #1
kiwidude
calibre/Sigil 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,230
Karma: 1345754
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite 3G, iPad 3, iPad Air
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, 06:47 PM   #2
chaley
"chaley", not "charley"
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: 5,922
Karma: 1673776
Join Date: Jan 2010
Location: France
Device: Many android devices
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, 06:51 PM   #3
kiwidude
calibre/Sigil 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,230
Karma: 1345754
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite 3G, iPad 3, iPad Air
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, 06: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: 26,450
Karma: 5383257
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, 06:55 PM   #5
chaley
"chaley", not "charley"
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: 5,922
Karma: 1673776
Join Date: Jan 2010
Location: France
Device: Many android devices
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, 06:58 PM   #6
chaley
"chaley", not "charley"
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: 5,922
Karma: 1673776
Join Date: Jan 2010
Location: France
Device: Many android devices
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, 07: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: 26,450
Karma: 5383257
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Done.
kovidgoyal is offline   Reply With Quote
Old 02-18-2011, 07:05 PM   #8
kiwidude
calibre/Sigil 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,230
Karma: 1345754
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite 3G, iPad 3, iPad Air
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, 07:10 PM   #9
chaley
"chaley", not "charley"
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: 5,922
Karma: 1673776
Join Date: Jan 2010
Location: France
Device: Many android devices
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, 07:21 PM   #10
kiwidude
calibre/Sigil 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,230
Karma: 1345754
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite 3G, iPad 3, iPad Air
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, 07: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: 26,450
Karma: 5383257
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, 07:52 PM   #12
kiwidude
calibre/Sigil 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,230
Karma: 1345754
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite 3G, iPad 3, iPad Air
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

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

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


All times are GMT -4. The time now is 06:17 PM.


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