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@4 | 30 | from vContent import Parser, Reader |
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@4 | 37 | class vCalendarParser(Parser): |
paul@4 | 38 | |
paul@4 | 39 | "A 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@4 | 49 | def decode_quoted_value(self, value): |
paul@4 | 50 | |
paul@4 | 51 | "Decode the given 'value', returning a list of decoded values." |
paul@4 | 52 | |
paul@4 | 53 | if value[0] == '"' and value[-1] == '"': |
paul@4 | 54 | return value[1:-1] |
paul@4 | 55 | else: |
paul@4 | 56 | return value |
paul@4 | 57 | |
paul@4 | 58 | def handleProperty(self, name, parameters, value): |
paul@4 | 59 | |
paul@4 | 60 | """ |
paul@4 | 61 | Record the property with the given 'name', 'parameters' and 'value' as |
paul@4 | 62 | part of the current component's children. |
paul@4 | 63 | """ |
paul@4 | 64 | |
paul@4 | 65 | decoded_parameters = {} |
paul@4 | 66 | for param_name, param_value in parameters.items(): |
paul@4 | 67 | if param_name in self.quoted_parameters: |
paul@4 | 68 | param_value = self.decode_quoted_value(param_value) |
paul@4 | 69 | separator = '","' |
paul@4 | 70 | else: |
paul@4 | 71 | separator = "," |
paul@4 | 72 | if param_name in self.multivalued_parameters: |
paul@4 | 73 | param_value = param_value.split(separator) |
paul@4 | 74 | decoded_parameters[param_name] = param_value |
paul@4 | 75 | |
paul@4 | 76 | return Parser.handleProperty(self, name, decoded_parameters, value) |
paul@4 | 77 | |
paul@4 | 78 | # Public functions. |
paul@4 | 79 | |
paul@4 | 80 | def parse(f, non_standard_newline=0): |
paul@4 | 81 | |
paul@4 | 82 | """ |
paul@4 | 83 | Parse the resource data found through the use of the file object 'f', which |
paul@4 | 84 | should provide Unicode data, and put the resource information in the given |
paul@4 | 85 | 'store'. (The codecs module can be used to open files or to wrap streams in |
paul@4 | 86 | order to provide Unicode data.) |
paul@4 | 87 | |
paul@4 | 88 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@4 | 89 | default) in order to attempt to process files with CR as the end of line |
paul@4 | 90 | character. |
paul@4 | 91 | |
paul@4 | 92 | As a result of parsing the resource, the root node of the imported resource |
paul@4 | 93 | is returned. |
paul@4 | 94 | """ |
paul@4 | 95 | |
paul@4 | 96 | reader = Reader(f, non_standard_newline=non_standard_newline) |
paul@4 | 97 | parser = vCalendarParser() |
paul@4 | 98 | return parser.parse(reader) |
paul@4 | 99 | |
paul@4 | 100 | # vim: tabstop=4 expandtab shiftwidth=4 |