diff options
author | Mike Crute <mcrute@gmail.com> | 2010-02-04 21:42:00 -0500 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2010-02-04 21:42:00 -0500 |
commit | c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9 (patch) | |
tree | 1fcedde085dfb4ca4bc6c5c1c014cefea1214974 | |
parent | d8fd583dfcc1f4abf44d84f320e1ad69a9032fc4 (diff) | |
download | kronos-c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9.tar.bz2 kronos-c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9.tar.xz kronos-c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9.zip |
Much better pass at the parser. Huzzah for TDD.
-rw-r--r-- | kronos/parser.py | 52 | ||||
-rw-r--r-- | kronos/tests/test_parser.py | 54 |
2 files changed, 35 insertions, 71 deletions
diff --git a/kronos/parser.py b/kronos/parser.py index 0c39b95..b18b4f9 100644 --- a/kronos/parser.py +++ b/kronos/parser.py | |||
@@ -14,48 +14,18 @@ class Activity(object): | |||
14 | self.description = description | 14 | self.description = description |
15 | self.category = category | 15 | self.category = category |
16 | 16 | ||
17 | def __repr__(self): | ||
18 | return "Activity({0!r}, description={1!r}, category={2!r})".format( | ||
19 | self.activity, self.description, self.category) | ||
20 | 17 | ||
18 | def parse_activity(text): | ||
19 | description = None | ||
20 | if ',' in text: | ||
21 | text, description = text.split(',', 1) | ||
22 | description = description.strip() | ||
21 | 23 | ||
22 | class ActivityParser(object): | 24 | category = None |
25 | if '@' in text: | ||
26 | text, category = text.split('@', 1) | ||
27 | category = category.strip() | ||
23 | 28 | ||
24 | def __init__(self, text): | 29 | activity = text.strip() |
25 | self.text = text | ||
26 | self.tokens = text.split(' ') | ||
27 | 30 | ||
28 | def parse(self): | 31 | return Activity(activity, description, category) |
29 | activity = Activity(self._parse_activity()) | ||
30 | activity.description = self._parse_description() | ||
31 | activity.category = self._parse_category() | ||
32 | |||
33 | return activity | ||
34 | |||
35 | def _parse_activity(self): | ||
36 | return self.text.split(',')[0].split('@')[0] | ||
37 | |||
38 | def _parse_description(self): | ||
39 | text = self.text | ||
40 | |||
41 | if ',' in self.text: | ||
42 | text = text.split(',', 1)[1] | ||
43 | |||
44 | if '@' in text: | ||
45 | text = text.split('@', 1)[0] | ||
46 | |||
47 | if text == self.text: | ||
48 | return None | ||
49 | |||
50 | return text.strip() | ||
51 | |||
52 | def _parse_category(self): | ||
53 | text = self.text | ||
54 | |||
55 | if '@' in text: | ||
56 | text = text.split('@', 1)[1] | ||
57 | |||
58 | if text == self.text: | ||
59 | return None | ||
60 | |||
61 | return text | ||
diff --git a/kronos/tests/test_parser.py b/kronos/tests/test_parser.py index e7d64a3..1f9ec40 100644 --- a/kronos/tests/test_parser.py +++ b/kronos/tests/test_parser.py | |||
@@ -9,50 +9,50 @@ Activity Parser Test Suite | |||
9 | 9 | ||
10 | 10 | ||
11 | from nose.tools import assert_equals | 11 | from nose.tools import assert_equals |
12 | from kronos.parser import ActivityParser | 12 | from kronos.parser import parse_activity |
13 | 13 | ||
14 | 14 | ||
15 | def parse_text(text): | ||
16 | return ActivityParser(text).parse() | ||
17 | |||
18 | class TestWhenParsingBasicFormat(object): | 15 | class TestWhenParsingBasicFormat(object): |
19 | 16 | ||
20 | def test_should_get_activity(self): | 17 | def test_should_get_activity(self): |
21 | results = parse_text("my activity, Some cool stuff!@Home") | 18 | results = parse_activity("my activity@Home, Some cool stuff!") |
19 | assert_equals(results.activity, "my activity") | ||
20 | |||
21 | results = parse_activity("my activity") | ||
22 | assert_equals(results.activity, "my activity") | 22 | assert_equals(results.activity, "my activity") |
23 | 23 | ||
24 | results = parse_text("my activity") | 24 | results = parse_activity("my activity@Home") |
25 | assert_equals(results.activity, "my activity") | 25 | assert_equals(results.activity, "my activity") |
26 | 26 | ||
27 | results = parse_text("my activity@Home") | 27 | results = parse_activity("my activity, Some cool stuff!") |
28 | assert_equals(results.activity, "my activity") | 28 | assert_equals(results.activity, "my activity") |
29 | 29 | ||
30 | def test_should_get_description(self): | 30 | def test_should_get_description(self): |
31 | results = parse_text("my activity, Some cool stuff!@Home") | 31 | results = parse_activity("my activity@Home, Some cool stuff!") |
32 | assert_equals(results.description, "Some cool stuff!") | 32 | assert_equals(results.description, "Some cool stuff!") |
33 | 33 | ||
34 | results = parse_text("my activity, Some cool stuff!") | 34 | results = parse_activity("my activity, Some cool stuff!") |
35 | assert_equals(results.description, "Some cool stuff!") | 35 | assert_equals(results.description, "Some cool stuff!") |
36 | 36 | ||
37 | results = parse_text("my activity") | 37 | results = parse_activity("my activity") |
38 | assert_equals(results.description, None) | 38 | assert_equals(results.description, None) |
39 | 39 | ||
40 | def test_should_get_category(self): | 40 | def test_should_get_category(self): |
41 | results = parse_text("my activity, Some cool stuff!@Home") | 41 | results = parse_activity("my activity@Home, Some cool stuff!") |
42 | assert_equals(results.category, "Home") | 42 | assert_equals(results.category, "Home") |
43 | 43 | ||
44 | results = parse_text("my activity, Some cool stuff!") | 44 | results = parse_activity("my activity, Some cool stuff!") |
45 | assert_equals(results.category, None) | 45 | assert_equals(results.category, None) |
46 | 46 | ||
47 | results = parse_text("my activity@Home") | 47 | results = parse_activity("my activity@Home") |
48 | assert_equals(results.category, "Home") | 48 | assert_equals(results.category, "Home") |
49 | 49 | ||
50 | 50 | ||
51 | class TestWhenParsingWithTags(object): | 51 | class TestWhenParsingWithTags(object): |
52 | 52 | ||
53 | def setup(self): | 53 | def setup(self): |
54 | test_input = "my activity, Some cool stuff!@Home #tag1 #tag2" | 54 | test_input = "my activity@Home, Some cool stuff! #tag1 #tag2" |
55 | self.results = ActivityParser(test_input).parse() | 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 | pass |
@@ -60,30 +60,24 @@ class TestWhenParsingWithTags(object): | |||
60 | 60 | ||
61 | class TestWhenParsingWithTimeOffset(object): | 61 | class TestWhenParsingWithTimeOffset(object): |
62 | 62 | ||
63 | def _parse_text(self, text): | ||
64 | return ActivityParser(text).parse() | ||
65 | |||
66 | def test_should_understand_minutes(self): | 63 | def test_should_understand_minutes(self): |
67 | test_input = "my activity, Some cool stuff!@Home 10m" | 64 | test_input = "my activity@Home, Some cool stuff! 10m" |
68 | test_input = "my activity, Some cool stuff!@Home 10M" | 65 | test_input = "my activity@Home, Some cool stuff! 10M" |
69 | 66 | ||
70 | def test_should_understand_hours(self): | 67 | def test_should_understand_hours(self): |
71 | test_input = "my activity, Some cool stuff!@Home 10h" | 68 | test_input = "my activity@Home, Some cool stuff! 10h" |
72 | test_input = "my activity, Some cool stuff!@Home 10H" | 69 | test_input = "my activity@Home, Some cool stuff! 10H" |
73 | 70 | ||
74 | def test_should_understand_seconds(self): | 71 | def test_should_understand_seconds(self): |
75 | test_input = "my activity, Some cool stuff!@Home 10s" | 72 | test_input = "my activity@Home, Some cool stuff! 10s" |
76 | test_input = "my activity, Some cool stuff!@Home 10S" | 73 | test_input = "my activity@Home, Some cool stuff! 10S" |
77 | 74 | ||
78 | 75 | ||
79 | class TestWhenParsingWithAbsoluteTime(object): | 76 | class TestWhenParsingWithAbsoluteTime(object): |
80 | 77 | ||
81 | def _parse_text(self, text): | ||
82 | return ActivityParser(text).parse() | ||
83 | |||
84 | def test_should_understand_military_time(self): | 78 | def test_should_understand_military_time(self): |
85 | test_input = "my activity, Some cool stuff!@Home 11:30-14:40" | 79 | test_input = "my activity@Home, Some cool stuff! 11:30-14:40" |
86 | 80 | ||
87 | def test_should_understand_simple_time(self): | 81 | def test_should_understand_simple_time(self): |
88 | test_input = "my activity, Some cool stuff!@Home 11:30AM-2:40PM" | 82 | test_input = "my activity@Home, Some cool stuff! 11:30AM-2:40PM" |
89 | test_input = "my activity, Some cool stuff!@Home 11:30A-2:40P" | 83 | test_input = "my activity@Home, Some cool stuff! 11:30A-2:40P" |