summaryrefslogtreecommitdiff
path: root/kronos/parser.py
blob: cf848e0031eef4732e3e748bf6e1917834d4fc12 (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
# 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):
    tokens = text.split(' ')
    tags = _parse_tags(tokens)
    text = _strip_tags(tokens)

    description = None
    if ',' in text:
        text, description = text.split(',', 1)
        description = description.strip()

    category = None
    if '@' in text:
        text, category = text.split('@', 1)
        category = category.strip()

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

    return activity


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


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