Skip to content

Commit

Permalink
Merge pull request #105 from elyezer/improved-parser
Browse files Browse the repository at this point in the history
Improve docstring parser
  • Loading branch information
sthirugn committed Jun 17, 2016
2 parents 2dae21f + 8afb2a5 commit 7dd7123
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions testimony/parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# coding=utf-8
"""Docstring parser utilities for Testimony."""
import re

from testimony.constants import DEFAULT_MINIMUM_TOKENS, DEFAULT_TOKENS

TOKEN_RE = re.compile(r'^@(\w+):\s+([^@]+)(\n|$)', flags=re.MULTILINE)


class DocstringParser(object):
"""Parse docstring extracting tokens."""
Expand Down Expand Up @@ -46,19 +50,13 @@ def parse(self, docstring=None):
return {}, {}
valid_tokens = {}
invalid_tokens = {}
for line in docstring.split('@'):
line = line.rstrip()
# Sometimes there are double new line characters in the middle. We
# need only one of those to print
line = line.replace('\n\n', '\n')
if len(line) > 0 and ':' in line:
token, value = line.split(':', 1)
token = token.lower()
value = value.strip()
if token in self.tokens:
valid_tokens[token] = value
else:
invalid_tokens[token] = value
for match in TOKEN_RE.finditer(docstring):
token = match.group(1).strip().lower()
value = match.group(2).strip()
if token in self.tokens:
valid_tokens[token] = value
else:
invalid_tokens[token] = value
return valid_tokens, invalid_tokens

def validate_tokens(self, tokens):
Expand Down

0 comments on commit 7dd7123

Please sign in to comment.