def test_twoSimpleEvents(self): """ Exporting a calendar with two events in it will result in a VCALENDAR component with both VEVENTs in it. """ yield populateCalendarsFrom( { "user01": { "calendar1": { "valentines-day.ics": (valentines, {}), "new-years-day.ics": (newYears, {}) } } }, self.store) expected = Component.newCalendar() a = Component.fromString(valentines) b = Component.fromString(newYears) for comp in a, b: for sub in comp.subcomponents(): expected.addComponent(sub) io = StringIO() yield exportToFile([(yield self.txn().calendarHomeWithUID("user01") ).calendarWithName("calendar1")], io) self.assertEquals(Component.fromString(io.getvalue()), expected)
def exportToFile(calendars, fileobj): """ Export some calendars to a file as their owner would see them. @param calendars: an iterable of L{ICalendar} providers (or L{Deferred}s of same). @param fileobj: an object with a C{write} method that will accept some iCalendar data. @return: a L{Deferred} which fires when the export is complete. (Note that the file will not be closed.) @rtype: L{Deferred} that fires with C{None} """ comp = Component.newCalendar() for calendar in calendars: calendar = yield calendar for obj in (yield calendar.calendarObjects()): evt = yield obj.filteredComponent(calendar.ownerCalendarHome().uid(), True) for sub in evt.subcomponents(): if sub.name() != 'VTIMEZONE': # Omit all VTIMEZONE components here - we will include them later # when we serialize the whole calendar. comp.addComponent(sub) fileobj.write(comp.getTextWithTimezones(True))
def test_twoSimpleEvents(self): """ Exporting a calendar with two events in it will result in a VCALENDAR component with both VEVENTs in it. """ yield populateCalendarsFrom( { "home1": { "calendar1": { "valentines-day.ics": (valentines, {}), "new-years-day.ics": (newYears, {}) } } }, self.store ) expected = Component.newCalendar() a = Component.fromString(valentines) b = Component.fromString(newYears) for comp in a, b: for sub in comp.subcomponents(): expected.addComponent(sub) io = StringIO() yield exportToFile( [(yield self.txn().calendarHomeWithUID("home1")) .calendarWithName("calendar1")], io ) self.assertEquals(Component.fromString(io.getvalue()), expected)
def exportToFile(calendars, fileobj): """ Export some calendars to a file as their owner would see them. @param calendars: an iterable of L{ICalendar} providers (or L{Deferred}s of same). @param fileobj: an object with a C{write} method that will accept some iCalendar data. @return: a L{Deferred} which fires when the export is complete. (Note that the file will not be closed.) @rtype: L{Deferred} that fires with C{None} """ comp = Component.newCalendar() for calendar in calendars: calendar = yield calendar for obj in (yield calendar.calendarObjects()): evt = yield obj.filteredComponent( calendar.ownerCalendarHome().uid(), True) for sub in evt.subcomponents(): if sub.name() != 'VTIMEZONE': # Omit all VTIMEZONE components here - we will include them later # when we serialize the whole calendar. comp.addComponent(sub) fileobj.write(comp.getTextWithTimezones(True))
def test_oneEventCalendar(self): """ Exporting an calendar with one event in it will result in just that event. """ yield populateCalendarsFrom( { "home1": { "calendar1": { "valentines-day.ics": (valentines, {}) } } }, self.store ) expected = Component.newCalendar() [theComponent] = Component.fromString(valentines).subcomponents() expected.addComponent(theComponent) io = StringIO() yield exportToFile( [(yield self.txn().calendarHomeWithUID("home1")) .calendarWithName("calendar1")], io ) self.assertEquals(Component.fromString(io.getvalue()), expected)
def test_oneEventCalendar(self): """ Exporting an calendar with one event in it will result in just that event. """ yield populateCalendarsFrom( { "user01": { "calendar1": { "valentines-day.ics": (valentines, {}) } } }, self.store ) expected = Component.newCalendar() [theComponent] = Component.fromString(valentines).subcomponents() expected.addComponent(theComponent) io = StringIO() yield exportToFile( [(yield self.txn().calendarHomeWithUID("user01")) .calendarWithName("calendar1")], io ) self.assertEquals(Component.fromString(io.getvalue()), expected)
def exportToDirectory(collections, dirname, convertToMailto=False): """ Export some calendars to a file as their owner would see them. @param calendars: an iterable of L{ICalendar} providers (or L{Deferred}s of same). @param dirname: the path to a directory to store calendar files in; each calendar being exported will have its own .ics file @return: a L{Deferred} which fires when the export is complete. (Note that the file will not be closed.) @rtype: L{Deferred} that fires with C{None} """ for collection in collections: if isinstance(collection, Calendar): homeUID = collection.ownerCalendarHome().uid() calendarProperties = collection.properties() comp = Component.newCalendar() for element, propertyName in ( (davxml.DisplayName, "NAME"), (customxml.CalendarColor, "COLOR"), ): value = calendarProperties.get(PropertyName.fromElement(element), None) if value: comp.addProperty(Property(propertyName, str(value))) source = "/calendars/__uids__/{}/{}/".format(homeUID, collection.name()) comp.addProperty(Property("SOURCE", source)) for obj in (yield collection.calendarObjects()): evt = yield obj.filteredComponent(homeUID, True) for sub in evt.subcomponents(): if sub.name() != 'VTIMEZONE': # Omit all VTIMEZONE components here - we will include them later # when we serialize the whole calendar. if convertToMailto: convertCUAsToMailto(sub) comp.addComponent(sub) filename = os.path.join(dirname, "{}_{}.ics".format(homeUID, collection.name())) with open(filename, 'wb') as fileobj: fileobj.write(comp.getTextWithTimezones(True)) else: # addressbook homeUID = collection.ownerAddressBookHome().uid() filename = os.path.join(dirname, "{}_{}.vcf".format(homeUID, collection.name())) with open(filename, 'wb') as fileobj: for obj in (yield collection.addressbookObjects()): vcard = yield obj.component() fileobj.write(vcard.getText())
def test_emptyCalendar(self): """ Exporting an empty calendar results in an empty calendar. """ io = StringIO() value = yield exportToFile([], io) # it doesn't return anything, it writes to the file. self.assertEquals(value, None) # but it should write a valid component to the file. self.assertEquals(Component.fromString(io.getvalue()), Component.newCalendar())
def test_emptyCalendar(self): """ Exporting an empty calendar results in an empty calendar. """ io = StringIO() value = yield exportToFile([], io) # it doesn't return anything, it writes to the file. self.assertEquals(value, None) # but it should write a valid component to the file. self.assertEquals(Component.fromString(io.getvalue()), Component.newCalendar())
def exportToDirectory(calendars, dirname): """ Export some calendars to a file as their owner would see them. @param calendars: an iterable of L{ICalendar} providers (or L{Deferred}s of same). @param dirname: the path to a directory to store calendar files in; each calendar being exported will have it's own .ics file @return: a L{Deferred} which fires when the export is complete. (Note that the file will not be closed.) @rtype: L{Deferred} that fires with C{None} """ for calendar in calendars: homeUID = calendar.ownerCalendarHome().uid() calendarProperties = calendar.properties() comp = Component.newCalendar() for element, propertyName in ((davxml.DisplayName, "NAME"), (customxml.CalendarColor, "COLOR")): value = calendarProperties.get(PropertyName.fromElement(element), None) if value: comp.addProperty(Property(propertyName, str(value))) source = "/calendars/__uids__/{}/{}/".format(homeUID, calendar.name()) comp.addProperty(Property("SOURCE", source)) for obj in (yield calendar.calendarObjects()): evt = yield obj.filteredComponent(homeUID, True) for sub in evt.subcomponents(): if sub.name() != "VTIMEZONE": # Omit all VTIMEZONE components here - we will include them later # when we serialize the whole calendar. comp.addComponent(sub) filename = os.path.join(dirname, "{}_{}.ics".format(homeUID, calendar.name())) fileobj = open(filename, "wb") fileobj.write(comp.getTextWithTimezones(True)) fileobj.close()