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 import recursiveFind
from utils.plastex import parsetex from utils.plastex import parsetex
reTitle = re.compile('(?<=beginsong\\{)(.(?<!\\}]))+')
reArtist = re.compile('(?<=by=)(.(?<![,\\]\\}]))+') reArtist = re.compile('(?<=by=)(.(?<![,\\]\\}]))+')
reAlbum = re.compile('(?<=album=)(.(?<![,\\]\\}]))+') reAlbum = re.compile('(?<=album=)(.(?<![,\\]\\}]))+')
class Song: class Song:
def __init__(self, title, artist, album, path, languages): def __init__(self, titles, artist, album, path, languages):
self.title = title self.titles = titles
self.artist = artist self.artist = artist
self.album = album self.album = album
self.path = path self.path = path
self.languages = languages self.languages = languages
def __repr__(self): 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": if platform.system() == "Linux":
from xdg.BaseDirectory import * from xdg.BaseDirectory import *
@ -82,7 +81,6 @@ class SongsList:
path = os.path.join(self._library, 'songs', filename) path = os.path.join(self._library, 'songs', filename)
with open(path, 'r+') as f: with open(path, 'r+') as f:
data = f.read() data = f.read()
title = reTitle.search(data).group(0)
artist = reArtist.search(data.replace("{","")).group(0) artist = reArtist.search(data.replace("{","")).group(0)
match = reAlbum.search(data.replace("{","")) match = reAlbum.search(data.replace("{",""))
if match: if match:
@ -93,7 +91,7 @@ class SongsList:
# Exécution de PlasTeX # Exécution de PlasTeX
data = parsetex(path) 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): def append_list(self, filelist):
"""Ajoute une liste de chansons à la liste """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 TODO : Un jour, il sera peut-être possible de laisser l'utilisateur
configurer l'ordre de ce tri. 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.album))
self.songs = sorted(self.songs, key=lambda x: locale.strxfrm(x.artist)) 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 from plasTeX.TeX import TeX
import codecs import codecs
import copy import copy
import os
from utils import songs import sys
class SongParser: class SongParser:
"""Classe singleton, pour ne charger qu'une fois les modules LaTeX""" """Analyseur syntaxique de fichiers .sg"""
_tex = None
@staticmethod
@classmethod def _create_TeX():
def _create_TeX(cls): tex = TeX()
cls._tex = TeX() tex.disableLogging()
cls._tex.disableLogging() tex.ownerDocument.context.loadBaseMacros()
cls._tex.ownerDocument.context.loadBaseMacros() tex.ownerDocument.context.loadPackage(tex, "babel")
cls._tex.ownerDocument.context.loadPackage(cls._tex, "babel") sys.path.append(os.path.dirname(__file__))
tex.ownerDocument.context.loadPackage(tex, "songs")
sys.path.pop()
return tex
@classmethod @classmethod
def parse(cls, filename): def parse(cls, filename):
if not cls._tex: tex = cls._create_TeX()
cls._create_TeX()
tex = copy.copy(cls._tex)
tex.input(codecs.open(filename, 'r+', 'utf-8', 'replace')) tex.input(codecs.open(filename, 'r+', 'utf-8', 'replace'))
return tex.parse() return tex.parse()
@ -43,6 +44,8 @@ def parsetex(filename):
} }
for node in doc.allChildNodes: for node in doc.allChildNodes:
if node.nodeName == "selectlanguage": 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 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