Debian Codegraph @

filters/show-ics-details.py

raw
1 #!/usr/bin/env python3
2
3 """Parse a vcard file given via stdin and output some details.
4 Currently the following details are displayed if present:
5
6 - start date and time
7 - the summary information of the event
8 - a list of attendees
9 - the description of the event
10
11 Please note: if multiple events are included in the data then only the
12 first one will be parsed and displayed!
13
14 REQUIREMENTS:
15 - Python 3
16 - Python 3 - vobject library
17
18 To use as a filter in aerc, add the following line to your aerc.config:
19 text/calendar=show-ics-details.py
20 """
21
22 import re
23 import sys
24
25 import vobject
26
27
28 def remove_mailto(message: str) -> str:
29 """Remove a possible existing 'mailto:' from the given message.
30
31 Keyword arguments:
32 message -- A message string.
33 """
34 return re.sub(r'^mailto:', '', message, flags=re.IGNORECASE)
35
36 def extract_field(cal: vobject.icalendar.VCalendar2_0, name: str) -> str:
37 """Extract the desired field from the given calendar object.
38
39 Keyword arguments:
40 cal -- A VCalendar 2.0 object.
41 name -- The field name.
42 """
43 try:
44 name = name.strip()
45 if name == 'attendees':
46 attendees = []
47 for attendee in cal.vevent.attendee_list:
48 attendees.append(remove_mailto(attendee.valueRepr()).strip())
49 return ', '.join(attendees)
50 elif name == 'description':
51 return cal.vevent.description.valueRepr().strip()
52 elif name == 'dtstart':
53 return str(cal.vevent.dtstart.valueRepr()).strip()
54 elif name == 'organizer':
55 return remove_mailto(cal.vevent.organizer.valueRepr()).strip()
56 elif name == 'summary':
57 return cal.vevent.summary.valueRepr().strip()
58 else:
59 return ''
60 except AttributeError:
61 return ''
62
63 attendees = ''
64 description = ''
65 dtstart = ''
66 error = ''
67 organizer = ''
68 summary = ''
69
70 try:
71 cal = vobject.readOne(sys.stdin)
72 attendees = extract_field(cal, 'attendees')
73 description = extract_field(cal, 'description')
74 dtstart = extract_field(cal, 'dtstart')
75 organizer = extract_field(cal, 'organizer')
76 summary = extract_field(cal, 'summary')
77 except vobject.base.ParseError:
78 error = '**Sorry, but we could not parse the calendar!**'
79
80 if error:
81 print(error)
82 print("")
83
84 print(f"Date/Time : {dtstart}")
85 print(f"Summary : {summary}")
86 print(f"Organizer : {organizer}")
87 print(f"Attendees : {attendees}")
88 print("")
89 print(description)