5c3a34f281d81f08ff6529bd5518af688c8c8a4a
[frenchie/icalparse.git] / icalparse.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2010 James French <[email protected]>
4
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22
23 import sys
24 import re
25
26 try:
27         import httplib2
28         urllib = False
29 except ImportError:
30         try:
31                 import urllib
32                 urllib = True
33         except:
34                 sys.stderr.write('Failed to find a suitable http downloader\n')
35                 raise
36
37 class InvalidICS(Exception): pass
38 class notJoined(Exception): pass
39
40 # RFC5545 and RFC5546 iCalendar registries contain upper case letters
41 # and dashes only and are separated from the value by a colon (:)
42 itemstart = re.compile('^[A-Z\-]+:.*')
43 def lineJoiner(oldcal):
44         '''Unfolds a calendar so that items can be parsed'''    
45         
46         cal = []
47         
48         # Strip newlines (
49         for line in oldcal:
50                 line = line.rstrip('\r\n')
51
52                 # Reassemble broken Lines
53                 if not line:
54                         if not cal: continue
55                         else: cal[-1] += '\\n'
56                 elif line[0] == ' ':
57                         if not cal: raise InvalidICS, 'First line of ICS must be element'
58                         line = line[1:len(line)]
59                         cal[-1] += line
60                 elif not itemstart.match(line):
61                         if not cal: raise InvalidICS, 'First line of ICS must be element'
62                         cal[-1] += '\\n' + line
63                 else:
64                         if cal: cal[-1] += '\r\n'
65                         cal.append(line)
66
67         cal[-1] += '\r\n'
68
69         return cal
70
71 def lineSplitter(oldcal, length=75):
72         '''Folds content lines to a specified length, returns a list'''
73
74         cal = []
75         sl = length - 1
76
77         for line in oldcal:
78                 # Line & line ending line ending fit inside length, do nothing
79                 if len(line.rstrip()) <= length:
80                         cal.append(line)
81                 else:
82                         brokenline = [line[0:length] + '\r\n']
83                         ll = length
84                         while ll < len(line.rstrip('\r\n')) + 1:
85                                 brokenline.append(' ' + line[ll:sl+ll].rstrip('\r\n') + '\r\n')
86                                 ll += sl
87                         cal += brokenline
88
89         return cal

UCC git Repository :: git.ucc.asn.au