From 5f6c2c4c3f9aeddd5e07e0d2bb25df15b2cc0eb0 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Thu, 4 Feb 2010 21:54:38 -0500 Subject: Implementing tag parsing --- kronos/parser.py | 19 +++++++++++++++++-- kronos/tests/test_parser.py | 5 ++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/kronos/parser.py b/kronos/parser.py index b18b4f9..8d93391 100644 --- a/kronos/parser.py +++ b/kronos/parser.py @@ -13,19 +13,34 @@ class Activity(object): self.activity = activity self.description = description self.category = category + self.tags = [] def parse_activity(text): description = None + tags = [] if ',' in text: text, description = text.split(',', 1) description = description.strip() + tokens = description.split(' ') + tags = parse_tags(tokens) + description = strip_tags(tokens) + category = None if '@' in text: text, category = text.split('@', 1) category = category.strip() - activity = text.strip() + activity = Activity(text.strip(), description, category) + activity.tags = tags + + return activity + + +def parse_tags(tokens): + return [token.lstrip('#') for token in tokens if token.startswith('#')] + - return Activity(activity, description, category) +def strip_tags(tokens): + return ' '.join([token for token in tokens if not token.startswith('#')]) diff --git a/kronos/tests/test_parser.py b/kronos/tests/test_parser.py index 1f9ec40..015ae06 100644 --- a/kronos/tests/test_parser.py +++ b/kronos/tests/test_parser.py @@ -55,7 +55,10 @@ class TestWhenParsingWithTags(object): self.results = parse_activity(test_input) def test_should_get_tags(self): - pass + assert_equals(self.results.tags, ['tag1', 'tag2']) + + def test_should_not_include_tags_in_description(self): + assert_equals(self.results.description, "Some cool stuff!") class TestWhenParsingWithTimeOffset(object): -- cgit v1.2.3