]> 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:

prettify icalparser output
[etc/mutt.git] / .mutt / icalparser
1 #!/usr/bin/python3
2
3 import sys
4 import os
5 from icalendar import Calendar, Event
6 from textwrap import fill, indent
7
8 FIELDS = ('summary','location','organiser',
9           'flags','uid','rrule')
10 COLSEP = ': '
11
12 COLUMNS = int(os.getenv('COLUMNS', 80))-10
13 WIDTH = max(len(i) for i in FIELDS)
14 INDENT = WIDTH + len(COLSEP)
15
16 def dtstrs(start, end):
17     start, end = start.dt, end.dt
18
19     def strftime(dt, strf):
20         return dt.astimezone().strftime(strf)
21
22     ret = []
23     if start.tzinfo == end.tzinfo:
24         ret.append(strftime(start, '%F %R'))
25
26     else:
27         ret.append(strftime(start, '%F %R %Z'))
28
29     if start.date() == end.date():
30         ret.append(strftime(end, '%T %Z'))
31
32     else:
33         ret.append(strftime(end, '%F %R %Z'))
34
35     return ret
36
37 def parse_ics_file(fp):
38     cal = Calendar.from_ical(fp.read())
39
40     for event in cal.walk():
41         if event.name != "VEVENT": continue
42         print('{} → {}'.format(*dtstrs(event.get('dtstart'), event.get('dtend'))))
43         flags = []
44         for k in ('status', 'class', 'transp', 'priority'):
45             t = event.get(k)
46             if t: flags.append((k, t))
47         event['flags'] = ' '.join('{}:{}'.format(*f) for f in flags)
48         for label in FIELDS:
49             if label in event:
50                 text = fill(event[label], width=COLUMNS-INDENT,
51                             initial_indent='', subsequent_indent=' '*INDENT)
52                 print(f'{label.capitalize():>{WIDTH}s}{COLSEP}{text}')
53
54         if 'description' in event:
55             output = ['']
56             lines = event['description'].split('\n')
57             for line in lines:
58                 output.append(fill(line, width=COLUMNS, initial_indent=' ',
59                                    subsequent_indent=' '))
60             print('\n'.join(output))
61
62 if __name__ == '__main__':
63
64     if len(sys.argv) > 1 and sys.argv[1] != '-':
65         for f in sys.argv[1:]:
66             with open(f) as fp:
67                 parse_ics_file(fp)
68     else:
69         parse_ics_file(sys.stdin)