1
0
mirror of https://gitlab.com/openlp/openlp.git synced 2024-10-02 12:57:39 +00:00

Use get_natural_key instead of _natural_sort_key

This commit is contained in:
Chris Hill 2016-04-03 11:57:39 +01:00
parent 8de2f013eb
commit 330a1758c8
3 changed files with 12 additions and 28 deletions

View File

@ -529,7 +529,7 @@ def get_natural_key(string):
key = [int(part) if part.isdigit() else get_locale_key(part) for part in key] key = [int(part) if part.isdigit() else get_locale_key(part) for part in key]
# Python 3 does not support comparison of different types anymore. So make sure, that we do not compare str # Python 3 does not support comparison of different types anymore. So make sure, that we do not compare str
# and int. # and int.
if string[0].isdigit(): if string and string[0].isdigit():
return [b''] + key return [b''] + key
return key return key

View File

@ -32,6 +32,7 @@ from openlp.core.common import Registry, AppLocation, Settings, check_directory_
from openlp.core.lib import MediaManagerItem, ItemCapabilities, PluginStatus, ServiceItemContext, \ from openlp.core.lib import MediaManagerItem, ItemCapabilities, PluginStatus, ServiceItemContext, \
check_item_selected, create_separated_list check_item_selected, create_separated_list
from openlp.core.lib.ui import create_widget_action from openlp.core.lib.ui import create_widget_action
from openlp.core.utils import get_natural_key
from openlp.plugins.songs.forms.editsongform import EditSongForm from openlp.plugins.songs.forms.editsongform import EditSongForm
from openlp.plugins.songs.forms.songmaintenanceform import SongMaintenanceForm from openlp.plugins.songs.forms.songmaintenanceform import SongMaintenanceForm
from openlp.plugins.songs.forms.songimportform import SongImportForm from openlp.plugins.songs.forms.songimportform import SongImportForm
@ -284,8 +285,10 @@ class SongMediaItem(MediaManagerItem):
""" """
log.debug('display results Author') log.debug('display results Author')
self.list_view.clear() self.list_view.clear()
search_results = sorted(search_results, key=lambda author: (get_natural_key(author.display_name)))
for author in search_results: for author in search_results:
for song in author.songs: songs = sorted(author.songs, key=lambda song: song.sort_key)
for song in songs:
# Do not display temporary songs # Do not display temporary songs
if song.temporary: if song.temporary:
continue continue
@ -303,8 +306,8 @@ class SongMediaItem(MediaManagerItem):
""" """
log.debug('display results Book') log.debug('display results Book')
self.list_view.clear() self.list_view.clear()
search_results = sorted(search_results, key=lambda songbook_entry: (songbook_entry.songbook.name, search_results = sorted(search_results, key=lambda songbook_entry: (get_natural_key(songbook_entry.songbook.name),
self._natural_sort_key(songbook_entry.entry))) get_natural_key(songbook_entry.entry)))
for songbook_entry in search_results: for songbook_entry in search_results:
if songbook_entry.song.temporary: if songbook_entry.song.temporary:
continue continue
@ -322,7 +325,7 @@ class SongMediaItem(MediaManagerItem):
""" """
log.debug('display results Topic') log.debug('display results Topic')
self.list_view.clear() self.list_view.clear()
search_results = sorted(search_results, key=lambda topic: self._natural_sort_key(topic.name)) search_results = sorted(search_results, key=lambda topic: get_natural_key(topic.name))
for topic in search_results: for topic in search_results:
songs = sorted(topic.songs, key=lambda song: song.sort_key) songs = sorted(topic.songs, key=lambda song: song.sort_key)
for song in songs: for song in songs:
@ -343,6 +346,8 @@ class SongMediaItem(MediaManagerItem):
""" """
log.debug('display results Themes') log.debug('display results Themes')
self.list_view.clear() self.list_view.clear()
search_results = sorted(search_results, key=lambda song: (get_natural_key(song.theme_name),
song.sort_key))
for song in search_results: for song in search_results:
# Do not display temporary songs # Do not display temporary songs
if song.temporary: if song.temporary:
@ -361,8 +366,8 @@ class SongMediaItem(MediaManagerItem):
""" """
log.debug('display results CCLI number') log.debug('display results CCLI number')
self.list_view.clear() self.list_view.clear()
songs = sorted(search_results, key=lambda song: self._natural_sort_key(song.ccli_number)) search_results = sorted(search_results, key=lambda song: get_natural_key(song.ccli_number))
for song in songs: for song in search_results:
# Do not display temporary songs # Do not display temporary songs
if song.temporary: if song.temporary:
continue continue
@ -688,14 +693,6 @@ class SongMediaItem(MediaManagerItem):
# List must be empty at the end # List must be empty at the end
return not author_list return not author_list
def _natural_sort_key(self, s):
"""
Return a tuple by which s is sorted.
:param s: A string value from the list we want to sort.
"""
return [int(text) if text.isdecimal() else text.lower()
for text in re.split('(\d+)', s)]
def search(self, string, show_error): def search(self, string, show_error):
""" """
Search for some songs Search for some songs

View File

@ -448,19 +448,6 @@ class TestMediaItem(TestCase, TestMixin):
# THEN: They should not match # THEN: They should not match
self.assertFalse(result, "Authors should not match") self.assertFalse(result, "Authors should not match")
def natural_sort_key_test(self):
"""
Test the _natural_sort_key function
"""
# GIVEN: A string to be converted into a sort key
string_sort_key = 'A1B12C'
# WHEN: We attempt to create a sort key
sort_key_result = self.media_item._natural_sort_key(string_sort_key)
# THEN: We should get back a tuple split on integers
self.assertEqual(sort_key_result, ['a', 1, 'b', 12, 'c'])
def build_remote_search_test(self): def build_remote_search_test(self):
""" """
Test results for the remote search api Test results for the remote search api