# 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()