Created
May 13, 2013 09:27
-
-
Save jimr/5567162 to your computer and use it in GitHub Desktop.
Pretty formatting of meeting invites (for use in mutt etc).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# To parse ics attachments in mutt, put this in your ~/.mailcap: | |
# | |
# text/calendar; /path/to/ical_print.py; copiousoutput | |
# | |
# Otherwise, you can either pass a an .ics file as a parameter, or pipe | |
# something to it on stdin. | |
# | |
# Requires icalendar & unideocode: | |
# | |
# % pip install icalendar unidecode | |
import fileinput | |
from unidecode import unidecode | |
from icalendar import Calendar, Event | |
from icalendar.prop import vCalAddress, vDDDTypes | |
def _fmt_event_attr(event, thing, label=None): | |
label = label or thing | |
val = event.get(thing, '') | |
if isinstance(val, vDDDTypes): | |
val = str(val.dt.strftime('%A %d %B - %H:%M')) | |
elif isinstance(val, vCalAddress): | |
val = val.params.get('CN') | |
else: | |
val = unidecode(unicode(val)) | |
return '%s: %s' % (label.ljust(12), val) | |
def print_calendar(cal_str): | |
cal = Calendar.from_ical(cal_str) | |
for part in cal.walk(): | |
if isinstance(part, Event): | |
print(_fmt_event_attr(part, 'organizer')) | |
print(_fmt_event_attr(part, 'summary')) | |
print(_fmt_event_attr(part, 'dtstart', 'start')) | |
print(_fmt_event_attr(part, 'dtend', 'end')) | |
print(_fmt_event_attr(part, 'location')) | |
print(_fmt_event_attr(part, 'contacts')) | |
print(_fmt_event_attr(part, 'description')) | |
if __name__ == '__main__': | |
lines = [] | |
for line in fileinput.input(): | |
lines.append(line) | |
print_calendar('\n'.join(lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment