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 01-07-2018, 10:08 AM   #1
annoywife
Member
annoywife began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jan 2015
Device: none
Access "Categories with hierarchical items"

Under calibre-debug, what is the right way to access the group of fields that I have defined in the GUI under "Look & Feel" --> "Tag Browser -->" "Categories with hierarchical items"? I have tried gprefs and similar fields in various modules, but I don't appear to be hitting it. Thanks!
annoywife is offline   Reply With Quote
Old 01-07-2018, 10:19 AM   #2
chaley
CC Android & calibre dev
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: 8,744
Karma: 2655880
Join Date: Jan 2010
Location: France
Device: Many android devices
This one is stored per library, along with a lot of others. You can see which by looking at the "preferences" table in metadata.db. The preference you are asking about is
Code:
self.db.prefs.get('categories_using_hierarchy', [])
This works with both new and old API db objects
chaley is offline   Reply With Quote
Old 01-07-2018, 11:34 AM   #3
annoywife
Member
annoywife began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jan 2015
Device: none
Thanks for the prompt reply. I am self-taught re Python/sqlite/etc., which means I am great in some areas and woefully deficient in others - which explains why I ask the below:

I read your response to mean accessing attributes of an object "db", but how should I go about accessing/creating this object? I know from the api documentation how to establish a cache object (i.e. from calibre.library import db; db=db('Path to calibre library folder').new_api), but am gathering that this is not what you mean since this db object has no attribute "prefs".

Thanks for your patience here.
annoywife is offline   Reply With Quote
Old 01-07-2018, 12:03 PM   #4
annoywife
Member
annoywife began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jan 2015
Device: none
I've now found a method that produces the result I am looking for:

from calibre.library import database2
db = database2.LibraryDatabase2('Path to calibre library folder')
print(db.prefs.get('categories_using_hierarchy'))

My revised question: is this the best/most efficient way to get this info? I don't have a good feel for how this object is different than the cache object "from calibre.library import db; db=db('Path to calibre library folder').new_api", so would be interested in your thoughts here as well. Thanks for you time once again.
annoywife is offline   Reply With Quote
Old 01-07-2018, 12:58 PM   #5
chaley
CC Android & calibre dev
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: 8,744
Karma: 2655880
Join Date: Jan 2010
Location: France
Device: Many android devices
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
Old 01-07-2018, 01:42 PM   #6
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: 33,393
Karma: 10205094
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
The db Cache objects API is documented in the manual, at https://manual.calibre-ebook.com/db_api.html
kovidgoyal is offline   Reply With Quote
Old 01-07-2018, 03:38 PM   #7
annoywife
Member
annoywife began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jan 2015
Device: none
Very helpful! Understand your point on always using "new_api". I saw the pref method here before, but until today didn't know the specific preference name I should enter to get what I wanted (i.e. 'categories_using_hierarchy'). Appreciate the hint as well - makes sense.

At the risk of overstaying my welcome in this thread, one final clarification: what's the right way to get a list of all possible preference names that I could pass to "db.pref()" such that, in the future, I could determine the calibre name for the specific preference I want? The initial answer above mentions looking at a preferences table - is this something that I would/should do using my new_api db object somehow?
annoywife is offline   Reply With Quote
Old 01-07-2018, 04:01 PM   #8
chaley
CC Android & calibre dev
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: 8,744
Karma: 2655880
Join Date: Jan 2010
Location: France
Device: Many android devices
Quote:
Originally Posted by annoywife View Post
At the risk of overstaying my welcome in this thread, one final clarification: what's the right way to get a list of all possible preference names that I could pass to "db.pref()" such that, in the future, I could determine the calibre name for the specific preference I want? The initial answer above mentions looking at a preferences table - is this something that I would/should do using my new_api db object somehow?
I think you are going down a rat hole with this. There is no way to get a list of "all possible preference names" because the existence of a preference in calibre does not mean that the preference has a value -- is entered into the prefs dict. Looking at the defined preferences only tells you which of them has been set, not which of them could be set. In addition, simply knowing the name does not tell you what it actually means. I think you would be better served by determining if a preference is of interest by examining the GUI and the source code related to what you are interested in.

That said, this snippet will give you the names of all the library-specific preferences. Personally I think it is easier to look at the db using some db explorer (I use SQLiteSpy) because that lets you explore the values of the preferences in addition to the names. And yes, this snippet uses the old API.
Code:
from calibre.db.legacy import LibraryDatabase
db = LibraryDatabase(sys.argv[1])
all_prefs = db.prefs.iterkeys()
for t in all_prefs:
	print t
chaley is offline   Reply With Quote
Old 01-07-2018, 04:22 PM   #9
annoywife
Member
annoywife began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Jan 2015
Device: none
Got it! I didn't get that when you initially mentioned looking at the preferences table, you meant using a tool outside of the Calibre api. Understand your point - I agree with looking at source code as the first step. Also appreciate you providing the old API code snippet.

I looked at metadata.db with Sqliteman and found the preferences table just as you mentioned, so I see how I could preference names next time if/as needed. Thanks very much for your helpful replies!
annoywife 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
All my documents from "My Items" list on Kindle Touch disappeared kinkle Kindle Developer's Corner 33 03-02-2016 02:42 PM
Kindle Paperwhite displaying "My Items (0)"; Ideas? CuriousJay Amazon Kindle 15 01-15-2014 03:58 AM
Recipe for Safari "Reading List" unread items anoved Recipes 6 03-19-2013 01:21 PM
Select multiple items in "Check library results" window Pepin33 Library Management 2 08-01-2012 11:10 AM
Kindle for PC "Failed to sync and check for new items" curtw Amazon Kindle 4 02-16-2011 10:20 PM


All times are GMT -4. The time now is 10:14 AM.


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