summaryrefslogtreecommitdiff
path: root/kronos/parser.py
blob: 29c7c03f125c8ed365b97937673aba3f5ae8e385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# vim: set filencoding=utf8
"""
Activity Parser

@author: Mike Crute (mcrute@ag.com)
@organization: American Greetings Interactive
@date: February 04, 2010
"""

from kronos.model import Activity


def parse_activity(text):
    tag_parser = TagHandler(text)
    text = tag_parser.text

    text, description = _clean_split(text, ',')
    text, category = _clean_split(text, '@')

    activity = Activity(text.strip(), description, category)
    activity.tags = tag_parser.tags

    return activity


def _clean_split(text, delimiter):
    if delimiter in text:
        text, wanted = text.split(delimiter, 1)
        return text, wanted.strip()
    else:
        return text, None


class TagHandler(object):

    def __init__(self, text):
        self.tokens = text.split(' ')

    def _parse_tags(self):
        return [token.lstrip('#') for token in self.tokens
                if token.startswith('#')]

    def _strip_tags(self):
        words = [token for token in self.tokens
                    if not token.startswith('#')]

        return ' '.join(words)

    @property
    def text(self):
        return self._strip_tags()

    @property
    def tags(self):
        return self._parse_tags()