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