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 01-07-2021, 12:03 PM   #1
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
problems with LibraryDatabase2

I want to update the find duplicates plugin to use get_proxy_metadata instead of get_metadata, as per chaley's advice here. There is no problem doing this when comparing duplicates in one library. However problems arise when trying to find cross library duplicates. The original code by kiwidude initializes the target library as follows:


Code:
from calibre.library.database2 import LibraryDatabase2
self.target_db = LibraryDatabase2(self.library_path, read_only=True, is_second_db=True)
problem with the above approach is that I cannot use new_api methods like get_proxy_metadata. I get the following error:

Code:
AttributeError: 'LibraryDatabase2' object has no attribute 'new_api'
My alternative approach would be initializing the target_db as follows:

Code:
from calibre.library import db as DB
self.target_db = DB(self.library_path, read_only=True)
Is there any reason to avoid using the second approach? I am having doubts because this seems like the obvious choice, so maybe kiwidude deliberately avoided that for a reason not obvious to me.
capink is offline   Reply With Quote
Old 01-07-2021, 12:41 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: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
LibraryDatabase2 is obsolete. It works with the new database introduced in V4 because Kovid supplied a "legacy" replacement.

For all new code it is best to use something like this:
Code:
def init_cache(library_path):
   from calibre.db.backend import DB
   from calibre.db.cache import Cache
   backend = DB(library_path)  # other named parameters as needed
   cache = Cache(backend)
   cache.init()
   return cache
Use the methods in Cache.

For older code that uses methods in LibraryDatabase2, use something like this:
Code:
from calibre.db.legacy import LibraryDatabase
db = LibraryDatabase(library_path) # other named parameters as needed
In this case the methods in "Cache" are available via db.new_api.

I think the new_api attribute might be available in LibraryDatabase2 through some sort of monkeypatching, but I am not sure of that.

Last edited by chaley; 01-07-2021 at 12:58 PM. Reason: Changed parameter of LibraryDatabase
chaley is offline   Reply With Quote
Old 01-07-2021, 12:42 PM   #3
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: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
Is there any reason to avoid using the second approach? I am having doubts because this seems like the obvious choice, so maybe kiwidude deliberately avoided that for a reason not obvious to me.
This didn't exist when Kiwidude wrote the plugin.
chaley is offline   Reply With Quote
Old 01-07-2021, 12:52 PM   #4
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Thank you for the reply. What does the extra Cache(backend) do exactly? What would be the difference if I skipped it?
capink is offline   Reply With Quote
Old 01-07-2021, 12:56 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: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
Thank you for the reply. What does the extra Cache(backend) do exactly? What would be the difference if I skipped it?
Cache contains the API you use, for example get_proxy_metadata. You very seldom use anything in Backend.
chaley is offline   Reply With Quote
Old 01-07-2021, 01:02 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: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
Code:
from calibre.library import db as DB
self.target_db = DB(self.library_path, read_only=True)
BTW: this is the same as my second example.
chaley is offline   Reply With Quote
Old 01-07-2021, 01:20 PM   #7
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
OK, got it. Thanks.
capink is offline   Reply With Quote
Old 01-07-2021, 09:50 PM   #8
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: 45,349
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
The canonical way to do this is:

Code:
from calibre.library import db
db = db('path/to/library/folder').new_api
kovidgoyal is offline   Reply With Quote
Old 01-08-2021, 09:21 AM   #9
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Thanks, Kovid. Will do it that way.
  • What is the earliest calibre version that supports this?
  • Is there any use for the older interface? the one you get from gui.current_db, or should I always use the new_api? I want to replace all db references with the new_api if possible. If not possible I will have to use a naming convention to differentiate them to avoid confusion.
  • when calibre passes references to db — e.g. library_changed(self, db) — which one does it reference?
capink is offline   Reply With Quote
Old 01-08-2021, 09:45 AM   #10
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: 45,349
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Lord knows, probably 2.0, possibly 1.0. No there is never a need to use the old interface in new code. It doesnt matter, you can alway use new_api, regardless of which interface it is. You can for instance do: db.new_api.new_api.new_api ad infinitum.
kovidgoyal is offline   Reply With Quote
Old 01-08-2021, 09:47 AM   #11
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Thanks.
capink is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kindle 3G Keyboard WiFI Potential Fix For On-Off, Charging Problems/Screen Problems Jim78 Amazon Kindle 0 09-25-2020 05:36 AM
Connection problems on Windows, no problems on Linux LiveUSB vM7iKtMlq9uWBBAK Kobo Reader 11 03-15-2019 10:17 AM
Glo Problems after updating to software ver. 3.2.0 (Problems found unrelated to update) Hiro-kun Kobo Reader 18 04-13-2014 06:38 PM
full page image problems with iBooks/cover problems in iTunes iain robinson ePub 1 06-28-2013 11:10 AM
[Android App] Nexus 7 Problems? Mac Problems? thetay24 Devices 4 08-31-2012 04:48 PM


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


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