import time import calendar import SOAPpy import mws from SOAPpy.Types import faultType, voidType, simplify, booleanType class CalendarMWS(mws.mwsAuthentication,mws.mwsGeneral): version = "v1.0" author = "Magnus Agust Skulason, maggias@gmail.com" def getEntriesInMonth(self, year, month, resultFilter = ""): # Fetch entries in the specified month try: intYear = 0 intMonth = 0 intYear = int(year) intMonth = int(month) output = [] # Open the calendar database cal = calendar.open() month = time.mktime((intYear,intMonth,1, 0,0,0, 0,0,0)) #execute the search entriesList = cal.monthly_instances(month) #go through the entries list for n in range(0, len(entriesList)): entryObject = cal[entriesList[n]['id']] itemOut = self.__createOutputItem__(entryObject, resultFilter) output.append(itemOut) return output except Exception, e: raise faultType("SOAP-ENV:Server","Fetching calendar entries failed.",e) def getEntriesAtDay(self, year, month, day, resultFilter = ""): # Fetch entries on the specified day try: intYear = int(year) intMonth = int(month) intDay = int(day) output = [] # Open the calendar database cal = calendar.open() # Day day = time.mktime((intYear,intMonth,intDay, 0,0,0, 0,0,0)) #execute the search entriesList = cal.daily_instances(day) #go through the entries list for n in range(0, len(entriesList)): entryObject = cal[entriesList[n]['id']]; itemOut = self.__createOutputItem__(entryObject, resultFilter) output.append(itemOut) return output except Exception, e: raise faultType("SOAP-ENV:Server","Fetching calendar entries failed.",e) def insertEntry(self, entryDetails): # Insert the entry specified in entryDetails (id,content,location,start_time,end_time,id,last_modified,alarm,priority,replication,as_vcalendar,type,cross_out_time,crossed_out) = self.__parseEntryDetails__(entryDetails) try: # Open the calendar database cal = calendar.open() calEntry = None if type == 'appointment': calEntry = cal.add_appointment() elif type == 'event': calEntry = cal.add_event() elif type == 'anniversary': calEntry = cal.add_anniversary() elif type == 'todo': calEntry = cal.add_todo() else: raise faultType("SOAP-ENV:Server","Creating calendar entry failed.","Unsuported type") if calEntry != None: if content != None: calEntry.content = content if location != None and location != "": calEntry.location = location if end_time != None and end_time != "": calEntry.set_time(start_time,end_time) else: calEntry.set_time(start_time) if alarm != None: calEntry.alarm = alarm #float if priority != None: calEntry.priority = priority #(1 = high, 2 = normal, 3 = low) else: calEntry.priority = 2 if type == 'todo': if crossed_out != None: calEntry.crossed_out = crossed_out # ( TRUE || FALSE ) else: calEntry.crossed_out = False if replication != None: calEntry.replication = replication # open, private, restricted else: calEntry.replication = 'private' calEntry.commit() itemOut = {} itemOut['id'] = calEntry.id itemOut['lastModified'] = SOAPpy.dateTimeType(calEntry.last_modified) return itemOut else: # Raise an exception raise faultType("SOAP-ENV:Server","Creating calendar entry failed.","type incorrect or missing") except Exception, e: raise faultType("SOAP-ENV:Server","Creating calendar entry failed.",e) def updateEntry(self, entryDetails): # Update the entry specified in entryDetails (id,content,location,start_time,end_time,id,last_modified,alarm,priority,replication,as_vcalendar,type,cross_out_time,crossed_out) = self.__parseEntryDetails__(entryDetails) try: # Open the calendar database cal = calendar.open() calEntry = None try: intId = int(id) calEntry = cal.__getitem__(intId) except Exception, e: print "here" print e raise faultType("SOAP-ENV:Server","Updating calendar entry failed.",e) if calEntry != None: print "here3" calEntry.begin() if content != None: calEntry.content = content print "here4" if location != None and location != "": calEntry.location = location print "here5" if end_time != None and end_time != "": calEntry.set_time(start_time,end_time) else: calEntry.set_time(start_time) print "here6" if alarm != None: calEntry.alarm = alarm #float print "here7" if priority != None: calEntry.priority = priority #(1 = high, 2 = normal, 3 = low) else: calEntry.priority = 2 print "here8" if type == 'todo': if crossed_out != None: calEntry.crossed_out = crossed_out # ( TRUE || FALSE ) else: calEntry.crossed_out = False print "here9" if replication != None and replication != "": print "replication" print replication calEntry.replication = replication # open, private, restricted else: print "else" calEntry.replication = 'private' print "here10" calEntry.commit() print "here11" return SOAPpy.dateTimeType(calEntry.last_modified) print "here12" else: # Raise an exception raise faultType("SOAP-ENV:Server","Updating calendar entry failed.","entry not found") except Exception, e: print "here2" print e raise faultType("SOAP-ENV:Server","Updating calendar entry failed.",e) def deleteEntry(self, id): # delete the entry wiht the specified id try: # Open the calendar database cal = calendar.open() try: cal.__delitem__(int(id)) except Exception, e: raise faultType("SOAP-ENV:Server","Deleting calendar entry failed.",e) except Exception, e: raise faultType("SOAP-ENV:Server","Deleting calendar entry failed.",e) return SOAPpy.Types.booleanType(True) def insertVCalendar(self, vcalendar): # Import the entries specified by the VCalendar string import calendar from SOAPpy.Types import faultType, voidType, simplify # The underlying PyS60 function dose not seem to work correctly, I haven't been able to successfully import any VCalendar string and I always get an empty array back # Successfully a array of integers should be returned, ids of the imported entries. # I also tested running test script 8 from http://www.mobilenin.com/pys60/resources/test_calendar.py on my device and nothing got imported. importedEntries = None try: # Open the calendar database cal = calendar.open() try: importedEntries = cal.import_vcalendars(vcalendar.encode('ascii','replace')) except Exception, e: raise faultType("SOAP-ENV:Server", "Importing vcalendar string failed", e) except Exception, e: raise faultType("SOAP-ENV:Server", "Importing vcalendar string failed",e) return importedEntries def getEntry(self, id, resultFilter = ""): # Get details about the entry with the specified id try: intId = int(id) # Open calendar database cal = calendar.open() calEntry = cal.__getitem__(int(id)) itemOut = self.__createOutputItem__(calEntry, resultFilter) return itemOut except Exception, e: raise faultType("SOAP-ENV:Server","getEntry entries failed.",e) def getEntries(self, startDate, endDate, searchStr = '', resultFilter = ""): # Get entries satisfying the search conditions try: #convert start_date and end_date floatStart_date = self.__dateTimeToEpoc__(startDate) floatEnd_date = self.__dateTimeToEpoc__(endDate) # convert the search string to unicode search_str = unicode(searchStr) # Open the calendar database cal = calendar.open() #execute the search entriesList = cal.find_instances(floatStart_date, floatEnd_date, search_str) output = [] for n in range(0, len(entriesList)): entryObject = cal[entriesList[n]['id']]; itemOut = self.__createOutputItem__(entryObject, resultFilter) output.append(itemOut) return output except Exception, e: raise faultType("SOAP-ENV:Server","getEntries entries failed.",e) def __parseEntryDetails__(self, entryDetails): # Converts an incomming calendarEntry XML Schema type to its python variables id = None content = None location = None start_time = None end_time = None id = None last_modified = None alarm = None priority = None replication = None as_vcalendar = None entryType = None cross_out_time = None crossed_out = None if 'id' in entryDetails._keyord: id = entryDetails['id'] if 'content' in entryDetails._keyord: content = entryDetails['content'] if 'location' in entryDetails._keyord: location = entryDetails['location'] if 'startTime' in entryDetails._keyord: start_timeValue = entryDetails['startTime'] #start_time = time.mktime((int(start_timeValue.split("T")[0].split("-")[0]),int(start_timeValue.split("T")[0].split("-")[1]),int(start_timeValue.split("T")[0].split("-")[2]), int(start_timeValue.split("T")[1].split(":")[0]),int(start_timeValue.split("T")[1].split(":")[1]),int(start_timeValue.split("T")[1].split(":")[2].split(".")[0]), 0,0,0)) start_time = self.__dateTimeToEpoc__(start_timeValue) if 'endTime' in entryDetails._keyord: end_timeValue = entryDetails['endTime'] #end_time = time.mktime((int(end_timeValue.split("T")[0].split("-")[0]),int(end_timeValue.split("T")[0].split("-")[1]),int(end_timeValue.split("T")[0].split("-")[2]), int(end_timeValue.split("T")[1].split(":")[0]),int(end_timeValue.split("T")[1].split(":")[1]),int(end_timeValue.split("T")[1].split(":")[2].split(".")[0]), 0,0,0)) end_time = self.__dateTimeToEpoc__(end_timeValue) if 'id' in entryDetails._keyord: id = entryDetails['id'] if 'lastModified' in entryDetails._keyord: last_modifiedValue = entryDetails['lastModified'] last_modified = self.__dateTimeToEpoc__(last_modifiedValue) if 'alarm' in entryDetails._keyord: alarmValue = entryDetails['alarm'] #alarm = time.mktime((int(alarmValue.split("T")[0].split("-")[0]),int(alarmValue.split("T")[0].split("-")[1]),int(alarmValue.split("T")[0].split("-")[2]), int(alarmValue.split("T")[1].split(":")[0]),int(alarmValue.split("T")[1].split(":")[1]),int(alarmValue.split("T")[1].split(":")[2].split(".")[0]), 0,0,0)) alarm = self.__dateTimeToEpoc__(alarmValue) if 'priority' in entryDetails._keyord: priority = int(entryDetails['priority']) if 'replication' in entryDetails._keyord: replication = entryDetails['replication'] if 'asVcalendar' in entryDetails._keyord: as_vcalendar = entryDetails['asVcalendar'] if 'entryType' in entryDetails._keyord: entryType = entryDetails['entryType'] if 'crossOutTime' in entryDetails._keyord: cross_out_time = entryDetails['crossOutTime'] if 'crossedOut' in entryDetails._keyord: crossed_out = entryDetails['crossedOut'] return (id,content,location,start_time,end_time,id,last_modified,alarm,priority,replication,as_vcalendar,entryType,cross_out_time,crossed_out) def __createOutputItem__(self, entryObject, resultFilter): print "getReturnSOAP" print self.getReturnSOAP() # Creates a PyS60 Calendar Entry Object to the XML Shcema type calendarEntry itemOut = {} if resultFilter == "" or resultFilter.find('content') != -1: itemOut['content'] = entryObject.content #if resultFilter == "" or resultFilter.find('get_repeat') != -1: # itemOut['get_repeat'] = entryObject.get_repeat() if resultFilter == "" or resultFilter.find('location') != -1: itemOut['location'] = entryObject.location if resultFilter == "" or resultFilter.find('startTime') != -1: if (self.getReturnSOAP()): itemOut['startTime'] = SOAPpy.dateTimeType(entryObject.start_time) else: itemOut['startTime'] = entryObject.start_time if resultFilter == "" or resultFilter.find('endTime') != -1: if (self.getReturnSOAP()): itemOut['endTime'] = SOAPpy.dateTimeType(entryObject.end_time) else: itemOut['endTime'] = entryObject.end_time if resultFilter == "" or resultFilter.find('id') != -1: itemOut['id'] = entryObject.id if resultFilter == "" or resultFilter.find('lastModified') != -1: if (self.getReturnSOAP()): itemOut['lastModified'] = SOAPpy.dateTimeType(entryObject.last_modified) else: itemOut['lastModified'] = entryObject.last_modified if resultFilter == "" or resultFilter.find('alarm') != -1: if (self.getReturnSOAP()): itemOut['alarm'] = SOAPpy.dateTimeType(entryObject.alarm) else: itemOut['alarm'] = entryObject.alarm if resultFilter == "" or resultFilter.find('priority') != -1: itemOut['priority'] = entryObject.priority #if resultFilter == "" or resultFilter.find('crossed_out') != -1: # itemOut['crossed_out'] = False if resultFilter == "" or resultFilter.find('replication') != -1: itemOut['replication'] = entryObject.replication if resultFilter == "" or resultFilter.find('asVcalendar') != -1: itemOut['asVcalendar'] = entryObject.as_vcalendar() # Get the entryType if (str(type(entryObject)) == str("")): if resultFilter == "" or resultFilter.find('entryType') != -1: itemOut['entryType'] = "appointment" elif (str(type(entryObject)) == str("")): if resultFilter == "" or resultFilter.find('entryType') != -1: itemOut['entryType'] = "event" elif (str(type(entryObject)) == str("")): if resultFilter == "" or resultFilter.find('entryType') != -1: itemOut['entryType'] = "anniversary" elif (str(type(entryObject)) == str("")): if resultFilter == "" or resultFilter.find('entryType') != -1: itemOut['entryType'] = "todo" if resultFilter == "" or resultFilter.find('crossedOut') != -1: if entryObject.crossed_out == 1: if (self.getReturnSOAP()): itemOut['crossedOut'] = SOAPpy.Types.booleanType(True) else: itemOut['crossedOut'] = True else: if (self.getReturnSOAP()): itemOut['crossedOut'] = SOAPpy.Types.booleanType(False) else: itemOut['crossedOut'] = False if resultFilter == "" or resultFilter.find('crossOutTime') != -1: itemOut['crossOutTime'] = entryObject.cross_out_time elif (str(type(entryObject)) == str("")): if resultFilter == "" or resultFilter.find('entryType') != -1: itemOut['entryType'] = "todolist" else: if resultFilter == "" or resultFilter.find('todoList') != -1: itemOut['entryType'] = "" return itemOut def __dateTimeToEpoc__(self, input): # Converts an incomming dateTime string to epoc seconds return time.mktime((int(input.split("T")[0].split("-")[0]),int(input.split("T")[0].split("-")[1]),int(input.split("T")[0].split("-")[2]), int(input.split("T")[1].split(":")[0]),int(input.split("T")[1].split(":")[1]),int(input.split("T")[1].split(":")[2].split(".")[0]), 0,0,0))