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