1.1 --- a/README.txt Sat Jun 15 02:27:21 2013 +0200
1.2 +++ b/README.txt Sat Jun 15 02:28:13 2013 +0200
1.3 @@ -358,6 +358,7 @@
1.4 --------------------------------------------------------------------
1.5
1.6 * Fixed iCalendar event writing.
1.7 + * Added an iCalendar parser.
1.8
1.9 New in EventAggregator 0.10.1 (Changes since EventAggregator 0.10)
1.10 ------------------------------------------------------------------
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/parsers/calendar.py Sat Jun 15 02:28:13 2013 +0200
2.3 @@ -0,0 +1,90 @@
2.4 +# -*- coding: iso-8859-1 -*-
2.5 +"""
2.6 + MoinMoin - calendar (EventAggregator)
2.7 +
2.8 + @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk>
2.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
2.10 +"""
2.11 +
2.12 +from MoinSupport import parseAttributes
2.13 +from EventAggregatorSupport.Formatting import formatEventsForOutputType
2.14 +from EventAggregatorSupport.Types import parseEvents, EventCalendar
2.15 +from codecs import getreader
2.16 +import vCalendar
2.17 +
2.18 +try:
2.19 + from cStringIO import StringIO
2.20 +except ImportError:
2.21 + from StringIO import StringIO
2.22 +
2.23 +Dependencies = ["pages"]
2.24 +
2.25 +# Parser support.
2.26 +
2.27 +class Parser:
2.28 +
2.29 + "Interpret and show calendar information in different ways."
2.30 +
2.31 + Dependencies = Dependencies
2.32 + extensions = [".ics"]
2.33 +
2.34 + # Input content types understood by this parser.
2.35 +
2.36 + input_mimetypes = ["text/calendar"]
2.37 +
2.38 + # Output content types preferred by this parser.
2.39 +
2.40 + output_mimetypes = ["text/html", "text/calendar"]
2.41 +
2.42 + def __init__(self, raw, request, **kw):
2.43 +
2.44 + """
2.45 + Initialise the parser with the given 'raw' data, 'request' and any
2.46 + keyword arguments that may have been supplied.
2.47 + """
2.48 +
2.49 + self.raw = raw
2.50 + self.request = request
2.51 + attrs = parseAttributes(kw.get("format_args", ""), False)
2.52 +
2.53 + self.fragment = attrs.get("fragment")
2.54 +
2.55 + def format(self, fmt, write=None):
2.56 +
2.57 + """
2.58 + Format a calendar using the given formatter 'fmt'. If the 'write'
2.59 + parameter is specified, use it to write output; otherwise, write output
2.60 + using the request.
2.61 + """
2.62 +
2.63 + (write or self.request.write)(fmt.text(self.raw))
2.64 +
2.65 + # Extra API methods.
2.66 +
2.67 + def formatForOutputType(self, mimetype, write=None):
2.68 +
2.69 + """
2.70 + Format a calendar for the given 'mimetype'. If the 'write' parameter is
2.71 + specified, use it to write output; otherwise, write output using the
2.72 + request.
2.73 + """
2.74 +
2.75 + # Write raw calendar information unchanged.
2.76 +
2.77 + if mimetype == "text/calendar":
2.78 + (write or request.write)(self.raw)
2.79 + else:
2.80 + # Make a Unicode-capable StringIO.
2.81 +
2.82 + f = getreader("utf-8")(StringIO(self.raw.encode("utf-8")))
2.83 + calendar = EventCalendar("", vCalendar.parse(f), {})
2.84 + formatEventsForOutputType(calendar.getEvents(), self.request, mimetype, write=write)
2.85 +
2.86 + # Class methods.
2.87 +
2.88 + def getOutputTypes(self):
2.89 + return self.output_mimetypes
2.90 +
2.91 + getOutputTypes = classmethod(getOutputTypes)
2.92 +
2.93 +# vim: tabstop=4 expandtab shiftwidth=4