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@5 | 37 | class vCalendarStreamParser(vContent.StreamParser): |
paul@4 | 38 | |
paul@5 | 39 | "A stream parser specifically for vCalendar/iCalendar." |
paul@4 | 40 | |
paul@4 | 41 | quoted_parameters = set([ |
paul@4 | 42 | "ALTREP", "DELEGATED-FROM", "DELEGATED-TO", "DIR", "MEMBER", "SENT-BY" |
paul@4 | 43 | ]) |
paul@4 | 44 | multivalued_parameters = set([ |
paul@4 | 45 | "DELEGATED-FROM", "DELEGATED-TO", "MEMBER" |
paul@4 | 46 | ]) |
paul@4 | 47 | quoted_types = set(["URI"]) |
paul@4 | 48 | |
paul@5 | 49 | def next(self): |
paul@5 | 50 | |
paul@5 | 51 | """ |
paul@5 | 52 | Return the next content item in the file as a tuple of the form |
paul@5 | 53 | (name, parameters, values). |
paul@5 | 54 | """ |
paul@5 | 55 | |
paul@5 | 56 | name, parameters, values = vContent.StreamParser.next(self) |
paul@5 | 57 | return name, self.decode_parameters(parameters), values |
paul@5 | 58 | |
paul@4 | 59 | def decode_quoted_value(self, value): |
paul@4 | 60 | |
paul@4 | 61 | "Decode the given 'value', returning a list of decoded values." |
paul@4 | 62 | |
paul@4 | 63 | if value[0] == '"' and value[-1] == '"': |
paul@4 | 64 | return value[1:-1] |
paul@4 | 65 | else: |
paul@4 | 66 | return value |
paul@4 | 67 | |
paul@5 | 68 | def decode_parameters(self, parameters): |
paul@4 | 69 | |
paul@4 | 70 | """ |
paul@5 | 71 | Decode the given 'parameters' according to the vCalendar specification. |
paul@4 | 72 | """ |
paul@4 | 73 | |
paul@4 | 74 | decoded_parameters = {} |
paul@5 | 75 | |
paul@4 | 76 | for param_name, param_value in parameters.items(): |
paul@4 | 77 | if param_name in self.quoted_parameters: |
paul@4 | 78 | param_value = self.decode_quoted_value(param_value) |
paul@4 | 79 | separator = '","' |
paul@4 | 80 | else: |
paul@4 | 81 | separator = "," |
paul@4 | 82 | if param_name in self.multivalued_parameters: |
paul@4 | 83 | param_value = param_value.split(separator) |
paul@4 | 84 | decoded_parameters[param_name] = param_value |
paul@4 | 85 | |
paul@5 | 86 | return decoded_parameters |
paul@5 | 87 | |
paul@5 | 88 | class vCalendarParser(vContent.Parser): |
paul@5 | 89 | |
paul@5 | 90 | "A parser specifically for vCalendar/iCalendar." |
paul@5 | 91 | |
paul@5 | 92 | def parse(self, f, parser_cls=None): |
paul@5 | 93 | return vContent.Parser.parse(self, f, vCalendarStreamParser) |
paul@4 | 94 | |
paul@4 | 95 | # Public functions. |
paul@4 | 96 | |
paul@4 | 97 | def parse(f, non_standard_newline=0): |
paul@4 | 98 | |
paul@4 | 99 | """ |
paul@4 | 100 | Parse the resource data found through the use of the file object 'f', which |
paul@5 | 101 | should provide Unicode data. (The codecs module can be used to open files or |
paul@5 | 102 | to wrap streams in order to provide Unicode data.) |
paul@4 | 103 | |
paul@4 | 104 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@4 | 105 | default) in order to attempt to process files with CR as the end of line |
paul@4 | 106 | character. |
paul@4 | 107 | |
paul@4 | 108 | As a result of parsing the resource, the root node of the imported resource |
paul@4 | 109 | is returned. |
paul@4 | 110 | """ |
paul@4 | 111 | |
paul@5 | 112 | return vContent.parser(f, non_standard_newline, vCalendarParser) |
paul@5 | 113 | |
paul@5 | 114 | def iterparse(f, non_standard_newline=0): |
paul@5 | 115 | |
paul@5 | 116 | """ |
paul@5 | 117 | Parse the resource data found through the use of the file object 'f', which |
paul@5 | 118 | should provide Unicode data. (The codecs module can be used to open files or |
paul@5 | 119 | to wrap streams in order to provide Unicode data.) |
paul@5 | 120 | |
paul@5 | 121 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@5 | 122 | default) in order to attempt to process files with CR as the end of line |
paul@5 | 123 | character. |
paul@5 | 124 | |
paul@5 | 125 | An iterator is returned which provides event tuples describing parsing |
paul@5 | 126 | events of the form (name, parameters, value). |
paul@5 | 127 | """ |
paul@5 | 128 | |
paul@5 | 129 | return vContent.iterparse(f, non_standard_newline, vCalendarStreamParser) |
paul@4 | 130 | |
paul@4 | 131 | # vim: tabstop=4 expandtab shiftwidth=4 |