Skip to content

Commit

Permalink
Merge pull request #770 from insanum/import-empty
Browse files Browse the repository at this point in the history
Fix crash when importing empty ICS, add summary message w/ event count
  • Loading branch information
dbarnett authored Sep 22, 2024
2 parents 82744ae + a9075a1 commit 6ba8de1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions gcalcli/gcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,8 @@ def ImportICS(self, verbose=False, dump=False, reminders=None,
'--use-legacy-import to restore the old behavior.\n\n')
time.sleep(1)

cal = self.cals[0]
imported_cnt = 0
for event in events_to_import:
if not event:
continue
Expand Down Expand Up @@ -1609,11 +1611,11 @@ def ImportICS(self, verbose=False, dump=False, reminders=None,
# Import event
import_method = (
self.get_events().import_ if (
self._event_should_use_new_import_api(event, self.cals[0]))
self._event_should_use_new_import_api(event, cal))
else self.get_events().insert)
try:
new_event = self._retry_with_backoff(
import_method(calendarId=self.cals[0]['id'], body=event))
import_method(calendarId=cal['id'], body=event))
except HttpError as e:
try:
is_skipped_dupe = any(detail.get('reason') == 'duplicate'
Expand All @@ -1635,8 +1637,12 @@ def ImportICS(self, verbose=False, dump=False, reminders=None,
self.printer.msg(f'Event details: {event}\n')
self.printer.debug_msg(f'Error details: {e}\n')
else:
imported_cnt += 1
hlink = new_event.get('htmlLink')
self.printer.msg(f'New event added: {hlink}\n', 'green')

# TODO: return the number of events added
self.printer.msg(
f"Added {imported_cnt} events to calendar {cal['id']}\n"
)

return True
5 changes: 4 additions & 1 deletion gcalcli/ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ def get_events(

events: list[Optional[EventBody]] = []
for v in vobject.readComponents(ics):
# Strangely, in empty calendar cases vobject sometimes returns
# Components with no vevent_list attribute at all.
vevents = getattr(v, 'vevent_list', [])
events.extend(
CreateEventFromVOBJ(
ve, verbose=verbose, default_tz=default_tz, printer=printer
)
for ve in v.vevent_list
for ve in vevents
)
return events

Expand Down

0 comments on commit 6ba8de1

Please sign in to comment.