From 362c6b87ce24c6c52ebeb6723db5dfb766d949f6 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 12 Dec 2015 23:46:11 +0100 Subject: [PATCH] Song errors contain a reference to the song they occur in --- patacrep/errors.py | 6 ++++++ patacrep/latex/syntax.py | 1 + patacrep/songs/__init__.py | 4 ++-- patacrep/songs/chordpro/__init__.py | 2 +- patacrep/songs/chordpro/ast.py | 4 ++-- patacrep/songs/errors.py | 22 ++++++++++------------ patacrep/songs/syntax.py | 12 ++++++------ 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/patacrep/errors.py b/patacrep/errors.py index d1d3a74f..2b7e7f06 100644 --- a/patacrep/errors.py +++ b/patacrep/errors.py @@ -104,6 +104,12 @@ class ParsingError(SongbookError): def __str__(self): return self.message +class SharedError(SongbookError): + """Error that is meant to be shared to third party tools using patacrep.""" + + def __str__(self): + raise NotImplementedError() + def notfound(filename, paths, message=None): """Return a string saying that file was not found in paths.""" if message is None: diff --git a/patacrep/latex/syntax.py b/patacrep/latex/syntax.py index 8915816b..2814b0ad 100644 --- a/patacrep/latex/syntax.py +++ b/patacrep/latex/syntax.py @@ -136,6 +136,7 @@ class LatexParser(Parser): symbols[0][symbols[1]] = symbols[3] symbols[0].update(symbols[4]) else: + # TODO put error in self.errors raise ParsingError("Do enclose arguments between braces.") @staticmethod diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 94b630c7..5c40591a 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -180,8 +180,8 @@ class Song: protocol=-1 ) - def __repr__(self): - return repr((self.titles, self.data, self.fullpath)) + def __str__(self): + return str(self.fullpath) def render(self, *args, **kwargs): """Return the code rendering this song. diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index c7c0bdf4..48a16d95 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -33,7 +33,7 @@ class ChordproSong(Song): self.titles = song.titles self.lang = song.get_data_argument('language', self.default_lang) self.data = song.meta - self.errors = song.errors + self.errors = [error(song=self) for error in song.error_builders] self.cached = { 'song': song, } diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index 380e6833..f18bd395 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -232,9 +232,9 @@ class Song(AST): self._subtitles = [] self.filename = filename if errors is None: - self.errors = [] + self.error_builders = [] else: - self.errors = errors + self.error_builders = errors for directive in directives: self.add(directive) diff --git a/patacrep/songs/errors.py b/patacrep/songs/errors.py index 21052d64..7737225f 100644 --- a/patacrep/songs/errors.py +++ b/patacrep/songs/errors.py @@ -1,37 +1,35 @@ """Errors in song definition (syntax errors, and so on)""" -class SongError(Exception): +from patacrep.errors import SharedError + +class SongError(SharedError): """Generic song error""" # pylint: disable=too-few-public-methods - type = "generic" - - def __init__(self, message): + def __init__(self, song, message): super().__init__() + self.song = song self.message = message def __str__(self): - raise NotImplementedError() + return "Song {}: {}".format(self.song, self.message) class SongSyntaxError(SongError): """Syntax error""" # pylint: disable=too-few-public-methods - type = "syntax" - - def __init__(self, line, message): - super().__init__(message) + def __init__(self, song, line, message): + super().__init__(song, message) #: Line of error. May be `None` if irrelevant. self.line = line def __str__(self): if self.line is not None: - return "Line {}: {}".format(self.line, self.message) + return "Song {}, line {}: {}".format(self.song, self.line, self.message) else: - return self.message + return "Song {}: {}".format(self.song, self.message) # class FileError(SongError): # type = "file" # # class LanguageError(SongError): -# type = "language" diff --git a/patacrep/songs/syntax.py b/patacrep/songs/syntax.py index 0ea5208b..db6c24c5 100644 --- a/patacrep/songs/syntax.py +++ b/patacrep/songs/syntax.py @@ -1,5 +1,6 @@ """Generic parsing classes and methods""" +import functools import logging from patacrep.songs import errors @@ -25,12 +26,11 @@ class Parser: def error(self, *, line=None, column=None, message=""): """Record and display an error message""" - self._errors.append( - errors.SongSyntaxError( - line=line, - message=message, - ) - ) + self._errors.append(functools.partial( + errors.SongSyntaxError, + line=line, + message=message, + )) coordinates = [] if line is not None: