1.1 --- a/EventAggregatorSupport/Resources.py Mon May 06 19:39:43 2013 +0200
1.2 +++ b/EventAggregatorSupport/Resources.py Tue May 07 23:57:19 2013 +0200
1.3 @@ -80,60 +80,73 @@
1.4 except (KeyError, ValueError):
1.5 pass
1.6 else:
1.7 - # Prevent local file access.
1.8 -
1.9 - if url.startswith("file:"):
1.10 - continue
1.11 -
1.12 - # Parameterise the URL.
1.13 - # Where other parameters are used, care must be taken to encode them
1.14 - # properly.
1.15 -
1.16 - url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or ""))
1.17 - url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or ""))
1.18 -
1.19 - # Get a parser.
1.20 - # NOTE: This could be done reactively by choosing a parser based on
1.21 - # NOTE: the content type provided by the URL.
1.22 -
1.23 - if format == "ical" and vCalendar is not None:
1.24 - parser = vCalendar.parse
1.25 - resource_cls = EventCalendar
1.26 - required_content_type = "text/calendar"
1.27 - else:
1.28 - continue
1.29 -
1.30 - # Obtain the resource, using a cached version if appropriate.
1.31 -
1.32 - max_cache_age = int(getattr(request.cfg, "event_aggregator_max_cache_age", "300"))
1.33 - data = getCachedResource(request, url, "EventAggregator", "wiki", max_cache_age)
1.34 - if not data:
1.35 - continue
1.36 -
1.37 - # Process the entry, parsing the content.
1.38 -
1.39 - f = StringIO(data)
1.40 - try:
1.41 - # Get the content type and encoding, making sure that the data
1.42 - # can be parsed.
1.43 -
1.44 - url, content_type, encoding, metadata = getCachedResourceMetadata(f)
1.45 -
1.46 - if content_type != required_content_type:
1.47 - continue
1.48 -
1.49 - # Send the data to the parser.
1.50 -
1.51 - uf = codecs.getreader(encoding or "utf-8")(f)
1.52 - try:
1.53 - resources.append(resource_cls(url, parser(uf), metadata))
1.54 - finally:
1.55 - uf.close()
1.56 - finally:
1.57 - f.close()
1.58 + resource = getEventResourcesFromSource(url, format, calendar_start, calendar_end, request)
1.59 + if resource:
1.60 + resources.append(resource)
1.61
1.62 return resources
1.63
1.64 +def getEventResourcesFromSource(url, format, calendar_start, calendar_end, request):
1.65 +
1.66 + """
1.67 + Return a resource object for the given 'url' providing content in the
1.68 + specified 'format', using the given 'calendar_start' and 'calendar_end' to
1.69 + parameterise requests to the sources and the 'request' to access
1.70 + configuration settings in the Wiki.
1.71 + """
1.72 +
1.73 + # Prevent local file access.
1.74 +
1.75 + if url.startswith("file:"):
1.76 + return None
1.77 +
1.78 + # Parameterise the URL.
1.79 + # Where other parameters are used, care must be taken to encode them
1.80 + # properly.
1.81 +
1.82 + url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or ""))
1.83 + url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or ""))
1.84 +
1.85 + # Get a parser.
1.86 + # NOTE: This could be done reactively by choosing a parser based on
1.87 + # NOTE: the content type provided by the URL.
1.88 +
1.89 + if format == "ical" and vCalendar is not None:
1.90 + parser = vCalendar.parse
1.91 + resource_cls = EventCalendar
1.92 + required_content_type = "text/calendar"
1.93 + else:
1.94 + return None
1.95 +
1.96 + # Obtain the resource, using a cached version if appropriate.
1.97 +
1.98 + max_cache_age = int(getattr(request.cfg, "event_aggregator_max_cache_age", "300"))
1.99 + data = getCachedResource(request, url, "EventAggregator", "wiki", max_cache_age)
1.100 + if not data:
1.101 + return None
1.102 +
1.103 + # Process the entry, parsing the content.
1.104 +
1.105 + f = StringIO(data)
1.106 + try:
1.107 + # Get the content type and encoding, making sure that the data
1.108 + # can be parsed.
1.109 +
1.110 + url, content_type, encoding, metadata = getCachedResourceMetadata(f)
1.111 +
1.112 + if content_type != required_content_type:
1.113 + return None
1.114 +
1.115 + # Send the data to the parser.
1.116 +
1.117 + uf = codecs.getreader(encoding or "utf-8")(f)
1.118 + try:
1.119 + return resource_cls(url, parser(uf), metadata)
1.120 + finally:
1.121 + uf.close()
1.122 + finally:
1.123 + f.close()
1.124 +
1.125 def getEventsFromResources(resources):
1.126
1.127 "Return a list of events supplied by the given event 'resources'."