diff options
author | Mike Crute <mcrute@gmail.com> | 2010-02-04 21:54:38 -0500 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2010-02-04 21:54:38 -0500 |
commit | 5f6c2c4c3f9aeddd5e07e0d2bb25df15b2cc0eb0 (patch) | |
tree | c9e28d004dc87b9f0a986333035662b55afabf7a | |
parent | c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9 (diff) | |
download | kronos-5f6c2c4c3f9aeddd5e07e0d2bb25df15b2cc0eb0.tar.bz2 kronos-5f6c2c4c3f9aeddd5e07e0d2bb25df15b2cc0eb0.tar.xz kronos-5f6c2c4c3f9aeddd5e07e0d2bb25df15b2cc0eb0.zip |
Implementing tag parsing
-rw-r--r-- | kronos/parser.py | 19 | ||||
-rw-r--r-- | 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): | |||
13 | self.activity = activity | 13 | self.activity = activity |
14 | self.description = description | 14 | self.description = description |
15 | self.category = category | 15 | self.category = category |
16 | self.tags = [] | ||
16 | 17 | ||
17 | 18 | ||
18 | def parse_activity(text): | 19 | def parse_activity(text): |
19 | description = None | 20 | description = None |
21 | tags = [] | ||
20 | if ',' in text: | 22 | if ',' in text: |
21 | text, description = text.split(',', 1) | 23 | text, description = text.split(',', 1) |
22 | description = description.strip() | 24 | description = description.strip() |
23 | 25 | ||
26 | tokens = description.split(' ') | ||
27 | tags = parse_tags(tokens) | ||
28 | description = strip_tags(tokens) | ||
29 | |||
24 | category = None | 30 | category = None |
25 | if '@' in text: | 31 | if '@' in text: |
26 | text, category = text.split('@', 1) | 32 | text, category = text.split('@', 1) |
27 | category = category.strip() | 33 | category = category.strip() |
28 | 34 | ||
29 | activity = text.strip() | 35 | activity = Activity(text.strip(), description, category) |
36 | activity.tags = tags | ||
37 | |||
38 | return activity | ||
39 | |||
40 | |||
41 | def parse_tags(tokens): | ||
42 | return [token.lstrip('#') for token in tokens if token.startswith('#')] | ||
43 | |||
30 | 44 | ||
31 | return Activity(activity, description, category) | 45 | def strip_tags(tokens): |
46 | 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): | |||
55 | self.results = parse_activity(test_input) | 55 | self.results = parse_activity(test_input) |
56 | 56 | ||
57 | def test_should_get_tags(self): | 57 | def test_should_get_tags(self): |
58 | pass | 58 | assert_equals(self.results.tags, ['tag1', 'tag2']) |
59 | |||
60 | def test_should_not_include_tags_in_description(self): | ||
61 | assert_equals(self.results.description, "Some cool stuff!") | ||
59 | 62 | ||
60 | 63 | ||
61 | class TestWhenParsingWithTimeOffset(object): | 64 | class TestWhenParsingWithTimeOffset(object): |