Browse Source

Better error messages

pull/176/head
Louis 9 years ago
parent
commit
10578710ef
  1. 1
      patacrep/latex/__init__.py
  2. 11
      patacrep/songs/chordpro/__init__.py
  3. 2
      patacrep/songs/chordpro/ast.py
  4. 2
      patacrep/songs/chordpro/lexer.py
  5. 20
      patacrep/songs/errors.py
  6. 2
      patacrep/songs/syntax.py
  7. 4
      patacrep/templates.py

1
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

11
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):

2
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,

2
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)

20
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

2
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."""

4
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

Loading…
Cancel
Save