summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2010-02-05 17:47:52 -0500
committerMike Crute <mcrute@gmail.com>2010-02-05 17:47:52 -0500
commit15c2c2ee1066dd21e19446fc583f536d0992c16a (patch)
tree2fae25267c558891b12a8341d8eec6631b5b2a58
parent6406cfb65124dcb8bf59deb2913375ed8dc518a0 (diff)
downloadkronos-15c2c2ee1066dd21e19446fc583f536d0992c16a.tar.bz2
kronos-15c2c2ee1066dd21e19446fc583f536d0992c16a.tar.xz
kronos-15c2c2ee1066dd21e19446fc583f536d0992c16a.zip
Re-factoring parser to better seperate concerns of parsing tags.
-rw-r--r--kronos/parser.py51
1 files changed, 33 insertions, 18 deletions
diff --git a/kronos/parser.py b/kronos/parser.py
index cf848e0..29c7c03 100644
--- a/kronos/parser.py
+++ b/kronos/parser.py
@@ -11,30 +11,45 @@ from kronos.model import Activity
11 11
12 12
13def parse_activity(text): 13def parse_activity(text):
14 tokens = text.split(' ') 14 tag_parser = TagHandler(text)
15 tags = _parse_tags(tokens) 15 text = tag_parser.text
16 text = _strip_tags(tokens)
17 16
18 description = None 17 text, description = _clean_split(text, ',')
19 if ',' in text: 18 text, category = _clean_split(text, '@')
20 text, description = text.split(',', 1)
21 description = description.strip()
22
23 category = None
24 if '@' in text:
25 text, category = text.split('@', 1)
26 category = category.strip()
27 19
28 activity = Activity(text.strip(), description, category) 20 activity = Activity(text.strip(), description, category)
29 activity.tags = tags 21 activity.tags = tag_parser.tags
30 22
31 return activity 23 return activity
32 24
33 25
34def _parse_tags(tokens): 26def _clean_split(text, delimiter):
35 return [token.lstrip('#') for token in tokens if token.startswith('#')] 27 if delimiter in text:
28 text, wanted = text.split(delimiter, 1)
29 return text, wanted.strip()
30 else:
31 return text, None
32
33
34class TagHandler(object):
35
36 def __init__(self, text):
37 self.tokens = text.split(' ')
38
39 def _parse_tags(self):
40 return [token.lstrip('#') for token in self.tokens
41 if token.startswith('#')]
42
43 def _strip_tags(self):
44 words = [token for token in self.tokens
45 if not token.startswith('#')]
46
47 return ' '.join(words)
36 48
49 @property
50 def text(self):
51 return self._strip_tags()
37 52
38def _strip_tags(tokens): 53 @property
39 words = [token for token in tokens if not token.startswith('#')] 54 def tags(self):
40 return ' '.join(words) 55 return self._parse_tags()