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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
28 class InvalidICS(Exception): pass
29 class IncompleteICS(InvalidICS): pass
31 def lineJoiner(oldcal):
32 '''Takes a string containing a calendar and returns an array of its lines'''
34 if not oldcal[0:15] == 'BEGIN:VCALENDAR':
35 raise InvalidICS, "Does not appear to be a valid ICS file"
37 if not 'END:VCALENDAR' in oldcal[-15:-1]:
38 raise IncompleteICS, "File appears to be incomplete"
40 if list(oldcal) == oldcal:
41 oldcal = '\r\n'.join(oldcal)
43 oldcal = oldcal.replace('\r\n ', '').replace('\r\n\t','')
44 return oldcal.strip().split('\r\n')
47 def lineFolder(oldcal, length=75):
48 '''Folds content lines to a specified length, returns a list'''
51 sys.stderr.write('WARN: lines > 75 octets are not RFC compliant\n')
57 # Line fits inside length, do nothing
58 if len(line.rstrip()) <= length:
61 brokenline = [line[0:length]]
63 while ll < len(line) + 1:
64 brokenline.append(line[ll:sl+ll])
66 brokenline = '\r\n '.join(brokenline)
67 cal.append(brokenline)
73 '''Takes a list of lines in a calendar file and returns a list of tuples
74 as (key, value) pairs'''
76 ical = [tuple(x.split(':',1)) for x in cal]
78 # Check that we got 2 items on every line
80 if not len(line) == 2:
81 raise InvalidICS, "Didn't find a content key on: %s"%(line)
87 '''Takes a list of tuples that make up a calendar file and returns it to a
90 return [':'.join(x) for x in ical]
93 def getContent(url='',stdin=False):
94 '''Generic content retriever, DO NOT use this function in a CGI script as
95 it can read from the local disk (which you probably don't want it to).
98 # Special case, if this is a HTTP url, return the data from it using
99 # the HTTP functions which attempt to play a bit nicer.
100 parsedURL = urlparse.urlparse(url)
101 if 'http' in parsedURL[0]: return getHTTPContent(url)
104 content = sys.stdin.read()
108 try: content = open(os.path.abspath(url),'r').read()
109 except (IOError, OSError), e:
110 sys.stderr.write('%s\n'%e)
114 # If we've survived, use python's generic URL opening library to handle it
117 res = urllib2.urlopen(url)
120 except (urllib2.URLError, OSError), e:
121 sys.stderr.write('%s\n'%e)
126 def getHTTPContent(url='',cache='.httplib2-cache'):
127 '''This function attempts to play nice when retrieving content from HTTP
128 services. It's what you should use in a CGI script.'''
135 if not url: return ''
137 if 'httplib2' in sys.modules:
138 try: h = httplib2.Http('.httplib2-cache')
139 except OSError: h = httplib2.Http()
143 if h: content = h.request(url)[1]
145 except ValueError, e:
146 sys.stderr.write('%s\n'%e)
150 content = urllib2.urlopen(url).read()
152 except (urllib2.URLError, OSError), e:
153 sys.stderr.write('%s\n'%e)
160 '''Attempts to load a series of rules into a list'''
166 rules = [getattr(parserrules, rule) for rule in dir(parserrules) if callable(getattr(parserrules, rule))]
170 def applyRules(ical, rules=[], verbose=False):
171 'Runs a series of rules on the lines in ical and mangles its output'
175 if rule.__doc__ and verbose:
179 out = rule(line[0],line[1])
185 # Drop lines that are boolean False
186 if not out and not out == None: continue
188 # If the rule did something and is a tuple or a list we'll accept it
189 # otherwise, pay no attention to the man behind the curtain
191 if tuple(out) == out or list(out) == out and len(out) == 2:
192 output.append(tuple(out))
203 def writeOutput(cal, outfile=''):
204 '''Takes a list of lines and outputs to the specified file'''
207 sys.stderr.write('Refusing to write out an empty file')
214 out = open(outfile, 'w')
215 except (IOError, OSError), e:
216 sys.stderr.write('%s\n'%e)
219 if cal[-1]: cal.append('')
221 out.write('\r\n'.join(cal))
223 if not out == sys.stdout:
227 if __name__ == '__main__':
228 from optparse import OptionParser
229 # If the user passed us a 'stdin' argument, we'll go with that,
230 # otherwise we'll try for a url opener
232 parser = OptionParser('usage: %prog [options] url')
233 parser.add_option('-s', '--stdin', action='store_true', dest='stdin',
234 default=False, help='Take a calendar from standard input')
235 parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
236 default=False, help='Be verbose when rules are being applied')
237 parser.add_option('-o', '--output', dest='outfile', default='',
238 help='Specify output file (defaults to standard output)')
240 (options, args) = parser.parse_args()
242 if not args and not options.stdin:
245 elif not options.stdin:
250 content = getContent(url, options.stdin)
251 cal = lineJoiner(content)
252 ical = applyRules(splitFields(cal), generateRules(), options.verbose)
253 output = lineFolder(joinFields(ical))
254 writeOutput(output, options.outfile)