]> git.madduck.net Git - etc/mutt.git/blob - .mutt/icalparser

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

show uid for events
[etc/mutt.git] / .mutt / icalparser
1 #!/usr/bin/python3
2
3 import sys
4 import os
5 from icalendar import Calendar, Event
6
7 def dtstrs(start, end):
8     start, end = start.dt, end.dt
9
10     def strftime(dt, strf):
11         return dt.astimezone().strftime(strf)
12
13     ret = []
14     if start.tzinfo == end.tzinfo:
15         ret.append(strftime(start, '%F %R'))
16
17     else:
18         ret.append(strftime(start, '%F %R %Z'))
19
20     if start.date() == end.date():
21         ret.append(strftime(end, '%T %Z'))
22
23     else:
24         ret.append(strftime(end, '%F %R %Z'))
25
26     return ret
27
28 def parse_ics_file(fp):
29     cal = Calendar.from_ical(fp.read())
30
31     for event in cal.walk():
32         if event.name != "VEVENT": continue
33         print('=' * (int(os.getenv('COLUMNS', 80))-10))
34         print('{} → {}'.format(*dtstrs(event.get('dtstart'), event.get('dtend'))))
35         for i in ('summary','organizer','location','uid','rrule'):
36             print('  {:9s}: {}'.format(i.capitalize(), event.get(i, f'(no {i})')))
37         flags = []
38         for k in ('status', 'class', 'transp', 'priority'):
39             t = event.get(k)
40             if t: flags.append((k, t))
41         print('  Flags    : ' + ' '.join('{}:{}'.format(*f) for f in flags))
42
43 if __name__ == '__main__':
44
45     if len(sys.argv) > 1:
46         for f in sys.argv[1:]:
47             fp = open(f)
48             parse_ics_file(fp)
49             fp.close()
50     else:
51         parse_ics_file(sys.stdin)