From 1a2950c2b294eee0be3415e5c2da6be4c7236585 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 9 Mar 2011 08:20:41 +0000 Subject: [PATCH 1/3] Clean up true/false matching --- src/calibre/gui2/library/models.py | 17 +------- src/calibre/library/caches.py | 62 +++++++++++++----------------- 2 files changed, 29 insertions(+), 50 deletions(-) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 33d12e8ab9..0658c0604a 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -22,7 +22,7 @@ from calibre.utils.icu import sort_key, strcmp as icu_strcmp from calibre.ebooks.metadata.meta import set_metadata as _set_metadata from calibre.utils.search_query_parser import SearchQueryParser from calibre.library.caches import _match, CONTAINS_MATCH, EQUALS_MATCH, \ - REGEXP_MATCH, MetadataBackup + REGEXP_MATCH, MetadataBackup, force_to_bool from calibre import strftime, isbytestring, prepare_string_for_xml from calibre.constants import filesystem_encoding, DEBUG from calibre.gui2.library import DEFAULT_SORT @@ -624,20 +624,7 @@ class BooksModel(QAbstractTableModel): # {{{ return None # displayed using a decorator def bool_type_decorator(r, idx=-1, bool_cols_are_tristate=True): - val = self.db.data[r][idx] - if isinstance(val, (str, unicode)): - try: - val = icu_lower(val) - if not val: - val = None - elif val in [_('yes'), _('checked'), 'true']: - val = True - elif val in [_('no'), _('unchecked'), 'false']: - val = False - else: - val = bool(int(val)) - except: - val = None + val = force_to_bool(self.db.data[r][idx]) if not bool_cols_are_tristate: if val is None or not val: return self.bool_no_icon diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 21a2622f33..7fa26bd9e9 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -144,6 +144,22 @@ def _match(query, value, matchkind): pass return False +def force_to_bool(self, val): + if isinstance(val, (str, unicode)): + try: + val = icu_lower(val) + if not val: + val = None + elif val in [_('yes'), _('checked'), 'true']: + val = True + elif val in [_('no'), _('unchecked'), 'false']: + val = False + else: + val = bool(int(val)) + except: + val = None + return val + class CacheRow(list): # {{{ def __init__(self, db, composites, val): @@ -532,37 +548,23 @@ class ResultCache(SearchQueryParser): # {{{ if item is None: continue - val = item[loc] - if isinstance(val, (str, unicode)): - try: - val = icu_lower(val) - if not val: - val = None - elif val in [_('yes'), _('checked'), 'true']: - val = True - elif val in [_('no'), _('unchecked'), 'false']: - val = False - else: - val = bool(int(val)) - except: - val = None - + val = force_to_bool(item[loc]) if not bools_are_tristate: if val is None or not val: # item is None or set to false - if query in [_('no'), _('unchecked'), 'false']: + if query in [_('no'), _('unchecked'), 'no', 'false']: matches.add(item[0]) else: # item is explicitly set to true - if query in [_('yes'), _('checked'), 'true']: + if query in [_('yes'), _('checked'), 'yes', 'true']: matches.add(item[0]) else: if val is None: - if query in [_('empty'), _('blank'), 'false']: + if query in [_('empty'), _('blank'), 'empty', 'false']: matches.add(item[0]) elif not val: # is not None and false - if query in [_('no'), _('unchecked'), 'true']: + if query in [_('no'), _('unchecked'), 'no', 'true']: matches.add(item[0]) else: # item is not None and true - if query in [_('yes'), _('checked'), 'true']: + if query in [_('yes'), _('checked'), 'yes', 'true']: matches.add(item[0]) return matches @@ -695,13 +697,14 @@ class ResultCache(SearchQueryParser): # {{{ if item is None: continue if not item[loc]: - if q == 'false': + if q == 'false' and matchkind == CONTAINS_MATCH: matches.add(item[0]) continue # item is empty. No possible matches below - if q == 'false': # Field has something in it, so a false query does not match + if q == 'false'and matchkind == CONTAINS_MATCH: + # Field has something in it, so a false query does not match continue - if q == 'true': + if q == 'true' and matchkind == CONTAINS_MATCH: if isinstance(item[loc], basestring): if item[loc].strip() == '': continue @@ -989,18 +992,7 @@ class SortKeyGenerator(object): val = 0.0 dt = 'float' elif sb == 'bool': - try: - v = icu_lower(val) - if not val: - val = None - elif v in [_('yes'), _('checked'), 'true']: - val = True - elif v in [_('no'), _('unchecked'), 'false']: - val = False - else: - val = bool(int(val)) - except: - val = None + val = force_to_bool(val) dt = 'bool' if dt == 'datetime': From 8f931ddb2f2844a5d6dd835bcaa30e184d922383 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 9 Mar 2011 08:23:15 +0000 Subject: [PATCH 2/3] ... --- src/calibre/library/caches.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 7fa26bd9e9..c77b03c112 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -144,7 +144,7 @@ def _match(query, value, matchkind): pass return False -def force_to_bool(self, val): +def force_to_bool(val): if isinstance(val, (str, unicode)): try: val = icu_lower(val) From 6482914216c136df79ef750fc46d51afccf4ec48 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 9 Mar 2011 08:59:10 +0000 Subject: [PATCH 3/3] Added the '_' to the non-translated values to prevent possible clashes with translations --- src/calibre/library/caches.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index c77b03c112..97ddaeb51a 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -551,20 +551,20 @@ class ResultCache(SearchQueryParser): # {{{ val = force_to_bool(item[loc]) if not bools_are_tristate: if val is None or not val: # item is None or set to false - if query in [_('no'), _('unchecked'), 'no', 'false']: + if query in [_('no'), _('unchecked'), '_no', 'false']: matches.add(item[0]) else: # item is explicitly set to true - if query in [_('yes'), _('checked'), 'yes', 'true']: + if query in [_('yes'), _('checked'), '_yes', 'true']: matches.add(item[0]) else: if val is None: - if query in [_('empty'), _('blank'), 'empty', 'false']: + if query in [_('empty'), _('blank'), '_empty', 'false']: matches.add(item[0]) elif not val: # is not None and false - if query in [_('no'), _('unchecked'), 'no', 'true']: + if query in [_('no'), _('unchecked'), '_no', 'true']: matches.add(item[0]) else: # item is not None and true - if query in [_('yes'), _('checked'), 'yes', 'true']: + if query in [_('yes'), _('checked'), '_yes', 'true']: matches.add(item[0]) return matches