mirror of https://github.com/patacrep/patacrep.git
Engine for LaTeX songbooks
http://www.patacrep.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.0 KiB
132 lines
4.0 KiB
11 years ago
|
#!/usr/bin/env python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
11 years ago
|
"""Song management."""
|
||
|
|
||
11 years ago
|
from unidecode import unidecode
|
||
11 years ago
|
import glob
|
||
11 years ago
|
import locale
|
||
|
import os.path
|
||
|
import re
|
||
|
|
||
11 years ago
|
from songbook_core.authors import processauthors
|
||
|
from songbook_core.plastex import parsetex
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
# pylint: disable=too-few-public-methods
|
||
|
class Song(object):
|
||
11 years ago
|
"""Song management"""
|
||
|
|
||
11 years ago
|
#: Ordre de tri
|
||
|
sort = []
|
||
|
#: Préfixes à ignorer pour le tri par titres
|
||
|
prefixes = []
|
||
|
#: Dictionnaire des options pour le traitement des auteurs
|
||
|
authwords = {"after": [], "ignore": [], "sep": []}
|
||
|
|
||
|
def __init__(self, path, languages, titles, args):
|
||
11 years ago
|
self.titles = titles
|
||
11 years ago
|
self.normalized_titles = [
|
||
|
locale.strxfrm(
|
||
|
unprefixed_title(
|
||
|
unidecode(unicode(title, "utf-8")),
|
||
|
self.prefixes
|
||
|
)
|
||
|
)
|
||
|
for title
|
||
|
in titles
|
||
|
]
|
||
11 years ago
|
self.args = args
|
||
|
self.path = path
|
||
11 years ago
|
self.languages = languages
|
||
|
if "by" in self.args.keys():
|
||
|
self.normalized_authors = [
|
||
|
locale.strxfrm(author)
|
||
|
for author
|
||
|
in processauthors(self.args["by"], **self.authwords)
|
||
|
]
|
||
|
else:
|
||
|
self.normalized_authors = []
|
||
|
|
||
|
def __repr__(self):
|
||
|
return repr((self.titles, self.args, self.path))
|
||
|
|
||
|
def __cmp__(self, other):
|
||
|
if not isinstance(other, Song):
|
||
|
return NotImplemented
|
||
|
for key in self.sort:
|
||
|
if key == "@title":
|
||
|
self_key = self.normalized_titles
|
||
|
other_key = other.normalized_titles
|
||
|
elif key == "@path":
|
||
|
self_key = locale.strxfrm(self.path)
|
||
|
other_key = locale.strxfrm(other.path)
|
||
|
elif key == "by":
|
||
|
self_key = self.normalized_authors
|
||
|
other_key = other.normalized_authors
|
||
|
else:
|
||
|
self_key = locale.strxfrm(self.args.get(key, ""))
|
||
|
other_key = locale.strxfrm(other.args.get(key, ""))
|
||
|
|
||
|
if self_key < other_key:
|
||
|
return -1
|
||
|
elif self_key > other_key:
|
||
|
return 1
|
||
|
return 0
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
def unprefixed_title(title, prefixes):
|
||
|
"""Remove the first prefix of the list in the beginning of title (if any).
|
||
|
"""
|
||
|
for prefix in prefixes:
|
||
|
match = re.compile(r"^(%s)\b\s*(.*)$" % prefix).match(title)
|
||
|
if match:
|
||
|
return match.group(2)
|
||
|
return title
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
class SongsList(object):
|
||
11 years ago
|
"""Manipulation et traitement de liste de chansons"""
|
||
|
|
||
|
def __init__(self, library, language):
|
||
11 years ago
|
self._songdir = os.path.join(library, 'songs')
|
||
11 years ago
|
self._language = language
|
||
|
|
||
|
# Liste triée des chansons
|
||
|
self.songs = []
|
||
|
|
||
|
def append(self, filename):
|
||
|
"""Ajout d'une chanson à la liste
|
||
|
|
||
|
Effets de bord : analyse syntaxique plus ou moins sommaire du fichier
|
||
|
pour en extraire et traiter certaines information (titre, langue,
|
||
|
album, etc.).
|
||
|
"""
|
||
|
# Exécution de PlasTeX
|
||
11 years ago
|
data = parsetex(filename)
|
||
11 years ago
|
|
||
11 years ago
|
song = Song(filename, data['languages'], data['titles'], data['args'])
|
||
11 years ago
|
low, high = 0, len(self.songs)
|
||
|
while low != high:
|
||
|
middle = (low + high) / 2
|
||
|
if song < self.songs[middle]:
|
||
|
high = middle
|
||
|
else:
|
||
|
low = middle + 1
|
||
|
self.songs.insert(low, song)
|
||
|
|
||
|
def append_list(self, filelist):
|
||
|
"""Ajoute une liste de chansons à la liste
|
||
|
|
||
11 years ago
|
L'argument est une liste de chaînes, représentant des noms de fichiers
|
||
|
sous la forme d'expressions régulières destinées à être analysées avec
|
||
|
le module glob.
|
||
11 years ago
|
"""
|
||
11 years ago
|
for regexp in filelist:
|
||
|
for filename in glob.iglob(os.path.join(self._songdir, regexp)):
|
||
|
self.append(filename)
|
||
11 years ago
|
|
||
|
def languages(self):
|
||
|
"""Renvoie la liste des langues utilisées par les chansons"""
|
||
|
return set().union(*[set(song.languages) for song in self.songs])
|