paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregator library |
paul@10 | 4 | |
paul@10 | 5 | @copyright: 2008, 2009 by Paul Boddie <paul@boddie.org.uk> |
paul@10 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@10 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@10 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@10 | 9 | """ |
paul@10 | 10 | |
paul@10 | 11 | from MoinMoin.Page import Page |
paul@10 | 12 | from MoinMoin import search, version |
paul@10 | 13 | import calendar |
paul@10 | 14 | import re |
paul@10 | 15 | |
paul@10 | 16 | __version__ = "0.1" |
paul@10 | 17 | |
paul@10 | 18 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 19 | |
paul@10 | 20 | category_regexp = None |
paul@10 | 21 | definition_list_regexp = re.compile(ur'^\s+(?P<term>.*?)::\s(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@10 | 22 | date_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})', re.UNICODE) |
paul@10 | 23 | month_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})', re.UNICODE) |
paul@10 | 24 | |
paul@10 | 25 | # Utility functions. |
paul@10 | 26 | |
paul@10 | 27 | def isMoin15(): |
paul@10 | 28 | return version.release.startswith("1.5.") |
paul@10 | 29 | |
paul@10 | 30 | def getCategoryPattern(request): |
paul@10 | 31 | global category_regexp |
paul@10 | 32 | |
paul@10 | 33 | try: |
paul@10 | 34 | return request.cfg.cache.page_category_regexact |
paul@10 | 35 | except AttributeError: |
paul@10 | 36 | |
paul@10 | 37 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 38 | |
paul@10 | 39 | if category_regexp is None: |
paul@10 | 40 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 41 | return category_regexp |
paul@10 | 42 | |
paul@10 | 43 | # The main activity functions. |
paul@10 | 44 | |
paul@10 | 45 | def getPages(pagename, request): |
paul@10 | 46 | |
paul@10 | 47 | "Return the links minus category links for 'pagename' using the 'request'." |
paul@10 | 48 | |
paul@10 | 49 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@10 | 50 | if isMoin15(): |
paul@10 | 51 | results = search.searchPages(request, query) |
paul@10 | 52 | results.sortByPagename() |
paul@10 | 53 | else: |
paul@10 | 54 | results = search.searchPages(request, query, "page_name") |
paul@10 | 55 | |
paul@10 | 56 | cat_pattern = getCategoryPattern(request) |
paul@10 | 57 | pages = [] |
paul@10 | 58 | for page in results.hits: |
paul@10 | 59 | if not cat_pattern.match(page.page_name): |
paul@10 | 60 | pages.append(page) |
paul@10 | 61 | return pages |
paul@10 | 62 | |
paul@10 | 63 | def getPrettyPageName(page): |
paul@10 | 64 | |
paul@10 | 65 | "Return a nicely formatted title/name for the given 'page'." |
paul@10 | 66 | |
paul@10 | 67 | return page.split_title(force=1).replace("_", " ").replace("/", u" ? ") |
paul@10 | 68 | |
paul@10 | 69 | def getEventDetails(page): |
paul@10 | 70 | |
paul@10 | 71 | "Return a dictionary of event details from the given 'page'." |
paul@10 | 72 | |
paul@10 | 73 | event_details = {} |
paul@10 | 74 | |
paul@10 | 75 | if page.pi["format"] == "wiki": |
paul@10 | 76 | for match in definition_list_regexp.finditer(page.body): |
paul@10 | 77 | |
paul@10 | 78 | # Permit case-insensitive list terms. |
paul@10 | 79 | |
paul@10 | 80 | term = match.group("term").lower() |
paul@10 | 81 | desc = match.group("desc") |
paul@10 | 82 | |
paul@10 | 83 | # Special value type handling. |
paul@10 | 84 | |
paul@10 | 85 | if term in ("start", "end"): |
paul@10 | 86 | desc = getDate(desc) |
paul@10 | 87 | elif term in ("topics",): |
paul@10 | 88 | desc = [value.strip() for value in desc.split(",")] |
paul@10 | 89 | |
paul@10 | 90 | if desc is not None: |
paul@10 | 91 | event_details[term] = desc |
paul@10 | 92 | |
paul@10 | 93 | return event_details |
paul@10 | 94 | |
paul@10 | 95 | def getDate(s): |
paul@10 | 96 | |
paul@10 | 97 | "Parse the string 's', extracting and returning a date string." |
paul@10 | 98 | |
paul@10 | 99 | m = date_regexp.search(s) |
paul@10 | 100 | if m: |
paul@10 | 101 | return tuple(map(int, m.groups())) |
paul@10 | 102 | else: |
paul@10 | 103 | return None |
paul@10 | 104 | |
paul@10 | 105 | def getMonth(s): |
paul@10 | 106 | |
paul@10 | 107 | "Parse the string 's', extracting and returning a month string." |
paul@10 | 108 | |
paul@10 | 109 | m = month_regexp.search(s) |
paul@10 | 110 | if m: |
paul@10 | 111 | return tuple(map(int, m.groups())) |
paul@10 | 112 | else: |
paul@10 | 113 | return None |
paul@10 | 114 | |
paul@10 | 115 | def daterange(first, last): |
paul@10 | 116 | results = [] |
paul@10 | 117 | |
paul@10 | 118 | months_only = len(first) == 2 |
paul@10 | 119 | start_year = first[0] |
paul@10 | 120 | end_year = last[0] |
paul@10 | 121 | |
paul@10 | 122 | for year in range(start_year, end_year + 1): |
paul@10 | 123 | if year < end_year: |
paul@10 | 124 | end_month = 12 |
paul@10 | 125 | else: |
paul@10 | 126 | end_month = last[1] |
paul@10 | 127 | |
paul@10 | 128 | if year > start_year: |
paul@10 | 129 | start_month = 1 |
paul@10 | 130 | else: |
paul@10 | 131 | start_month = first[1] |
paul@10 | 132 | |
paul@10 | 133 | for month in range(start_month, end_month + 1): |
paul@10 | 134 | if months_only: |
paul@10 | 135 | results.append((year, month)) |
paul@10 | 136 | else: |
paul@10 | 137 | if month < end_month: |
paul@10 | 138 | _wd, end_day = calendar.monthrange(year, month) |
paul@10 | 139 | else: |
paul@10 | 140 | end_day = last[2] |
paul@10 | 141 | |
paul@10 | 142 | if month > start_month: |
paul@10 | 143 | start_day = 1 |
paul@10 | 144 | else: |
paul@10 | 145 | start_day = first[2] |
paul@10 | 146 | |
paul@10 | 147 | for day in range(start_day, end_day + 1): |
paul@10 | 148 | results.append((year, month, day)) |
paul@10 | 149 | |
paul@10 | 150 | return results |
paul@10 | 151 | |
paul@10 | 152 | def nextdate(date): |
paul@10 | 153 | year, month, day = date |
paul@10 | 154 | _wd, end_day = calendar.monthrange(year, month) |
paul@10 | 155 | if day == end_day: |
paul@10 | 156 | if month == 12: |
paul@10 | 157 | return (year + 1, 1, 1) |
paul@10 | 158 | else: |
paul@10 | 159 | return (year, month + 1, 1) |
paul@10 | 160 | else: |
paul@10 | 161 | return (year, month, day + 1) |
paul@10 | 162 | |
paul@10 | 163 | def getEvents(request, category_names, calendar_start=None, calendar_end=None): |
paul@10 | 164 | |
paul@10 | 165 | """ |
paul@10 | 166 | Using the 'request', generate a list of events found on pages belonging to |
paul@10 | 167 | the specified 'category_names', using the optional 'calendar_start' and |
paul@10 | 168 | 'calendar_end' month tuples of the form (year, month) to indicate a window |
paul@10 | 169 | of interest. |
paul@10 | 170 | |
paul@10 | 171 | Return a list of events, a dictionary mapping months to event lists (within |
paul@10 | 172 | the window of interest), a list of all events within the window of interest, |
paul@10 | 173 | the earliest month of an event within the window of interest, and the latest |
paul@10 | 174 | month of an event within the window of interest. |
paul@10 | 175 | """ |
paul@10 | 176 | |
paul@10 | 177 | events = [] |
paul@10 | 178 | shown_events = {} |
paul@10 | 179 | all_shown_events = [] |
paul@10 | 180 | |
paul@10 | 181 | earliest = None |
paul@10 | 182 | latest = None |
paul@10 | 183 | |
paul@10 | 184 | for category_name in category_names: |
paul@10 | 185 | |
paul@10 | 186 | # Get the pages and page names in the category. |
paul@10 | 187 | |
paul@10 | 188 | pages_in_category = getPages(category_name, request) |
paul@10 | 189 | |
paul@10 | 190 | # Visit each page in the category. |
paul@10 | 191 | |
paul@10 | 192 | for page_in_category in pages_in_category: |
paul@10 | 193 | pagename = page_in_category.page_name |
paul@10 | 194 | |
paul@10 | 195 | # Get a real page, not a result page. |
paul@10 | 196 | |
paul@10 | 197 | real_page_in_category = Page(request, pagename) |
paul@10 | 198 | event_details = getEventDetails(real_page_in_category) |
paul@10 | 199 | |
paul@10 | 200 | # Define the event as the page together with its details. |
paul@10 | 201 | |
paul@10 | 202 | event = (real_page_in_category, event_details) |
paul@10 | 203 | events.append(event) |
paul@10 | 204 | |
paul@10 | 205 | # Test for the suitability of the event. |
paul@10 | 206 | |
paul@10 | 207 | if event_details.has_key("start") and event_details.has_key("end"): |
paul@10 | 208 | |
paul@10 | 209 | start_month = event_details["start"][:2] |
paul@10 | 210 | end_month = event_details["end"][:2] |
paul@10 | 211 | |
paul@10 | 212 | # Compare the months of the dates to the requested calendar |
paul@10 | 213 | # window, if any. |
paul@10 | 214 | |
paul@10 | 215 | if (calendar_start is None or end_month >= calendar_start) and \ |
paul@10 | 216 | (calendar_end is None or start_month <= calendar_end): |
paul@10 | 217 | |
paul@10 | 218 | all_shown_events.append(event) |
paul@10 | 219 | |
paul@10 | 220 | if earliest is None or start_month < earliest: |
paul@10 | 221 | earliest = start_month |
paul@10 | 222 | if latest is None or end_month > latest: |
paul@10 | 223 | latest = end_month |
paul@10 | 224 | |
paul@10 | 225 | # Store the event in the month-specific dictionary. |
paul@10 | 226 | |
paul@10 | 227 | first = max(start_month, calendar_start or start_month) |
paul@10 | 228 | last = min(end_month, calendar_end or end_month) |
paul@10 | 229 | |
paul@10 | 230 | for event_month in daterange(first, last): |
paul@10 | 231 | if not shown_events.has_key(event_month): |
paul@10 | 232 | shown_events[event_month] = [] |
paul@10 | 233 | shown_events[event_month].append(event) |
paul@10 | 234 | |
paul@10 | 235 | return events, shown_events, all_shown_events, earliest, latest |
paul@10 | 236 | |
paul@10 | 237 | # vim: tabstop=4 expandtab shiftwidth=4 |