View Single Post
Old 01-07-2018, 12:58 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,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Any new calibre code should use the "new_api", so you should use the first method you described to get a db object.
Code:
from calibre.library import db; 
db=db('Path to calibre library folder').new_api
Without the .new_api you get an instance of calibre.db.legacy.LibraryDatabase. Occasionally legacy methods are easier to use, but in my experience not very often.

A "new_api" object is an instance of calibre.db.cache.Cache. Look there for "public" methods, properties, and attributes. For example db.prefs is a method (line 609):
Code:
    @read_api
    def pref(self, name, default=None):
        ' Return the value for the specified preference or the value specified as ``default`` if the preference is not set. '
        return self.backend.prefs.get(name, default)
Finally, and offered as a hint, in most cases you should specify a default when using .get(). In calibre, preferences can be and often are None. For example,
Code:
db.prefs.get('categories_using_hierarchy')
will return None or a list, meaning you must test it. If you instead use
Code:
db.prefs.get('categories_using_hierarchy', [])
then you will always get a list, simplifying subsequent processing.
chaley is offline   Reply With Quote