From f915daed36ff2b772148ece4bb4cb456da6456a4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 6 Jun 2016 16:24:20 +0530 Subject: [PATCH] =?UTF-8?q?Fix=20#509=20(Fix=20for=20composite=20column=20?= =?UTF-8?q?sorting=20problem=20=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/calibre/db/fields.py | 3 +-- src/calibre/db/utils.py | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/calibre/db/fields.py b/src/calibre/db/fields.py index 5117c4cbad..908f739213 100644 --- a/src/calibre/db/fields.py +++ b/src/calibre/db/fields.py @@ -8,14 +8,13 @@ __license__ = 'GPL v3' __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' -from locale import atof from threading import Lock from collections import defaultdict, Counter from functools import partial from calibre.db.tables import ONE_ONE, MANY_ONE, MANY_MANY, null from calibre.db.write import Writer -from calibre.db.utils import force_to_bool +from calibre.db.utils import force_to_bool, atof from calibre.ebooks.metadata import title_sort, author_to_author_sort from calibre.utils.config_base import tweaks from calibre.utils.icu import sort_key diff --git a/src/calibre/db/utils.py b/src/calibre/db/utils.py index f66136502c..9e24d9523e 100644 --- a/src/calibre/db/utils.py +++ b/src/calibre/db/utils.py @@ -7,12 +7,13 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' import os, errno, cPickle, sys, re +from locale import localeconv from collections import OrderedDict, namedtuple from future_builtins import map from threading import Lock from calibre import as_unicode, prints -from calibre.constants import cache_dir +from calibre.constants import cache_dir, get_windows_number_formats, iswindows def force_to_bool(val): if isinstance(val, (str, unicode)): @@ -356,5 +357,20 @@ class ThumbnailCache(object): if hasattr(self, 'total_size'): self._apply_size() - - +number_separators = None +def atof(string): + # Python 2.x does not handle unicode number separators correctly, so we + # have to implement our own + global number_separators + if number_separators is None: + if iswindows: + number_separators = get_windows_number_formats() + else: + lc = localeconv() + t, d = lc['thousands_sep'], lc['decimal_point'] + if isinstance(t, bytes): + t = t.decode('utf-8', 'ignore') or ',' + if isinstance(d, bytes): + d = d.decode('utf-8', 'ignore') or '.' + number_separators = t, d + return float(string.replace(number_separators[1], '.').replace(number_separators[0], ''))