1# Copyright (c) 2014-2021 Cedric Bellegarde <cedric.bellegarde@adishatz.org> 2# This program is free software: you can redistribute it and/or modify 3# it under the terms of the GNU General Public License as published by 4# the Free Software Foundation, either version 3 of the License, or 5# (at your option) any later version. 6# This program is distributed in the hope that it will be useful, 7# but WITHOUT ANY WARRANTY; without even the implied warranty of 8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9# GNU General Public License for more details. 10# You should have received a copy of the GNU General Public License 11# along with this program. If not, see <http://www.gnu.org/licenses/>. 12 13from locale import getlocale, strcoll 14from importlib import import_module 15 16# Ugly magic to dynamically adapt to the current locale... 17try: 18 # Try loading 'index_of' function of the current locale 19 _module = import_module('lollypop.locales.%s' % getlocale()[0].lower()) 20 index_of = _module.index_of 21except: 22 # In case the locale doesn't need special care, fall back to a naive 23 # implementation 24 def index_of(string): 25 """ 26 Get index of a string in a locale-aware manner. 27 This is the fallback, which simply returns the first character. 28 @param string as str 29 @return str 30 """ 31 if string: 32 return string[0] 33 else: 34 return "" 35 36 37class LocalizedCollation(object): 38 """ 39 COLLATE LOCALIZED missing from default sqlite installation 40 Android only 41 """ 42 43 def __init__(self): 44 pass 45 46 def __call__(self, v1, v2): 47 i1 = index_of(v1).upper() 48 i2 = index_of(v2).upper() 49 if strcoll(i1, i2) < 0: 50 return -1 51 elif strcoll(i1, i2) == 0: 52 return strcoll(v1, v2) 53 else: 54 return 1 55