| Author: | Jeffrey Harris |
|---|---|
| Date: | 2006-02-10 |
You may have heard of vCard and vCalendar.
You probably asked yourself:
Blame it on the Versit consortium.
Who uses the various standards?
These formats have similar, odd, syntax.
Attempting to avoid NIH syndrome, looked for existing options.
They tended to be:
So I had a thought.
A library for contact and event data.
Lots of Python iCalendar code written simultaneously.
iCalendar maps nicely to XML.
BEGIN:VEVENT DTSTART;TZID=US/Pacific:200602092000 END:VEVENT <VEVENT> <DTSTART TZID="US/Pacific">200602092000</DTSTART> </VEVENT>
Why didn't they just use XML?
vobject parses iCalendar-style syntax
At this point, the tree contains:
We want:
Can they transform themselves?
Items in the tree can be assigned behaviors.
Once a behavior is applied, a component:
Behaviors provide a transformToNative method.
BayPiggies.ics
BEGIN:VCALENDAR VERSION:2.0 PRODID:Irrelevant BEGIN:VTIMEZONE TZID:US/Pacific ... [elided for brevity] END:VTIMEZONE BEGIN:VEVENT SUMMARY:BayPiggies UID:1234XYZ@foobar DTSTART;TZID=US/Pacific:20060209T193000 DTEND;TZID=US/Pacific:20060209T210000 RRULE:FREQ=MONTHLY;BYDAY=+2TH END:VEVENT END:VCALENDAR
>>> import vobject
>>> f = file('BayPiggies.ics')
>>> cal = vobject.readOne(f)
>>> ev = cal.vevent
>>> ev.summary
<SUMMARY{}BayPiggies>
>>> ev.summary.value
u'BayPiggies'
>>> start = cal.vevent.dtstart >>> print start.value 2006-02-09 19:30:00-08:00 >>> tz = start.value.tzinfo >>> tz <tzicalvtz 'US/Pacific'> >>> cal.vevent.dtend.value - start.value datetime.timedelta(0, 5400)
>>> cal.vevent.rruleset <dateutil.rrule.rruleset instance at 0x4e014c> >>> for dt in cal.vevent.rruleset[:3]: ... print dt 2006-02-09 19:30:00-08:00 2006-03-09 19:30:00-08:00 2006-04-13 19:30:00-07:00
Of course, you can go the other direction.
>>> import vobject
>>> cal = vobject.iCalendar()
>>> ev = cal.add('vevent')
>>> start = ev.add('dtstart')
>>> from datetime import datetime
>>> start.value = datetime(2006,2,9,7,30, tzinfo = tz)
>>> ev.add('summary').value = "BayPiggies"
>>> cal.prettyPrint()
VCALENDAR
VEVENT
DTSTART: 2006-02-09 07:30:00-08:00
SUMMARY: BayPiggies
Walking through the output:
>>> lines = cal.serialize().splitlines() >>> print '\n'.join(lines[0:3]) BEGIN:VCALENDAR VERSION:2.0 PRODID:-//PYVOBJECT//NONSGML Version 1//EN
A VTIMEZONE is calculated from the tzinfo class.
>>> print '\n'.join(lines[3:12]) BEGIN:VTIMEZONE TZID:US/Pacific BEGIN:STANDARD DTSTART:20001029T020000 RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10 TZNAME:US/Pacific TZOFFSETFROM:-0700 TZOFFSETTO:-0800 END:STANDARD
UID is required, so VEVENT's behavior generated one.
>>> print '\n'.join(lines[20:]) BEGIN:VEVENT UID:20060210T024827Z-20213@Wind DTSTART;TZID=US/Pacific:20060209T073000 SUMMARY:BayPiggies END:VEVENT END:VCALENDAR
Working with multiple VEVENTs
More powerful recurrence expansion