comparison gpp/gcalendar/calendar.py @ 68:cce1fc3f8752

#16, GCalendar: clean up exception class. Put a try catch around the ExecuteBatch, as I saw it fail. No need to add any logging.
author Brian Neal <bgneal@gmail.com>
date Sun, 28 Jun 2009 18:48:08 +0000
parents dbd703f7d63a
children 9c18250972d5
comparison
equal deleted inserted replaced
67:4ec8d1d0ccbd 68:cce1fc3f8752
17 17
18 from gcalendar.models import Event 18 from gcalendar.models import Event
19 19
20 20
21 class CalendarError(Exception): 21 class CalendarError(Exception):
22 def __init__(self, errs): 22 def __init__(self, msg):
23 self.errs = errs 23 self.msg = msg
24 24
25 def __str__(self): 25 def __str__(self):
26 return repr(self.errs) 26 return repr(self.msg)
27 27
28 28
29 class Calendar(object): 29 class Calendar(object):
30 DATE_FMT = '%Y-%m-%d' 30 DATE_FMT = '%Y-%m-%d'
31 DATE_TIME_FMT = DATE_FMT + 'T%H:%M:%S' 31 DATE_TIME_FMT = DATE_FMT + 'T%H:%M:%S'
39 self.insert_feed = '/calendar/feeds/%s/private/full' % calendar_id 39 self.insert_feed = '/calendar/feeds/%s/private/full' % calendar_id
40 self.batch_feed = '%s/batch' % self.insert_feed 40 self.batch_feed = '%s/batch' % self.insert_feed
41 try: 41 try:
42 self.client.ProgrammaticLogin() 42 self.client.ProgrammaticLogin()
43 except BadAuthentication: 43 except BadAuthentication:
44 raise CalendarError(['Incorrect password']) 44 raise CalendarError('Incorrect password')
45 except Exception, e: 45 except Exception, e:
46 raise CalendarError([e]) 46 raise CalendarError(str(e))
47 47
48 def sync_events(self, qs): 48 def sync_events(self, qs):
49 request_feed = CalendarEventFeed() 49 request_feed = CalendarEventFeed()
50 for model in qs: 50 for model in qs:
51 if model.status == Event.NEW_APRV: 51 if model.status == Event.NEW_APRV:
58 event = self._retrieve_event(model) 58 event = self._retrieve_event(model)
59 request_feed.AddDelete(entry=event) 59 request_feed.AddDelete(entry=event)
60 else: 60 else:
61 assert False, 'unexpected status in sync_events' 61 assert False, 'unexpected status in sync_events'
62 62
63 response_feed = self.client.ExecuteBatch(request_feed, self.batch_feed) 63 try:
64 response_feed = self.client.ExecuteBatch(request_feed, self.batch_feed)
65 except Exception, e:
66 raise CalendarError('ExecuteBatch exception: %s' % e)
67
64 err_msgs = [] 68 err_msgs = []
65 for entry in response_feed.entry: 69 for entry in response_feed.entry:
66 i = int(entry.batch_id.text) 70 i = int(entry.batch_id.text)
67 code = int(entry.batch_status.code) 71 code = int(entry.batch_status.code)
68 72
84 if error: 88 if error:
85 err_msgs.append('%s - (%d) %s' % \ 89 err_msgs.append('%s - (%d) %s' % \
86 (qs[i].title, code, entry.batch_status.reason)) 90 (qs[i].title, code, entry.batch_status.reason))
87 91
88 if len(err_msgs) > 0: 92 if len(err_msgs) > 0:
89 raise CalendarError(err_msgs) 93 raise CalendarError(', '.join(err_msgs))
90 94
91 def _retrieve_event(self, model): 95 def _retrieve_event(self, model):
92 try: 96 try:
93 event = self.client.GetCalendarEventEntry(model.google_id) 97 event = self.client.GetCalendarEventEntry(model.google_id)
94 except Exception: 98 except Exception, e:
95 raise CalendarError(['Could not retrieve event from Google: %s' % model.what]) 99 raise CalendarError('Could not retrieve event from Google: %s, %s' \
100 % (model.what, e))
96 return event 101 return event
97 102
98 def _populate_event(self, model, event): 103 def _populate_event(self, model, event):
99 """Populates a gdata event from an Event model object.""" 104 """Populates a gdata event from an Event model object."""
100 event.title = atom.Title(text=model.what) 105 event.title = atom.Title(text=model.what)
133 s = d.strftime(self.DATE_TIME_FMT) 138 s = d.strftime(self.DATE_TIME_FMT)
134 else: 139 else:
135 try: 140 try:
136 tz = pytz.timezone(tz_name) 141 tz = pytz.timezone(tz_name)
137 except pytz.UnknownTimeZoneError: 142 except pytz.UnknownTimeZoneError:
138 raise CalendarError(['Invalid time zone: %s' (tz_name,)]) 143 raise CalendarError('Invalid time zone: %s' (tz_name,))
139 local = tz.localize(d) 144 local = tz.localize(d)
140 zulu = local.astimezone(FixedOffset(0)) 145 zulu = local.astimezone(FixedOffset(0))
141 s = zulu.strftime(self.DATE_TIME_TZ_FMT) 146 s = zulu.strftime(self.DATE_TIME_TZ_FMT)
142 147
143 return s 148 return s
144 149
145 # vim: ts=4 sw=4