plint

French poetry validator
git clone https://a3nm.net/git/plint/
Log | Files | Refs | README

commit ca26d8e8663da50b06f5dd5954b7be6dc9d6bdd4
parent 3c63f5beb20b16cb4f55dfa10f698e5ced430023
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Wed, 14 Mar 2012 23:24:42 +0100

custom templates

Diffstat:
TODO | 3++-
plint_web.py | 15+++++++++++----
template.py | 14++++++++------
3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/TODO b/TODO @@ -1,4 +1,4 @@ -standalone letters +standalone letters, numbers TODO undo for irc @@ -8,3 +8,4 @@ Help text in French link to drime, and from drime +label for radios diff --git a/plint_web.py b/plint_web.py @@ -64,12 +64,19 @@ def q(): return env.get_template('error.html').render(**d) if not re.match("^[a-z]+$", d['template']): return env.get_template('error.html').render(**d) + if d['template'] == 'custom': + x = request.forms.get('custom_template') + else: + try: + f = open("static/tpl/" + d['template'] + ".tpl") + x = f.read() + f.close() + except IOError: + return env.get_template('error.html').render(**d) try: - f = open("static/tpl/" + d['template'] + ".tpl") - except IOError: + templ = template.Template(x) + except ValueError: return env.get_template('error.html').render(**d) - templ = template.Template(f) - f.close() r = [] firsterror = None nerror = 0 diff --git a/template.py b/template.py @@ -16,6 +16,8 @@ class Pattern: def parse_metric(self): """Parse from a metric description""" verse = [int(x) for x in self.metric.split('/')] + if sum(verse) > 16: + raise ValueError self.hemistiches = [] self.length = 0 for v in verse: @@ -24,23 +26,23 @@ class Pattern: self.length = self.hemistiches.pop() class Template: - def __init__(self, stream): + def __init__(self, string): self.template = [] self.pattern_line_no = 0 - self.load(stream) + self.load(string) self.line_no = 0 self.position = 0 self.env = {} self.femenv = {} self.reject_errors = False - def load(self, stream): - """Load from a stream""" - for line in stream.readlines(): + def load(self, s): + """Load from a string""" + for line in s.split('\n'): line = line.strip() self.pattern_line_no += 1 if line != '' and line[0] != '#': - self.template.append(self.parse_line(line.lstrip().rstrip())) + self.template.append(self.parse_line(line.strip())) def count(self, align): """total weight of an align"""