paul@367 | 1 | # -*- coding: iso-8859-1 -*- |
paul@367 | 2 | """ |
paul@367 | 3 | MoinMoin - calendar (EventAggregator) |
paul@367 | 4 | |
paul@392 | 5 | @copyright: 2012, 2013, 2014 by Paul Boddie <paul@boddie.org.uk> |
paul@367 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@367 | 7 | """ |
paul@367 | 8 | |
paul@393 | 9 | from MoinSupport import parseAttributes, RawParser |
paul@392 | 10 | from EventAggregatorSupport.Formatting import formatEventsForOutputType, \ |
paul@392 | 11 | formatEvent |
paul@428 | 12 | from EventAggregatorSupport.Types import parseEventsInCalendar |
paul@367 | 13 | |
paul@367 | 14 | Dependencies = ["pages"] |
paul@367 | 15 | |
paul@367 | 16 | # Parser support. |
paul@367 | 17 | |
paul@367 | 18 | class Parser: |
paul@367 | 19 | |
paul@367 | 20 | "Interpret and show calendar information in different ways." |
paul@367 | 21 | |
paul@367 | 22 | Dependencies = Dependencies |
paul@367 | 23 | extensions = [".ics"] |
paul@367 | 24 | |
paul@367 | 25 | # Input content types understood by this parser. |
paul@367 | 26 | |
paul@367 | 27 | input_mimetypes = ["text/calendar"] |
paul@367 | 28 | |
paul@367 | 29 | # Output content types preferred by this parser. |
paul@367 | 30 | |
paul@367 | 31 | output_mimetypes = ["text/html", "text/calendar"] |
paul@367 | 32 | |
paul@367 | 33 | def __init__(self, raw, request, **kw): |
paul@367 | 34 | |
paul@367 | 35 | """ |
paul@367 | 36 | Initialise the parser with the given 'raw' data, 'request' and any |
paul@367 | 37 | keyword arguments that may have been supplied. |
paul@367 | 38 | """ |
paul@367 | 39 | |
paul@367 | 40 | self.raw = raw |
paul@367 | 41 | self.request = request |
paul@367 | 42 | attrs = parseAttributes(kw.get("format_args", ""), False) |
paul@367 | 43 | |
paul@367 | 44 | self.fragment = attrs.get("fragment") |
paul@367 | 45 | |
paul@367 | 46 | def format(self, fmt, write=None): |
paul@367 | 47 | |
paul@367 | 48 | """ |
paul@367 | 49 | Format a calendar using the given formatter 'fmt'. If the 'write' |
paul@367 | 50 | parameter is specified, use it to write output; otherwise, write output |
paul@367 | 51 | using the request. |
paul@367 | 52 | """ |
paul@367 | 53 | |
paul@428 | 54 | for event in parseEventsInCalendar(self.raw).getEvents(): |
paul@393 | 55 | formatEvent(event, self.request, fmt, write=write, parser_cls=RawParser) |
paul@367 | 56 | |
paul@367 | 57 | # Extra API methods. |
paul@367 | 58 | |
paul@367 | 59 | def formatForOutputType(self, mimetype, write=None): |
paul@367 | 60 | |
paul@367 | 61 | """ |
paul@367 | 62 | Format a calendar for the given 'mimetype'. If the 'write' parameter is |
paul@367 | 63 | specified, use it to write output; otherwise, write output using the |
paul@367 | 64 | request. |
paul@367 | 65 | """ |
paul@367 | 66 | |
paul@367 | 67 | # Write raw calendar information unchanged. |
paul@367 | 68 | |
paul@367 | 69 | if mimetype == "text/calendar": |
paul@367 | 70 | (write or request.write)(self.raw) |
paul@367 | 71 | else: |
paul@428 | 72 | events = parseEventsInCalendar(self.raw).getEvents() |
paul@395 | 73 | formatEventsForOutputType(events, self.request, mimetype, write=write) |
paul@367 | 74 | |
paul@367 | 75 | # Class methods. |
paul@367 | 76 | |
paul@367 | 77 | def getOutputTypes(self): |
paul@367 | 78 | return self.output_mimetypes |
paul@367 | 79 | |
paul@367 | 80 | getOutputTypes = classmethod(getOutputTypes) |
paul@367 | 81 | |
paul@367 | 82 | # vim: tabstop=4 expandtab shiftwidth=4 |