From 0400c9a71bb354ba659daa471d1f461d28b50c3a Mon Sep 17 00:00:00 2001 From: noah morrison Date: Sat, 1 Nov 2014 12:50:26 -0400 Subject: [PATCH] DRY'd the code some Added a function to grab literals and tag keys, since the code was very similar and in two places. Also the grab_literal function is probably more efficent, as it doesn't peek so much. --- chevron.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/chevron.py b/chevron.py index fc845da..c7a3226 100755 --- a/chevron.py +++ b/chevron.py @@ -34,7 +34,11 @@ def tokenize(template): pass def get(amount=1): - return template.read(amount) + data = template.read(amount) + if len(data) != amount: + raise EOFError() + + return data def peek(ahead=0, amount=1): current = template.tell() @@ -45,6 +49,14 @@ def tokenize(template): raise EOFError() return data + def grab_literal(until=None): + until = until or l_del + literal = get() + while literal[-2:] != until: + literal += get() + + return literal[:-2] + tag_types = { '!': 'comment', '#': 'section', @@ -64,27 +76,18 @@ def tokenize(template): r_del = '}}' while not template.closed: try: - size = 0 - while peek(size, 2) != l_del: - size += 1 + literal = grab_literal() except EOFError: - yield ('literal', get(size)) return - if size != 0: - yield ('literal', get(size)) - - get(2) + if literal: + yield ('literal', literal) tag_type = tag_types.get(peek(0, 1), 'variable') if tag_type != 'variable': template.seek(template.tell() + 1) - size = 0 - while peek(size, 2) != r_del: - size += 1 - - tag_key = get(size).strip() + tag_key = grab_literal(r_del).strip() if tag_type == 'no escape?': if peek(0, 3) == '}}}': @@ -105,14 +108,11 @@ def tokenize(template): if tag_key != last_section: raise UnclosedSection() - get(2) yield (tag_type, tag_key) if open_sections: raise UnclosedSection() - return - if __name__ == '__main__': data = argv[1] -- 2.47.3