Logo Search packages:      
Sourcecode: lernid version File versions  Download package


# -*- coding: utf-8 -*-
# Copyright (C) 2009 Jono Bacon <jono@ubuntu.com>
# Copyright (C) 2010 Michael Budde <mbudde@gmail.com>
#This program is free software: you can redistribute it and/or modify it
#under the terms of the GNU General Public License version 3, as published
#by the Free Software Foundation.
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranties of
#PURPOSE.  See the GNU General Public License for more details.
#You should have received a copy of the GNU General Public License along
#with this program.  If not, see <http://www.gnu.org/licenses/>.

import re
import vobject
import urllib

import lernid.DateTime as dt

class Session(object):

    PAST, NOW, FUTURE = range(3)

    def __init__(self, **kwargs):
        for k, v in kwargs.iteritems():
            if k in ('title', 'description', 'instructors', 'local_start', 'local_end', 'slides', 'event'):
                setattr(self, '_'+k, v)

    def title(self):
        return self._title

    def description(self):
        return self._description

    def instructors(self):
        return self._instructors

    def start_local_date(self):
        # Translators: Local date representation
        return self._local_start.strftime(_('%d %B %Y'))

    def start_local_time(self):
        # Translators: Local time representation
        return self._local_start.strftime(_('%H:%M'))

    def end_local_time(self):
        # Translators: Local time representation
        return self._local_end.strftime(_('%H:%M'))

    def start_datetime(self):
        return self._local_start

    def end_datetime(self):
        return self._local_end

    def slides(self):
        return getattr(self, '_slides', None)

    def evenet(self):
        return self._event

    def state(self):
        now, start, end = dt.now_local(), self.start_datetime, self.end_datetime
        if now < start:
            return self.FUTURE
        if start < now < end:
            return self.NOW
        if now > end:
            return self.PAST

def parse_ical(event):
    """Parse iCal schedule for event and generate a list of Session objects"""

    ical = urllib.urlopen(event.icalurl)
    cal = vobject.readOne(ical)

    sessions = []

    eventstart_local = dt.parse_time(event.eventstart, '%Y-%m-%d %H:%M:%S')
    eventend_local = dt.parse_time(event.eventend, '%Y-%m-%d %H:%M:%S')

    for session in cal.vevent_list:
        local_start = dt.as_local(session.dtstart.value)
        local_end = dt.as_local(session.dtend.value)

        if eventstart_local < local_start < eventend_local:
            if hasattr(session, "description"):
                session_data = parse_ical_description(session.description.value)
                title = session.summary.value,
                local_start = local_start,
                local_end = local_end,

    # reverse the list to get the events in chronological order
    sessions.sort(lambda x,y: cmp(x.start_datetime, y.start_datetime))
    return sessions

def parse_ical_description(desc):
    session = {}
    for line in desc.splitlines():
        items = [s.strip() for s in line.split(None, 1)]
        if len(items) != 2:
        key, value = items
        key = key[:-1].lower()
        if key in ('description', 'slides', 'event'):
            session[str(key)] = value
        elif key == 'instructor' or key == 'instructors':
            instructors = [s.strip() for s in value.split(',')]
            session['instructors'] = instructors
    return session

Generated by  Doxygen 1.6.0   Back to index