-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathicalview.py
More file actions
executable file
·95 lines (75 loc) · 2.57 KB
/
icalview.py
File metadata and controls
executable file
·95 lines (75 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/env python3
from icalendar import Calendar
from dateutil import tz
from datetime import datetime
import sys
import io
import re
r_atendee = re.compile(r'mailto:', re.IGNORECASE)
def format_atendee(a):
return r_atendee.sub('', a)
def main():
cal = sys.stdin.read()
lines = []
linesx = []
for line in cal.splitlines():
if line.startswith('X-'):
# Don't process unsupported entries with dateutil.tz to
# avoid throwing an exception in tzical. We'll show them
# out to the user later.
linesx.append(line)
else:
lines.append(line)
tmpfile = io.StringIO()
tmpfile.write("\n".join(lines))
tmpfile.seek(0)
try:
tzical = tz.tzical(tmpfile)
except ValueError:
sys.stderr.write(
"Sorry, I could not read the calendar file properly.\n" +
"Please e-mail it to the developer so I can be fixed.\n"
)
sys.exit(1)
try:
icaltz = tzical.get()
except ValueError:
# No Timezone in iCal, assume UTC?
icaltz = tz.tzutc()
cal = Calendar.from_ical(cal)
events = cal.walk('VEVENT')
evnum = len(events)
for n, e in enumerate(events):
if evnum > 1:
print('** Event {}:'.format(n+1))
# DEBUG
#print e
#print "--------"
start = e['dtstart'].dt.replace(tzinfo=icaltz).astimezone(tz.tzlocal())
end = e['dtend'].dt.replace(tzinfo=icaltz).astimezone(tz.tzlocal())
print('Event: {}'.format(e['summary']))
print('Start: {}'.format(start.strftime('%a, %Y-%m-%d %H:%M %Z')))
print('End: {}'.format(end.strftime('%a, %Y-%m-%d %H:%M %Z')))
if e.get('organizer'):
print('Organizer: {}'.format(format_atendee(e['organizer'])))
if e.get('status'):
print('Status: {}'.format(e['status']))
if e.get('location'):
print('Location: {}'.format(e['location']))
if e.get('attendee'):
print('Atendee(s):')
if isinstance(e['attendee'], list):
for a in e['attendee']:
print(" {}".format(format_atendee(a)))
else:
print(" {}".format(format_atendee(e['attendee'])))
if e.get('description'):
print('\n{}'.format(e['description']))
if e.get('comment'):
print('Comment:\n{}'.format(e['comment']))
if linesx:
print("\n-----")
for line in linesx:
print(line)
if __name__ == "__main__":
sys.exit(main())