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