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