From 10578710ef3dda5fec19475a1f300427d941c6b6 Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 27 Dec 2015 23:10:10 +0100 Subject: [PATCH] Better error messages --- patacrep/latex/__init__.py | 1 - patacrep/songs/chordpro/__init__.py | 11 +++++++---- patacrep/songs/chordpro/ast.py | 2 +- patacrep/songs/chordpro/lexer.py | 2 +- patacrep/songs/errors.py | 20 +++++++++++++++++--- patacrep/songs/syntax.py | 2 +- patacrep/templates.py | 4 ++++ 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/patacrep/latex/__init__.py b/patacrep/latex/__init__.py index c43ba3a0..f1755186 100644 --- a/patacrep/latex/__init__.py +++ b/patacrep/latex/__init__.py @@ -138,7 +138,6 @@ def lang2babel(lang, *, raise_unknown=False): try: return BABEL_LANGUAGES[checklanguage(lang)] except UnknownLanguage as error: - LOGGER.warning(str(error)) if raise_unknown: raise return error.babel diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index 3abf58cd..e61fbd1e 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -11,7 +11,7 @@ import jinja2 from patacrep import encoding, files, pkg_datapath from patacrep.songs import Song from patacrep.songs.chordpro.syntax import parse_song -from patacrep.songs.errors import FileNotFound +from patacrep.songs.errors import FileNotFound, SongUnknownLanguage from patacrep.templates import Renderer from patacrep.latex import lang2babel, UnknownLanguage from patacrep.files import path2posix @@ -145,11 +145,14 @@ class Chordpro2LatexSong(ChordproSong): try: return lang2babel(lang, raise_unknown=True) except UnknownLanguage as error: - error.message = "Song {}: {}".format( - self.fullpath, + new_error = SongUnknownLanguage( + self, + error.original, + error.fallback, error.message, ) - self.errors.append(error) + LOGGER.warning(new_error) + self.errors.append(new_error) return error.babel class Chordpro2ChordproSong(ChordproSong): diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index ff0bc4cb..769803fa 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -269,7 +269,7 @@ class Song(AST): # methods listed in ``METADATA_ADD``. if data.keyword not in AVAILABLE_DIRECTIVES: message = "Ignoring unknown directive '{}'.".format(data.keyword) - LOGGER.warning("File {}, line {}: {}".format(self.filename, data.lineno, message)) + LOGGER.warning("Song {}, line {}: {}".format(self.filename, data.lineno, message)) self.error_builders.append(functools.partial( errors.SongSyntaxError, line=data.lineno, diff --git a/patacrep/songs/chordpro/lexer.py b/patacrep/songs/chordpro/lexer.py index 93214448..9d0f3b89 100644 --- a/patacrep/songs/chordpro/lexer.py +++ b/patacrep/songs/chordpro/lexer.py @@ -150,7 +150,7 @@ class ChordProLexer: message=message, )) if self.filename is not None: - message = "File {}, line {}: {}".format(self.filename, token.lexer.lineno, message) + message = "Song {}, line {}: {}".format(self.filename, token.lexer.lineno, message) else: message = "Line {}: {}".format(token.lexer.lineno, message) LOGGER.warning(message) diff --git a/patacrep/songs/errors.py b/patacrep/songs/errors.py index 7c9bac4b..cee94b65 100644 --- a/patacrep/songs/errors.py +++ b/patacrep/songs/errors.py @@ -12,7 +12,13 @@ class SongError(SharedError): self.message = message def __str__(self): - return "Song {}: {}".format(self.song, self.message) + return "{}: {}".format(self._human_song(), self.message) + + def _human_song(self): + return "Datadir '{}', song '{}'".format( + self.song.datadir, + self.song.subpath, + ) class SongSyntaxError(SongError): """Syntax error""" @@ -25,12 +31,20 @@ class SongSyntaxError(SongError): def __str__(self): if self.line is not None: - return "Song {}, line {}: {}".format(self.song, self.line, self.message) + return "{}, line {}: {}".format(self._human_song(), self.line, self.message) else: - return "Song {}: {}".format(self.song, self.message) + return "{}: {}".format(self._human_song(), self.message) class FileNotFound(SongError): """File not found error""" def __init__(self, song, filename): super().__init__(song, "File '{}' not found.".format(filename)) + +class SongUnknownLanguage(SongError): + """Song language is not known.""" + + def __init__(self, song, original, fallback, message): + super().__init__(song, message) + self.original = original + self.fallback = fallback diff --git a/patacrep/songs/syntax.py b/patacrep/songs/syntax.py index db6c24c5..aeeb85a1 100644 --- a/patacrep/songs/syntax.py +++ b/patacrep/songs/syntax.py @@ -47,7 +47,7 @@ class Parser: if self.filename is None: LOGGER.warning(text) else: - LOGGER.warning("File {}: {}".format(self.filename, text)) + LOGGER.warning("Song {}: {}".format(self.filename, text)) def p_error(self, token): """Manage parsing errors.""" diff --git a/patacrep/templates.py b/patacrep/templates.py index e1ba8ea3..0b4d981b 100644 --- a/patacrep/templates.py +++ b/patacrep/templates.py @@ -1,5 +1,6 @@ """Template for .tex generation settings and utilities""" +import logging import re import json @@ -12,6 +13,8 @@ from patacrep import errors, files from patacrep.latex import lang2babel, UnknownLanguage import patacrep.encoding +LOGGER = logging.getLogger(__name__) + _LATEX_SUBS = ( (re.compile(r'\\'), r'\\textbackslash'), (re.compile(r'([{}_#%&$])'), r'\\\1'), @@ -107,6 +110,7 @@ class Renderer: return lang2babel(lang, raise_unknown=True) except UnknownLanguage as error: error.message = "Songbook: {}".format(error.message) + LOGGER.warning(error.message) self.errors.append(error) return error.babel