diff options
author | Mike Crute <mcrute@gmail.com> | 2010-02-04 21:21:06 -0500 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2010-02-04 21:21:06 -0500 |
commit | d8fd583dfcc1f4abf44d84f320e1ad69a9032fc4 (patch) | |
tree | 3a079ea658d128d3be1fdc2b0fca06e4d5fa1140 | |
parent | 331d9da27b66ad0d2f0ddb88020d853af51d3ada (diff) | |
download | kronos-d8fd583dfcc1f4abf44d84f320e1ad69a9032fc4.tar.bz2 kronos-d8fd583dfcc1f4abf44d84f320e1ad69a9032fc4.tar.xz kronos-d8fd583dfcc1f4abf44d84f320e1ad69a9032fc4.zip |
First pass at a naive parser. Committing for posterity.
-rw-r--r-- | kronos/parser.py | 51 | ||||
-rw-r--r-- | kronos/tests/test_parser.py | 40 |
2 files changed, 76 insertions, 15 deletions
diff --git a/kronos/parser.py b/kronos/parser.py index 5f79144..0c39b95 100644 --- a/kronos/parser.py +++ b/kronos/parser.py | |||
@@ -9,12 +9,53 @@ Activity Parser | |||
9 | 9 | ||
10 | class Activity(object): | 10 | class Activity(object): |
11 | 11 | ||
12 | activity = "my activity" | 12 | def __init__(self, activity, description=None, category=None): |
13 | description = "Some cool stuff!" | 13 | self.activity = activity |
14 | category = "Home" | 14 | self.description = description |
15 | self.category = category | ||
16 | |||
17 | def __repr__(self): | ||
18 | return "Activity({0!r}, description={1!r}, category={2!r})".format( | ||
19 | self.activity, self.description, self.category) | ||
15 | 20 | ||
16 | 21 | ||
17 | class ActivityParser(object): | 22 | class ActivityParser(object): |
18 | 23 | ||
19 | def parse(self, text): | 24 | def __init__(self, text): |
20 | return Activity() | 25 | self.text = text |
26 | self.tokens = text.split(' ') | ||
27 | |||
28 | def parse(self): | ||
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 676a471..e7d64a3 100644 --- a/kronos/tests/test_parser.py +++ b/kronos/tests/test_parser.py | |||
@@ -12,27 +12,47 @@ from nose.tools import assert_equals | |||
12 | from kronos.parser import ActivityParser | 12 | from kronos.parser import ActivityParser |
13 | 13 | ||
14 | 14 | ||
15 | class TestWhenParsingBasicFormat(object): | 15 | def parse_text(text): |
16 | return ActivityParser(text).parse() | ||
16 | 17 | ||
17 | def setup(self): | 18 | class TestWhenParsingBasicFormat(object): |
18 | test_input = "my activity, Some cool stuff!@Home" | ||
19 | self.results = ActivityParser().parse(test_input) | ||
20 | 19 | ||
21 | def test_should_get_activity(self): | 20 | def test_should_get_activity(self): |
22 | assert_equals(self.results.activity, "my activity") | 21 | results = parse_text("my activity, Some cool stuff!@Home") |
22 | assert_equals(results.activity, "my activity") | ||
23 | |||
24 | results = parse_text("my activity") | ||
25 | assert_equals(results.activity, "my activity") | ||
26 | |||
27 | results = parse_text("my activity@Home") | ||
28 | assert_equals(results.activity, "my activity") | ||
23 | 29 | ||
24 | def test_should_get_description(self): | 30 | def test_should_get_description(self): |
25 | assert_equals(self.results.description, "Some cool stuff!") | 31 | results = parse_text("my activity, Some cool stuff!@Home") |
32 | assert_equals(results.description, "Some cool stuff!") | ||
33 | |||
34 | results = parse_text("my activity, Some cool stuff!") | ||
35 | assert_equals(results.description, "Some cool stuff!") | ||
36 | |||
37 | results = parse_text("my activity") | ||
38 | assert_equals(results.description, None) | ||
26 | 39 | ||
27 | def test_should_get_category(self): | 40 | def test_should_get_category(self): |
28 | assert_equals(self.results.category, "Home") | 41 | results = parse_text("my activity, Some cool stuff!@Home") |
42 | assert_equals(results.category, "Home") | ||
43 | |||
44 | results = parse_text("my activity, Some cool stuff!") | ||
45 | assert_equals(results.category, None) | ||
46 | |||
47 | results = parse_text("my activity@Home") | ||
48 | assert_equals(results.category, "Home") | ||
29 | 49 | ||
30 | 50 | ||
31 | class TestWhenParsingWithTags(object): | 51 | class TestWhenParsingWithTags(object): |
32 | 52 | ||
33 | def setup(self): | 53 | def setup(self): |
34 | test_input = "my activity, Some cool stuff!@Home #tag1 #tag2" | 54 | test_input = "my activity, Some cool stuff!@Home #tag1 #tag2" |
35 | self.results = ActivityParser().parse(test_input) | 55 | self.results = ActivityParser(test_input).parse() |
36 | 56 | ||
37 | def test_should_get_tags(self): | 57 | def test_should_get_tags(self): |
38 | pass | 58 | pass |
@@ -41,7 +61,7 @@ class TestWhenParsingWithTags(object): | |||
41 | class TestWhenParsingWithTimeOffset(object): | 61 | class TestWhenParsingWithTimeOffset(object): |
42 | 62 | ||
43 | def _parse_text(self, text): | 63 | def _parse_text(self, text): |
44 | return ActivityParser().parse(text) | 64 | return ActivityParser(text).parse() |
45 | 65 | ||
46 | def test_should_understand_minutes(self): | 66 | def test_should_understand_minutes(self): |
47 | test_input = "my activity, Some cool stuff!@Home 10m" | 67 | test_input = "my activity, Some cool stuff!@Home 10m" |
@@ -59,7 +79,7 @@ class TestWhenParsingWithTimeOffset(object): | |||
59 | class TestWhenParsingWithAbsoluteTime(object): | 79 | class TestWhenParsingWithAbsoluteTime(object): |
60 | 80 | ||
61 | def _parse_text(self, text): | 81 | def _parse_text(self, text): |
62 | return ActivityParser().parse(text) | 82 | return ActivityParser(text).parse() |
63 | 83 | ||
64 | def test_should_understand_military_time(self): | 84 | def test_should_understand_military_time(self): |
65 | test_input = "my activity, Some cool stuff!@Home 11:30-14:40" | 85 | test_input = "my activity, Some cool stuff!@Home 11:30-14:40" |