1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/parsers/calendar.py Sat Jun 15 02:28:13 2013 +0200
1.3 @@ -0,0 +1,90 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - calendar (EventAggregator)
1.7 +
1.8 + @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinSupport import parseAttributes
1.13 +from EventAggregatorSupport.Formatting import formatEventsForOutputType
1.14 +from EventAggregatorSupport.Types import parseEvents, EventCalendar
1.15 +from codecs import getreader
1.16 +import vCalendar
1.17 +
1.18 +try:
1.19 + from cStringIO import StringIO
1.20 +except ImportError:
1.21 + from StringIO import StringIO
1.22 +
1.23 +Dependencies = ["pages"]
1.24 +
1.25 +# Parser support.
1.26 +
1.27 +class Parser:
1.28 +
1.29 + "Interpret and show calendar information in different ways."
1.30 +
1.31 + Dependencies = Dependencies
1.32 + extensions = [".ics"]
1.33 +
1.34 + # Input content types understood by this parser.
1.35 +
1.36 + input_mimetypes = ["text/calendar"]
1.37 +
1.38 + # Output content types preferred by this parser.
1.39 +
1.40 + output_mimetypes = ["text/html", "text/calendar"]
1.41 +
1.42 + def __init__(self, raw, request, **kw):
1.43 +
1.44 + """
1.45 + Initialise the parser with the given 'raw' data, 'request' and any
1.46 + keyword arguments that may have been supplied.
1.47 + """
1.48 +
1.49 + self.raw = raw
1.50 + self.request = request
1.51 + attrs = parseAttributes(kw.get("format_args", ""), False)
1.52 +
1.53 + self.fragment = attrs.get("fragment")
1.54 +
1.55 + def format(self, fmt, write=None):
1.56 +
1.57 + """
1.58 + Format a calendar using the given formatter 'fmt'. If the 'write'
1.59 + parameter is specified, use it to write output; otherwise, write output
1.60 + using the request.
1.61 + """
1.62 +
1.63 + (write or self.request.write)(fmt.text(self.raw))
1.64 +
1.65 + # Extra API methods.
1.66 +
1.67 + def formatForOutputType(self, mimetype, write=None):
1.68 +
1.69 + """
1.70 + Format a calendar for the given 'mimetype'. If the 'write' parameter is
1.71 + specified, use it to write output; otherwise, write output using the
1.72 + request.
1.73 + """
1.74 +
1.75 + # Write raw calendar information unchanged.
1.76 +
1.77 + if mimetype == "text/calendar":
1.78 + (write or request.write)(self.raw)
1.79 + else:
1.80 + # Make a Unicode-capable StringIO.
1.81 +
1.82 + f = getreader("utf-8")(StringIO(self.raw.encode("utf-8")))
1.83 + calendar = EventCalendar("", vCalendar.parse(f), {})
1.84 + formatEventsForOutputType(calendar.getEvents(), self.request, mimetype, write=write)
1.85 +
1.86 + # Class methods.
1.87 +
1.88 + def getOutputTypes(self):
1.89 + return self.output_mimetypes
1.90 +
1.91 + getOutputTypes = classmethod(getOutputTypes)
1.92 +
1.93 +# vim: tabstop=4 expandtab shiftwidth=4