Saved search categories

This commit is contained in:
Kovid Goyal 2013-01-25 13:52:35 +05:30
parent 6b4aad42d0
commit dc0f280cae
3 changed files with 58 additions and 46 deletions

View File

@ -15,6 +15,7 @@ from future_builtins import map
from calibre.library.field_metadata import TagsIcons from calibre.library.field_metadata import TagsIcons
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.utils.search_query_parser import saved_searches
CATEGORY_SORTS = ('name', 'popularity', 'rating') # This has to be a tuple not a set CATEGORY_SORTS = ('name', 'popularity', 'rating') # This has to be a tuple not a set
@ -158,55 +159,67 @@ def get_categories(dbcache, sort='name', book_ids=None, icon_map=None):
# User categories # User categories
user_categories = clean_user_categories(dbcache).copy() user_categories = clean_user_categories(dbcache).copy()
# We want to use same node in the user category as in the source if user_categories:
# category. To do that, we need to find the original Tag node. There is # We want to use same node in the user category as in the source
# a time/space tradeoff here. By converting the tags into a map, we can # category. To do that, we need to find the original Tag node. There is
# do the verification in the category loop much faster, at the cost of # a time/space tradeoff here. By converting the tags into a map, we can
# temporarily duplicating the categories lists. # do the verification in the category loop much faster, at the cost of
taglist = {} # temporarily duplicating the categories lists.
for c, items in categories.iteritems(): taglist = {}
taglist[c] = dict(map(lambda t:(icu_lower(t.name), t), items)) for c, items in categories.iteritems():
taglist[c] = dict(map(lambda t:(icu_lower(t.name), t), items))
muc = dbcache.pref('grouped_search_make_user_categories', []) muc = dbcache.pref('grouped_search_make_user_categories', [])
gst = dbcache.pref('grouped_search_terms', {}) gst = dbcache.pref('grouped_search_terms', {})
for c in gst: for c in gst:
if c not in muc: if c not in muc:
continue continue
user_categories[c] = [] user_categories[c] = []
for sc in gst[c]: for sc in gst[c]:
if sc in categories.keys(): if sc in categories.keys():
for t in categories[sc]: for t in categories[sc]:
user_categories[c].append([t.name, sc, 0]) user_categories[c].append([t.name, sc, 0])
gst_icon = icon_map['gst'] if icon_map else None gst_icon = icon_map['gst'] if icon_map else None
for user_cat in sorted(user_categories.iterkeys(), key=sort_key): for user_cat in sorted(user_categories.iterkeys(), key=sort_key):
items = [] items = []
names_seen = {} names_seen = {}
for name, label, ign in user_categories[user_cat]: for name, label, ign in user_categories[user_cat]:
n = icu_lower(name) n = icu_lower(name)
if label in taglist and n in taglist[label]: if label in taglist and n in taglist[label]:
if user_cat in gst: if user_cat in gst:
# for gst items, make copy and consolidate the tags by name. # for gst items, make copy and consolidate the tags by name.
if n in names_seen: if n in names_seen:
t = names_seen[n] t = names_seen[n]
t.id_set |= taglist[label][n].id_set t.id_set |= taglist[label][n].id_set
t.count += taglist[label][n].count t.count += taglist[label][n].count
t.tooltip = t.tooltip.replace(')', ', ' + label + ')') t.tooltip = t.tooltip.replace(')', ', ' + label + ')')
else:
t = copy.copy(taglist[label][n])
t.icon = gst_icon
names_seen[t.name] = t
items.append(t)
else: else:
t = copy.copy(taglist[label][n]) items.append(taglist[label][n])
t.icon = gst_icon # else: do nothing, to not include nodes w zero counts
names_seen[t.name] = t cat_name = '@' + user_cat # add the '@' to avoid name collision
items.append(t) # Not a problem if we accumulate entries in the icon map
else: if icon_map is not None:
items.append(taglist[label][n]) icon_map[cat_name] = icon_map['user:']
# else: do nothing, to not include nodes w zero counts categories[cat_name] = sort_categories(items, sort)
cat_name = '@' + user_cat # add the '@' to avoid name collision
# Not a problem if we accumulate entries in the icon map
if icon_map is not None:
icon_map[cat_name] = icon_map['user:']
categories[cat_name] = sort_categories(items, sort)
# TODO: saved searches #### Finally, the saved searches category ####
items = []
icon = None
if icon_map and 'search' in icon_map:
icon = icon_map['search']
ss = saved_searches()
for srch in ss.names():
items.append(Tag(srch, tooltip=ss.lookup(srch),
sort=srch, icon=icon, category='search',
is_editable=False))
if len(items):
categories['search'] = items
return categories return categories

View File

@ -17,7 +17,6 @@ from calibre.utils.icu import primary_find
from calibre.utils.localization import lang_map, canonicalize_lang from calibre.utils.localization import lang_map, canonicalize_lang
from calibre.utils.search_query_parser import SearchQueryParser, ParseException from calibre.utils.search_query_parser import SearchQueryParser, ParseException
# TODO: Thread safety of saved searches
CONTAINS_MATCH = 0 CONTAINS_MATCH = 0
EQUALS_MATCH = 1 EQUALS_MATCH = 1
REGEXP_MATCH = 2 REGEXP_MATCH = 2

Binary file not shown.