#!/usr/bin/env python

import rclexecm
import sys

try:
    from icalendar import Calendar, Event
except:
    print "RECFILTERROR HELPERNOTFOUND python:icalendar"
    sys.exit(1);

class IcalExtractor:
    def __init__(self, em):
        self.file = ""
	self.contents = []
        self.em = em
        em.setmimetype("text/plain")

    def extractone(self, index):
        if index >= len(self.contents):
            return(False, "", "", True)
        docdata = self.contents[index].as_string()
	#self.em.rclog(docdata)

        iseof = rclexecm.RclExecM.noteof
        if self.currentindex >= len(self.contents) -1:
            iseof = rclexecm.RclExecM.eofnext
        return (True, docdata, str(index), iseof)

    ###### File type handler api, used by rclexecm ---------->
    def openfile(self, params):
        self.file = params["filename:"]
        try:
            self.cal = Calendar.from_string(open(self.file,'rb').read())
        except:
            return False
        # Skip the top level object
        self.currentindex = 1
        self.contents = self.cal.walk()
        return True

    def getipath(self, params):
        try:
            index = int(params["ipath:"])
        except:
            return False
        return self.extractone(index)
        
    def getnext(self, params):
        if self.currentindex >= len(self.contents):
            #em.rclog("getnext: EOF hit")
            return (False, "", "", rclexecm.RclExecM.eofnow)
        else:
            ret= self.extractone(self.currentindex)
            self.currentindex += 1
            return ret

e = rclexecm.RclExecM()
e.mainloop(IcalExtractor(e))
