Browse Source

Analyse syntaxique correcte des titres des chansons

remotes/origin/next
Louis 12 years ago
parent
commit
b4ef426736
  1. 12
      songbook.py
  2. 33
      utils/plastex.py
  3. 19
      utils/songs.py

12
songbook.py

@ -14,20 +14,19 @@ import platform
from utils import recursiveFind
from utils.plastex import parsetex
reTitle = re.compile('(?<=beginsong\\{)(.(?<!\\}]))+')
reArtist = re.compile('(?<=by=)(.(?<![,\\]\\}]))+')
reAlbum = re.compile('(?<=album=)(.(?<![,\\]\\}]))+')
class Song:
def __init__(self, title, artist, album, path, languages):
self.title = title
def __init__(self, titles, artist, album, path, languages):
self.titles = titles
self.artist = artist
self.album = album
self.path = path
self.languages = languages
def __repr__(self):
return repr((self.title, self.artist, self.album, self.path))
return repr((self.titles, self.artist, self.album, self.path))
if platform.system() == "Linux":
from xdg.BaseDirectory import *
@ -82,7 +81,6 @@ class SongsList:
path = os.path.join(self._library, 'songs', filename)
with open(path, 'r+') as f:
data = f.read()
title = reTitle.search(data).group(0)
artist = reArtist.search(data.replace("{","")).group(0)
match = reAlbum.search(data.replace("{",""))
if match:
@ -93,7 +91,7 @@ class SongsList:
# Exécution de PlasTeX
data = parsetex(path)
self.songs.append(Song(title, artist, album, path, **data))
self.songs.append(Song(artist = artist, album = album, path = path, **data))
def append_list(self, filelist):
"""Ajoute une liste de chansons à la liste
@ -109,7 +107,7 @@ class SongsList:
TODO : Un jour, il sera peut-être possible de laisser l'utilisateur
configurer l'ordre de ce tri.
"""
self.songs = sorted(self.songs, key=lambda x: locale.strxfrm(unprefixed(x.title, self._prefixes)))
self.songs = sorted(self.songs, key=lambda x: locale.strxfrm(unprefixed(x.titles[0], self._prefixes)))
self.songs = sorted(self.songs, key=lambda x: locale.strxfrm(x.album))
self.songs = sorted(self.songs, key=lambda x: locale.strxfrm(x.artist))

33
utils/plastex.py

@ -4,25 +4,26 @@
from plasTeX.TeX import TeX
import codecs
import copy
from utils import songs
import os
import sys
class SongParser:
"""Classe singleton, pour ne charger qu'une fois les modules LaTeX"""
_tex = None
@classmethod
def _create_TeX(cls):
cls._tex = TeX()
cls._tex.disableLogging()
cls._tex.ownerDocument.context.loadBaseMacros()
cls._tex.ownerDocument.context.loadPackage(cls._tex, "babel")
"""Analyseur syntaxique de fichiers .sg"""
@staticmethod
def _create_TeX():
tex = TeX()
tex.disableLogging()
tex.ownerDocument.context.loadBaseMacros()
tex.ownerDocument.context.loadPackage(tex, "babel")
sys.path.append(os.path.dirname(__file__))
tex.ownerDocument.context.loadPackage(tex, "songs")
sys.path.pop()
return tex
@classmethod
def parse(cls, filename):
if not cls._tex:
cls._create_TeX()
tex = copy.copy(cls._tex)
tex = cls._create_TeX()
tex.input(codecs.open(filename, 'r+', 'utf-8', 'replace'))
return tex.parse()
@ -43,6 +44,8 @@ def parsetex(filename):
}
for node in doc.allChildNodes:
if node.nodeName == "selectlanguage":
data["languages"].add(node.argSource)
data["languages"].add(node.attributes['lang'])
if node.nodeName == "beginsong":
data["titles"] = node.attributes["titles"]
return data

19
utils/songs.py

@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import plasTeX
class beginsong(plasTeX.Command):
args = '{titles}[ args:dict ]'
def invoke(self, tex):
plasTeX.Command.invoke(self, tex)
# Parsing title
titles = []
for token in self.attributes['titles'].allChildNodes:
if token.nodeName != '\\':
titles.append(token.source.encode('utf-8'))
self.attributes['titles'] = titles
# Parsing keyval arguments
pass
Loading…
Cancel
Save