paul@4 | 1 | #!/usr/bin/env python |
paul@4 | 2 | |
paul@4 | 3 | """ |
paul@4 | 4 | Parsing of vCalendar and iCalendar files. |
paul@4 | 5 | |
paul@14 | 6 | Copyright (C) 2008, 2009, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@4 | 7 | |
paul@4 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@14 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@14 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@14 | 11 | version. |
paul@4 | 12 | |
paul@4 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@4 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@14 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@4 | 16 | details. |
paul@4 | 17 | |
paul@14 | 18 | You should have received a copy of the GNU General Public License along with |
paul@14 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@4 | 20 | |
paul@4 | 21 | -------- |
paul@4 | 22 | |
paul@4 | 23 | References: |
paul@4 | 24 | |
paul@16 | 25 | RFC 5545: Internet Calendaring and Scheduling Core Object Specification |
paul@16 | 26 | (iCalendar) |
paul@18 | 27 | http://tools.ietf.org/html/rfc5545 |
paul@16 | 28 | |
paul@4 | 29 | RFC 2445: Internet Calendaring and Scheduling Core Object Specification |
paul@4 | 30 | (iCalendar) |
paul@18 | 31 | http://tools.ietf.org/html/rfc2445 |
paul@4 | 32 | """ |
paul@4 | 33 | |
paul@5 | 34 | import vContent |
paul@24 | 35 | import re |
paul@4 | 36 | |
paul@4 | 37 | try: |
paul@4 | 38 | set |
paul@4 | 39 | except NameError: |
paul@4 | 40 | from sets import Set as set |
paul@4 | 41 | |
paul@7 | 42 | # Format details. |
paul@7 | 43 | |
paul@7 | 44 | QUOTED_PARAMETERS = set([ |
paul@7 | 45 | "ALTREP", "DELEGATED-FROM", "DELEGATED-TO", "DIR", "MEMBER", "SENT-BY" |
paul@7 | 46 | ]) |
paul@7 | 47 | MULTIVALUED_PARAMETERS = set([ |
paul@17 | 48 | "DELEGATED-FROM", "DELEGATED-TO", "MEMBER" |
paul@7 | 49 | ]) |
paul@7 | 50 | QUOTED_TYPES = set(["URI"]) |
paul@7 | 51 | |
paul@25 | 52 | unquoted_separator_regexp = re.compile(r"(?<!\\)([,;])") |
paul@24 | 53 | |
paul@7 | 54 | # Parser classes. |
paul@7 | 55 | |
paul@5 | 56 | class vCalendarStreamParser(vContent.StreamParser): |
paul@4 | 57 | |
paul@5 | 58 | "A stream parser specifically for vCalendar/iCalendar." |
paul@4 | 59 | |
paul@5 | 60 | def next(self): |
paul@5 | 61 | |
paul@5 | 62 | """ |
paul@5 | 63 | Return the next content item in the file as a tuple of the form |
paul@7 | 64 | (name, parameters, value). |
paul@5 | 65 | """ |
paul@5 | 66 | |
paul@7 | 67 | name, parameters, value = vContent.StreamParser.next(self) |
paul@22 | 68 | return name, self.decode_parameters(parameters), value |
paul@7 | 69 | |
paul@7 | 70 | def decode_content(self, value): |
paul@7 | 71 | |
paul@24 | 72 | """ |
paul@24 | 73 | Decode the given 'value' (which may represent a collection of distinct |
paul@24 | 74 | values), replacing quoted separator characters. |
paul@24 | 75 | """ |
paul@24 | 76 | |
paul@25 | 77 | sep = None |
paul@25 | 78 | values = [] |
paul@25 | 79 | |
paul@25 | 80 | for i, s in enumerate(unquoted_separator_regexp.split(value)): |
paul@25 | 81 | if i % 2 != 0: |
paul@25 | 82 | if not sep: |
paul@25 | 83 | sep = s |
paul@25 | 84 | continue |
paul@25 | 85 | values.append(self.decode_content_value(s)) |
paul@25 | 86 | |
paul@25 | 87 | if sep == ",": |
paul@25 | 88 | return values |
paul@25 | 89 | elif sep == ";": |
paul@25 | 90 | return tuple(values) |
paul@25 | 91 | else: |
paul@25 | 92 | return values[0] |
paul@24 | 93 | |
paul@24 | 94 | def decode_content_value(self, value): |
paul@24 | 95 | |
paul@7 | 96 | "Decode the given 'value', replacing quoted separator characters." |
paul@7 | 97 | |
paul@7 | 98 | # Replace quoted characters (see 4.3.11 in RFC 2445). |
paul@7 | 99 | |
paul@7 | 100 | value = vContent.StreamParser.decode_content(self, value) |
paul@13 | 101 | return value.replace(r"\,", ",").replace(r"\;", ";") |
paul@7 | 102 | |
paul@7 | 103 | # Internal methods. |
paul@5 | 104 | |
paul@4 | 105 | def decode_quoted_value(self, value): |
paul@4 | 106 | |
paul@4 | 107 | "Decode the given 'value', returning a list of decoded values." |
paul@4 | 108 | |
paul@4 | 109 | if value[0] == '"' and value[-1] == '"': |
paul@4 | 110 | return value[1:-1] |
paul@4 | 111 | else: |
paul@4 | 112 | return value |
paul@4 | 113 | |
paul@5 | 114 | def decode_parameters(self, parameters): |
paul@4 | 115 | |
paul@4 | 116 | """ |
paul@5 | 117 | Decode the given 'parameters' according to the vCalendar specification. |
paul@4 | 118 | """ |
paul@4 | 119 | |
paul@4 | 120 | decoded_parameters = {} |
paul@5 | 121 | |
paul@4 | 122 | for param_name, param_value in parameters.items(): |
paul@7 | 123 | if param_name in QUOTED_PARAMETERS: |
paul@4 | 124 | param_value = self.decode_quoted_value(param_value) |
paul@4 | 125 | separator = '","' |
paul@4 | 126 | else: |
paul@4 | 127 | separator = "," |
paul@7 | 128 | if param_name in MULTIVALUED_PARAMETERS: |
paul@4 | 129 | param_value = param_value.split(separator) |
paul@4 | 130 | decoded_parameters[param_name] = param_value |
paul@4 | 131 | |
paul@5 | 132 | return decoded_parameters |
paul@5 | 133 | |
paul@5 | 134 | class vCalendarParser(vContent.Parser): |
paul@5 | 135 | |
paul@5 | 136 | "A parser specifically for vCalendar/iCalendar." |
paul@5 | 137 | |
paul@5 | 138 | def parse(self, f, parser_cls=None): |
paul@12 | 139 | return vContent.Parser.parse(self, f, (parser_cls or vCalendarStreamParser)) |
paul@4 | 140 | |
paul@7 | 141 | # Writer classes. |
paul@7 | 142 | |
paul@7 | 143 | class vCalendarStreamWriter(vContent.StreamWriter): |
paul@7 | 144 | |
paul@7 | 145 | "A stream writer specifically for vCard." |
paul@7 | 146 | |
paul@11 | 147 | # Overridden methods. |
paul@7 | 148 | |
paul@7 | 149 | def encode_parameters(self, parameters): |
paul@7 | 150 | |
paul@7 | 151 | """ |
paul@7 | 152 | Encode the given 'parameters' according to the vCalendar specification. |
paul@7 | 153 | """ |
paul@7 | 154 | |
paul@7 | 155 | encoded_parameters = {} |
paul@7 | 156 | |
paul@7 | 157 | for param_name, param_value in parameters.items(): |
paul@7 | 158 | if param_name in QUOTED_PARAMETERS: |
paul@11 | 159 | param_value = self.encode_quoted_parameter_value(param_value) |
paul@7 | 160 | separator = '","' |
paul@7 | 161 | else: |
paul@7 | 162 | separator = "," |
paul@7 | 163 | if param_name in MULTIVALUED_PARAMETERS: |
paul@7 | 164 | param_value = separator.join(param_value) |
paul@7 | 165 | encoded_parameters[param_name] = param_value |
paul@7 | 166 | |
paul@7 | 167 | return encoded_parameters |
paul@7 | 168 | |
paul@7 | 169 | def encode_content(self, value): |
paul@7 | 170 | |
paul@24 | 171 | """ |
paul@24 | 172 | Encode the given 'value' (which may be a list or tuple of separate |
paul@24 | 173 | values), quoting characters and separating collections of values. |
paul@24 | 174 | """ |
paul@24 | 175 | |
paul@25 | 176 | if isinstance(value, list): |
paul@25 | 177 | sep = "," |
paul@25 | 178 | elif isinstance(value, tuple): |
paul@25 | 179 | sep = ";" |
paul@25 | 180 | else: |
paul@24 | 181 | value = [value] |
paul@25 | 182 | sep = "" |
paul@24 | 183 | |
paul@25 | 184 | return sep.join([self.encode_content_value(v) for v in value]) |
paul@24 | 185 | |
paul@24 | 186 | def encode_content_value(self, value): |
paul@24 | 187 | |
paul@11 | 188 | "Encode the given 'value', quoting characters." |
paul@7 | 189 | |
paul@7 | 190 | # Replace quoted characters (see 4.3.11 in RFC 2445). |
paul@7 | 191 | |
paul@7 | 192 | value = vContent.StreamWriter.encode_content(self, value) |
paul@13 | 193 | return value.replace(";", r"\;").replace(",", r"\,") |
paul@7 | 194 | |
paul@4 | 195 | # Public functions. |
paul@4 | 196 | |
paul@11 | 197 | def parse(stream_or_string, encoding=None, non_standard_newline=0): |
paul@4 | 198 | |
paul@4 | 199 | """ |
paul@9 | 200 | Parse the resource data found through the use of the 'stream_or_string', |
paul@9 | 201 | which is either a stream providing Unicode data (the codecs module can be |
paul@9 | 202 | used to open files or to wrap streams in order to provide Unicode data) or a |
paul@9 | 203 | filename identifying a file to be parsed. |
paul@4 | 204 | |
paul@11 | 205 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 206 | by the file to be parsed. |
paul@11 | 207 | |
paul@4 | 208 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@4 | 209 | default) in order to attempt to process files with CR as the end of line |
paul@4 | 210 | character. |
paul@4 | 211 | |
paul@4 | 212 | As a result of parsing the resource, the root node of the imported resource |
paul@4 | 213 | is returned. |
paul@4 | 214 | """ |
paul@4 | 215 | |
paul@11 | 216 | return vContent.parse(stream_or_string, encoding, non_standard_newline, vCalendarParser) |
paul@5 | 217 | |
paul@11 | 218 | def iterparse(stream_or_string, encoding=None, non_standard_newline=0): |
paul@5 | 219 | |
paul@5 | 220 | """ |
paul@9 | 221 | Parse the resource data found through the use of the 'stream_or_string', |
paul@9 | 222 | which is either a stream providing Unicode data (the codecs module can be |
paul@9 | 223 | used to open files or to wrap streams in order to provide Unicode data) or a |
paul@9 | 224 | filename identifying a file to be parsed. |
paul@5 | 225 | |
paul@11 | 226 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 227 | by the file to be parsed. |
paul@11 | 228 | |
paul@5 | 229 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@5 | 230 | default) in order to attempt to process files with CR as the end of line |
paul@5 | 231 | character. |
paul@5 | 232 | |
paul@5 | 233 | An iterator is returned which provides event tuples describing parsing |
paul@5 | 234 | events of the form (name, parameters, value). |
paul@5 | 235 | """ |
paul@5 | 236 | |
paul@11 | 237 | return vContent.iterparse(stream_or_string, encoding, non_standard_newline, vCalendarStreamParser) |
paul@11 | 238 | |
paul@21 | 239 | def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None): |
paul@4 | 240 | |
paul@11 | 241 | """ |
paul@21 | 242 | Return a writer which will either send data to the resource found through |
paul@21 | 243 | the use of 'stream_or_string' or using the given 'write' operation. |
paul@21 | 244 | |
paul@21 | 245 | The 'stream_or_string' parameter may be either a stream accepting Unicode |
paul@21 | 246 | data (the codecs module can be used to open files or to wrap streams in |
paul@21 | 247 | order to accept Unicode data) or a filename identifying a file to be |
paul@21 | 248 | written. |
paul@11 | 249 | |
paul@11 | 250 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 251 | by the file to be written. |
paul@11 | 252 | |
paul@11 | 253 | The optional 'line_length' can be used to specify how long lines should be |
paul@11 | 254 | in the resulting data. |
paul@11 | 255 | """ |
paul@11 | 256 | |
paul@21 | 257 | return vContent.iterwrite(stream_or_string, write, encoding, line_length, vCalendarStreamWriter) |
paul@8 | 258 | |
paul@4 | 259 | # vim: tabstop=4 expandtab shiftwidth=4 |