1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vCalendar.py Sat Oct 18 01:41:15 2008 +0200
1.3 @@ -0,0 +1,100 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Parsing of vCalendar and iCalendar files.
1.8 +
1.9 +Copyright (C) 2008 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU Lesser General Public License as published by the Free
1.13 +Software Foundation; either version 3 of the License, or (at your option) any
1.14 +later version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU Lesser General Public License along
1.22 +with this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +
1.24 +--------
1.25 +
1.26 +References:
1.27 +
1.28 +RFC 2445: Internet Calendaring and Scheduling Core Object Specification
1.29 + (iCalendar)
1.30 + http://rfc.net/rfc2445.html
1.31 +"""
1.32 +
1.33 +from vContent import Parser, Reader
1.34 +
1.35 +try:
1.36 + set
1.37 +except NameError:
1.38 + from sets import Set as set
1.39 +
1.40 +class vCalendarParser(Parser):
1.41 +
1.42 + "A parser specifically for vCalendar/iCalendar."
1.43 +
1.44 + quoted_parameters = set([
1.45 + "ALTREP", "DELEGATED-FROM", "DELEGATED-TO", "DIR", "MEMBER", "SENT-BY"
1.46 + ])
1.47 + multivalued_parameters = set([
1.48 + "DELEGATED-FROM", "DELEGATED-TO", "MEMBER"
1.49 + ])
1.50 + quoted_types = set(["URI"])
1.51 +
1.52 + def decode_quoted_value(self, value):
1.53 +
1.54 + "Decode the given 'value', returning a list of decoded values."
1.55 +
1.56 + if value[0] == '"' and value[-1] == '"':
1.57 + return value[1:-1]
1.58 + else:
1.59 + return value
1.60 +
1.61 + def handleProperty(self, name, parameters, value):
1.62 +
1.63 + """
1.64 + Record the property with the given 'name', 'parameters' and 'value' as
1.65 + part of the current component's children.
1.66 + """
1.67 +
1.68 + decoded_parameters = {}
1.69 + for param_name, param_value in parameters.items():
1.70 + if param_name in self.quoted_parameters:
1.71 + param_value = self.decode_quoted_value(param_value)
1.72 + separator = '","'
1.73 + else:
1.74 + separator = ","
1.75 + if param_name in self.multivalued_parameters:
1.76 + param_value = param_value.split(separator)
1.77 + decoded_parameters[param_name] = param_value
1.78 +
1.79 + return Parser.handleProperty(self, name, decoded_parameters, value)
1.80 +
1.81 +# Public functions.
1.82 +
1.83 +def parse(f, non_standard_newline=0):
1.84 +
1.85 + """
1.86 + Parse the resource data found through the use of the file object 'f', which
1.87 + should provide Unicode data, and put the resource information in the given
1.88 + 'store'. (The codecs module can be used to open files or to wrap streams in
1.89 + order to provide Unicode data.)
1.90 +
1.91 + The optional 'non_standard_newline' can be set to a true value (unlike the
1.92 + default) in order to attempt to process files with CR as the end of line
1.93 + character.
1.94 +
1.95 + As a result of parsing the resource, the root node of the imported resource
1.96 + is returned.
1.97 + """
1.98 +
1.99 + reader = Reader(f, non_standard_newline=non_standard_newline)
1.100 + parser = vCalendarParser()
1.101 + return parser.parse(reader)
1.102 +
1.103 +# vim: tabstop=4 expandtab shiftwidth=4