]> git.madduck.net Git - etc/mutt.git/blob - .config/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:

Add 7z handling to container-extractor
[etc/mutt.git] / .config / 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(event):
17     start, end = event.get('dtstart').dt, event.get('dtend')
18     if not end:
19         duration = event.get('duration').dt
20         end = start + duration
21     else:
22         end = end.dt
23
24     def strftime(dt, strf):
25         return dt.astimezone().strftime(strf)
26
27     ret = []
28     if start.tzinfo == end.tzinfo:
29         ret.append(strftime(start, '%F %R'))
30
31     else:
32         ret.append(strftime(start, '%F %R %Z'))
33
34     if start.date() == end.date():
35         ret.append(strftime(end, '%T %Z'))
36
37     else:
38         ret.append(strftime(end, '%F %R %Z'))
39
40     return ret
41
42 def parse_ics_file(fp):
43     cal = Calendar.from_ical(fp.read())
44
45     for event in cal.walk():
46         if event.name != "VEVENT": continue
47         print('{} → {}'.format(*dtstrs(event)))
48         flags = []
49         for k in ('status', 'class', 'transp', 'priority'):
50             t = event.get(k)
51             if t: flags.append((k, t))
52         event['flags'] = ' '.join('{}:{}'.format(*f) for f in flags)
53         for label in FIELDS:
54             if label in event:
55                 text = fill(str(event[label]), width=COLUMNS-INDENT,
56                             initial_indent='', subsequent_indent=' '*INDENT)
57                 print(f'{label.capitalize():>{WIDTH}s}{COLSEP}{text}')
58
59         if 'description' in event:
60             output = ['']
61             lines = event['description'].split('\n')
62             for line in lines:
63                 output.append(fill(line, width=COLUMNS, initial_indent=' ',
64                                    subsequent_indent=' '))
65             print('\n'.join(output))
66
67 if __name__ == '__main__':
68
69     if len(sys.argv) > 1 and sys.argv[1] != '-':
70         for f in sys.argv[1:]:
71             with open(f) as fp:
72                 parse_ics_file(fp)
73     else:
74         parse_ics_file(sys.stdin)