-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcalendar.py
executable file
·130 lines (98 loc) · 4.09 KB
/
vcalendar.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: iso-8859-1 -*-
# Skapat av Christian Davén 2004
import error
from i18n import *
def isUTF8(string):
"Klarar åtminstone åäö men inte hela UTF-8!"
if "Ã" in string: return True
return False
def splitLine(line):
"Delar raden vid första kolonet. Följande tecken kan innehålla kolon."
pos = line.find(":")
return (line[:pos], line[pos+1:].strip())
# -----------------------------------------------------------
class Reader:
"Läser en starkt begränsad form av vCalendar-data"
def verifyFormat(self, data):
if data[0].startswith("BEGIN:VCALENDAR") and \
data[-1].startswith("END:VCALENDAR"):
return True
else:
return False
def read(self, input):
"Läser vCalendar-data antingen från en lista eller från en fil"
if not isinstance(input, list):
try:
input = file(input).readlines()
except IOError:
raise error.ReadError("Could not read from " + str(input))
if not self.verifyFormat(input):
raise error.DataError("Bad vCalendar data")
events = []
summarycontinues = False
import encodings
for line in input:
# får UTF8-kodade värden från Daisy
# och Latin1-ditto från TimeEdit;
# gör om dem till unicode-strängar
if isUTF8(line):
line = unicode(line, "utf8")
else:
line = unicode(line, "latin_1")
(key, value) = splitLine(line)
if key == "BEGIN" and value == "VEVENT":
id = date = begin = end = location = summary = ""
elif key == "END" and value == "VEVENT":
if id:
events.append({"id": id, "date": date, "begin": begin, "end": end,
"location": location, "summary": summary})
id = date = begin = end = location = summary = ""
elif key.startswith("UID"):
id = value
elif key.startswith("DTSTART"):
start = value.split("T")
date = start[0]
begin = start[1]
elif key.startswith("DTEND"):
end = value.split("T")[1]
elif key.startswith("SUMMARY"):
summary = value
if value[-1] == "=":
# långa rader kan brytas med '='
summarycontinues = True
elif key.startswith("LOCATION"):
location = value
elif summarycontinues:
summary = summary[:-1] + line.strip()
summarycontinues = False
return events
# -----------------------------------------------------------
class Writer:
"Skriver vCalendar-filer"
def __init__(self, encoding = "latin_1"):
self.encoding = encoding
def write(self, events, UTC = True):
"Skriver valda händelser till vCalendar-fil"
import settings
data = ["BEGIN:VCALENDAR\n", "VERSION:1.0\n", "PRODID:-//cda//NONSGML KTHTT//EN\n"]
for event in events:
data.append("BEGIN:VEVENT")
data.append("\nUID:" + str(event.getID()))
begin = event.begin
end = event.end
if UTC:
# de flesta program utgår från att tiden
# är angiven i UTC utan hänsyn till sommartid
begin -= 3600 # justerar tidszon
end -= 3600
if event.date.isDaylightSavingTime():
begin -= 3600 # justerar ev. sommartid
end -= 3600
data.append("\nDTSTART:" + str(event.date) + "T" + str(begin) + "Z")
data.append("\nDTEND:" + str(event.date) + "T" + str(end) + "Z")
data.append("\nSUMMARY:" + event.getDescriptionWithoutLocation().encode(self.encoding))
data.append("\nLOCATION:" + event.location.encode(self.encoding))
data.append("\nCATEGORIES:" + settings.event_export_category)
data.append("\nEND:VEVENT\n")
data.append("END:VCALENDAR")
return data