paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregator library |
paul@10 | 4 | |
paul@67 | 5 | @copyright: 2008, 2009, 2010 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@24 | 13 | from MoinMoin import wikiutil |
paul@10 | 14 | import calendar |
paul@11 | 15 | import datetime |
paul@24 | 16 | import time |
paul@10 | 17 | import re |
paul@10 | 18 | |
paul@65 | 19 | __version__ = "0.5" |
paul@10 | 20 | |
paul@22 | 21 | # Date labels. |
paul@22 | 22 | |
paul@22 | 23 | month_labels = ["January", "February", "March", "April", "May", "June", |
paul@22 | 24 | "July", "August", "September", "October", "November", "December"] |
paul@22 | 25 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@22 | 26 | |
paul@10 | 27 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 28 | |
paul@10 | 29 | category_regexp = None |
paul@47 | 30 | |
paul@47 | 31 | # Page parsing. |
paul@47 | 32 | |
paul@47 | 33 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?)::\s)(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@47 | 34 | category_membership_regexp = re.compile(ur"^\s*((Category\S+)(\s+Category\S+)*)\s*$", re.MULTILINE | re.UNICODE) |
paul@47 | 35 | |
paul@47 | 36 | # Value parsing. |
paul@47 | 37 | |
paul@10 | 38 | date_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})', re.UNICODE) |
paul@10 | 39 | month_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})', re.UNICODE) |
paul@19 | 40 | verbatim_regexp = re.compile(ur'(?:' |
paul@19 | 41 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@19 | 42 | ur'|' |
paul@19 | 43 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@19 | 44 | ur'|' |
paul@19 | 45 | ur'`(?P<monospace>.*?)`' |
paul@19 | 46 | ur'|' |
paul@19 | 47 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@19 | 48 | ur')', re.UNICODE) |
paul@10 | 49 | |
paul@10 | 50 | # Utility functions. |
paul@10 | 51 | |
paul@10 | 52 | def isMoin15(): |
paul@10 | 53 | return version.release.startswith("1.5.") |
paul@10 | 54 | |
paul@10 | 55 | def getCategoryPattern(request): |
paul@10 | 56 | global category_regexp |
paul@10 | 57 | |
paul@10 | 58 | try: |
paul@10 | 59 | return request.cfg.cache.page_category_regexact |
paul@10 | 60 | except AttributeError: |
paul@10 | 61 | |
paul@10 | 62 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 63 | |
paul@10 | 64 | if category_regexp is None: |
paul@10 | 65 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 66 | return category_regexp |
paul@10 | 67 | |
paul@67 | 68 | # Textual representations. |
paul@67 | 69 | |
paul@67 | 70 | def getHTTPTimeString(tmtuple): |
paul@67 | 71 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( |
paul@67 | 72 | weekday_labels[tmtuple.tm_wday], |
paul@67 | 73 | tmtuple.tm_mday, |
paul@67 | 74 | month_labels[tmtuple.tm_mon -1], # zero-based labels |
paul@67 | 75 | tmtuple.tm_year, |
paul@67 | 76 | tmtuple.tm_hour, |
paul@67 | 77 | tmtuple.tm_min, |
paul@67 | 78 | tmtuple.tm_sec |
paul@67 | 79 | ) |
paul@67 | 80 | |
paul@67 | 81 | def getSimpleWikiText(text): |
paul@67 | 82 | |
paul@67 | 83 | """ |
paul@67 | 84 | Return the plain text representation of the given 'text' which may employ |
paul@67 | 85 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@67 | 86 | text. |
paul@67 | 87 | """ |
paul@67 | 88 | |
paul@67 | 89 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@67 | 90 | |
paul@67 | 91 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@67 | 92 | |
paul@67 | 93 | def getEncodedWikiText(text): |
paul@67 | 94 | |
paul@67 | 95 | "Encode the given 'text' in a verbatim representation." |
paul@67 | 96 | |
paul@67 | 97 | return "<<Verbatim(%s)>>" % text |
paul@67 | 98 | |
paul@67 | 99 | def getPrettyTitle(title): |
paul@67 | 100 | |
paul@67 | 101 | "Return a nicely formatted version of the given 'title'." |
paul@67 | 102 | |
paul@67 | 103 | return title.replace("_", " ").replace("/", u" ? ") |
paul@67 | 104 | |
paul@67 | 105 | def getMonthLabel(month): |
paul@67 | 106 | |
paul@67 | 107 | "Return an unlocalised label for the given 'month'." |
paul@67 | 108 | |
paul@67 | 109 | return month_labels[month - 1] # zero-based labels |
paul@67 | 110 | |
paul@67 | 111 | def getDayLabel(weekday): |
paul@67 | 112 | |
paul@67 | 113 | "Return an unlocalised label for the given 'weekday'." |
paul@67 | 114 | |
paul@67 | 115 | return weekday_labels[weekday] |
paul@67 | 116 | |
paul@19 | 117 | # Action support functions. |
paul@19 | 118 | |
paul@67 | 119 | def getPageRevision(page): |
paul@67 | 120 | |
paul@67 | 121 | "Return the revision details dictionary for the given 'page'." |
paul@67 | 122 | |
paul@67 | 123 | # From Page.edit_info... |
paul@67 | 124 | |
paul@67 | 125 | if hasattr(page, "editlog_entry"): |
paul@67 | 126 | line = page.editlog_entry() |
paul@67 | 127 | else: |
paul@67 | 128 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@67 | 129 | |
paul@67 | 130 | timestamp = line.ed_time_usecs |
paul@67 | 131 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@67 | 132 | return {"timestamp" : time.gmtime(mtime), "comment" : line.comment} |
paul@67 | 133 | |
paul@67 | 134 | # Category discovery and searching. |
paul@67 | 135 | |
paul@19 | 136 | def getCategories(request): |
paul@19 | 137 | |
paul@19 | 138 | """ |
paul@19 | 139 | From the AdvancedSearch macro, return a list of category page names using |
paul@19 | 140 | the given 'request'. |
paul@19 | 141 | """ |
paul@19 | 142 | |
paul@19 | 143 | # This will return all pages with "Category" in the title. |
paul@19 | 144 | |
paul@19 | 145 | cat_filter = getCategoryPattern(request).search |
paul@19 | 146 | return request.rootpage.getPageList(filter=cat_filter) |
paul@19 | 147 | |
paul@19 | 148 | def getCategoryMapping(category_pagenames, request): |
paul@19 | 149 | |
paul@19 | 150 | """ |
paul@19 | 151 | For the given 'category_pagenames' return a list of tuples of the form |
paul@19 | 152 | (category name, category page name) using the given 'request'. |
paul@19 | 153 | """ |
paul@19 | 154 | |
paul@19 | 155 | cat_pattern = getCategoryPattern(request) |
paul@19 | 156 | mapping = [] |
paul@19 | 157 | for pagename in category_pagenames: |
paul@19 | 158 | name = cat_pattern.match(pagename).group("key") |
paul@19 | 159 | if name != "Category": |
paul@19 | 160 | mapping.append((name, pagename)) |
paul@19 | 161 | mapping.sort() |
paul@19 | 162 | return mapping |
paul@19 | 163 | |
paul@67 | 164 | def getCategoryPages(pagename, request): |
paul@29 | 165 | |
paul@67 | 166 | """ |
paul@67 | 167 | Return the pages associated with the given category 'pagename' using the |
paul@67 | 168 | 'request'. |
paul@67 | 169 | """ |
paul@10 | 170 | |
paul@10 | 171 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@10 | 172 | if isMoin15(): |
paul@10 | 173 | results = search.searchPages(request, query) |
paul@10 | 174 | results.sortByPagename() |
paul@10 | 175 | else: |
paul@10 | 176 | results = search.searchPages(request, query, "page_name") |
paul@10 | 177 | |
paul@10 | 178 | cat_pattern = getCategoryPattern(request) |
paul@10 | 179 | pages = [] |
paul@10 | 180 | for page in results.hits: |
paul@10 | 181 | if not cat_pattern.match(page.page_name): |
paul@10 | 182 | pages.append(page) |
paul@10 | 183 | return pages |
paul@10 | 184 | |
paul@67 | 185 | # The main activity functions. |
paul@67 | 186 | |
paul@67 | 187 | class EventPage: |
paul@67 | 188 | |
paul@67 | 189 | "An event page." |
paul@67 | 190 | |
paul@67 | 191 | def __init__(self, page): |
paul@67 | 192 | self.page = page |
paul@67 | 193 | self.details = None |
paul@67 | 194 | self.body = None |
paul@67 | 195 | self.categories = None |
paul@67 | 196 | |
paul@67 | 197 | def copyPage(self, page): |
paul@67 | 198 | |
paul@67 | 199 | "Copy the body of the given 'page'." |
paul@67 | 200 | |
paul@67 | 201 | self.body = page.getBody() |
paul@67 | 202 | |
paul@67 | 203 | def getPageURL(self, request): |
paul@67 | 204 | |
paul@67 | 205 | "Using 'request', return the URL of this page." |
paul@24 | 206 | |
paul@67 | 207 | page = self.page |
paul@67 | 208 | |
paul@67 | 209 | if isMoin15(): |
paul@67 | 210 | return request.getQualifiedURL(page.url(request)) |
paul@67 | 211 | else: |
paul@67 | 212 | return request.getQualifiedURL(page.url(request, relative=0)) |
paul@67 | 213 | |
paul@67 | 214 | def getFormat(self): |
paul@67 | 215 | |
paul@67 | 216 | "Get the format used on this page." |
paul@24 | 217 | |
paul@67 | 218 | if isMoin15(): |
paul@67 | 219 | return "wiki" # page.pi_format |
paul@67 | 220 | else: |
paul@67 | 221 | return self.page.pi["format"] |
paul@67 | 222 | |
paul@67 | 223 | def getRevisions(self): |
paul@67 | 224 | |
paul@67 | 225 | "Return a list of page revisions." |
paul@67 | 226 | |
paul@67 | 227 | return self.page.getRevList() |
paul@67 | 228 | |
paul@67 | 229 | def getPageRevision(self): |
paul@24 | 230 | |
paul@67 | 231 | "Return the revision details dictionary for this page." |
paul@67 | 232 | |
paul@67 | 233 | return getPageRevision(self.page) |
paul@67 | 234 | |
paul@67 | 235 | def getPageName(self): |
paul@67 | 236 | |
paul@67 | 237 | "Return the page name." |
paul@67 | 238 | |
paul@67 | 239 | return self.page.page_name |
paul@24 | 240 | |
paul@67 | 241 | def getPrettyPageName(self): |
paul@67 | 242 | |
paul@67 | 243 | "Return a nicely formatted title/name for this page." |
paul@67 | 244 | |
paul@67 | 245 | return getPrettyPageName(self.page) |
paul@67 | 246 | |
paul@67 | 247 | def getBody(self): |
paul@67 | 248 | |
paul@67 | 249 | "Get the current page body." |
paul@47 | 250 | |
paul@67 | 251 | if self.body is None: |
paul@67 | 252 | self.body = self.page.get_raw_body() |
paul@67 | 253 | return self.body |
paul@67 | 254 | |
paul@67 | 255 | def getEventDetails(self): |
paul@67 | 256 | |
paul@67 | 257 | "Return a dictionary of event details from this page." |
paul@67 | 258 | |
paul@67 | 259 | if self.details is None: |
paul@67 | 260 | self.details = {} |
paul@47 | 261 | |
paul@67 | 262 | if self.getFormat() == "wiki": |
paul@67 | 263 | for match in definition_list_regexp.finditer(self.getBody()): |
paul@67 | 264 | |
paul@67 | 265 | # Skip commented-out items. |
paul@47 | 266 | |
paul@67 | 267 | if match.group("optcomment"): |
paul@67 | 268 | continue |
paul@67 | 269 | |
paul@67 | 270 | # Permit case-insensitive list terms. |
paul@67 | 271 | |
paul@67 | 272 | term = match.group("term").lower() |
paul@67 | 273 | desc = match.group("desc") |
paul@67 | 274 | |
paul@67 | 275 | # Special value type handling. |
paul@27 | 276 | |
paul@67 | 277 | # Dates. |
paul@67 | 278 | |
paul@67 | 279 | if term in ("start", "end"): |
paul@67 | 280 | desc = getDate(desc) |
paul@67 | 281 | |
paul@67 | 282 | # Lists (whose elements may be quoted). |
paul@67 | 283 | |
paul@67 | 284 | elif term in ("topics", "categories"): |
paul@67 | 285 | desc = [getSimpleWikiText(value.strip()) for value in desc.split(",")] |
paul@67 | 286 | |
paul@67 | 287 | # Labels which may well be quoted. |
paul@67 | 288 | |
paul@67 | 289 | elif term in ("title", "summary", "description"): |
paul@67 | 290 | desc = getSimpleWikiText(desc) |
paul@67 | 291 | |
paul@67 | 292 | if desc is not None: |
paul@67 | 293 | self.details[term] = desc |
paul@67 | 294 | |
paul@67 | 295 | return self.details |
paul@67 | 296 | |
paul@67 | 297 | def getCategoryMembership(self): |
paul@27 | 298 | |
paul@67 | 299 | "Get the category names from this page." |
paul@67 | 300 | |
paul@67 | 301 | if self.categories is None: |
paul@67 | 302 | body = self.getBody() |
paul@67 | 303 | match = category_membership_regexp.search(body) |
paul@67 | 304 | self.categories = match.findall().split() |
paul@67 | 305 | |
paul@67 | 306 | return self.categories |
paul@67 | 307 | |
paul@67 | 308 | def getEventSummary(self, event_parent=None): |
paul@27 | 309 | |
paul@67 | 310 | """ |
paul@67 | 311 | Return either the given title or summary of the event described by this |
paul@67 | 312 | page, according to the page's event details, or using the pretty version |
paul@67 | 313 | of the page name. |
paul@67 | 314 | |
paul@67 | 315 | If the optional 'event_parent' is specified, any page beneath the given |
paul@67 | 316 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@67 | 317 | if its name is used as the summary. |
paul@67 | 318 | """ |
paul@67 | 319 | |
paul@67 | 320 | event_details = self.getEventDetails() |
paul@10 | 321 | |
paul@67 | 322 | if event_details.has_key("title"): |
paul@67 | 323 | return event_details["title"] |
paul@67 | 324 | elif event_details.has_key("summary"): |
paul@67 | 325 | return event_details["summary"] |
paul@67 | 326 | else: |
paul@67 | 327 | # If appropriate, remove the parent details and "/" character. |
paul@67 | 328 | |
paul@67 | 329 | title = self.getPageName() |
paul@10 | 330 | |
paul@67 | 331 | if event_parent is not None and title.startswith(event_parent): |
paul@67 | 332 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@67 | 333 | |
paul@67 | 334 | return getPrettyTitle(title) |
paul@67 | 335 | |
paul@67 | 336 | def setEventDetails(self, event_details): |
paul@67 | 337 | |
paul@67 | 338 | "Set the 'event_details' for this page." |
paul@67 | 339 | |
paul@67 | 340 | self.details = event_details |
paul@67 | 341 | |
paul@67 | 342 | def setCategoryMembership(self, category_names): |
paul@10 | 343 | |
paul@67 | 344 | """ |
paul@67 | 345 | Set the category membership for the page using the specified |
paul@67 | 346 | 'category_names'. |
paul@67 | 347 | """ |
paul@67 | 348 | |
paul@67 | 349 | self.categories = category_names |
paul@67 | 350 | |
paul@67 | 351 | def flushEventDetails(self): |
paul@67 | 352 | |
paul@67 | 353 | "Flush the current event details to this page's body text." |
paul@10 | 354 | |
paul@67 | 355 | new_body_parts = [] |
paul@67 | 356 | end_of_last_match = 0 |
paul@67 | 357 | body = self.getBody() |
paul@67 | 358 | event_details = self.getEventDetails() |
paul@67 | 359 | |
paul@67 | 360 | for match in definition_list_regexp.finditer(body): |
paul@47 | 361 | |
paul@67 | 362 | # Add preceding text to the new body. |
paul@67 | 363 | |
paul@67 | 364 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@67 | 365 | end_of_last_match = match.end() |
paul@67 | 366 | |
paul@67 | 367 | # Get the matching regions, adding the term to the new body. |
paul@67 | 368 | |
paul@67 | 369 | new_body_parts.append(match.group("wholeterm")) |
paul@47 | 370 | |
paul@10 | 371 | # Permit case-insensitive list terms. |
paul@10 | 372 | |
paul@10 | 373 | term = match.group("term").lower() |
paul@10 | 374 | desc = match.group("desc") |
paul@10 | 375 | |
paul@10 | 376 | # Special value type handling. |
paul@10 | 377 | |
paul@67 | 378 | if event_details.has_key(term): |
paul@19 | 379 | |
paul@67 | 380 | # Dates. |
paul@47 | 381 | |
paul@67 | 382 | if term in ("start", "end"): |
paul@67 | 383 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@47 | 384 | |
paul@67 | 385 | # Lists (whose elements may be quoted). |
paul@47 | 386 | |
paul@67 | 387 | elif term in ("topics", "categories"): |
paul@67 | 388 | desc = ", ".join(getEncodedWikiText(event_details[term])) |
paul@47 | 389 | |
paul@67 | 390 | # Labels which may well be quoted. |
paul@47 | 391 | |
paul@67 | 392 | elif term in ("title", "summary"): |
paul@67 | 393 | desc = getEncodedWikiText(event_details[term]) |
paul@47 | 394 | |
paul@67 | 395 | # Text which need not be quoted, but it will be Wiki text. |
paul@55 | 396 | |
paul@67 | 397 | elif term in ("description",): |
paul@67 | 398 | desc = event_details[term] |
paul@55 | 399 | |
paul@67 | 400 | new_body_parts.append(desc) |
paul@10 | 401 | |
paul@67 | 402 | else: |
paul@67 | 403 | new_body_parts.append(body[end_of_last_match:]) |
paul@10 | 404 | |
paul@67 | 405 | self.body = "".join(new_body_parts) |
paul@11 | 406 | |
paul@67 | 407 | def flushCategoryMembership(self): |
paul@17 | 408 | |
paul@67 | 409 | "Flush the category membership to the page body." |
paul@11 | 410 | |
paul@67 | 411 | body = self.getBody() |
paul@67 | 412 | category_names = self.getCategoryMembership() |
paul@67 | 413 | match = category_membership_regexp.search(body) |
paul@10 | 414 | |
paul@67 | 415 | if match: |
paul@67 | 416 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@10 | 417 | |
paul@67 | 418 | def saveChanges(self): |
paul@10 | 419 | |
paul@67 | 420 | "Save changes to the event." |
paul@10 | 421 | |
paul@67 | 422 | self.flushEventDetails() |
paul@67 | 423 | self.flushCategoryMembership() |
paul@67 | 424 | self.page.saveText(self.getBody(), 0) |
paul@10 | 425 | |
paul@67 | 426 | def linkToPage(self, request, text, query_string=None): |
paul@11 | 427 | |
paul@67 | 428 | """ |
paul@67 | 429 | Using 'request', return a link to this page with the given link 'text' |
paul@67 | 430 | and optional 'query_string'. |
paul@67 | 431 | """ |
paul@11 | 432 | |
paul@67 | 433 | return linkToPage(request, self.page, text, query_string) |
paul@13 | 434 | |
paul@10 | 435 | def getEvents(request, category_names, calendar_start=None, calendar_end=None): |
paul@10 | 436 | |
paul@10 | 437 | """ |
paul@10 | 438 | Using the 'request', generate a list of events found on pages belonging to |
paul@10 | 439 | the specified 'category_names', using the optional 'calendar_start' and |
paul@10 | 440 | 'calendar_end' month tuples of the form (year, month) to indicate a window |
paul@10 | 441 | of interest. |
paul@10 | 442 | |
paul@10 | 443 | Return a list of events, a dictionary mapping months to event lists (within |
paul@10 | 444 | the window of interest), a list of all events within the window of interest, |
paul@10 | 445 | the earliest month of an event within the window of interest, and the latest |
paul@10 | 446 | month of an event within the window of interest. |
paul@10 | 447 | """ |
paul@10 | 448 | |
paul@12 | 449 | # Re-order the window, if appropriate. |
paul@12 | 450 | |
paul@12 | 451 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@12 | 452 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@12 | 453 | |
paul@10 | 454 | events = [] |
paul@10 | 455 | shown_events = {} |
paul@10 | 456 | all_shown_events = [] |
paul@17 | 457 | processed_pages = set() |
paul@10 | 458 | |
paul@10 | 459 | earliest = None |
paul@10 | 460 | latest = None |
paul@10 | 461 | |
paul@10 | 462 | for category_name in category_names: |
paul@10 | 463 | |
paul@10 | 464 | # Get the pages and page names in the category. |
paul@10 | 465 | |
paul@67 | 466 | pages_in_category = getCategoryPages(category_name, request) |
paul@10 | 467 | |
paul@10 | 468 | # Visit each page in the category. |
paul@10 | 469 | |
paul@10 | 470 | for page_in_category in pages_in_category: |
paul@10 | 471 | pagename = page_in_category.page_name |
paul@10 | 472 | |
paul@17 | 473 | # Only process each page once. |
paul@17 | 474 | |
paul@17 | 475 | if pagename in processed_pages: |
paul@17 | 476 | continue |
paul@17 | 477 | else: |
paul@17 | 478 | processed_pages.add(pagename) |
paul@17 | 479 | |
paul@10 | 480 | # Get a real page, not a result page. |
paul@10 | 481 | |
paul@67 | 482 | event_page = EventPage(Page(request, pagename)) |
paul@67 | 483 | event_details = event_page.getEventDetails() |
paul@10 | 484 | |
paul@67 | 485 | # Remember the event page. |
paul@10 | 486 | |
paul@67 | 487 | events.append(event_page) |
paul@10 | 488 | |
paul@10 | 489 | # Test for the suitability of the event. |
paul@10 | 490 | |
paul@10 | 491 | if event_details.has_key("start") and event_details.has_key("end"): |
paul@10 | 492 | |
paul@67 | 493 | start_month = event_details["start"].as_month() |
paul@67 | 494 | end_month = event_details["end"].as_month() |
paul@10 | 495 | |
paul@10 | 496 | # Compare the months of the dates to the requested calendar |
paul@10 | 497 | # window, if any. |
paul@10 | 498 | |
paul@10 | 499 | if (calendar_start is None or end_month >= calendar_start) and \ |
paul@10 | 500 | (calendar_end is None or start_month <= calendar_end): |
paul@10 | 501 | |
paul@67 | 502 | all_shown_events.append(event_page) |
paul@10 | 503 | |
paul@10 | 504 | if earliest is None or start_month < earliest: |
paul@10 | 505 | earliest = start_month |
paul@10 | 506 | if latest is None or end_month > latest: |
paul@10 | 507 | latest = end_month |
paul@10 | 508 | |
paul@10 | 509 | # Store the event in the month-specific dictionary. |
paul@10 | 510 | |
paul@10 | 511 | first = max(start_month, calendar_start or start_month) |
paul@10 | 512 | last = min(end_month, calendar_end or end_month) |
paul@10 | 513 | |
paul@67 | 514 | for event_month in first.months_until(last): |
paul@10 | 515 | if not shown_events.has_key(event_month): |
paul@10 | 516 | shown_events[event_month] = [] |
paul@67 | 517 | shown_events[event_month].append(event_page) |
paul@10 | 518 | |
paul@10 | 519 | return events, shown_events, all_shown_events, earliest, latest |
paul@10 | 520 | |
paul@29 | 521 | def setEventTimestamps(request, events): |
paul@29 | 522 | |
paul@29 | 523 | """ |
paul@29 | 524 | Using 'request', set timestamp details in the details dictionary of each of |
paul@67 | 525 | the 'events'. |
paul@29 | 526 | |
paul@29 | 527 | Retutn the latest timestamp found. |
paul@29 | 528 | """ |
paul@29 | 529 | |
paul@29 | 530 | latest = None |
paul@29 | 531 | |
paul@67 | 532 | for event_page in events: |
paul@67 | 533 | event_details = event_page.getEventDetails() |
paul@29 | 534 | |
paul@29 | 535 | # Get the initial revision of the page. |
paul@29 | 536 | |
paul@67 | 537 | revisions = event_page.getRevisions() |
paul@67 | 538 | event_page_initial = Page(request, event_page.getPageName(), rev=revisions[-1]) |
paul@29 | 539 | |
paul@29 | 540 | # Get the created and last modified times. |
paul@29 | 541 | |
paul@30 | 542 | initial_revision = getPageRevision(event_page_initial) |
paul@30 | 543 | event_details["created"] = initial_revision["timestamp"] |
paul@67 | 544 | latest_revision = event_page.getPageRevision() |
paul@30 | 545 | event_details["last-modified"] = latest_revision["timestamp"] |
paul@29 | 546 | event_details["sequence"] = len(revisions) - 1 |
paul@30 | 547 | event_details["last-comment"] = latest_revision["comment"] |
paul@29 | 548 | |
paul@29 | 549 | if latest is None or latest < event_details["last-modified"]: |
paul@29 | 550 | latest = event_details["last-modified"] |
paul@29 | 551 | |
paul@29 | 552 | return latest |
paul@29 | 553 | |
paul@26 | 554 | def compareEvents(event1, event2): |
paul@26 | 555 | |
paul@67 | 556 | "Compare 'event1' and 'event2' by start and end date." |
paul@26 | 557 | |
paul@67 | 558 | event_details1 = event1.getEventDetails() |
paul@67 | 559 | event_details2 = event2.getEventDetails() |
paul@26 | 560 | return cmp( |
paul@26 | 561 | (event_details1["start"], event_details1["end"]), |
paul@26 | 562 | (event_details2["start"], event_details2["end"]) |
paul@26 | 563 | ) |
paul@26 | 564 | |
paul@26 | 565 | def getOrderedEvents(events): |
paul@26 | 566 | |
paul@26 | 567 | """ |
paul@26 | 568 | Return a list with the given 'events' ordered according to their start and |
paul@67 | 569 | end dates. |
paul@26 | 570 | """ |
paul@26 | 571 | |
paul@26 | 572 | ordered_events = events[:] |
paul@26 | 573 | ordered_events.sort(compareEvents) |
paul@26 | 574 | return ordered_events |
paul@26 | 575 | |
paul@13 | 576 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest): |
paul@13 | 577 | |
paul@13 | 578 | """ |
paul@13 | 579 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 580 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 581 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 582 | specific period of interest. |
paul@13 | 583 | """ |
paul@13 | 584 | |
paul@13 | 585 | # Define the period as starting with any specified start month or the |
paul@13 | 586 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 587 | # event known. |
paul@13 | 588 | |
paul@13 | 589 | first = calendar_start or earliest |
paul@13 | 590 | last = calendar_end or latest |
paul@13 | 591 | |
paul@13 | 592 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 593 | # events in the requested period, and there was no start or end month |
paul@13 | 594 | # specified, show only the month indicated by the start or end of the |
paul@13 | 595 | # requested period. If all events were to be shown but none were found show |
paul@13 | 596 | # the current month. |
paul@13 | 597 | |
paul@13 | 598 | if first is None: |
paul@13 | 599 | first = last or getCurrentMonth() |
paul@13 | 600 | if last is None: |
paul@13 | 601 | last = first or getCurrentMonth() |
paul@13 | 602 | |
paul@13 | 603 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 604 | |
paul@13 | 605 | return min(first, last), last |
paul@13 | 606 | |
paul@15 | 607 | def getCoverage(start, end, events): |
paul@15 | 608 | |
paul@15 | 609 | """ |
paul@15 | 610 | Within the period defined by the 'start' and 'end' dates, determine the |
paul@15 | 611 | coverage of the days in the period by the given 'events', returning a set of |
paul@15 | 612 | covered days, along with a list of slots, where each slot contains a tuple |
paul@15 | 613 | of the form (set of covered days, events). |
paul@15 | 614 | """ |
paul@15 | 615 | |
paul@15 | 616 | all_events = [] |
paul@15 | 617 | full_coverage = set() |
paul@15 | 618 | |
paul@15 | 619 | # Get event details. |
paul@15 | 620 | |
paul@67 | 621 | for event_page in events: |
paul@67 | 622 | event_details = event_page.getEventDetails() |
paul@15 | 623 | |
paul@15 | 624 | # Test for the event in the period. |
paul@15 | 625 | |
paul@15 | 626 | if event_details["start"] <= end and event_details["end"] >= start: |
paul@15 | 627 | |
paul@15 | 628 | # Find the coverage of this period for the event. |
paul@15 | 629 | |
paul@15 | 630 | event_start = max(event_details["start"], start) |
paul@15 | 631 | event_end = min(event_details["end"], end) |
paul@67 | 632 | event_coverage = set(event_start.days_until(event_end)) |
paul@15 | 633 | |
paul@15 | 634 | # Update the overall coverage. |
paul@15 | 635 | |
paul@15 | 636 | full_coverage.update(event_coverage) |
paul@15 | 637 | |
paul@15 | 638 | # Try and fit the event into the events list. |
paul@15 | 639 | |
paul@15 | 640 | for i, (coverage, covered_events) in enumerate(all_events): |
paul@15 | 641 | |
paul@15 | 642 | # Where the event does not overlap with the current |
paul@15 | 643 | # element, add it alongside existing events. |
paul@15 | 644 | |
paul@15 | 645 | if not coverage.intersection(event_coverage): |
paul@67 | 646 | covered_events.append(event_page) |
paul@15 | 647 | all_events[i] = coverage.union(event_coverage), covered_events |
paul@15 | 648 | break |
paul@15 | 649 | |
paul@15 | 650 | # Make a new element in the list if the event cannot be |
paul@15 | 651 | # marked alongside existing events. |
paul@15 | 652 | |
paul@15 | 653 | else: |
paul@67 | 654 | all_events.append((event_coverage, [event_page])) |
paul@15 | 655 | |
paul@15 | 656 | return full_coverage, all_events |
paul@15 | 657 | |
paul@67 | 658 | # Date-related functions. |
paul@67 | 659 | |
paul@67 | 660 | class Period: |
paul@67 | 661 | |
paul@67 | 662 | "A simple period of time." |
paul@67 | 663 | |
paul@67 | 664 | def __init__(self, data): |
paul@67 | 665 | self.data = data |
paul@67 | 666 | |
paul@67 | 667 | def months(self): |
paul@67 | 668 | return self.data[0] * 12 + self.data[1] |
paul@67 | 669 | |
paul@67 | 670 | class Month: |
paul@67 | 671 | |
paul@67 | 672 | "A simple year-month representation." |
paul@67 | 673 | |
paul@67 | 674 | def __init__(self, data): |
paul@67 | 675 | self.data = tuple(data) |
paul@67 | 676 | |
paul@67 | 677 | def __repr__(self): |
paul@67 | 678 | return "%s(%r)" % (self.__class__.__name__, self.data) |
paul@67 | 679 | |
paul@67 | 680 | def __str__(self): |
paul@67 | 681 | return "%04d-%02d" % self.as_tuple()[:2] |
paul@67 | 682 | |
paul@67 | 683 | def __hash__(self): |
paul@67 | 684 | return hash(self.as_tuple()) |
paul@67 | 685 | |
paul@67 | 686 | def as_tuple(self): |
paul@67 | 687 | return self.data |
paul@67 | 688 | |
paul@67 | 689 | def as_date(self, day): |
paul@67 | 690 | return Date(self.as_tuple() + (day,)) |
paul@67 | 691 | |
paul@67 | 692 | def year(self): |
paul@67 | 693 | return self.data[0] |
paul@67 | 694 | |
paul@67 | 695 | def month(self): |
paul@67 | 696 | return self.data[1] |
paul@67 | 697 | |
paul@67 | 698 | def month_properties(self): |
paul@67 | 699 | |
paul@67 | 700 | """ |
paul@67 | 701 | Return the weekday of the 1st of the month, along with the number of |
paul@67 | 702 | days, as a tuple. |
paul@67 | 703 | """ |
paul@67 | 704 | |
paul@67 | 705 | year, month = self.data |
paul@67 | 706 | return calendar.monthrange(year, month) |
paul@67 | 707 | |
paul@67 | 708 | def month_update(self, n=1): |
paul@67 | 709 | |
paul@67 | 710 | "Return the month updated by 'n' months." |
paul@67 | 711 | |
paul@67 | 712 | year, month = self.data |
paul@67 | 713 | return Month((year + (month - 1 + n) / 12, (month - 1 + n) % 12 + 1)) |
paul@67 | 714 | |
paul@67 | 715 | def next_month(self): |
paul@67 | 716 | |
paul@67 | 717 | "Return the month following this one." |
paul@67 | 718 | |
paul@67 | 719 | return self.month_update(1) |
paul@67 | 720 | |
paul@67 | 721 | def previous_month(self): |
paul@67 | 722 | |
paul@67 | 723 | "Return the month preceding this one." |
paul@67 | 724 | |
paul@67 | 725 | return self.month_update(-1) |
paul@67 | 726 | |
paul@67 | 727 | def __sub__(self, start): |
paul@67 | 728 | |
paul@67 | 729 | """ |
paul@67 | 730 | Return the difference in years and months between this month and the |
paul@67 | 731 | 'start' month as a period. |
paul@67 | 732 | """ |
paul@67 | 733 | |
paul@67 | 734 | return Period([(x - y) for x, y in zip(self.data, start.data)]) |
paul@67 | 735 | |
paul@67 | 736 | def __cmp__(self, other): |
paul@67 | 737 | return cmp(self.data, other.data) |
paul@67 | 738 | |
paul@67 | 739 | def until(self, end, nextfn, prevfn): |
paul@67 | 740 | month = self |
paul@67 | 741 | months = [month] |
paul@67 | 742 | if month < end: |
paul@67 | 743 | while month < end: |
paul@67 | 744 | month = nextfn(month) |
paul@67 | 745 | months.append(month) |
paul@67 | 746 | elif month > end: |
paul@67 | 747 | while month > end: |
paul@67 | 748 | month = prevfn(month) |
paul@67 | 749 | months.append(month) |
paul@67 | 750 | return months |
paul@67 | 751 | |
paul@67 | 752 | def months_until(self, end): |
paul@67 | 753 | return self.until(end, Month.next_month, Month.previous_month) |
paul@67 | 754 | |
paul@67 | 755 | class Date(Month): |
paul@67 | 756 | |
paul@67 | 757 | "A simple year-month-day representation." |
paul@67 | 758 | |
paul@67 | 759 | def __str__(self): |
paul@67 | 760 | return "%04d-%02d-%02d" % self.as_tuple()[:3] |
paul@67 | 761 | |
paul@67 | 762 | def as_month(self): |
paul@67 | 763 | return Month(self.data[:2]) |
paul@67 | 764 | |
paul@67 | 765 | def day(self): |
paul@67 | 766 | return self.data[2] |
paul@67 | 767 | |
paul@67 | 768 | def next_day(self): |
paul@67 | 769 | |
paul@67 | 770 | "Return the date following this one." |
paul@67 | 771 | |
paul@67 | 772 | year, month, day = self.data |
paul@67 | 773 | _wd, end_day = calendar.monthrange(year, month) |
paul@67 | 774 | if day == end_day: |
paul@67 | 775 | if month == 12: |
paul@67 | 776 | return Date((year + 1, 1, 1)) |
paul@67 | 777 | else: |
paul@67 | 778 | return Date((year, month + 1, 1)) |
paul@67 | 779 | else: |
paul@67 | 780 | return Date((year, month, day + 1)) |
paul@67 | 781 | |
paul@67 | 782 | def previous_day(self): |
paul@67 | 783 | |
paul@67 | 784 | "Return the date preceding this one." |
paul@67 | 785 | |
paul@67 | 786 | year, month, day = self.data |
paul@67 | 787 | if day == 1: |
paul@67 | 788 | if month == 1: |
paul@67 | 789 | return Date((year - 1, 12, 31)) |
paul@67 | 790 | else: |
paul@67 | 791 | _wd, end_day = calendar.monthrange(year, month - 1) |
paul@67 | 792 | return Date((year, month - 1, end_day)) |
paul@67 | 793 | else: |
paul@67 | 794 | return Date((year, month, day - 1)) |
paul@67 | 795 | |
paul@67 | 796 | def days_until(self, end): |
paul@67 | 797 | return self.until(end, Date.next_day, Date.previous_day) |
paul@67 | 798 | |
paul@67 | 799 | def getDate(s): |
paul@67 | 800 | |
paul@67 | 801 | "Parse the string 's', extracting and returning a date string." |
paul@67 | 802 | |
paul@67 | 803 | m = date_regexp.search(s) |
paul@67 | 804 | if m: |
paul@67 | 805 | return Date(map(int, m.groups())) |
paul@67 | 806 | else: |
paul@67 | 807 | return None |
paul@67 | 808 | |
paul@67 | 809 | def getMonth(s): |
paul@67 | 810 | |
paul@67 | 811 | "Parse the string 's', extracting and returning a month string." |
paul@67 | 812 | |
paul@67 | 813 | m = month_regexp.search(s) |
paul@67 | 814 | if m: |
paul@67 | 815 | return Month(map(int, m.groups())) |
paul@67 | 816 | else: |
paul@67 | 817 | return None |
paul@67 | 818 | |
paul@67 | 819 | def getCurrentMonth(): |
paul@67 | 820 | |
paul@67 | 821 | "Return the current month as a (year, month) tuple." |
paul@67 | 822 | |
paul@67 | 823 | today = datetime.date.today() |
paul@67 | 824 | return Month((today.year, today.month)) |
paul@67 | 825 | |
paul@67 | 826 | def getCurrentYear(): |
paul@67 | 827 | |
paul@67 | 828 | "Return the current year." |
paul@67 | 829 | |
paul@67 | 830 | today = datetime.date.today() |
paul@67 | 831 | return today.year |
paul@67 | 832 | |
paul@19 | 833 | # User interface functions. |
paul@19 | 834 | |
paul@55 | 835 | def getParameter(request, name, default=None): |
paul@55 | 836 | return request.form.get(name, [default])[0] |
paul@23 | 837 | |
paul@58 | 838 | def getQualifiedParameter(request, calendar_name, argname, default=None): |
paul@58 | 839 | argname = getQualifiedParameterName(calendar_name, argname) |
paul@58 | 840 | return getParameter(request, argname, default) |
paul@58 | 841 | |
paul@58 | 842 | def getQualifiedParameterName(calendar_name, argname): |
paul@58 | 843 | if calendar_name is None: |
paul@58 | 844 | return argname |
paul@58 | 845 | else: |
paul@58 | 846 | return "%s-%s" % (calendar_name, argname) |
paul@58 | 847 | |
paul@19 | 848 | def getParameterMonth(arg): |
paul@67 | 849 | |
paul@67 | 850 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@67 | 851 | |
paul@19 | 852 | n = None |
paul@19 | 853 | |
paul@19 | 854 | if arg.startswith("current"): |
paul@19 | 855 | date = getCurrentMonth() |
paul@19 | 856 | if len(arg) > 8: |
paul@19 | 857 | n = int(arg[7:]) |
paul@19 | 858 | |
paul@19 | 859 | elif arg.startswith("yearstart"): |
paul@67 | 860 | date = Month((getCurrentYear(), 1)) |
paul@19 | 861 | if len(arg) > 10: |
paul@19 | 862 | n = int(arg[9:]) |
paul@19 | 863 | |
paul@19 | 864 | elif arg.startswith("yearend"): |
paul@67 | 865 | date = Month((getCurrentYear(), 12)) |
paul@19 | 866 | if len(arg) > 8: |
paul@19 | 867 | n = int(arg[7:]) |
paul@19 | 868 | |
paul@19 | 869 | else: |
paul@19 | 870 | date = getMonth(arg) |
paul@19 | 871 | |
paul@19 | 872 | if n is not None: |
paul@67 | 873 | date = date.month_update(n) |
paul@19 | 874 | |
paul@19 | 875 | return date |
paul@19 | 876 | |
paul@19 | 877 | def getFormMonth(request, calendar_name, argname): |
paul@67 | 878 | |
paul@67 | 879 | """ |
paul@67 | 880 | Return the month from the 'request' for the calendar with the given |
paul@67 | 881 | 'calendar_name' using the parameter having the given 'argname'. |
paul@67 | 882 | """ |
paul@67 | 883 | |
paul@58 | 884 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@19 | 885 | if arg is not None: |
paul@19 | 886 | return getParameterMonth(arg) |
paul@19 | 887 | else: |
paul@19 | 888 | return None |
paul@19 | 889 | |
paul@23 | 890 | def getFormMonthPair(request, yeararg, montharg): |
paul@67 | 891 | |
paul@67 | 892 | """ |
paul@67 | 893 | Return the month from the 'request' for the calendar with the given |
paul@67 | 894 | 'calendar_name' using the parameters having the given 'yeararg' and |
paul@67 | 895 | 'montharg' names. |
paul@67 | 896 | """ |
paul@67 | 897 | |
paul@23 | 898 | year = getParameter(request, yeararg) |
paul@23 | 899 | month = getParameter(request, montharg) |
paul@23 | 900 | if year and month: |
paul@67 | 901 | return Month((int(year), int(month))) |
paul@23 | 902 | else: |
paul@23 | 903 | return None |
paul@23 | 904 | |
paul@67 | 905 | # Page-related functions. |
paul@67 | 906 | |
paul@19 | 907 | def getPrettyPageName(page): |
paul@19 | 908 | |
paul@19 | 909 | "Return a nicely formatted title/name for the given 'page'." |
paul@19 | 910 | |
paul@27 | 911 | if isMoin15(): |
paul@27 | 912 | title = page.split_title(page.request, force=1) |
paul@27 | 913 | else: |
paul@27 | 914 | title = page.split_title(force=1) |
paul@27 | 915 | |
paul@55 | 916 | return getPrettyTitle(title) |
paul@55 | 917 | |
paul@27 | 918 | def linkToPage(request, page, text, query_string=None): |
paul@27 | 919 | |
paul@27 | 920 | """ |
paul@27 | 921 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@27 | 922 | optional 'query_string'. |
paul@27 | 923 | """ |
paul@27 | 924 | |
paul@27 | 925 | text = wikiutil.escape(text) |
paul@27 | 926 | |
paul@27 | 927 | if isMoin15(): |
paul@27 | 928 | url = wikiutil.quoteWikinameURL(page.page_name) |
paul@27 | 929 | if query_string is not None: |
paul@27 | 930 | url = "%s?%s" % (url, query_string) |
paul@27 | 931 | return wikiutil.link_tag(request, url, text, getattr(page, "formatter", None)) |
paul@27 | 932 | else: |
paul@27 | 933 | return page.link_to_raw(request, text, query_string) |
paul@27 | 934 | |
paul@10 | 935 | # vim: tabstop=4 expandtab shiftwidth=4 |