Browse Source

Simplifying normalization of strings used to sort songs

pull/47/head
Louis 10 years ago
parent
commit
e87c6d4d3f
  1. 18
      songbook_core/content/sorted.py
  2. 2
      songbook_core/index.py
  3. 18
      songbook_core/songs.py

18
songbook_core/content/sorted.py

@ -7,18 +7,28 @@ from songbook_core.content.song import OnlySongsError, process_songs
DEFAULT_SORT = ['by', 'album', '@title'] 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 key_generator(sort):
def ordered_song_keys(song): def ordered_song_keys(song):
songkey = [] songkey = []
for key in sort: for key in sort:
if key == "@title": if key == "@title":
songkey.append(song.normalized_titles) field = song.unprefixed_titles
elif key == "@path": elif key == "@path":
songkey.append(locale.strxfrm(song.path)) field = song.path
elif key == "by": elif key == "by":
songkey.append(song.normalized_authors) field = song.authors
else: else:
songkey.append(locale.strxfrm(song.args.get(key, ""))) field = song.args.get(key, "")
songkey.append(normalize_field(field))
return songkey return songkey
return ordered_song_keys return ordered_song_keys

2
songbook_core/index.py

@ -30,7 +30,7 @@ def sortkey(value):
don't forget to call locale.setlocale(locale.LC_ALL, '')). It also handles don't forget to call locale.setlocale(locale.LC_ALL, '')). It also handles
the sort with latex escape sequences. 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): def process_sxd(filename):

18
songbook_core/songs.py

@ -18,12 +18,10 @@ class Song(object):
# Data extraction from the song with plastex # Data extraction from the song with plastex
data = parsetex(filename) data = parsetex(filename)
self.titles = data['titles'] self.titles = data['titles']
self.normalized_titles = [ self.unprefixed_titles = [
locale.strxfrm( unprefixed_title(
unprefixed_title( unidecode(unicode(title, "utf-8")),
unidecode(unicode(title, "utf-8")), config['titleprefixwords']
config['titleprefixwords']
)
) )
for title for title
in self.titles in self.titles
@ -32,13 +30,9 @@ class Song(object):
self.path = filename self.path = filename
self.languages = data['languages'] self.languages = data['languages']
if "by" in self.args.keys(): if "by" in self.args.keys():
self.normalized_authors = [ self.authors = processauthors(self.args["by"], **config["authwords"])
locale.strxfrm(author)
for author
in processauthors(self.args["by"], **config["authwords"])
]
else: else:
self.normalized_authors = [] self.authors = []
def __repr__(self): def __repr__(self):
return repr((self.titles, self.args, self.path)) return repr((self.titles, self.args, self.path))

Loading…
Cancel
Save