From 352ee20933bb0f3c5ea37187247ff466bd203755 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sat, 11 Jan 2014 08:30:52 +0100 Subject: [PATCH 1/2] Allow searching for tags in the device view. Works for books on the device that came from calibre or have matched a calibre book in the past. --- src/calibre/gui2/library/models.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 45fcd0ab36..16c7060d5b 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -1104,7 +1104,8 @@ class OnDeviceSearch(SearchQueryParser): # {{{ 'format', 'formats', 'title', - 'inlibrary' + 'inlibrary', + 'tags' ] def __init__(self, model): @@ -1142,7 +1143,8 @@ class OnDeviceSearch(SearchQueryParser): # {{{ 'author': lambda x: ' & '.join(getattr(x, 'authors')).lower(), 'collections':lambda x: ','.join(getattr(x, 'device_collections')).lower(), 'format':lambda x: os.path.splitext(x.path)[1].lower(), - 'inlibrary':lambda x : getattr(x, 'in_library') + 'inlibrary':lambda x : getattr(x, 'in_library'), + 'tags':lambda x : getattr(x, 'tags') } for x in ('author', 'format'): q[x+'s'] = q[x] @@ -1169,10 +1171,12 @@ class OnDeviceSearch(SearchQueryParser): # {{{ else: m = matchkind - if locvalue == 'collections': - vals = accessor(row).split(',') - else: - vals = [accessor(row)] + vals = accessor(row) + if not isinstance(vals, list): + if locvalue == 'collections': + vals = accessor(row).split(',') + else: + vals = [accessor(row)] if _match(query, vals, m, use_primary_find_in_search=upf): matches.add(index) break From 6001342b32c4f08ca940f27e2de938d89a151b97 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sat, 11 Jan 2014 09:23:25 +0100 Subject: [PATCH 2/2] Don't search tags when the expression is unprefixed. Some performance improvements and defensive code. --- src/calibre/gui2/library/models.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 16c7060d5b..ad3fc5d0b2 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -1136,7 +1136,7 @@ class OnDeviceSearch(SearchQueryParser): # {{{ if location not in self.USABLE_LOCATIONS: return set([]) matches = set([]) - all_locs = set(self.USABLE_LOCATIONS) - set(['all']) + all_locs = set(self.USABLE_LOCATIONS) - set(['all', 'tags']) locations = all_locs if location == 'all' else [location] q = { 'title' : lambda x : getattr(x, 'title').lower(), @@ -1144,7 +1144,7 @@ class OnDeviceSearch(SearchQueryParser): # {{{ 'collections':lambda x: ','.join(getattr(x, 'device_collections')).lower(), 'format':lambda x: os.path.splitext(x.path)[1].lower(), 'inlibrary':lambda x : getattr(x, 'in_library'), - 'tags':lambda x : getattr(x, 'tags') + 'tags':lambda x : getattr(x, 'tags', []) } for x in ('author', 'format'): q[x+'s'] = q[x] @@ -1172,11 +1172,10 @@ class OnDeviceSearch(SearchQueryParser): # {{{ m = matchkind vals = accessor(row) - if not isinstance(vals, list): - if locvalue == 'collections': - vals = accessor(row).split(',') - else: - vals = [accessor(row)] + if vals is None: + vals = '' + if isinstance(vals, basestring): + vals = vals.split(',') if locvalue == 'collections' else [vals] if _match(query, vals, m, use_primary_find_in_search=upf): matches.add(index) break