From e87c6d4d3fcd71c57230e02dd92e50a46dec7fa1 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 14 Jun 2014 20:09:39 +0200 Subject: [PATCH] Simplifying normalization of strings used to sort songs --- songbook_core/content/sorted.py | 18 ++++++++++++++---- songbook_core/index.py | 2 +- songbook_core/songs.py | 18 ++++++------------ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/songbook_core/content/sorted.py b/songbook_core/content/sorted.py index 6bd1c665..d4ae45df 100644 --- a/songbook_core/content/sorted.py +++ b/songbook_core/content/sorted.py @@ -7,18 +7,28 @@ from songbook_core.content.song import OnlySongsError, process_songs DEFAULT_SORT = ['by', 'album', '@title'] +def normalize_string(string): + return locale.strxfrm(string.lower().strip()) + +def normalize_field(field): + if isinstance(field, basestring): + return normalize_string(field) + elif isinstance(field, list): + return [normalize_string(string) for string in field] + def key_generator(sort): def ordered_song_keys(song): songkey = [] for key in sort: if key == "@title": - songkey.append(song.normalized_titles) + field = song.unprefixed_titles elif key == "@path": - songkey.append(locale.strxfrm(song.path)) + field = song.path elif key == "by": - songkey.append(song.normalized_authors) + field = song.authors else: - songkey.append(locale.strxfrm(song.args.get(key, ""))) + field = song.args.get(key, "") + songkey.append(normalize_field(field)) return songkey return ordered_song_keys diff --git a/songbook_core/index.py b/songbook_core/index.py index 734e4ec3..67c1d410 100755 --- a/songbook_core/index.py +++ b/songbook_core/index.py @@ -30,7 +30,7 @@ def sortkey(value): don't forget to call locale.setlocale(locale.LC_ALL, '')). It also handles the sort with latex escape sequences. """ - return locale.strxfrm(unidecode(simpleparse(value).replace(' ', 'A'))) + return locale.strxfrm(unidecode(simpleparse(value).replace(' ', 'A')).lower()) def process_sxd(filename): diff --git a/songbook_core/songs.py b/songbook_core/songs.py index a3909d1c..f959466a 100644 --- a/songbook_core/songs.py +++ b/songbook_core/songs.py @@ -18,12 +18,10 @@ class Song(object): # Data extraction from the song with plastex data = parsetex(filename) self.titles = data['titles'] - self.normalized_titles = [ - locale.strxfrm( - unprefixed_title( - unidecode(unicode(title, "utf-8")), - config['titleprefixwords'] - ) + self.unprefixed_titles = [ + unprefixed_title( + unidecode(unicode(title, "utf-8")), + config['titleprefixwords'] ) for title in self.titles @@ -32,13 +30,9 @@ class Song(object): self.path = filename self.languages = data['languages'] if "by" in self.args.keys(): - self.normalized_authors = [ - locale.strxfrm(author) - for author - in processauthors(self.args["by"], **config["authwords"]) - ] + self.authors = processauthors(self.args["by"], **config["authwords"]) else: - self.normalized_authors = [] + self.authors = [] def __repr__(self): return repr((self.titles, self.args, self.path))