1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/parsers/xcalendar.py Tue Jan 28 23:30:33 2014 +0100
1.3 @@ -0,0 +1,83 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - xcalendar (EventAggregator)
1.7 +
1.8 + @copyright: 2012, 2013, 2014 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, RawParser
1.13 +from EventAggregatorSupport.Formatting import formatEventsForOutputType, \
1.14 + formatEvent
1.15 +from EventAggregatorSupport.Types import parseEventsInPage, EventPage, \
1.16 + parseEventsInXMLCalendar
1.17 +
1.18 +Dependencies = ["pages"]
1.19 +
1.20 +# Parser support.
1.21 +
1.22 +class Parser:
1.23 +
1.24 + "Interpret and show calendar information in different ways."
1.25 +
1.26 + Dependencies = Dependencies
1.27 + extensions = [".xcs"]
1.28 +
1.29 + # Input content types understood by this parser.
1.30 +
1.31 + input_mimetypes = ["application/calendar+xml"]
1.32 +
1.33 + # Output content types preferred by this parser.
1.34 +
1.35 + output_mimetypes = ["text/html", "application/calendar+xml"]
1.36 +
1.37 + def __init__(self, raw, request, **kw):
1.38 +
1.39 + """
1.40 + Initialise the parser with the given 'raw' data, 'request' and any
1.41 + keyword arguments that may have been supplied.
1.42 + """
1.43 +
1.44 + self.raw = raw
1.45 + self.request = request
1.46 + attrs = parseAttributes(kw.get("format_args", ""), False)
1.47 +
1.48 + self.fragment = attrs.get("fragment")
1.49 +
1.50 + def format(self, fmt, write=None):
1.51 +
1.52 + """
1.53 + Format a calendar using the given formatter 'fmt'. If the 'write'
1.54 + parameter is specified, use it to write output; otherwise, write output
1.55 + using the request.
1.56 + """
1.57 +
1.58 + for event in parseEventsInXMLCalendar(self.raw):
1.59 + formatEvent(event, self.request, fmt, write=write, parser_cls=RawParser)
1.60 +
1.61 + # Extra API methods.
1.62 +
1.63 + def formatForOutputType(self, mimetype, write=None):
1.64 +
1.65 + """
1.66 + Format a calendar for the given 'mimetype'. If the 'write' parameter is
1.67 + specified, use it to write output; otherwise, write output using the
1.68 + request.
1.69 + """
1.70 +
1.71 + # Write raw calendar information unchanged.
1.72 +
1.73 + if mimetype == "application/calendar+xml":
1.74 + (write or request.write)(self.raw)
1.75 + else:
1.76 + events = parseEventsInXMLCalendar(self.raw)
1.77 + formatEventsForOutputType(events, self.request, mimetype, write=write)
1.78 +
1.79 + # Class methods.
1.80 +
1.81 + def getOutputTypes(self):
1.82 + return self.output_mimetypes
1.83 +
1.84 + getOutputTypes = classmethod(getOutputTypes)
1.85 +
1.86 +# vim: tabstop=4 expandtab shiftwidth=4