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