1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vCalendar.py Sun Sep 21 23:37:24 2014 +0200
1.3 @@ -0,0 +1,259 @@
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, 2009, 2011, 2013 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 General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +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 General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +
1.24 +--------
1.25 +
1.26 +References:
1.27 +
1.28 +RFC 5545: Internet Calendaring and Scheduling Core Object Specification
1.29 + (iCalendar)
1.30 + http://tools.ietf.org/html/rfc5545
1.31 +
1.32 +RFC 2445: Internet Calendaring and Scheduling Core Object Specification
1.33 + (iCalendar)
1.34 + http://tools.ietf.org/html/rfc2445
1.35 +"""
1.36 +
1.37 +import vContent
1.38 +import re
1.39 +
1.40 +try:
1.41 + set
1.42 +except NameError:
1.43 + from sets import Set as set
1.44 +
1.45 +# Format details.
1.46 +
1.47 +QUOTED_PARAMETERS = set([
1.48 + "ALTREP", "DELEGATED-FROM", "DELEGATED-TO", "DIR", "MEMBER", "SENT-BY"
1.49 + ])
1.50 +MULTIVALUED_PARAMETERS = set([
1.51 + "DELEGATED-FROM", "DELEGATED-TO", "MEMBER"
1.52 + ])
1.53 +QUOTED_TYPES = set(["URI"])
1.54 +
1.55 +unquoted_separator_regexp = re.compile(r"(?<!\\)([,;])")
1.56 +
1.57 +# Parser classes.
1.58 +
1.59 +class vCalendarStreamParser(vContent.StreamParser):
1.60 +
1.61 + "A stream parser specifically for vCalendar/iCalendar."
1.62 +
1.63 + def next(self):
1.64 +
1.65 + """
1.66 + Return the next content item in the file as a tuple of the form
1.67 + (name, parameters, value).
1.68 + """
1.69 +
1.70 + name, parameters, value = vContent.StreamParser.next(self)
1.71 + return name, self.decode_parameters(parameters), value
1.72 +
1.73 + def decode_content(self, value):
1.74 +
1.75 + """
1.76 + Decode the given 'value' (which may represent a collection of distinct
1.77 + values), replacing quoted separator characters.
1.78 + """
1.79 +
1.80 + sep = None
1.81 + values = []
1.82 +
1.83 + for i, s in enumerate(unquoted_separator_regexp.split(value)):
1.84 + if i % 2 != 0:
1.85 + if not sep:
1.86 + sep = s
1.87 + continue
1.88 + values.append(self.decode_content_value(s))
1.89 +
1.90 + if sep == ",":
1.91 + return values
1.92 + elif sep == ";":
1.93 + return tuple(values)
1.94 + else:
1.95 + return values[0]
1.96 +
1.97 + def decode_content_value(self, value):
1.98 +
1.99 + "Decode the given 'value', replacing quoted separator characters."
1.100 +
1.101 + # Replace quoted characters (see 4.3.11 in RFC 2445).
1.102 +
1.103 + value = vContent.StreamParser.decode_content(self, value)
1.104 + return value.replace(r"\,", ",").replace(r"\;", ";")
1.105 +
1.106 + # Internal methods.
1.107 +
1.108 + def decode_quoted_value(self, value):
1.109 +
1.110 + "Decode the given 'value', returning a list of decoded values."
1.111 +
1.112 + if value[0] == '"' and value[-1] == '"':
1.113 + return value[1:-1]
1.114 + else:
1.115 + return value
1.116 +
1.117 + def decode_parameters(self, parameters):
1.118 +
1.119 + """
1.120 + Decode the given 'parameters' according to the vCalendar specification.
1.121 + """
1.122 +
1.123 + decoded_parameters = {}
1.124 +
1.125 + for param_name, param_value in parameters.items():
1.126 + if param_name in QUOTED_PARAMETERS:
1.127 + param_value = self.decode_quoted_value(param_value)
1.128 + separator = '","'
1.129 + else:
1.130 + separator = ","
1.131 + if param_name in MULTIVALUED_PARAMETERS:
1.132 + param_value = param_value.split(separator)
1.133 + decoded_parameters[param_name] = param_value
1.134 +
1.135 + return decoded_parameters
1.136 +
1.137 +class vCalendarParser(vContent.Parser):
1.138 +
1.139 + "A parser specifically for vCalendar/iCalendar."
1.140 +
1.141 + def parse(self, f, parser_cls=None):
1.142 + return vContent.Parser.parse(self, f, (parser_cls or vCalendarStreamParser))
1.143 +
1.144 +# Writer classes.
1.145 +
1.146 +class vCalendarStreamWriter(vContent.StreamWriter):
1.147 +
1.148 + "A stream writer specifically for vCalendar."
1.149 +
1.150 + # Overridden methods.
1.151 +
1.152 + def encode_parameters(self, parameters):
1.153 +
1.154 + """
1.155 + Encode the given 'parameters' according to the vCalendar specification.
1.156 + """
1.157 +
1.158 + encoded_parameters = {}
1.159 +
1.160 + for param_name, param_value in parameters.items():
1.161 + if param_name in QUOTED_PARAMETERS:
1.162 + param_value = self.encode_quoted_parameter_value(param_value)
1.163 + separator = '","'
1.164 + else:
1.165 + separator = ","
1.166 + if param_name in MULTIVALUED_PARAMETERS:
1.167 + param_value = separator.join(param_value)
1.168 + encoded_parameters[param_name] = param_value
1.169 +
1.170 + return encoded_parameters
1.171 +
1.172 + def encode_content(self, value):
1.173 +
1.174 + """
1.175 + Encode the given 'value' (which may be a list or tuple of separate
1.176 + values), quoting characters and separating collections of values.
1.177 + """
1.178 +
1.179 + if isinstance(value, list):
1.180 + sep = ","
1.181 + elif isinstance(value, tuple):
1.182 + sep = ";"
1.183 + else:
1.184 + value = [value]
1.185 + sep = ""
1.186 +
1.187 + return sep.join([self.encode_content_value(v) for v in value])
1.188 +
1.189 + def encode_content_value(self, value):
1.190 +
1.191 + "Encode the given 'value', quoting characters."
1.192 +
1.193 + # Replace quoted characters (see 4.3.11 in RFC 2445).
1.194 +
1.195 + value = vContent.StreamWriter.encode_content(self, value)
1.196 + return value.replace(";", r"\;").replace(",", r"\,")
1.197 +
1.198 +# Public functions.
1.199 +
1.200 +def parse(stream_or_string, encoding=None, non_standard_newline=0):
1.201 +
1.202 + """
1.203 + Parse the resource data found through the use of the 'stream_or_string',
1.204 + which is either a stream providing Unicode data (the codecs module can be
1.205 + used to open files or to wrap streams in order to provide Unicode data) or a
1.206 + filename identifying a file to be parsed.
1.207 +
1.208 + The optional 'encoding' can be used to specify the character encoding used
1.209 + by the file to be parsed.
1.210 +
1.211 + The optional 'non_standard_newline' can be set to a true value (unlike the
1.212 + default) in order to attempt to process files with CR as the end of line
1.213 + character.
1.214 +
1.215 + As a result of parsing the resource, the root node of the imported resource
1.216 + is returned.
1.217 + """
1.218 +
1.219 + return vContent.parse(stream_or_string, encoding, non_standard_newline, vCalendarParser)
1.220 +
1.221 +def iterparse(stream_or_string, encoding=None, non_standard_newline=0):
1.222 +
1.223 + """
1.224 + Parse the resource data found through the use of the 'stream_or_string',
1.225 + which is either a stream providing Unicode data (the codecs module can be
1.226 + used to open files or to wrap streams in order to provide Unicode data) or a
1.227 + filename identifying a file to be parsed.
1.228 +
1.229 + The optional 'encoding' can be used to specify the character encoding used
1.230 + by the file to be parsed.
1.231 +
1.232 + The optional 'non_standard_newline' can be set to a true value (unlike the
1.233 + default) in order to attempt to process files with CR as the end of line
1.234 + character.
1.235 +
1.236 + An iterator is returned which provides event tuples describing parsing
1.237 + events of the form (name, parameters, value).
1.238 + """
1.239 +
1.240 + return vContent.iterparse(stream_or_string, encoding, non_standard_newline, vCalendarStreamParser)
1.241 +
1.242 +def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None):
1.243 +
1.244 + """
1.245 + Return a writer which will either send data to the resource found through
1.246 + the use of 'stream_or_string' or using the given 'write' operation.
1.247 +
1.248 + The 'stream_or_string' parameter may be either a stream accepting Unicode
1.249 + data (the codecs module can be used to open files or to wrap streams in
1.250 + order to accept Unicode data) or a filename identifying a file to be
1.251 + written.
1.252 +
1.253 + The optional 'encoding' can be used to specify the character encoding used
1.254 + by the file to be written.
1.255 +
1.256 + The optional 'line_length' can be used to specify how long lines should be
1.257 + in the resulting data.
1.258 + """
1.259 +
1.260 + return vContent.iterwrite(stream_or_string, write, encoding, line_length, vCalendarStreamWriter)
1.261 +
1.262 +# vim: tabstop=4 expandtab shiftwidth=4