From 15c2c2ee1066dd21e19446fc583f536d0992c16a Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Fri, 5 Feb 2010 17:47:52 -0500 Subject: Re-factoring parser to better seperate concerns of parsing tags. --- kronos/parser.py | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/kronos/parser.py b/kronos/parser.py index cf848e0..29c7c03 100644 --- a/kronos/parser.py +++ b/kronos/parser.py @@ -11,30 +11,45 @@ from kronos.model import Activity def parse_activity(text): - tokens = text.split(' ') - tags = _parse_tags(tokens) - text = _strip_tags(tokens) + tag_parser = TagHandler(text) + text = tag_parser.text - description = None - if ',' in text: - text, description = text.split(',', 1) - description = description.strip() - - category = None - if '@' in text: - text, category = text.split('@', 1) - category = category.strip() + text, description = _clean_split(text, ',') + text, category = _clean_split(text, '@') activity = Activity(text.strip(), description, category) - activity.tags = tags + activity.tags = tag_parser.tags return activity -def _parse_tags(tokens): - return [token.lstrip('#') for token in tokens if token.startswith('#')] +def _clean_split(text, delimiter): + if delimiter in text: + text, wanted = text.split(delimiter, 1) + return text, wanted.strip() + else: + return text, None + + +class TagHandler(object): + + def __init__(self, text): + self.tokens = text.split(' ') + + def _parse_tags(self): + return [token.lstrip('#') for token in self.tokens + if token.startswith('#')] + + def _strip_tags(self): + words = [token for token in self.tokens + if not token.startswith('#')] + + return ' '.join(words) + @property + def text(self): + return self._strip_tags() -def _strip_tags(tokens): - words = [token for token in tokens if not token.startswith('#')] - return ' '.join(words) + @property + def tags(self): + return self._parse_tags() -- cgit v1.2.3