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