# HG changeset patch # User Paul Boddie # Date 1264723553 -3600 # Node ID 817c6d531cf81af179c9ca1eb365506c3625628e # Parent e3ab5b5e9464be3d09a1578c964dc5420cf6c03c Made a separate Event class whose instances hold the details of each event featured on an event page, adding support to the EventPage class for the retrieval of many events on a page, renaming various methods and moving others. diff -r e3ab5b5e9464 -r 817c6d531cf8 EventAggregatorSupport.py --- a/EventAggregatorSupport.py Thu Jan 28 23:57:15 2010 +0100 +++ b/EventAggregatorSupport.py Fri Jan 29 01:05:53 2010 +0100 @@ -16,6 +16,11 @@ import time import re +try: + set +except NameError: + from sets import Set as set + __version__ = "0.5" # Date labels. @@ -190,23 +195,10 @@ def __init__(self, page): self.page = page - self.details = None + self.events = None self.body = None self.categories = None - def __cmp__(self, other): - - """ - Compare this object with 'other' using the event start and end details. - """ - - event_details1 = self.getEventDetails() - event_details2 = other.getEventDetails() - return cmp( - (event_details1["start"], event_details1["end"]), - (event_details2["start"], event_details2["end"]) - ) - def copyPage(self, page): "Copy the body of the given 'page'." @@ -265,12 +257,13 @@ self.body = self.page.get_raw_body() return self.body - def getEventDetails(self): + def getEvents(self): + + "Return a list of events from this page." - "Return a dictionary of event details from this page." - - if self.details is None: - self.details = {} + if self.events is None: + details = {} + self.events = [Event(self, details)] if self.getFormat() == "wiki": for match in definition_list_regexp.finditer(self.getBody()): @@ -303,9 +296,23 @@ desc = getSimpleWikiText(desc) if desc is not None: - self.details[term] = desc + + # Handle apparent duplicates by creating a new set of + # details. + + if details.has_key(term): + details = {} + self.events.append(Event(self, details)) - return self.details + details[term] = desc + + return self.events + + def setEvents(self, events): + + "Set the given 'events' on this page." + + self.events = events def getCategoryMembership(self): @@ -318,40 +325,6 @@ return self.categories - def getEventSummary(self, event_parent=None): - - """ - Return either the given title or summary of the event described by this - page, according to the page's event details, or using the pretty version - of the page name. - - If the optional 'event_parent' is specified, any page beneath the given - 'event_parent' page in the page hierarchy will omit this parent information - if its name is used as the summary. - """ - - event_details = self.getEventDetails() - - if event_details.has_key("title"): - return event_details["title"] - elif event_details.has_key("summary"): - return event_details["summary"] - else: - # If appropriate, remove the parent details and "/" character. - - title = self.getPageName() - - if event_parent is not None and title.startswith(event_parent): - title = title[len(event_parent.rstrip("/")) + 1:] - - return getPrettyTitle(title) - - def setEventDetails(self, event_details): - - "Set the 'event_details' for this page." - - self.details = event_details - def setCategoryMembership(self, category_names): """ @@ -368,14 +341,18 @@ new_body_parts = [] end_of_last_match = 0 body = self.getBody() - event_details = self.getEventDetails() + + events = iter(self.getEvents()) + + event = events.next() + event_details = event.getDetails() + replaced_terms = set() for match in definition_list_regexp.finditer(body): # Add preceding text to the new body. new_body_parts.append(body[end_of_last_match:match.start()]) - end_of_last_match = match.end() # Get the matching regions, adding the term to the new body. @@ -386,6 +363,21 @@ term = match.group("term").lower() desc = match.group("desc") + # Check that the term has not already been substituted. If so, + # get the next event. + + if term in replaced_terms: + try: + event = events.next() + + # No more events. + + except StopIteration: + break + + event_details = event.getDetails() + replaced_terms = set() + # Special value type handling. if event_details.has_key(term): @@ -410,10 +402,17 @@ elif term in ("description",): desc = event_details[term] + replaced_terms.add(term) + new_body_parts.append(desc) - else: - new_body_parts.append(body[end_of_last_match:]) + # Remember where in the page has been processed. + + end_of_last_match = match.end() + + # Write the rest of the page. + + new_body_parts.append(body[end_of_last_match:]) self.body = "".join(new_body_parts) @@ -445,6 +444,79 @@ return linkToPage(request, self.page, text, query_string) +class Event: + + "A description of an event." + + def __init__(self, page, details): + self.page = page + self.details = details + + def __cmp__(self, other): + + """ + Compare this object with 'other' using the event start and end details. + """ + + details1 = self.details + details2 = other.details + return cmp( + (details1["start"], details1["end"]), + (details2["start"], details2["end"]) + ) + + def getPage(self): + + "Return the page describing this event." + + return self.page + + def setPage(self, page): + + "Set the 'page' describing this event." + + self.page = page + + def getSummary(self, event_parent=None): + + """ + Return either the given title or summary of the event according to the + event details, or a summary made from using the pretty version of the + page name. + + If the optional 'event_parent' is specified, any page beneath the given + 'event_parent' page in the page hierarchy will omit this parent information + if its name is used as the summary. + """ + + event_details = self.details + + if event_details.has_key("title"): + return event_details["title"] + elif event_details.has_key("summary"): + return event_details["summary"] + else: + # If appropriate, remove the parent details and "/" character. + + title = self.page.getPageName() + + if event_parent is not None and title.startswith(event_parent): + title = title[len(event_parent.rstrip("/")) + 1:] + + return getPrettyTitle(title) + + def getDetails(self): + + "Return the details for this event." + + return self.details + + def setDetails(self, event_details): + + "Set the 'event_details' for this event." + + self.details = event_details + def getEvents(request, category_names, calendar_start=None, calendar_end=None): """ @@ -493,41 +565,45 @@ # Get a real page, not a result page. event_page = EventPage(Page(request, pagename)) - event_details = event_page.getEventDetails() - # Remember the event page. + # Get all events described in the page. - events.append(event_page) + for event in event_page.getEvents(): + event_details = event.getDetails() - # Test for the suitability of the event. + # Remember the event. + + events.append(event) - if event_details.has_key("start") and event_details.has_key("end"): + # Test for the suitability of the event. + + if event_details.has_key("start") and event_details.has_key("end"): - start_month = event_details["start"].as_month() - end_month = event_details["end"].as_month() + start_month = event_details["start"].as_month() + end_month = event_details["end"].as_month() - # Compare the months of the dates to the requested calendar - # window, if any. + # Compare the months of the dates to the requested calendar + # window, if any. - if (calendar_start is None or end_month >= calendar_start) and \ - (calendar_end is None or start_month <= calendar_end): + if (calendar_start is None or end_month >= calendar_start) and \ + (calendar_end is None or start_month <= calendar_end): - all_shown_events.append(event_page) + all_shown_events.append(event) - if earliest is None or start_month < earliest: - earliest = start_month - if latest is None or end_month > latest: - latest = end_month + if earliest is None or start_month < earliest: + earliest = start_month + if latest is None or end_month > latest: + latest = end_month - # Store the event in the month-specific dictionary. + # Store the event in the month-specific dictionary. - first = max(start_month, calendar_start or start_month) - last = min(end_month, calendar_end or end_month) + first = max(start_month, calendar_start or start_month) + last = min(end_month, calendar_end or end_month) - for event_month in first.months_until(last): - if not shown_events.has_key(event_month): - shown_events[event_month] = [] - shown_events[event_month].append(event_page) + for event_month in first.months_until(last): + if not shown_events.has_key(event_month): + shown_events[event_month] = [] + shown_events[event_month].append(event) return events, shown_events, all_shown_events, earliest, latest @@ -542,8 +618,9 @@ latest = None - for event_page in events: - event_details = event_page.getEventDetails() + for event in events: + event_details = event.getDetails() + event_page = event.getPage() # Get the initial revision of the page. @@ -620,8 +697,8 @@ # Get event details. - for event_page in events: - event_details = event_page.getEventDetails() + for event in events: + event_details = event.getDetails() # Test for the event in the period. @@ -645,7 +722,7 @@ # element, add it alongside existing events. if not coverage.intersection(event_coverage): - covered_events.append(event_page) + covered_events.append(event) all_events[i] = coverage.union(event_coverage), covered_events break @@ -653,7 +730,7 @@ # marked alongside existing events. else: - all_events.append((event_coverage, [event_page])) + all_events.append((event_coverage, [event])) return full_coverage, all_events diff -r e3ab5b5e9464 -r 817c6d531cf8 actions/EventAggregatorNewEvent.py --- a/actions/EventAggregatorNewEvent.py Thu Jan 28 23:57:15 2010 +0100 +++ b/actions/EventAggregatorNewEvent.py Fri Jan 29 01:05:53 2010 +0100 @@ -254,7 +254,8 @@ "title" : title, "summary" : title, "description" : description } - new_event_page.setEventDetails(event_details) + new_event = EventAggregatorSupport.Event(new_event_page, event_details) + new_event_page.setEvents([new_event]) new_event_page.setCategoryMembership(category_pagenames) new_event_page.saveChanges() diff -r e3ab5b5e9464 -r 817c6d531cf8 actions/EventAggregatorSummary.py --- a/actions/EventAggregatorSummary.py Thu Jan 28 23:57:15 2010 +0100 +++ b/actions/EventAggregatorSummary.py Fri Jan 29 01:05:53 2010 +0100 @@ -219,12 +219,13 @@ request.write("PRODID:-//MoinMoin//EventAggregatorSummary\r\n") request.write("VERSION:2.0\r\n") - for event_page in all_shown_events: - event_details = event_page.getEventDetails() + for event in all_shown_events: + event_page = event.getPage() + event_details = event.getDetails() # Get the summary details. - event_summary = event_page.getEventSummary() + event_summary = event.getSummary() link = event_page.getPageURL(request) # Output the event details. @@ -274,12 +275,13 @@ ordered_events = EventAggregatorSupport.getOrderedEvents(all_shown_events) ordered_events.reverse() - for event_page in ordered_events: - event_details = event_page.getEventDetails() + for event in ordered_events: + event_page = event.getPage() + event_details = event.getDetails() # Get the summary details. - event_summary = event_page.getEventSummary() + event_summary = event.getSummary() link = event_page.getPageURL(request) request.write('\r\n') diff -r e3ab5b5e9464 -r 817c6d531cf8 macros/EventAggregator.py --- a/macros/EventAggregator.py Thu Jan 28 23:57:15 2010 +0100 +++ b/macros/EventAggregator.py Fri Jan 29 01:05:53 2010 +0100 @@ -410,9 +410,10 @@ # Show the events in order. - for event_page in ordered_events: - event_summary = event_page.getEventSummary(parent_name) - event_details = event_page.getEventDetails() + for event in ordered_events: + event_page = event.getPage() + event_summary = event.getSummary(parent_name) + event_details = event.getDetails() # Prepare CSS classes with category-related styling. @@ -623,8 +624,9 @@ # Get event details for the current day. - for event_page in events: - event_details = event_page.getEventDetails() + for event in events: + event_page = event.getPage() + event_details = event.getDetails() if not (event_details["start"] <= date <= event_details["end"]): continue @@ -633,7 +635,7 @@ starts_today = event_details["start"] == date ends_today = event_details["end"] == date - event_summary = event_page.getEventSummary(parent_name) + event_summary = event.getSummary(parent_name) # Generate a colour for the event. @@ -837,9 +839,10 @@ # Show the events in order. - for event_page in ordered_events: - event_details = event_page.getEventDetails() - event_summary = event_page.getEventSummary(parent_name) + for event in ordered_events: + event_page = event.getPage() + event_details = event.getDetails() + event_summary = event.getSummary(parent_name) output.append(fmt.listitem(on=1, attr={"class" : "event-listing"}))