From d5205848963be4dac741aea462d3647945205ea8 Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Wed, 15 Aug 2018 12:06:28 +1200 Subject: [PATCH] add icalparser --- .gitignore.d/mutt | 1 + .mutt/icalparser | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 .mutt/icalparser diff --git a/.gitignore.d/mutt b/.gitignore.d/mutt index ce028e0..bfea42a 100644 --- a/.gitignore.d/mutt +++ b/.gitignore.d/mutt @@ -15,6 +15,7 @@ !/.mutt/.gitignore !/.mutt/headers !/.mutt/hooks +!/.mutt/icalparser !/.mutt/keybindings !/.mutt/list-mailboxes !/.mutt/lists diff --git a/.mutt/icalparser b/.mutt/icalparser new file mode 100755 index 0000000..b2b1af3 --- /dev/null +++ b/.mutt/icalparser @@ -0,0 +1,51 @@ +#!/usr/bin/python3 + +import sys +import os +from icalendar import Calendar, Event + +def dtstrs(start, end): + start, end = start.dt, end.dt + + def strftime(dt, strf): + return dt.astimezone().strftime(strf) + + ret = [] + if start.tzinfo == end.tzinfo: + ret.append(strftime(start, '%F %R')) + + else: + ret.append(strftime(start, '%F %R %Z')) + + if start.date() == end.date(): + ret.append(strftime(end, '%T %Z')) + + else: + ret.append(strftime(end, '%F %R %Z')) + + return ret + +def parse_ics_file(fp): + cal = Calendar.from_ical(fp.read()) + + for event in cal.walk(): + if event.name != "VEVENT": continue + print('=' * (int(os.getenv('COLUMNS', 80))-10)) + print('{} → {}'.format(*dtstrs(event.get('dtstart'), event.get('dtend')))) + for i in ('summary','organizer','location','rrule'): + print(' {:9s}: {}'.format(i.capitalize(), event.get(i, f'(no {i})'))) + flags = [] + for k in ('status', 'class', 'transp', 'priority'): + t = event.get(k) + if t: flags.append((k, t)) + print(' Flags : ' + ' '.join('{}:{}'.format(*f) for f in flags)) + +if __name__ == '__main__': + + if len(sys.argv) > 1: + for f in sys.argv[1:]: + fp = open(f) + parse_ics_file(fp) + fp.close() + else: + parse_ics_file(sys.stdin) -- 2.39.5