summaryrefslogtreecommitdiff
path: root/kronos/parser.py
blob: f0543f9d4568c1aa1edec20fd839e75209f73568 (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
# vim: set filencoding=utf8
"""
Activity Parser

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

class Activity(object):

    def __init__(self, activity, description=None, category=None):
        self.activity = activity
        self.description = description
        self.category = category
        self.tags = []


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)