summaryrefslogtreecommitdiff
path: root/kronos/parser.py
blob: 147682f9d5dbf5f7771057ae19c68cd35da2773d (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
# 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):
    description = None
    tags = []
    if ',' in text:
        text, description = text.split(',', 1)
        description = description.strip()

        tokens = description.split(' ')
        tags = _parse_tags(tokens)
        description = _strip_tags(tokens)

    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)