Merge lp:~timfreund/python-snippets/calendar-snippets into lp:~jonobacon/python-snippets/trunk

Proposed by Tim Freund
Status: Needs review
Proposed branch: lp:~timfreund/python-snippets/calendar-snippets
Merge into: lp:~jonobacon/python-snippets/trunk
Diff against target: 101 lines (+80/-0)
3 files modified
CATEGORIES (+1/-0)
calendar/createevent.py (+47/-0)
calendar/readical.py (+32/-0)
To merge this branch: bzr merge lp:~timfreund/python-snippets/calendar-snippets
Reviewer Review Type Date Requested Status
Jono Bacon Pending
Review via email: mp+25854@code.launchpad.net

Description of the change

Two snippets: one reads an iCal calendar from the network and iterates through the events, and the other takes an iCal VEVENT and adds it to the user's Evolution calendar.

To post a comment you must log in.

Unmerged revisions

101. By Tim Freund

Create an event in the local user's Evolution calendar

100. By Tim Freund

Read an iCal calendar and iterate through the events.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CATEGORIES'
2--- CATEGORIES 2010-04-24 18:34:32 +0000
3+++ CATEGORIES 2010-05-23 19:45:36 +0000
4@@ -15,6 +15,7 @@
5 Application Indicator Application indicator examples.
6 bzrlib Bazaar source control system Python module.
7 Cairo Cairo drawing examples
8+ calendar Calendar examples
9 Clutter Clutter toolkit examples.
10 csv csv Python module, reading and writing CSV files
11 dbus dbus messaging system
12
13=== added directory 'calendar'
14=== added file 'calendar/createevent.py'
15--- calendar/createevent.py 1970-01-01 00:00:00 +0000
16+++ calendar/createevent.py 2010-05-23 19:45:36 +0000
17@@ -0,0 +1,47 @@
18+#!/usr/bin/env python
19+#
20+# [SNIPPET_NAME: Create a Calendar Event]
21+# [SNIPPET_CATEGORIES: calendar]
22+# [SNIPPET_DESCRIPTION: Create a Calendar Event in your local Evolution calendar]
23+# [SNIPPET_AUTHOR: Tim Freund <tim@freunds.net>]
24+# [SNIPPET_LICENSE: MIT]
25+
26+
27+
28+try:
29+ from evolution import ecal
30+except ImportError:
31+ print """The python-evolution package is not installed.
32+On Ubuntu? Run the following: 'sudo apt-get install python-evolution'"""
33+
34+# Find the user's 'Personal' calendar
35+for cal_meta in ecal.list_calendars():
36+ # cal_meta[0] is the calendar name
37+ # cal_meta[1] is the calendar url
38+ if cal_meta[0] == 'Personal':
39+ print "Opening %s" % cal_meta[1]
40+ calendar = ecal.open_calendar_source(cal_meta[1], ecal.CAL_SOURCE_TYPE_EVENT)
41+
42+# Get an ical VEVENT string. See the 'Read an iCal calendar' snippet
43+# for an example of downloading an iCal calendar from the network.
44+ical_string = """BEGIN:VEVENT
45+UID:fq2vt055fu3rjv24t32mv8fv80@google.com
46+DTSTART:20100521T140000Z
47+DTEND:20100521T150000Z
48+CLASS:PUBLIC
49+CREATED:20100523T185147Z
50+DESCRIPTION:Discussing the status of the Acire calendar snippets.
51+DTSTAMP:20100523T190209Z
52+LAST-MODIFIED:20100523T185636Z
53+LOCATION:14 West 10 Street\, Kansas City\, MO 64105
54+SEQUENCE:4
55+STATUS:CONFIRMED
56+SUMMARY:Status Meeting
57+TRANSP:OPAQUE
58+END:VEVENT
59+"""
60+
61+event = ecal.ECalComponent(ecal.CAL_COMPONENT_EVENT, ical_string)
62+# events added will show up immediately in the Evolution UI
63+calendar.add_object(event)
64+
65
66=== added file 'calendar/readical.py'
67--- calendar/readical.py 1970-01-01 00:00:00 +0000
68+++ calendar/readical.py 2010-05-23 19:45:36 +0000
69@@ -0,0 +1,32 @@
70+#!/usr/bin/env python
71+#
72+# [SNIPPET_NAME: Read an iCal calendar]
73+# [SNIPPET_CATEGORIES: calendar]
74+# [SNIPPET_DESCRIPTION: Read an iCal calendar with python-vobject]
75+# [SNIPPET_AUTHOR: Tim Freund <tim@freunds.net>]
76+# [SNIPPET_LICENSE: MIT]
77+
78+try:
79+ import vobject
80+except ImportError:
81+ print """The vobject package is not installed.
82+On Ubuntu? Run the following: 'sudo apt-get install python-vobject'
83+Other users can find the package here: http://vobject.skyhouseconsulting.com/"""
84+
85+# First we need the ical document. We will use urllib2 to download
86+# one from the net.
87+from urllib2 import urlopen
88+ical_string = urlopen('http://tim.freunds.net/media/files/python-snippets/acire.ical').read()
89+
90+# create a vcalendar object from the ical string:
91+calendar = vobject.readOne(ical_string)
92+
93+# print event details for each event in the calendar
94+for vevent in calendar.vevent_list:
95+ print "Event: %s" % vevent.summary.value
96+ print "\tLocated at: %s" % vevent.location.value
97+ print "\tStart time: %s" % vevent.dtstart.value.strftime("%Y-%m-%d %H:%M")
98+ print "\tEnd time: %s" % vevent.dtend.value.strftime("%Y-%m-%d %H:%M")
99+
100+ # ical vevent formatted strings can be retrieved by calling the serialize method:
101+ # print vevent.serialize()

Subscribers

People subscribed via source and target branches