# HG changeset patch # User Paul Boddie # Date 1367963839 -7200 # Node ID ae498018f49d09925540deee4d50a86f5e9332a5 # Parent 1c01f53b8d6e73f266e579c9fc277366567e4c4a Moved the resource parsing into a separate function in anticipation of the handling of resource collections such as RSS or Atom feeds. diff -r 1c01f53b8d6e -r ae498018f49d EventAggregatorSupport/Resources.py --- a/EventAggregatorSupport/Resources.py Mon May 06 19:39:43 2013 +0200 +++ b/EventAggregatorSupport/Resources.py Tue May 07 23:57:19 2013 +0200 @@ -80,60 +80,73 @@ except (KeyError, ValueError): pass else: - # Prevent local file access. - - if url.startswith("file:"): - continue - - # Parameterise the URL. - # Where other parameters are used, care must be taken to encode them - # properly. - - url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or "")) - url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or "")) - - # Get a parser. - # NOTE: This could be done reactively by choosing a parser based on - # NOTE: the content type provided by the URL. - - if format == "ical" and vCalendar is not None: - parser = vCalendar.parse - resource_cls = EventCalendar - required_content_type = "text/calendar" - else: - continue - - # Obtain the resource, using a cached version if appropriate. - - max_cache_age = int(getattr(request.cfg, "event_aggregator_max_cache_age", "300")) - data = getCachedResource(request, url, "EventAggregator", "wiki", max_cache_age) - if not data: - continue - - # Process the entry, parsing the content. - - f = StringIO(data) - try: - # Get the content type and encoding, making sure that the data - # can be parsed. - - url, content_type, encoding, metadata = getCachedResourceMetadata(f) - - if content_type != required_content_type: - continue - - # Send the data to the parser. - - uf = codecs.getreader(encoding or "utf-8")(f) - try: - resources.append(resource_cls(url, parser(uf), metadata)) - finally: - uf.close() - finally: - f.close() + resource = getEventResourcesFromSource(url, format, calendar_start, calendar_end, request) + if resource: + resources.append(resource) return resources +def getEventResourcesFromSource(url, format, calendar_start, calendar_end, request): + + """ + Return a resource object for the given 'url' providing content in the + specified 'format', using the given 'calendar_start' and 'calendar_end' to + parameterise requests to the sources and the 'request' to access + configuration settings in the Wiki. + """ + + # Prevent local file access. + + if url.startswith("file:"): + return None + + # Parameterise the URL. + # Where other parameters are used, care must be taken to encode them + # properly. + + url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or "")) + url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or "")) + + # Get a parser. + # NOTE: This could be done reactively by choosing a parser based on + # NOTE: the content type provided by the URL. + + if format == "ical" and vCalendar is not None: + parser = vCalendar.parse + resource_cls = EventCalendar + required_content_type = "text/calendar" + else: + return None + + # Obtain the resource, using a cached version if appropriate. + + max_cache_age = int(getattr(request.cfg, "event_aggregator_max_cache_age", "300")) + data = getCachedResource(request, url, "EventAggregator", "wiki", max_cache_age) + if not data: + return None + + # Process the entry, parsing the content. + + f = StringIO(data) + try: + # Get the content type and encoding, making sure that the data + # can be parsed. + + url, content_type, encoding, metadata = getCachedResourceMetadata(f) + + if content_type != required_content_type: + return None + + # Send the data to the parser. + + uf = codecs.getreader(encoding or "utf-8")(f) + try: + return resource_cls(url, parser(uf), metadata) + finally: + uf.close() + finally: + f.close() + def getEventsFromResources(resources): "Return a list of events supplied by the given event 'resources'."