plint

French poetry validator (local mirror of https://gitlab.com/a3nm/plint)
git clone https://a3nm.net/git/plint/
Log | Files | Refs | README

commit 0675d80036de4205ca5bfe47ab5b0e55bc97b099
parent 6da878ea48722a4e4c13f2b1ffc3f31595734f80
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Wed, 14 Mar 2012 02:40:14 +0100

better error reporting

Diffstat:
error.py | 65+++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 49 insertions(+), 16 deletions(-)

diff --git a/error.py b/error.py @@ -17,24 +17,33 @@ class Error: def say(self, l): return self.prefix + l - def report(self, s, t = []): + def report(self, s, short=False, t = []): l = [] - l.append(self.say("error: %s" % (s))) - l.append(self.say("Line is: %s" % (self.line))) - for x in t: - l.append(self.say(x)) + if short: + l.append(s) + else: + l.append(self.say("error: %s" % (s))) + if short: + if t != []: + l.append("Line is: %s" % (self.line)) + for x in t: + l.append(x) + else: + l.append(self.say("Line is: %s" % (self.line))) + for x in t: + l.append(self.say(x)) return '\n'.join(l) - + class ErrorBadRhyme(Error): def __init__(self, expected, inferred): Error.__init__(self) self.expected = expected self.inferred = inferred - def report(self): + def report(self, short=False): return Error.report(self, "Bad rhyme %s for type %s (expected %s, inferred %s)" % (self.kind, self.get_id(), self.fmt(self.expected), - self.fmt(self.inferred))) + self.fmt(self.inferred)), short) class ErrorBadRhymeGenre(ErrorBadRhyme): def fmt(self, l): @@ -57,14 +66,14 @@ class ErrorBadRhymeSound(ErrorBadRhyme): ok = [] if len(pron) > 0: ok.append("") - return '/'.join(list(pron)) + return '/'.join([common.to_xsampa(x[-4:]) for x in pron]) def get_id(self): return self.pattern.myid - def report(self): + def report(self, short=False): return Error.report(self, "Bad rhyme %s for type %s (expected %s)" - % (self.kind, self.pattern.myid, self.fmt(self.expected))) + % (self.kind, self.pattern.myid, self.fmt(self.expected)), short) @property def kind(self): @@ -75,22 +84,41 @@ class ErrorBadMetric(Error): Error.__init__(self) self.possible = possible + def restore_elid(self, chunk): + if isinstance(chunk, tuple): + return [chunk] + try: + if chunk[-1] != "`": + return [chunk] + except KeyError: + return [chunk] + return [chunk[:-1], ("e", 0)] + def align(self, align): score, align = align align, feminine, hemis = align + align = sum([self.restore_elid(chunk) for chunk in align], []) line = self.line l2 = [] count = 0 ccount = 0 + last_he = 0 summary = [] + offset = 0 done = False - for x in [''] + align: + for x in align: if isinstance(x, tuple): orig = "" while len(line) > 0 and common.is_vowels(line[0]): orig += line[0] line = line[1:] - l2 += ('{:^'+str(len(orig))+'}').format(str(x[1])) + add = ('{:^'+str(len(orig))+'}').format(str(x[1])) + if offset > 0 and len(add) > 0 and add[-1] == ' ': + offset -= 1 + add = add[:-1] + l2 += add + if len(add) > len(orig): + offset = len(add) - len(orig) count += x[1] ccount += x[1] done = False @@ -99,13 +127,14 @@ class ErrorBadMetric(Error): while len(line) > 0 and not common.is_vowels(line[0]): orig += line[0] line = line[1:] - if count in hemis.keys() and not done: + if count in hemis.keys() and not done and last_he < count: done = True summary.append(str(ccount)) ccount = 0 summary.append(hemistiches.hemis_types[hemis[count]]) l2 += ('{:^'+str(len(orig))+'}' ).format(hemistiches.hemis_types[hemis[count]]) + last_he = count else: l2 += ' ' * len(orig) summary.append(str(ccount)+':') @@ -113,13 +142,17 @@ class ErrorBadMetric(Error): summary = ('{:^9}').format(''.join(summary)) return summary + result - def report(self): + def report(self, short=False): num = min(len(self.possible), 4) + truncated = num < len(self.possible) return Error.report( self, ("Bad metric (expected %s, inferred %d option%s)" % - (self.pattern.metric, num, ('s' if len(self.possible) != 1 else + (self.pattern.metric, + len(self.possible), ('s' if len(self.possible) != 1 else ''))), + short, list(map(self.align, self.possible[:num])) + + (["... other options omitted ..."] if truncated else []) )