1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - EventAggregator resource acquisition and access 4 5 @copyright: 2008, 2009, 2010, 2011, 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 EventAggregatorSupport.Filter import * 10 from EventAggregatorSupport.Types import * 11 12 from DateSupport import Date, Month 13 from MoinSupport import * 14 from MoinRemoteSupport import getCachedResource, getCachedResourceMetadata 15 16 import codecs 17 import urllib 18 19 try: 20 from cStringIO import StringIO 21 except ImportError: 22 from StringIO import StringIO 23 24 try: 25 import vCalendar 26 except ImportError: 27 vCalendar = None 28 29 # Obtaining event containers and events from such containers. 30 31 def getEventPages(pages): 32 33 "Return a list of events found on the given 'pages'." 34 35 # Get real pages instead of result pages. 36 37 return map(EventPage, pages) 38 39 def getAllEventSources(request): 40 41 "Return all event sources defined in the Wiki using the 'request'." 42 43 sources_page = getattr(request.cfg, "event_aggregator_sources_page", "EventSourcesDict") 44 45 # Remote sources are accessed via dictionary page definitions. 46 47 return getWikiDict(sources_page, request) 48 49 def getEventResources(sources, calendar_start, calendar_end, request): 50 51 """ 52 Return resource objects for the given 'sources' using the given 53 'calendar_start' and 'calendar_end' to parameterise requests to the sources, 54 and the 'request' to access configuration settings in the Wiki. 55 """ 56 57 sources_dict = getAllEventSources(request) 58 if not sources_dict: 59 return [] 60 61 # Use dates for the calendar limits. 62 63 if isinstance(calendar_start, Date): 64 pass 65 elif isinstance(calendar_start, Month): 66 calendar_start = calendar_start.as_date(1) 67 68 if isinstance(calendar_end, Date): 69 pass 70 elif isinstance(calendar_end, Month): 71 calendar_end = calendar_end.as_date(-1) 72 73 resources = [] 74 75 for source in sources: 76 try: 77 details = sources_dict[source].split() 78 url = details[0] 79 format = (details[1:] or ["ical"])[0] 80 except (KeyError, ValueError): 81 pass 82 else: 83 # Prevent local file access. 84 85 if url.startswith("file:"): 86 continue 87 88 # Parameterise the URL. 89 # Where other parameters are used, care must be taken to encode them 90 # properly. 91 92 url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or "")) 93 url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or "")) 94 95 # Get a parser. 96 # NOTE: This could be done reactively by choosing a parser based on 97 # NOTE: the content type provided by the URL. 98 99 if format == "ical" and vCalendar is not None: 100 parser = vCalendar.parse 101 resource_cls = EventCalendar 102 required_content_type = "text/calendar" 103 else: 104 continue 105 106 # Obtain the resource, using a cached version if appropriate. 107 108 max_cache_age = int(getattr(request.cfg, "event_aggregator_max_cache_age", "300")) 109 data = getCachedResource(request, url, "EventAggregator", "wiki", max_cache_age) 110 if not data: 111 continue 112 113 # Process the entry, parsing the content. 114 115 f = StringIO(data) 116 try: 117 # Get the content type and encoding, making sure that the data 118 # can be parsed. 119 120 url, content_type, encoding, metadata = getCachedResourceMetadata(f) 121 122 if content_type != required_content_type: 123 continue 124 125 # Send the data to the parser. 126 127 uf = codecs.getreader(encoding or "utf-8")(f) 128 try: 129 resources.append(resource_cls(url, parser(uf), metadata)) 130 finally: 131 uf.close() 132 finally: 133 f.close() 134 135 return resources 136 137 def getEventsFromResources(resources): 138 139 "Return a list of events supplied by the given event 'resources'." 140 141 events = [] 142 143 for resource in resources: 144 145 # Get all events described by the resource. 146 147 for event in resource.getEvents(): 148 149 # Remember the event. 150 151 events.append(event) 152 153 return events 154 155 # Page-related functions. 156 157 def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): 158 159 """ 160 Using the given 'template_page', complete the 'new_page' by copying the 161 template and adding the given 'event_details' (a dictionary of event 162 fields), setting also the 'category_pagenames' to define category 163 membership. 164 """ 165 166 event_page = EventPage(template_page) 167 new_event_page = EventPage(new_page) 168 new_event_page.copyPage(event_page) 169 170 if new_event_page.getFormat() == "wiki": 171 new_event = Event(new_event_page, event_details) 172 new_event_page.setEvents([new_event]) 173 new_event_page.setCategoryMembership(category_pagenames) 174 new_event_page.flushEventDetails() 175 176 return new_event_page.getBody() 177 178 # Event selection from request parameters. 179 180 def getEventsUsingParameters(category_names, search_pattern, remote_sources, 181 calendar_start, calendar_end, resolution, request): 182 183 "Get the events according to the resolution of the calendar." 184 185 if search_pattern: 186 results = getPagesForSearch(search_pattern, request) 187 else: 188 results = [] 189 190 results += getAllCategoryPages(category_names, request) 191 pages = getPagesFromResults(results, request) 192 events = getEventsFromResources(getEventPages(pages)) 193 events += getEventsFromResources(getEventResources(remote_sources, calendar_start, calendar_end, request)) 194 all_shown_events = getEventsInPeriod(events, getCalendarPeriod(calendar_start, calendar_end)) 195 earliest, latest = getEventLimits(all_shown_events) 196 197 # Get a concrete period of time. 198 199 first, last = getConcretePeriod(calendar_start, calendar_end, earliest, latest, resolution) 200 201 return all_shown_events, first, last 202 203 # vim: tabstop=4 expandtab shiftwidth=4