1.1 --- a/EventAggregatorSupport/Resources.py Sun Mar 30 20:04:32 2014 +0200
1.2 +++ b/EventAggregatorSupport/Resources.py Sun Mar 30 20:32:50 2014 +0200
1.3 @@ -12,6 +12,9 @@
1.4 from DateSupport import Date, Month
1.5 from MoinSupport import *
1.6 from MoinRemoteSupport import getCachedResource, getCachedResourceMetadata, imapreader
1.7 +from ItemSupport import ItemStore
1.8 +
1.9 +from MoinMoin.Page import Page
1.10
1.11 import urllib
1.12
1.13 @@ -26,9 +29,9 @@
1.14
1.15 "Return a list of events found on the given 'pages'."
1.16
1.17 - # Get real pages instead of result pages.
1.18 + # Get event pages instead of result pages.
1.19
1.20 - return map(EventPage, pages)
1.21 + return EventResourceCollection(map(EventPage, pages))
1.22
1.23 def getAllEventSources(request):
1.24
1.25 @@ -64,24 +67,51 @@
1.26 elif isinstance(calendar_end, Month):
1.27 calendar_end = calendar_end.as_date(-1)
1.28
1.29 - resources = []
1.30 + resources = EventResourceCollection()
1.31
1.32 for source in sources:
1.33 - try:
1.34 - details = sources_dict[source].split()
1.35 - details.extend([None] * (3 - len(details)))
1.36 - url = details[0]
1.37 - format = details[1] or "ical"
1.38 - expected_content_type = details[2]
1.39 - except (KeyError, ValueError):
1.40 - pass
1.41 + details = sources_dict[source].split()
1.42 + type = details[0]
1.43 + resource = None
1.44 +
1.45 + # Support non-URL sources.
1.46 +
1.47 + if type == "store":
1.48 + details = details[1:]
1.49 +
1.50 + details.extend([None] * 3)
1.51 + page_name, store_name, lockdir_name = details[:3]
1.52 +
1.53 + resource = getEventResourcesFromStore(page_name, store_name, lockdir_name, request)
1.54 +
1.55 + # Unrecognised types are treated as URLs.
1.56 +
1.57 else:
1.58 + if type == "url":
1.59 + details = details[1:]
1.60 +
1.61 + details.extend([None] * 3)
1.62 + url, format, expected_content_type = details[:3]
1.63 + format = format or "ical"
1.64 +
1.65 resource = getEventResourcesFromSource(url, format, expected_content_type, calendar_start, calendar_end, request)
1.66 - if resource:
1.67 - resources.append(resource)
1.68 +
1.69 + if resource:
1.70 + resources.append(resource)
1.71
1.72 return resources
1.73
1.74 +def getEventResourcesFromStore(page_name, store_name, lockdir_name, request):
1.75 +
1.76 + """
1.77 + Return a resource object for the store indicated by the given 'page_name'
1.78 + and 'store_name' (with the accompanying 'lockdir_name', specified as None
1.79 + for a computed name to be used).
1.80 + """
1.81 +
1.82 + store = ItemStore(Page(request, page_name), store_name, lockdir_name)
1.83 + return parseEventsInStore(store, request)
1.84 +
1.85 def getEventResourcesFromSource(url, format, expected_content_type, calendar_start, calendar_end, request):
1.86
1.87 """
1.88 @@ -117,14 +147,8 @@
1.89 # NOTE: This could be done reactively by choosing a parser based on
1.90 # NOTE: the content type provided by the URL.
1.91
1.92 - if format == "ical":
1.93 - parser = parseEventsInCalendarFromResource
1.94 - required_content_type = expected_content_type or "text/calendar"
1.95 - elif format == "xcal":
1.96 - parser = parseEventsInXMLCalendarsFromResource
1.97 - required_content_type = expected_content_type or "multipart/mixed"
1.98 - else:
1.99 - return None
1.100 + parser, default_content_type = getParserForFormat(format)
1.101 + required_content_type = expected_content_type or default_content_type
1.102
1.103 # Obtain the resource, using a cached version if appropriate.
1.104
1.105 @@ -152,23 +176,18 @@
1.106 finally:
1.107 f.close()
1.108
1.109 -def getEventsFromResources(resources):
1.110 -
1.111 - "Return a list of events supplied by the given event 'resources'."
1.112 +def getParserForFormat(format):
1.113
1.114 - events = []
1.115 -
1.116 - for resource in resources:
1.117 + "Return a parser and default content type for the given 'format'."
1.118
1.119 - # Get all events described by the resource.
1.120 -
1.121 - for event in resource.getEvents():
1.122 -
1.123 - # Remember the event.
1.124 -
1.125 - events.append(event)
1.126 -
1.127 - return events
1.128 + if format == "ical":
1.129 + return parseEventsInCalendarFromResource, "text/calendar"
1.130 + elif format == "xcal":
1.131 + return parseEventsInXMLCalendarFromResource, "application/calendar+xml"
1.132 + elif format == "mbox":
1.133 + return parseEventsInMailboxFromResource, "multipart/mixed"
1.134 + else:
1.135 + return None, None
1.136
1.137 # Page-related functions.
1.138
1.139 @@ -207,8 +226,8 @@
1.140
1.141 results += getAllCategoryPages(category_names, request)
1.142 pages = getPagesFromResults(results, request)
1.143 - events = getEventsFromResources(getEventPages(pages))
1.144 - events += getEventsFromResources(getEventResources(remote_sources, calendar_start, calendar_end, request))
1.145 + events = getEventPages(pages).getEvents()
1.146 + events += getEventResources(remote_sources, calendar_start, calendar_end, request).getEvents()
1.147 all_shown_events = getEventsInPeriod(events, getCalendarPeriod(calendar_start, calendar_end))
1.148 earliest, latest = getEventLimits(all_shown_events)
1.149