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 resource = getEventResourcesFromSource(url, format, calendar_start, calendar_end, request) 84 if resource: 85 resources.append(resource) 86 87 return resources 88 89 def getEventResourcesFromSource(url, format, calendar_start, calendar_end, request): 90 91 """ 92 Return a resource object for the given 'url' providing content in the 93 specified 'format', using the given 'calendar_start' and 'calendar_end' to 94 parameterise requests to the sources and the 'request' to access 95 configuration settings in the Wiki. 96 """ 97 98 # Prevent local file access. 99 100 if url.startswith("file:"): 101 return None 102 103 # Parameterise the URL. 104 # Where other parameters are used, care must be taken to encode them 105 # properly. 106 107 url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or "")) 108 url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or "")) 109 110 # Get a parser. 111 # NOTE: This could be done reactively by choosing a parser based on 112 # NOTE: the content type provided by the URL. 113 114 if format == "ical" and vCalendar is not None: 115 parser = vCalendar.parse 116 resource_cls = EventCalendar 117 required_content_type = "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 uf = codecs.getreader(encoding or "utf-8")(f) 143 try: 144 return resource_cls(url, parser(uf), metadata) 145 finally: 146 uf.close() 147 finally: 148 f.close() 149 150 def getEventsFromResources(resources): 151 152 "Return a list of events supplied by the given event 'resources'." 153 154 events = [] 155 156 for resource in resources: 157 158 # Get all events described by the resource. 159 160 for event in resource.getEvents(): 161 162 # Remember the event. 163 164 events.append(event) 165 166 return events 167 168 # Page-related functions. 169 170 def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): 171 172 """ 173 Using the given 'template_page', complete the 'new_page' by copying the 174 template and adding the given 'event_details' (a dictionary of event 175 fields), setting also the 'category_pagenames' to define category 176 membership. 177 """ 178 179 event_page = EventPage(template_page) 180 new_event_page = EventPage(new_page) 181 new_event_page.copyPage(event_page) 182 183 if new_event_page.getFormat() == "wiki": 184 new_event = Event(new_event_page, event_details) 185 new_event_page.setEvents([new_event]) 186 new_event_page.setCategoryMembership(category_pagenames) 187 new_event_page.flushEventDetails() 188 189 return new_event_page.getBody() 190 191 # Event selection from request parameters. 192 193 def getEventsUsingParameters(category_names, search_pattern, remote_sources, 194 calendar_start, calendar_end, resolution, request): 195 196 "Get the events according to the resolution of the calendar." 197 198 if search_pattern: 199 results = getPagesForSearch(search_pattern, request) 200 else: 201 results = [] 202 203 results += getAllCategoryPages(category_names, request) 204 pages = getPagesFromResults(results, request) 205 events = getEventsFromResources(getEventPages(pages)) 206 events += getEventsFromResources(getEventResources(remote_sources, calendar_start, calendar_end, request)) 207 all_shown_events = getEventsInPeriod(events, getCalendarPeriod(calendar_start, calendar_end)) 208 earliest, latest = getEventLimits(all_shown_events) 209 210 # Get a concrete period of time. 211 212 first, last = getConcretePeriod(calendar_start, calendar_end, earliest, latest, resolution) 213 214 return all_shown_events, first, last 215 216 # vim: tabstop=4 expandtab shiftwidth=4