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 /kronos/parser.py | |
parent | d8fd583dfcc1f4abf44d84f320e1ad69a9032fc4 (diff) | |
download | kronos-c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9.tar.bz2 kronos-c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9.tar.xz kronos-c4fd2cc59dad0d4ca91a881b48bd064cc9c395f9.zip |
Much better pass at the parser. Huzzah for TDD.
Diffstat (limited to 'kronos/parser.py')
-rw-r--r-- | kronos/parser.py | 52 |
1 files changed, 11 insertions, 41 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 | ||