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@4 | 35 | |
paul@4 | 36 | try: |
paul@4 | 37 | set |
paul@4 | 38 | except NameError: |
paul@4 | 39 | from sets import Set as set |
paul@4 | 40 | |
paul@7 | 41 | # Format details. |
paul@7 | 42 | |
paul@7 | 43 | QUOTED_PARAMETERS = set([ |
paul@7 | 44 | "ALTREP", "DELEGATED-FROM", "DELEGATED-TO", "DIR", "MEMBER", "SENT-BY" |
paul@7 | 45 | ]) |
paul@7 | 46 | MULTIVALUED_PARAMETERS = set([ |
paul@17 | 47 | "DELEGATED-FROM", "DELEGATED-TO", "MEMBER" |
paul@7 | 48 | ]) |
paul@7 | 49 | QUOTED_TYPES = set(["URI"]) |
paul@7 | 50 | |
paul@7 | 51 | # Parser classes. |
paul@7 | 52 | |
paul@5 | 53 | class vCalendarStreamParser(vContent.StreamParser): |
paul@4 | 54 | |
paul@5 | 55 | "A stream parser specifically for vCalendar/iCalendar." |
paul@4 | 56 | |
paul@5 | 57 | def next(self): |
paul@5 | 58 | |
paul@5 | 59 | """ |
paul@5 | 60 | Return the next content item in the file as a tuple of the form |
paul@7 | 61 | (name, parameters, value). |
paul@5 | 62 | """ |
paul@5 | 63 | |
paul@7 | 64 | name, parameters, value = vContent.StreamParser.next(self) |
paul@7 | 65 | return name, self.decode_parameters(parameters), self.decode_content(value) |
paul@7 | 66 | |
paul@7 | 67 | def decode_content(self, value): |
paul@7 | 68 | |
paul@7 | 69 | "Decode the given 'value', replacing quoted separator characters." |
paul@7 | 70 | |
paul@7 | 71 | # Replace quoted characters (see 4.3.11 in RFC 2445). |
paul@7 | 72 | |
paul@7 | 73 | value = vContent.StreamParser.decode_content(self, value) |
paul@13 | 74 | return value.replace(r"\,", ",").replace(r"\;", ";") |
paul@7 | 75 | |
paul@7 | 76 | # Internal methods. |
paul@5 | 77 | |
paul@4 | 78 | def decode_quoted_value(self, value): |
paul@4 | 79 | |
paul@4 | 80 | "Decode the given 'value', returning a list of decoded values." |
paul@4 | 81 | |
paul@4 | 82 | if value[0] == '"' and value[-1] == '"': |
paul@4 | 83 | return value[1:-1] |
paul@4 | 84 | else: |
paul@4 | 85 | return value |
paul@4 | 86 | |
paul@5 | 87 | def decode_parameters(self, parameters): |
paul@4 | 88 | |
paul@4 | 89 | """ |
paul@5 | 90 | Decode the given 'parameters' according to the vCalendar specification. |
paul@4 | 91 | """ |
paul@4 | 92 | |
paul@4 | 93 | decoded_parameters = {} |
paul@5 | 94 | |
paul@4 | 95 | for param_name, param_value in parameters.items(): |
paul@7 | 96 | if param_name in QUOTED_PARAMETERS: |
paul@4 | 97 | param_value = self.decode_quoted_value(param_value) |
paul@4 | 98 | separator = '","' |
paul@4 | 99 | else: |
paul@4 | 100 | separator = "," |
paul@7 | 101 | if param_name in MULTIVALUED_PARAMETERS: |
paul@4 | 102 | param_value = param_value.split(separator) |
paul@4 | 103 | decoded_parameters[param_name] = param_value |
paul@4 | 104 | |
paul@5 | 105 | return decoded_parameters |
paul@5 | 106 | |
paul@5 | 107 | class vCalendarParser(vContent.Parser): |
paul@5 | 108 | |
paul@5 | 109 | "A parser specifically for vCalendar/iCalendar." |
paul@5 | 110 | |
paul@5 | 111 | def parse(self, f, parser_cls=None): |
paul@12 | 112 | return vContent.Parser.parse(self, f, (parser_cls or vCalendarStreamParser)) |
paul@4 | 113 | |
paul@7 | 114 | # Writer classes. |
paul@7 | 115 | |
paul@7 | 116 | class vCalendarStreamWriter(vContent.StreamWriter): |
paul@7 | 117 | |
paul@7 | 118 | "A stream writer specifically for vCard." |
paul@7 | 119 | |
paul@11 | 120 | # Overridden methods. |
paul@7 | 121 | |
paul@7 | 122 | def encode_parameters(self, parameters): |
paul@7 | 123 | |
paul@7 | 124 | """ |
paul@7 | 125 | Encode the given 'parameters' according to the vCalendar specification. |
paul@7 | 126 | """ |
paul@7 | 127 | |
paul@7 | 128 | encoded_parameters = {} |
paul@7 | 129 | |
paul@7 | 130 | for param_name, param_value in parameters.items(): |
paul@7 | 131 | if param_name in QUOTED_PARAMETERS: |
paul@11 | 132 | param_value = self.encode_quoted_parameter_value(param_value) |
paul@7 | 133 | separator = '","' |
paul@7 | 134 | else: |
paul@7 | 135 | separator = "," |
paul@7 | 136 | if param_name in MULTIVALUED_PARAMETERS: |
paul@7 | 137 | param_value = separator.join(param_value) |
paul@7 | 138 | encoded_parameters[param_name] = param_value |
paul@7 | 139 | |
paul@7 | 140 | return encoded_parameters |
paul@7 | 141 | |
paul@7 | 142 | def encode_content(self, value): |
paul@7 | 143 | |
paul@11 | 144 | "Encode the given 'value', quoting characters." |
paul@7 | 145 | |
paul@7 | 146 | # Replace quoted characters (see 4.3.11 in RFC 2445). |
paul@7 | 147 | |
paul@7 | 148 | value = vContent.StreamWriter.encode_content(self, value) |
paul@13 | 149 | return value.replace(";", r"\;").replace(",", r"\,") |
paul@7 | 150 | |
paul@4 | 151 | # Public functions. |
paul@4 | 152 | |
paul@11 | 153 | def parse(stream_or_string, encoding=None, non_standard_newline=0): |
paul@4 | 154 | |
paul@4 | 155 | """ |
paul@9 | 156 | Parse the resource data found through the use of the 'stream_or_string', |
paul@9 | 157 | which is either a stream providing Unicode data (the codecs module can be |
paul@9 | 158 | used to open files or to wrap streams in order to provide Unicode data) or a |
paul@9 | 159 | filename identifying a file to be parsed. |
paul@4 | 160 | |
paul@11 | 161 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 162 | by the file to be parsed. |
paul@11 | 163 | |
paul@4 | 164 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@4 | 165 | default) in order to attempt to process files with CR as the end of line |
paul@4 | 166 | character. |
paul@4 | 167 | |
paul@4 | 168 | As a result of parsing the resource, the root node of the imported resource |
paul@4 | 169 | is returned. |
paul@4 | 170 | """ |
paul@4 | 171 | |
paul@11 | 172 | return vContent.parse(stream_or_string, encoding, non_standard_newline, vCalendarParser) |
paul@5 | 173 | |
paul@11 | 174 | def iterparse(stream_or_string, encoding=None, non_standard_newline=0): |
paul@5 | 175 | |
paul@5 | 176 | """ |
paul@9 | 177 | Parse the resource data found through the use of the 'stream_or_string', |
paul@9 | 178 | which is either a stream providing Unicode data (the codecs module can be |
paul@9 | 179 | used to open files or to wrap streams in order to provide Unicode data) or a |
paul@9 | 180 | filename identifying a file to be parsed. |
paul@5 | 181 | |
paul@11 | 182 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 183 | by the file to be parsed. |
paul@11 | 184 | |
paul@5 | 185 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@5 | 186 | default) in order to attempt to process files with CR as the end of line |
paul@5 | 187 | character. |
paul@5 | 188 | |
paul@5 | 189 | An iterator is returned which provides event tuples describing parsing |
paul@5 | 190 | events of the form (name, parameters, value). |
paul@5 | 191 | """ |
paul@5 | 192 | |
paul@11 | 193 | return vContent.iterparse(stream_or_string, encoding, non_standard_newline, vCalendarStreamParser) |
paul@11 | 194 | |
paul@11 | 195 | def iterwrite(stream_or_string, encoding=None, line_length=None): |
paul@4 | 196 | |
paul@11 | 197 | """ |
paul@11 | 198 | Return a writer which will send data to the resource found through the use |
paul@11 | 199 | of 'stream_or_string', which is either a stream accepting Unicode data (the |
paul@11 | 200 | codecs module can be used to open files or to wrap streams in order to |
paul@11 | 201 | accept Unicode data) or a filename identifying a file to be parsed. |
paul@11 | 202 | |
paul@11 | 203 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 204 | by the file to be written. |
paul@11 | 205 | |
paul@11 | 206 | The optional 'line_length' can be used to specify how long lines should be |
paul@11 | 207 | in the resulting data. |
paul@11 | 208 | """ |
paul@11 | 209 | |
paul@11 | 210 | return vContent.iterwrite(stream_or_string, encoding, line_length, vCalendarStreamWriter) |
paul@8 | 211 | |
paul@4 | 212 | # vim: tabstop=4 expandtab shiftwidth=4 |