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@69 | 19 | try: |
paul@69 | 20 | set |
paul@69 | 21 | except NameError: |
paul@69 | 22 | from sets import Set as set |
paul@69 | 23 | |
paul@88 | 24 | __version__ = "0.6" |
paul@10 | 25 | |
paul@22 | 26 | # Date labels. |
paul@22 | 27 | |
paul@22 | 28 | month_labels = ["January", "February", "March", "April", "May", "June", |
paul@22 | 29 | "July", "August", "September", "October", "November", "December"] |
paul@22 | 30 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@22 | 31 | |
paul@10 | 32 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 33 | |
paul@10 | 34 | category_regexp = None |
paul@47 | 35 | |
paul@47 | 36 | # Page parsing. |
paul@47 | 37 | |
paul@84 | 38 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@47 | 39 | category_membership_regexp = re.compile(ur"^\s*((Category\S+)(\s+Category\S+)*)\s*$", re.MULTILINE | re.UNICODE) |
paul@47 | 40 | |
paul@47 | 41 | # Value parsing. |
paul@47 | 42 | |
paul@93 | 43 | country_code_regexp = re.compile(ur'(?:^|\W)(?P<code>[A-Z]{2})(?:$|\W+$)', re.UNICODE) |
paul@92 | 44 | |
paul@92 | 45 | month_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})' |
paul@92 | 46 | date_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})' |
paul@92 | 47 | time_regexp_str = ur'(?P<hour>[0-2][0-9]):(?P<minute>[0-5][0-9])(?::(?P<second>[0-6][0-9]))?' |
paul@94 | 48 | timezone_offset_str = ur'(?:UTC)?(?P<sign>[-+])(?P<hours>[0-9]{2})(?::?(?P<minutes>[0-9]{2}))?' |
paul@95 | 49 | timezone_regexp_str = ur'(?P<zone>' + timezone_offset_str + ')' |
paul@92 | 50 | datetime_regexp_str = date_regexp_str + ur'(?:\s+' + time_regexp_str + ur'(?:\s+' + timezone_regexp_str + ur')?)?' |
paul@92 | 51 | |
paul@92 | 52 | month_regexp = re.compile(month_regexp_str, re.UNICODE) |
paul@92 | 53 | date_regexp = re.compile(date_regexp_str, re.UNICODE) |
paul@92 | 54 | time_regexp = re.compile(time_regexp_str, re.UNICODE) |
paul@92 | 55 | datetime_regexp = re.compile(datetime_regexp_str, re.UNICODE) |
paul@94 | 56 | timezone_offset_regexp = re.compile(timezone_offset_str, re.UNICODE) |
paul@92 | 57 | |
paul@19 | 58 | verbatim_regexp = re.compile(ur'(?:' |
paul@19 | 59 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@19 | 60 | ur'|' |
paul@19 | 61 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@19 | 62 | ur'|' |
paul@19 | 63 | ur'`(?P<monospace>.*?)`' |
paul@19 | 64 | ur'|' |
paul@19 | 65 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@19 | 66 | ur')', re.UNICODE) |
paul@10 | 67 | |
paul@10 | 68 | # Utility functions. |
paul@10 | 69 | |
paul@10 | 70 | def isMoin15(): |
paul@10 | 71 | return version.release.startswith("1.5.") |
paul@10 | 72 | |
paul@10 | 73 | def getCategoryPattern(request): |
paul@10 | 74 | global category_regexp |
paul@10 | 75 | |
paul@10 | 76 | try: |
paul@10 | 77 | return request.cfg.cache.page_category_regexact |
paul@10 | 78 | except AttributeError: |
paul@10 | 79 | |
paul@10 | 80 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 81 | |
paul@10 | 82 | if category_regexp is None: |
paul@10 | 83 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 84 | return category_regexp |
paul@10 | 85 | |
paul@92 | 86 | def int_or_none(x): |
paul@92 | 87 | if x is None: |
paul@92 | 88 | return x |
paul@92 | 89 | else: |
paul@92 | 90 | return int(x) |
paul@92 | 91 | |
paul@67 | 92 | # Textual representations. |
paul@67 | 93 | |
paul@67 | 94 | def getHTTPTimeString(tmtuple): |
paul@67 | 95 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( |
paul@67 | 96 | weekday_labels[tmtuple.tm_wday], |
paul@67 | 97 | tmtuple.tm_mday, |
paul@67 | 98 | month_labels[tmtuple.tm_mon -1], # zero-based labels |
paul@67 | 99 | tmtuple.tm_year, |
paul@67 | 100 | tmtuple.tm_hour, |
paul@67 | 101 | tmtuple.tm_min, |
paul@67 | 102 | tmtuple.tm_sec |
paul@67 | 103 | ) |
paul@67 | 104 | |
paul@67 | 105 | def getSimpleWikiText(text): |
paul@67 | 106 | |
paul@67 | 107 | """ |
paul@67 | 108 | Return the plain text representation of the given 'text' which may employ |
paul@67 | 109 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@67 | 110 | text. |
paul@67 | 111 | """ |
paul@67 | 112 | |
paul@67 | 113 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@67 | 114 | |
paul@67 | 115 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@67 | 116 | |
paul@67 | 117 | def getEncodedWikiText(text): |
paul@67 | 118 | |
paul@67 | 119 | "Encode the given 'text' in a verbatim representation." |
paul@67 | 120 | |
paul@67 | 121 | return "<<Verbatim(%s)>>" % text |
paul@67 | 122 | |
paul@67 | 123 | def getPrettyTitle(title): |
paul@67 | 124 | |
paul@67 | 125 | "Return a nicely formatted version of the given 'title'." |
paul@67 | 126 | |
paul@67 | 127 | return title.replace("_", " ").replace("/", u" ? ") |
paul@67 | 128 | |
paul@67 | 129 | def getMonthLabel(month): |
paul@67 | 130 | |
paul@67 | 131 | "Return an unlocalised label for the given 'month'." |
paul@67 | 132 | |
paul@67 | 133 | return month_labels[month - 1] # zero-based labels |
paul@67 | 134 | |
paul@67 | 135 | def getDayLabel(weekday): |
paul@67 | 136 | |
paul@67 | 137 | "Return an unlocalised label for the given 'weekday'." |
paul@67 | 138 | |
paul@67 | 139 | return weekday_labels[weekday] |
paul@67 | 140 | |
paul@19 | 141 | # Action support functions. |
paul@19 | 142 | |
paul@67 | 143 | def getPageRevision(page): |
paul@67 | 144 | |
paul@67 | 145 | "Return the revision details dictionary for the given 'page'." |
paul@67 | 146 | |
paul@67 | 147 | # From Page.edit_info... |
paul@67 | 148 | |
paul@67 | 149 | if hasattr(page, "editlog_entry"): |
paul@67 | 150 | line = page.editlog_entry() |
paul@67 | 151 | else: |
paul@67 | 152 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@67 | 153 | |
paul@89 | 154 | # Similar to Page.mtime_usecs behaviour... |
paul@89 | 155 | |
paul@89 | 156 | if line: |
paul@89 | 157 | timestamp = line.ed_time_usecs |
paul@89 | 158 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@89 | 159 | comment = line.comment |
paul@89 | 160 | else: |
paul@89 | 161 | mtime = 0 |
paul@89 | 162 | comment = "" |
paul@89 | 163 | |
paul@89 | 164 | return {"timestamp" : time.gmtime(mtime), "comment" : comment} |
paul@67 | 165 | |
paul@67 | 166 | # Category discovery and searching. |
paul@67 | 167 | |
paul@19 | 168 | def getCategories(request): |
paul@19 | 169 | |
paul@19 | 170 | """ |
paul@19 | 171 | From the AdvancedSearch macro, return a list of category page names using |
paul@19 | 172 | the given 'request'. |
paul@19 | 173 | """ |
paul@19 | 174 | |
paul@19 | 175 | # This will return all pages with "Category" in the title. |
paul@19 | 176 | |
paul@19 | 177 | cat_filter = getCategoryPattern(request).search |
paul@19 | 178 | return request.rootpage.getPageList(filter=cat_filter) |
paul@19 | 179 | |
paul@19 | 180 | def getCategoryMapping(category_pagenames, request): |
paul@19 | 181 | |
paul@19 | 182 | """ |
paul@19 | 183 | For the given 'category_pagenames' return a list of tuples of the form |
paul@19 | 184 | (category name, category page name) using the given 'request'. |
paul@19 | 185 | """ |
paul@19 | 186 | |
paul@19 | 187 | cat_pattern = getCategoryPattern(request) |
paul@19 | 188 | mapping = [] |
paul@19 | 189 | for pagename in category_pagenames: |
paul@19 | 190 | name = cat_pattern.match(pagename).group("key") |
paul@19 | 191 | if name != "Category": |
paul@19 | 192 | mapping.append((name, pagename)) |
paul@19 | 193 | mapping.sort() |
paul@19 | 194 | return mapping |
paul@19 | 195 | |
paul@67 | 196 | def getCategoryPages(pagename, request): |
paul@29 | 197 | |
paul@67 | 198 | """ |
paul@67 | 199 | Return the pages associated with the given category 'pagename' using the |
paul@67 | 200 | 'request'. |
paul@67 | 201 | """ |
paul@10 | 202 | |
paul@10 | 203 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@10 | 204 | if isMoin15(): |
paul@10 | 205 | results = search.searchPages(request, query) |
paul@10 | 206 | results.sortByPagename() |
paul@10 | 207 | else: |
paul@10 | 208 | results = search.searchPages(request, query, "page_name") |
paul@10 | 209 | |
paul@10 | 210 | cat_pattern = getCategoryPattern(request) |
paul@10 | 211 | pages = [] |
paul@10 | 212 | for page in results.hits: |
paul@10 | 213 | if not cat_pattern.match(page.page_name): |
paul@10 | 214 | pages.append(page) |
paul@10 | 215 | return pages |
paul@10 | 216 | |
paul@67 | 217 | # The main activity functions. |
paul@67 | 218 | |
paul@67 | 219 | class EventPage: |
paul@67 | 220 | |
paul@67 | 221 | "An event page." |
paul@67 | 222 | |
paul@67 | 223 | def __init__(self, page): |
paul@67 | 224 | self.page = page |
paul@69 | 225 | self.events = None |
paul@67 | 226 | self.body = None |
paul@67 | 227 | self.categories = None |
paul@67 | 228 | |
paul@67 | 229 | def copyPage(self, page): |
paul@67 | 230 | |
paul@67 | 231 | "Copy the body of the given 'page'." |
paul@67 | 232 | |
paul@67 | 233 | self.body = page.getBody() |
paul@67 | 234 | |
paul@67 | 235 | def getPageURL(self, request): |
paul@67 | 236 | |
paul@67 | 237 | "Using 'request', return the URL of this page." |
paul@24 | 238 | |
paul@67 | 239 | page = self.page |
paul@67 | 240 | |
paul@67 | 241 | if isMoin15(): |
paul@67 | 242 | return request.getQualifiedURL(page.url(request)) |
paul@67 | 243 | else: |
paul@67 | 244 | return request.getQualifiedURL(page.url(request, relative=0)) |
paul@67 | 245 | |
paul@67 | 246 | def getFormat(self): |
paul@67 | 247 | |
paul@67 | 248 | "Get the format used on this page." |
paul@24 | 249 | |
paul@67 | 250 | if isMoin15(): |
paul@67 | 251 | return "wiki" # page.pi_format |
paul@67 | 252 | else: |
paul@67 | 253 | return self.page.pi["format"] |
paul@67 | 254 | |
paul@67 | 255 | def getRevisions(self): |
paul@67 | 256 | |
paul@67 | 257 | "Return a list of page revisions." |
paul@67 | 258 | |
paul@67 | 259 | return self.page.getRevList() |
paul@67 | 260 | |
paul@67 | 261 | def getPageRevision(self): |
paul@24 | 262 | |
paul@67 | 263 | "Return the revision details dictionary for this page." |
paul@67 | 264 | |
paul@67 | 265 | return getPageRevision(self.page) |
paul@67 | 266 | |
paul@67 | 267 | def getPageName(self): |
paul@67 | 268 | |
paul@67 | 269 | "Return the page name." |
paul@67 | 270 | |
paul@67 | 271 | return self.page.page_name |
paul@24 | 272 | |
paul@67 | 273 | def getPrettyPageName(self): |
paul@67 | 274 | |
paul@67 | 275 | "Return a nicely formatted title/name for this page." |
paul@67 | 276 | |
paul@67 | 277 | return getPrettyPageName(self.page) |
paul@67 | 278 | |
paul@67 | 279 | def getBody(self): |
paul@67 | 280 | |
paul@67 | 281 | "Get the current page body." |
paul@47 | 282 | |
paul@67 | 283 | if self.body is None: |
paul@67 | 284 | self.body = self.page.get_raw_body() |
paul@67 | 285 | return self.body |
paul@67 | 286 | |
paul@69 | 287 | def getEvents(self): |
paul@69 | 288 | |
paul@69 | 289 | "Return a list of events from this page." |
paul@67 | 290 | |
paul@69 | 291 | if self.events is None: |
paul@69 | 292 | details = {} |
paul@69 | 293 | self.events = [Event(self, details)] |
paul@47 | 294 | |
paul@67 | 295 | if self.getFormat() == "wiki": |
paul@67 | 296 | for match in definition_list_regexp.finditer(self.getBody()): |
paul@67 | 297 | |
paul@67 | 298 | # Skip commented-out items. |
paul@47 | 299 | |
paul@67 | 300 | if match.group("optcomment"): |
paul@67 | 301 | continue |
paul@67 | 302 | |
paul@67 | 303 | # Permit case-insensitive list terms. |
paul@67 | 304 | |
paul@67 | 305 | term = match.group("term").lower() |
paul@67 | 306 | desc = match.group("desc") |
paul@67 | 307 | |
paul@67 | 308 | # Special value type handling. |
paul@27 | 309 | |
paul@67 | 310 | # Dates. |
paul@67 | 311 | |
paul@67 | 312 | if term in ("start", "end"): |
paul@67 | 313 | desc = getDate(desc) |
paul@67 | 314 | |
paul@67 | 315 | # Lists (whose elements may be quoted). |
paul@67 | 316 | |
paul@67 | 317 | elif term in ("topics", "categories"): |
paul@84 | 318 | desc = [getSimpleWikiText(value.strip()) for value in desc.split(",") if value.strip()] |
paul@67 | 319 | |
paul@67 | 320 | # Labels which may well be quoted. |
paul@67 | 321 | |
paul@92 | 322 | elif term in ("title", "summary", "description", "location"): |
paul@67 | 323 | desc = getSimpleWikiText(desc) |
paul@67 | 324 | |
paul@67 | 325 | if desc is not None: |
paul@69 | 326 | |
paul@69 | 327 | # Handle apparent duplicates by creating a new set of |
paul@69 | 328 | # details. |
paul@69 | 329 | |
paul@69 | 330 | if details.has_key(term): |
paul@92 | 331 | |
paul@92 | 332 | # Make a new event. |
paul@92 | 333 | |
paul@69 | 334 | details = {} |
paul@69 | 335 | self.events.append(Event(self, details)) |
paul@67 | 336 | |
paul@69 | 337 | details[term] = desc |
paul@69 | 338 | |
paul@69 | 339 | return self.events |
paul@69 | 340 | |
paul@69 | 341 | def setEvents(self, events): |
paul@69 | 342 | |
paul@69 | 343 | "Set the given 'events' on this page." |
paul@69 | 344 | |
paul@69 | 345 | self.events = events |
paul@67 | 346 | |
paul@67 | 347 | def getCategoryMembership(self): |
paul@27 | 348 | |
paul@67 | 349 | "Get the category names from this page." |
paul@67 | 350 | |
paul@67 | 351 | if self.categories is None: |
paul@67 | 352 | body = self.getBody() |
paul@67 | 353 | match = category_membership_regexp.search(body) |
paul@67 | 354 | self.categories = match.findall().split() |
paul@67 | 355 | |
paul@67 | 356 | return self.categories |
paul@67 | 357 | |
paul@67 | 358 | def setCategoryMembership(self, category_names): |
paul@10 | 359 | |
paul@67 | 360 | """ |
paul@67 | 361 | Set the category membership for the page using the specified |
paul@67 | 362 | 'category_names'. |
paul@67 | 363 | """ |
paul@67 | 364 | |
paul@67 | 365 | self.categories = category_names |
paul@67 | 366 | |
paul@67 | 367 | def flushEventDetails(self): |
paul@67 | 368 | |
paul@67 | 369 | "Flush the current event details to this page's body text." |
paul@10 | 370 | |
paul@67 | 371 | new_body_parts = [] |
paul@67 | 372 | end_of_last_match = 0 |
paul@67 | 373 | body = self.getBody() |
paul@69 | 374 | |
paul@69 | 375 | events = iter(self.getEvents()) |
paul@69 | 376 | |
paul@69 | 377 | event = events.next() |
paul@69 | 378 | event_details = event.getDetails() |
paul@69 | 379 | replaced_terms = set() |
paul@67 | 380 | |
paul@67 | 381 | for match in definition_list_regexp.finditer(body): |
paul@47 | 382 | |
paul@10 | 383 | # Permit case-insensitive list terms. |
paul@10 | 384 | |
paul@10 | 385 | term = match.group("term").lower() |
paul@10 | 386 | desc = match.group("desc") |
paul@10 | 387 | |
paul@69 | 388 | # Check that the term has not already been substituted. If so, |
paul@69 | 389 | # get the next event. |
paul@69 | 390 | |
paul@69 | 391 | if term in replaced_terms: |
paul@69 | 392 | try: |
paul@69 | 393 | event = events.next() |
paul@69 | 394 | |
paul@69 | 395 | # No more events. |
paul@69 | 396 | |
paul@69 | 397 | except StopIteration: |
paul@69 | 398 | break |
paul@69 | 399 | |
paul@69 | 400 | event_details = event.getDetails() |
paul@69 | 401 | replaced_terms = set() |
paul@69 | 402 | |
paul@75 | 403 | # Add preceding text to the new body. |
paul@75 | 404 | |
paul@75 | 405 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@75 | 406 | |
paul@75 | 407 | # Get the matching regions, adding the term to the new body. |
paul@75 | 408 | |
paul@75 | 409 | new_body_parts.append(match.group("wholeterm")) |
paul@75 | 410 | |
paul@10 | 411 | # Special value type handling. |
paul@10 | 412 | |
paul@67 | 413 | if event_details.has_key(term): |
paul@19 | 414 | |
paul@67 | 415 | # Dates. |
paul@47 | 416 | |
paul@67 | 417 | if term in ("start", "end"): |
paul@67 | 418 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@47 | 419 | |
paul@67 | 420 | # Lists (whose elements may be quoted). |
paul@47 | 421 | |
paul@67 | 422 | elif term in ("topics", "categories"): |
paul@92 | 423 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@47 | 424 | |
paul@92 | 425 | # Labels which must be quoted. |
paul@47 | 426 | |
paul@67 | 427 | elif term in ("title", "summary"): |
paul@67 | 428 | desc = getEncodedWikiText(event_details[term]) |
paul@47 | 429 | |
paul@67 | 430 | # Text which need not be quoted, but it will be Wiki text. |
paul@55 | 431 | |
paul@92 | 432 | elif term in ("description", "link", "location"): |
paul@67 | 433 | desc = event_details[term] |
paul@55 | 434 | |
paul@69 | 435 | replaced_terms.add(term) |
paul@69 | 436 | |
paul@75 | 437 | # Add the replaced value. |
paul@75 | 438 | |
paul@67 | 439 | new_body_parts.append(desc) |
paul@10 | 440 | |
paul@69 | 441 | # Remember where in the page has been processed. |
paul@69 | 442 | |
paul@69 | 443 | end_of_last_match = match.end() |
paul@69 | 444 | |
paul@69 | 445 | # Write the rest of the page. |
paul@69 | 446 | |
paul@69 | 447 | new_body_parts.append(body[end_of_last_match:]) |
paul@10 | 448 | |
paul@67 | 449 | self.body = "".join(new_body_parts) |
paul@11 | 450 | |
paul@67 | 451 | def flushCategoryMembership(self): |
paul@17 | 452 | |
paul@67 | 453 | "Flush the category membership to the page body." |
paul@11 | 454 | |
paul@67 | 455 | body = self.getBody() |
paul@67 | 456 | category_names = self.getCategoryMembership() |
paul@67 | 457 | match = category_membership_regexp.search(body) |
paul@10 | 458 | |
paul@67 | 459 | if match: |
paul@67 | 460 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@10 | 461 | |
paul@67 | 462 | def saveChanges(self): |
paul@10 | 463 | |
paul@67 | 464 | "Save changes to the event." |
paul@10 | 465 | |
paul@67 | 466 | self.flushEventDetails() |
paul@67 | 467 | self.flushCategoryMembership() |
paul@67 | 468 | self.page.saveText(self.getBody(), 0) |
paul@10 | 469 | |
paul@67 | 470 | def linkToPage(self, request, text, query_string=None): |
paul@11 | 471 | |
paul@67 | 472 | """ |
paul@67 | 473 | Using 'request', return a link to this page with the given link 'text' |
paul@67 | 474 | and optional 'query_string'. |
paul@67 | 475 | """ |
paul@11 | 476 | |
paul@67 | 477 | return linkToPage(request, self.page, text, query_string) |
paul@13 | 478 | |
paul@69 | 479 | class Event: |
paul@69 | 480 | |
paul@69 | 481 | "A description of an event." |
paul@69 | 482 | |
paul@69 | 483 | def __init__(self, page, details): |
paul@69 | 484 | self.page = page |
paul@69 | 485 | self.details = details |
paul@69 | 486 | |
paul@69 | 487 | def __cmp__(self, other): |
paul@69 | 488 | |
paul@69 | 489 | """ |
paul@69 | 490 | Compare this object with 'other' using the event start and end details. |
paul@69 | 491 | """ |
paul@69 | 492 | |
paul@69 | 493 | details1 = self.details |
paul@69 | 494 | details2 = other.details |
paul@69 | 495 | return cmp( |
paul@69 | 496 | (details1["start"], details1["end"]), |
paul@69 | 497 | (details2["start"], details2["end"]) |
paul@69 | 498 | ) |
paul@69 | 499 | |
paul@69 | 500 | def getPage(self): |
paul@69 | 501 | |
paul@69 | 502 | "Return the page describing this event." |
paul@69 | 503 | |
paul@69 | 504 | return self.page |
paul@69 | 505 | |
paul@69 | 506 | def setPage(self, page): |
paul@69 | 507 | |
paul@69 | 508 | "Set the 'page' describing this event." |
paul@69 | 509 | |
paul@69 | 510 | self.page = page |
paul@69 | 511 | |
paul@69 | 512 | def getSummary(self, event_parent=None): |
paul@69 | 513 | |
paul@69 | 514 | """ |
paul@69 | 515 | Return either the given title or summary of the event according to the |
paul@69 | 516 | event details, or a summary made from using the pretty version of the |
paul@69 | 517 | page name. |
paul@69 | 518 | |
paul@69 | 519 | If the optional 'event_parent' is specified, any page beneath the given |
paul@69 | 520 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@69 | 521 | if its name is used as the summary. |
paul@69 | 522 | """ |
paul@69 | 523 | |
paul@69 | 524 | event_details = self.details |
paul@69 | 525 | |
paul@69 | 526 | if event_details.has_key("title"): |
paul@69 | 527 | return event_details["title"] |
paul@69 | 528 | elif event_details.has_key("summary"): |
paul@69 | 529 | return event_details["summary"] |
paul@69 | 530 | else: |
paul@69 | 531 | # If appropriate, remove the parent details and "/" character. |
paul@69 | 532 | |
paul@69 | 533 | title = self.page.getPageName() |
paul@69 | 534 | |
paul@74 | 535 | if event_parent and title.startswith(event_parent): |
paul@69 | 536 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@69 | 537 | |
paul@69 | 538 | return getPrettyTitle(title) |
paul@69 | 539 | |
paul@69 | 540 | def getDetails(self): |
paul@69 | 541 | |
paul@69 | 542 | "Return the details for this event." |
paul@69 | 543 | |
paul@69 | 544 | return self.details |
paul@69 | 545 | |
paul@69 | 546 | def setDetails(self, event_details): |
paul@69 | 547 | |
paul@69 | 548 | "Set the 'event_details' for this event." |
paul@69 | 549 | |
paul@69 | 550 | self.details = event_details |
paul@69 | 551 | |
paul@10 | 552 | def getEvents(request, category_names, calendar_start=None, calendar_end=None): |
paul@10 | 553 | |
paul@10 | 554 | """ |
paul@10 | 555 | Using the 'request', generate a list of events found on pages belonging to |
paul@10 | 556 | the specified 'category_names', using the optional 'calendar_start' and |
paul@10 | 557 | 'calendar_end' month tuples of the form (year, month) to indicate a window |
paul@10 | 558 | of interest. |
paul@10 | 559 | |
paul@10 | 560 | Return a list of events, a dictionary mapping months to event lists (within |
paul@10 | 561 | the window of interest), a list of all events within the window of interest, |
paul@10 | 562 | the earliest month of an event within the window of interest, and the latest |
paul@10 | 563 | month of an event within the window of interest. |
paul@10 | 564 | """ |
paul@10 | 565 | |
paul@12 | 566 | # Re-order the window, if appropriate. |
paul@12 | 567 | |
paul@12 | 568 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@12 | 569 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@12 | 570 | |
paul@10 | 571 | events = [] |
paul@10 | 572 | shown_events = {} |
paul@10 | 573 | all_shown_events = [] |
paul@17 | 574 | processed_pages = set() |
paul@10 | 575 | |
paul@10 | 576 | earliest = None |
paul@10 | 577 | latest = None |
paul@10 | 578 | |
paul@10 | 579 | for category_name in category_names: |
paul@10 | 580 | |
paul@10 | 581 | # Get the pages and page names in the category. |
paul@10 | 582 | |
paul@67 | 583 | pages_in_category = getCategoryPages(category_name, request) |
paul@10 | 584 | |
paul@10 | 585 | # Visit each page in the category. |
paul@10 | 586 | |
paul@10 | 587 | for page_in_category in pages_in_category: |
paul@10 | 588 | pagename = page_in_category.page_name |
paul@10 | 589 | |
paul@17 | 590 | # Only process each page once. |
paul@17 | 591 | |
paul@17 | 592 | if pagename in processed_pages: |
paul@17 | 593 | continue |
paul@17 | 594 | else: |
paul@17 | 595 | processed_pages.add(pagename) |
paul@17 | 596 | |
paul@10 | 597 | # Get a real page, not a result page. |
paul@10 | 598 | |
paul@67 | 599 | event_page = EventPage(Page(request, pagename)) |
paul@10 | 600 | |
paul@69 | 601 | # Get all events described in the page. |
paul@10 | 602 | |
paul@69 | 603 | for event in event_page.getEvents(): |
paul@69 | 604 | event_details = event.getDetails() |
paul@10 | 605 | |
paul@69 | 606 | # Remember the event. |
paul@69 | 607 | |
paul@69 | 608 | events.append(event) |
paul@10 | 609 | |
paul@69 | 610 | # Test for the suitability of the event. |
paul@69 | 611 | |
paul@69 | 612 | if event_details.has_key("start") and event_details.has_key("end"): |
paul@10 | 613 | |
paul@69 | 614 | start_month = event_details["start"].as_month() |
paul@69 | 615 | end_month = event_details["end"].as_month() |
paul@10 | 616 | |
paul@69 | 617 | # Compare the months of the dates to the requested calendar |
paul@69 | 618 | # window, if any. |
paul@10 | 619 | |
paul@69 | 620 | if (calendar_start is None or end_month >= calendar_start) and \ |
paul@69 | 621 | (calendar_end is None or start_month <= calendar_end): |
paul@10 | 622 | |
paul@69 | 623 | all_shown_events.append(event) |
paul@10 | 624 | |
paul@69 | 625 | if earliest is None or start_month < earliest: |
paul@69 | 626 | earliest = start_month |
paul@69 | 627 | if latest is None or end_month > latest: |
paul@69 | 628 | latest = end_month |
paul@10 | 629 | |
paul@69 | 630 | # Store the event in the month-specific dictionary. |
paul@10 | 631 | |
paul@69 | 632 | first = max(start_month, calendar_start or start_month) |
paul@69 | 633 | last = min(end_month, calendar_end or end_month) |
paul@10 | 634 | |
paul@69 | 635 | for event_month in first.months_until(last): |
paul@69 | 636 | if not shown_events.has_key(event_month): |
paul@69 | 637 | shown_events[event_month] = [] |
paul@69 | 638 | shown_events[event_month].append(event) |
paul@10 | 639 | |
paul@10 | 640 | return events, shown_events, all_shown_events, earliest, latest |
paul@10 | 641 | |
paul@29 | 642 | def setEventTimestamps(request, events): |
paul@29 | 643 | |
paul@29 | 644 | """ |
paul@29 | 645 | Using 'request', set timestamp details in the details dictionary of each of |
paul@67 | 646 | the 'events'. |
paul@29 | 647 | |
paul@29 | 648 | Retutn the latest timestamp found. |
paul@29 | 649 | """ |
paul@29 | 650 | |
paul@29 | 651 | latest = None |
paul@29 | 652 | |
paul@69 | 653 | for event in events: |
paul@69 | 654 | event_details = event.getDetails() |
paul@69 | 655 | event_page = event.getPage() |
paul@29 | 656 | |
paul@29 | 657 | # Get the initial revision of the page. |
paul@29 | 658 | |
paul@67 | 659 | revisions = event_page.getRevisions() |
paul@67 | 660 | event_page_initial = Page(request, event_page.getPageName(), rev=revisions[-1]) |
paul@29 | 661 | |
paul@29 | 662 | # Get the created and last modified times. |
paul@29 | 663 | |
paul@30 | 664 | initial_revision = getPageRevision(event_page_initial) |
paul@30 | 665 | event_details["created"] = initial_revision["timestamp"] |
paul@67 | 666 | latest_revision = event_page.getPageRevision() |
paul@30 | 667 | event_details["last-modified"] = latest_revision["timestamp"] |
paul@29 | 668 | event_details["sequence"] = len(revisions) - 1 |
paul@30 | 669 | event_details["last-comment"] = latest_revision["comment"] |
paul@29 | 670 | |
paul@29 | 671 | if latest is None or latest < event_details["last-modified"]: |
paul@29 | 672 | latest = event_details["last-modified"] |
paul@29 | 673 | |
paul@29 | 674 | return latest |
paul@29 | 675 | |
paul@26 | 676 | def getOrderedEvents(events): |
paul@26 | 677 | |
paul@26 | 678 | """ |
paul@26 | 679 | Return a list with the given 'events' ordered according to their start and |
paul@67 | 680 | end dates. |
paul@26 | 681 | """ |
paul@26 | 682 | |
paul@26 | 683 | ordered_events = events[:] |
paul@68 | 684 | ordered_events.sort() |
paul@26 | 685 | return ordered_events |
paul@26 | 686 | |
paul@13 | 687 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest): |
paul@13 | 688 | |
paul@13 | 689 | """ |
paul@13 | 690 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 691 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 692 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 693 | specific period of interest. |
paul@13 | 694 | """ |
paul@13 | 695 | |
paul@13 | 696 | # Define the period as starting with any specified start month or the |
paul@13 | 697 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 698 | # event known. |
paul@13 | 699 | |
paul@13 | 700 | first = calendar_start or earliest |
paul@13 | 701 | last = calendar_end or latest |
paul@13 | 702 | |
paul@13 | 703 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 704 | # events in the requested period, and there was no start or end month |
paul@13 | 705 | # specified, show only the month indicated by the start or end of the |
paul@13 | 706 | # requested period. If all events were to be shown but none were found show |
paul@13 | 707 | # the current month. |
paul@13 | 708 | |
paul@13 | 709 | if first is None: |
paul@13 | 710 | first = last or getCurrentMonth() |
paul@13 | 711 | if last is None: |
paul@13 | 712 | last = first or getCurrentMonth() |
paul@13 | 713 | |
paul@13 | 714 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 715 | |
paul@13 | 716 | return min(first, last), last |
paul@13 | 717 | |
paul@15 | 718 | def getCoverage(start, end, events): |
paul@15 | 719 | |
paul@15 | 720 | """ |
paul@15 | 721 | Within the period defined by the 'start' and 'end' dates, determine the |
paul@15 | 722 | coverage of the days in the period by the given 'events', returning a set of |
paul@15 | 723 | covered days, along with a list of slots, where each slot contains a tuple |
paul@15 | 724 | of the form (set of covered days, events). |
paul@15 | 725 | """ |
paul@15 | 726 | |
paul@15 | 727 | all_events = [] |
paul@15 | 728 | full_coverage = set() |
paul@15 | 729 | |
paul@15 | 730 | # Get event details. |
paul@15 | 731 | |
paul@69 | 732 | for event in events: |
paul@69 | 733 | event_details = event.getDetails() |
paul@15 | 734 | |
paul@15 | 735 | # Test for the event in the period. |
paul@15 | 736 | |
paul@15 | 737 | if event_details["start"] <= end and event_details["end"] >= start: |
paul@15 | 738 | |
paul@15 | 739 | # Find the coverage of this period for the event. |
paul@15 | 740 | |
paul@15 | 741 | event_start = max(event_details["start"], start) |
paul@15 | 742 | event_end = min(event_details["end"], end) |
paul@67 | 743 | event_coverage = set(event_start.days_until(event_end)) |
paul@15 | 744 | |
paul@15 | 745 | # Update the overall coverage. |
paul@15 | 746 | |
paul@15 | 747 | full_coverage.update(event_coverage) |
paul@15 | 748 | |
paul@15 | 749 | # Try and fit the event into the events list. |
paul@15 | 750 | |
paul@15 | 751 | for i, (coverage, covered_events) in enumerate(all_events): |
paul@15 | 752 | |
paul@15 | 753 | # Where the event does not overlap with the current |
paul@15 | 754 | # element, add it alongside existing events. |
paul@15 | 755 | |
paul@15 | 756 | if not coverage.intersection(event_coverage): |
paul@69 | 757 | covered_events.append(event) |
paul@15 | 758 | all_events[i] = coverage.union(event_coverage), covered_events |
paul@15 | 759 | break |
paul@15 | 760 | |
paul@15 | 761 | # Make a new element in the list if the event cannot be |
paul@15 | 762 | # marked alongside existing events. |
paul@15 | 763 | |
paul@15 | 764 | else: |
paul@69 | 765 | all_events.append((event_coverage, [event])) |
paul@15 | 766 | |
paul@15 | 767 | return full_coverage, all_events |
paul@15 | 768 | |
paul@67 | 769 | # Date-related functions. |
paul@67 | 770 | |
paul@67 | 771 | class Period: |
paul@67 | 772 | |
paul@67 | 773 | "A simple period of time." |
paul@67 | 774 | |
paul@67 | 775 | def __init__(self, data): |
paul@67 | 776 | self.data = data |
paul@67 | 777 | |
paul@67 | 778 | def months(self): |
paul@67 | 779 | return self.data[0] * 12 + self.data[1] |
paul@67 | 780 | |
paul@92 | 781 | class Temporal: |
paul@67 | 782 | |
paul@92 | 783 | "A simple temporal representation, common to dates and times." |
paul@67 | 784 | |
paul@67 | 785 | def __init__(self, data): |
paul@92 | 786 | self.data = list(data) |
paul@67 | 787 | |
paul@67 | 788 | def __repr__(self): |
paul@67 | 789 | return "%s(%r)" % (self.__class__.__name__, self.data) |
paul@67 | 790 | |
paul@67 | 791 | def __hash__(self): |
paul@67 | 792 | return hash(self.as_tuple()) |
paul@67 | 793 | |
paul@67 | 794 | def as_tuple(self): |
paul@92 | 795 | return tuple(self.data) |
paul@92 | 796 | |
paul@92 | 797 | def __cmp__(self, other): |
paul@92 | 798 | data = self.as_tuple() |
paul@92 | 799 | other_data = other.as_tuple() |
paul@92 | 800 | length = min(len(data), len(other_data)) |
paul@92 | 801 | return cmp(self.data[:length], other.data[:length]) |
paul@92 | 802 | |
paul@92 | 803 | class Month(Temporal): |
paul@92 | 804 | |
paul@92 | 805 | "A simple year-month representation." |
paul@92 | 806 | |
paul@92 | 807 | def __str__(self): |
paul@92 | 808 | return "%04d-%02d" % self.as_tuple()[:2] |
paul@67 | 809 | |
paul@93 | 810 | def as_datetime(self, day, hour, minute, second, zone): |
paul@93 | 811 | return DateTime(self.as_tuple() + (day, hour, minute, second, zone)) |
paul@93 | 812 | |
paul@67 | 813 | def as_date(self, day): |
paul@67 | 814 | return Date(self.as_tuple() + (day,)) |
paul@67 | 815 | |
paul@92 | 816 | def as_month(self): |
paul@92 | 817 | return self |
paul@92 | 818 | |
paul@67 | 819 | def year(self): |
paul@67 | 820 | return self.data[0] |
paul@67 | 821 | |
paul@67 | 822 | def month(self): |
paul@67 | 823 | return self.data[1] |
paul@67 | 824 | |
paul@67 | 825 | def month_properties(self): |
paul@67 | 826 | |
paul@67 | 827 | """ |
paul@67 | 828 | Return the weekday of the 1st of the month, along with the number of |
paul@67 | 829 | days, as a tuple. |
paul@67 | 830 | """ |
paul@67 | 831 | |
paul@92 | 832 | year, month = self.as_tuple()[:2] |
paul@67 | 833 | return calendar.monthrange(year, month) |
paul@67 | 834 | |
paul@67 | 835 | def month_update(self, n=1): |
paul@67 | 836 | |
paul@67 | 837 | "Return the month updated by 'n' months." |
paul@67 | 838 | |
paul@92 | 839 | year, month = self.as_tuple()[:2] |
paul@67 | 840 | return Month((year + (month - 1 + n) / 12, (month - 1 + n) % 12 + 1)) |
paul@67 | 841 | |
paul@67 | 842 | def next_month(self): |
paul@67 | 843 | |
paul@67 | 844 | "Return the month following this one." |
paul@67 | 845 | |
paul@67 | 846 | return self.month_update(1) |
paul@67 | 847 | |
paul@67 | 848 | def previous_month(self): |
paul@67 | 849 | |
paul@67 | 850 | "Return the month preceding this one." |
paul@67 | 851 | |
paul@67 | 852 | return self.month_update(-1) |
paul@67 | 853 | |
paul@67 | 854 | def __sub__(self, start): |
paul@67 | 855 | |
paul@67 | 856 | """ |
paul@67 | 857 | Return the difference in years and months between this month and the |
paul@67 | 858 | 'start' month as a period. |
paul@67 | 859 | """ |
paul@67 | 860 | |
paul@67 | 861 | return Period([(x - y) for x, y in zip(self.data, start.data)]) |
paul@67 | 862 | |
paul@92 | 863 | def until(self, start, end, nextfn, prevfn): |
paul@92 | 864 | |
paul@92 | 865 | """ |
paul@92 | 866 | Return a collection of units of time by starting from the given 'start' |
paul@92 | 867 | and stepping across intervening units until 'end' is reached, using the |
paul@92 | 868 | given 'nextfn' and 'prevfn' to step from one unit to the next. |
paul@92 | 869 | """ |
paul@67 | 870 | |
paul@92 | 871 | current = start |
paul@92 | 872 | units = [current] |
paul@92 | 873 | if current < end: |
paul@92 | 874 | while current < end: |
paul@92 | 875 | current = nextfn(current) |
paul@92 | 876 | units.append(current) |
paul@92 | 877 | elif current > end: |
paul@92 | 878 | while current > end: |
paul@92 | 879 | current = prevfn(current) |
paul@92 | 880 | units.append(current) |
paul@92 | 881 | return units |
paul@67 | 882 | |
paul@67 | 883 | def months_until(self, end): |
paul@92 | 884 | |
paul@92 | 885 | "Return the collection of months from this month until 'end'." |
paul@92 | 886 | |
paul@92 | 887 | return self.until(self.as_month(), end.as_month(), Month.next_month, Month.previous_month) |
paul@67 | 888 | |
paul@67 | 889 | class Date(Month): |
paul@67 | 890 | |
paul@67 | 891 | "A simple year-month-day representation." |
paul@67 | 892 | |
paul@67 | 893 | def __str__(self): |
paul@67 | 894 | return "%04d-%02d-%02d" % self.as_tuple()[:3] |
paul@67 | 895 | |
paul@93 | 896 | def as_datetime(self, hour, minute, second, zone): |
paul@93 | 897 | return DateTime(self.as_tuple() + (hour, minute, second, zone)) |
paul@93 | 898 | |
paul@92 | 899 | def as_date(self): |
paul@92 | 900 | return self |
paul@92 | 901 | |
paul@67 | 902 | def as_month(self): |
paul@67 | 903 | return Month(self.data[:2]) |
paul@67 | 904 | |
paul@67 | 905 | def day(self): |
paul@67 | 906 | return self.data[2] |
paul@67 | 907 | |
paul@67 | 908 | def next_day(self): |
paul@67 | 909 | |
paul@67 | 910 | "Return the date following this one." |
paul@67 | 911 | |
paul@92 | 912 | year, month, day = self.as_tuple()[:3] |
paul@67 | 913 | _wd, end_day = calendar.monthrange(year, month) |
paul@67 | 914 | if day == end_day: |
paul@67 | 915 | if month == 12: |
paul@67 | 916 | return Date((year + 1, 1, 1)) |
paul@67 | 917 | else: |
paul@67 | 918 | return Date((year, month + 1, 1)) |
paul@67 | 919 | else: |
paul@67 | 920 | return Date((year, month, day + 1)) |
paul@67 | 921 | |
paul@67 | 922 | def previous_day(self): |
paul@67 | 923 | |
paul@67 | 924 | "Return the date preceding this one." |
paul@67 | 925 | |
paul@92 | 926 | year, month, day = self.as_tuple()[:3] |
paul@67 | 927 | if day == 1: |
paul@67 | 928 | if month == 1: |
paul@67 | 929 | return Date((year - 1, 12, 31)) |
paul@67 | 930 | else: |
paul@67 | 931 | _wd, end_day = calendar.monthrange(year, month - 1) |
paul@67 | 932 | return Date((year, month - 1, end_day)) |
paul@67 | 933 | else: |
paul@67 | 934 | return Date((year, month, day - 1)) |
paul@67 | 935 | |
paul@67 | 936 | def days_until(self, end): |
paul@92 | 937 | |
paul@92 | 938 | "Return the collection of days from this date until 'end'." |
paul@92 | 939 | |
paul@92 | 940 | return self.until(self.as_date(), end.as_date(), Date.next_day, Date.previous_day) |
paul@92 | 941 | |
paul@92 | 942 | class DateTime(Date): |
paul@92 | 943 | |
paul@92 | 944 | "A simple date plus time representation." |
paul@92 | 945 | |
paul@92 | 946 | def __init__(self, data): |
paul@92 | 947 | Date.__init__(self, data) |
paul@92 | 948 | |
paul@92 | 949 | def __str__(self): |
paul@92 | 950 | if self.has_time(): |
paul@92 | 951 | data = self.as_tuple() |
paul@92 | 952 | time_str = " %02d:%02d" % data[3:5] |
paul@92 | 953 | if data[5] is not None: |
paul@92 | 954 | time_str += ":%02d" % data[5] |
paul@92 | 955 | if data[6] is not None: |
paul@92 | 956 | time_str += " %s" % data[6] |
paul@92 | 957 | else: |
paul@92 | 958 | time_str = "" |
paul@92 | 959 | |
paul@92 | 960 | return Date.__str__(self) + time_str |
paul@92 | 961 | |
paul@93 | 962 | def as_datetime(self): |
paul@93 | 963 | return self |
paul@93 | 964 | |
paul@92 | 965 | def as_date(self): |
paul@92 | 966 | return Date(self.data[:3]) |
paul@92 | 967 | |
paul@92 | 968 | def has_time(self): |
paul@92 | 969 | return self.data[3] is not None and self.data[4] is not None |
paul@92 | 970 | |
paul@92 | 971 | def seconds(self): |
paul@92 | 972 | return self.data[5] |
paul@92 | 973 | |
paul@92 | 974 | def time_zone(self): |
paul@92 | 975 | return self.data[6] |
paul@92 | 976 | |
paul@93 | 977 | def set_time_zone(self, value): |
paul@92 | 978 | self.data[6] = value |
paul@92 | 979 | |
paul@92 | 980 | def padded(self): |
paul@92 | 981 | |
paul@92 | 982 | "Return a datetime with missing fields defined as being zero." |
paul@92 | 983 | |
paul@92 | 984 | data = map(lambda x: x or 0, self.data[:6]) + self.data[6:] |
paul@92 | 985 | return DateTime(data) |
paul@92 | 986 | |
paul@93 | 987 | def to_utc(self): |
paul@93 | 988 | |
paul@93 | 989 | """ |
paul@93 | 990 | Return this object converted to UTC, or None if such a conversion is not |
paul@93 | 991 | defined. |
paul@93 | 992 | """ |
paul@93 | 993 | |
paul@93 | 994 | offset = self.utc_offset() |
paul@93 | 995 | if offset: |
paul@93 | 996 | hours, minutes = offset |
paul@93 | 997 | |
paul@93 | 998 | # Invert the offset to get the correction. |
paul@93 | 999 | |
paul@93 | 1000 | hours, minutes = -hours, -minutes |
paul@93 | 1001 | |
paul@93 | 1002 | # Get the components. |
paul@93 | 1003 | |
paul@93 | 1004 | hour, minute, second, zone = self.as_tuple()[3:] |
paul@93 | 1005 | date = self.as_date() |
paul@93 | 1006 | |
paul@93 | 1007 | # Add the minutes and hours. |
paul@93 | 1008 | |
paul@93 | 1009 | minute += minutes |
paul@93 | 1010 | if minute < 0: |
paul@93 | 1011 | hour -= 1 |
paul@93 | 1012 | minute += 60 |
paul@93 | 1013 | elif minute > 59: |
paul@93 | 1014 | hour += 1 |
paul@93 | 1015 | minute -= 60 |
paul@93 | 1016 | |
paul@93 | 1017 | hour += hours |
paul@93 | 1018 | if hour < 0: |
paul@93 | 1019 | date = date.previous_day() |
paul@93 | 1020 | hour += 24 |
paul@93 | 1021 | elif hour > 23: |
paul@93 | 1022 | date = date.next_day() |
paul@93 | 1023 | hour -= 24 |
paul@93 | 1024 | |
paul@93 | 1025 | return date.as_datetime(hour, minute, second, "UTC") |
paul@93 | 1026 | else: |
paul@93 | 1027 | return None |
paul@93 | 1028 | |
paul@93 | 1029 | def utc_offset(self): |
paul@93 | 1030 | |
paul@93 | 1031 | "Return the UTC offset in hours and minutes." |
paul@93 | 1032 | |
paul@93 | 1033 | zone = self.time_zone() |
paul@95 | 1034 | if not zone: |
paul@95 | 1035 | return None |
paul@93 | 1036 | |
paul@94 | 1037 | # Attempt to return a UTC offset where an explicit offset has been set. |
paul@93 | 1038 | |
paul@94 | 1039 | match = timezone_offset_regexp.match(zone) |
paul@94 | 1040 | if match: |
paul@94 | 1041 | if match.group("sign") == "-": |
paul@93 | 1042 | sign = -1 |
paul@93 | 1043 | else: |
paul@93 | 1044 | sign = 1 |
paul@93 | 1045 | |
paul@94 | 1046 | hours = int(match.group("hours")) * sign |
paul@94 | 1047 | minutes = int(match.group("minutes") or 0) * sign |
paul@94 | 1048 | return hours, minutes |
paul@94 | 1049 | |
paul@93 | 1050 | return None |
paul@93 | 1051 | |
paul@93 | 1052 | def getCountry(s): |
paul@92 | 1053 | |
paul@93 | 1054 | "Find a country code in the given string 's'." |
paul@93 | 1055 | |
paul@93 | 1056 | match = country_code_regexp.search(s) |
paul@92 | 1057 | |
paul@93 | 1058 | if match: |
paul@93 | 1059 | return match.group("code") |
paul@93 | 1060 | else: |
paul@93 | 1061 | return None |
paul@67 | 1062 | |
paul@67 | 1063 | def getDate(s): |
paul@67 | 1064 | |
paul@92 | 1065 | "Parse the string 's', extracting and returning a datetime object." |
paul@67 | 1066 | |
paul@92 | 1067 | m = datetime_regexp.search(s) |
paul@67 | 1068 | if m: |
paul@92 | 1069 | groups = list(m.groups()) |
paul@92 | 1070 | |
paul@94 | 1071 | # Convert date and time data to integer or None. |
paul@92 | 1072 | |
paul@94 | 1073 | return DateTime(map(int_or_none, groups[:6]) + [m.group("zone")]) |
paul@67 | 1074 | else: |
paul@67 | 1075 | return None |
paul@67 | 1076 | |
paul@88 | 1077 | def getDateStrings(s): |
paul@88 | 1078 | |
paul@88 | 1079 | "Parse the string 's', extracting and returning all date strings." |
paul@88 | 1080 | |
paul@88 | 1081 | start = 0 |
paul@88 | 1082 | m = date_regexp.search(s, start) |
paul@88 | 1083 | l = [] |
paul@88 | 1084 | while m: |
paul@88 | 1085 | l.append("-".join(m.groups())) |
paul@88 | 1086 | m = date_regexp.search(s, m.end()) |
paul@88 | 1087 | return l |
paul@88 | 1088 | |
paul@67 | 1089 | def getMonth(s): |
paul@67 | 1090 | |
paul@88 | 1091 | "Parse the string 's', extracting and returning a month object." |
paul@67 | 1092 | |
paul@67 | 1093 | m = month_regexp.search(s) |
paul@67 | 1094 | if m: |
paul@67 | 1095 | return Month(map(int, m.groups())) |
paul@67 | 1096 | else: |
paul@67 | 1097 | return None |
paul@67 | 1098 | |
paul@67 | 1099 | def getCurrentMonth(): |
paul@67 | 1100 | |
paul@67 | 1101 | "Return the current month as a (year, month) tuple." |
paul@67 | 1102 | |
paul@67 | 1103 | today = datetime.date.today() |
paul@67 | 1104 | return Month((today.year, today.month)) |
paul@67 | 1105 | |
paul@67 | 1106 | def getCurrentYear(): |
paul@67 | 1107 | |
paul@67 | 1108 | "Return the current year." |
paul@67 | 1109 | |
paul@67 | 1110 | today = datetime.date.today() |
paul@67 | 1111 | return today.year |
paul@67 | 1112 | |
paul@19 | 1113 | # User interface functions. |
paul@19 | 1114 | |
paul@55 | 1115 | def getParameter(request, name, default=None): |
paul@92 | 1116 | |
paul@92 | 1117 | """ |
paul@92 | 1118 | Using the given 'request', return the value of the parameter with the given |
paul@92 | 1119 | 'name', returning the optional 'default' (or None) if no value was supplied |
paul@92 | 1120 | in the 'request'. |
paul@92 | 1121 | """ |
paul@92 | 1122 | |
paul@55 | 1123 | return request.form.get(name, [default])[0] |
paul@23 | 1124 | |
paul@58 | 1125 | def getQualifiedParameter(request, calendar_name, argname, default=None): |
paul@92 | 1126 | |
paul@92 | 1127 | """ |
paul@92 | 1128 | Using the given 'request', 'calendar_name' and 'argname', retrieve the |
paul@92 | 1129 | value of the qualified parameter, returning the optional 'default' (or None) |
paul@92 | 1130 | if no value was supplied in the 'request'. |
paul@92 | 1131 | """ |
paul@92 | 1132 | |
paul@58 | 1133 | argname = getQualifiedParameterName(calendar_name, argname) |
paul@58 | 1134 | return getParameter(request, argname, default) |
paul@58 | 1135 | |
paul@58 | 1136 | def getQualifiedParameterName(calendar_name, argname): |
paul@92 | 1137 | |
paul@92 | 1138 | """ |
paul@92 | 1139 | Return the qualified parameter name using the given 'calendar_name' and |
paul@92 | 1140 | 'argname'. |
paul@92 | 1141 | """ |
paul@92 | 1142 | |
paul@58 | 1143 | if calendar_name is None: |
paul@58 | 1144 | return argname |
paul@58 | 1145 | else: |
paul@58 | 1146 | return "%s-%s" % (calendar_name, argname) |
paul@58 | 1147 | |
paul@19 | 1148 | def getParameterMonth(arg): |
paul@67 | 1149 | |
paul@67 | 1150 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@67 | 1151 | |
paul@19 | 1152 | n = None |
paul@19 | 1153 | |
paul@19 | 1154 | if arg.startswith("current"): |
paul@19 | 1155 | date = getCurrentMonth() |
paul@19 | 1156 | if len(arg) > 8: |
paul@19 | 1157 | n = int(arg[7:]) |
paul@19 | 1158 | |
paul@19 | 1159 | elif arg.startswith("yearstart"): |
paul@67 | 1160 | date = Month((getCurrentYear(), 1)) |
paul@19 | 1161 | if len(arg) > 10: |
paul@19 | 1162 | n = int(arg[9:]) |
paul@19 | 1163 | |
paul@19 | 1164 | elif arg.startswith("yearend"): |
paul@67 | 1165 | date = Month((getCurrentYear(), 12)) |
paul@19 | 1166 | if len(arg) > 8: |
paul@19 | 1167 | n = int(arg[7:]) |
paul@19 | 1168 | |
paul@19 | 1169 | else: |
paul@19 | 1170 | date = getMonth(arg) |
paul@19 | 1171 | |
paul@19 | 1172 | if n is not None: |
paul@67 | 1173 | date = date.month_update(n) |
paul@19 | 1174 | |
paul@19 | 1175 | return date |
paul@19 | 1176 | |
paul@19 | 1177 | def getFormMonth(request, calendar_name, argname): |
paul@67 | 1178 | |
paul@67 | 1179 | """ |
paul@67 | 1180 | Return the month from the 'request' for the calendar with the given |
paul@67 | 1181 | 'calendar_name' using the parameter having the given 'argname'. |
paul@67 | 1182 | """ |
paul@67 | 1183 | |
paul@58 | 1184 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@19 | 1185 | if arg is not None: |
paul@19 | 1186 | return getParameterMonth(arg) |
paul@19 | 1187 | else: |
paul@19 | 1188 | return None |
paul@19 | 1189 | |
paul@23 | 1190 | def getFormMonthPair(request, yeararg, montharg): |
paul@67 | 1191 | |
paul@67 | 1192 | """ |
paul@67 | 1193 | Return the month from the 'request' for the calendar with the given |
paul@67 | 1194 | 'calendar_name' using the parameters having the given 'yeararg' and |
paul@67 | 1195 | 'montharg' names. |
paul@67 | 1196 | """ |
paul@67 | 1197 | |
paul@23 | 1198 | year = getParameter(request, yeararg) |
paul@23 | 1199 | month = getParameter(request, montharg) |
paul@23 | 1200 | if year and month: |
paul@67 | 1201 | return Month((int(year), int(month))) |
paul@23 | 1202 | else: |
paul@23 | 1203 | return None |
paul@23 | 1204 | |
paul@67 | 1205 | # Page-related functions. |
paul@67 | 1206 | |
paul@19 | 1207 | def getPrettyPageName(page): |
paul@19 | 1208 | |
paul@19 | 1209 | "Return a nicely formatted title/name for the given 'page'." |
paul@19 | 1210 | |
paul@27 | 1211 | if isMoin15(): |
paul@27 | 1212 | title = page.split_title(page.request, force=1) |
paul@27 | 1213 | else: |
paul@27 | 1214 | title = page.split_title(force=1) |
paul@27 | 1215 | |
paul@55 | 1216 | return getPrettyTitle(title) |
paul@55 | 1217 | |
paul@27 | 1218 | def linkToPage(request, page, text, query_string=None): |
paul@27 | 1219 | |
paul@27 | 1220 | """ |
paul@27 | 1221 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@27 | 1222 | optional 'query_string'. |
paul@27 | 1223 | """ |
paul@27 | 1224 | |
paul@27 | 1225 | text = wikiutil.escape(text) |
paul@27 | 1226 | |
paul@27 | 1227 | if isMoin15(): |
paul@27 | 1228 | url = wikiutil.quoteWikinameURL(page.page_name) |
paul@27 | 1229 | if query_string is not None: |
paul@27 | 1230 | url = "%s?%s" % (url, query_string) |
paul@27 | 1231 | return wikiutil.link_tag(request, url, text, getattr(page, "formatter", None)) |
paul@27 | 1232 | else: |
paul@27 | 1233 | return page.link_to_raw(request, text, query_string) |
paul@27 | 1234 | |
paul@88 | 1235 | def getFullPageName(parent, title): |
paul@88 | 1236 | |
paul@88 | 1237 | """ |
paul@88 | 1238 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@88 | 1239 | and 'title' (a simple page name). |
paul@88 | 1240 | """ |
paul@88 | 1241 | |
paul@88 | 1242 | if parent: |
paul@88 | 1243 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@88 | 1244 | else: |
paul@88 | 1245 | return title |
paul@88 | 1246 | |
paul@88 | 1247 | def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): |
paul@88 | 1248 | |
paul@88 | 1249 | """ |
paul@88 | 1250 | Using the given 'template_page', complete the 'new_page' by copying the |
paul@88 | 1251 | template and adding the given 'event_details' (a dictionary of event |
paul@88 | 1252 | fields), setting also the 'category_pagenames' to define category |
paul@88 | 1253 | membership. |
paul@88 | 1254 | """ |
paul@88 | 1255 | |
paul@88 | 1256 | event_page = EventPage(template_page) |
paul@88 | 1257 | new_event_page = EventPage(new_page) |
paul@88 | 1258 | new_event_page.copyPage(event_page) |
paul@88 | 1259 | |
paul@88 | 1260 | if new_event_page.getFormat() == "wiki": |
paul@88 | 1261 | new_event = Event(new_event_page, event_details) |
paul@88 | 1262 | new_event_page.setEvents([new_event]) |
paul@88 | 1263 | new_event_page.setCategoryMembership(category_pagenames) |
paul@88 | 1264 | new_event_page.saveChanges() |
paul@88 | 1265 | |
paul@10 | 1266 | # vim: tabstop=4 expandtab shiftwidth=4 |