paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregator library |
paul@10 | 4 | |
paul@137 | 5 | @copyright: 2008, 2009, 2010, 2011 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@122 | 18 | import bisect |
paul@122 | 19 | import operator |
paul@10 | 20 | |
paul@69 | 21 | try: |
paul@69 | 22 | set |
paul@69 | 23 | except NameError: |
paul@69 | 24 | from sets import Set as set |
paul@69 | 25 | |
paul@100 | 26 | try: |
paul@100 | 27 | import pytz |
paul@100 | 28 | except ImportError: |
paul@100 | 29 | pytz = None |
paul@100 | 30 | |
paul@137 | 31 | escape = wikiutil.escape |
paul@137 | 32 | |
paul@109 | 33 | __version__ = "0.7" |
paul@10 | 34 | |
paul@22 | 35 | # Date labels. |
paul@22 | 36 | |
paul@22 | 37 | month_labels = ["January", "February", "March", "April", "May", "June", |
paul@22 | 38 | "July", "August", "September", "October", "November", "December"] |
paul@22 | 39 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@22 | 40 | |
paul@10 | 41 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 42 | |
paul@10 | 43 | category_regexp = None |
paul@47 | 44 | |
paul@47 | 45 | # Page parsing. |
paul@47 | 46 | |
paul@84 | 47 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@158 | 48 | category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE) |
paul@47 | 49 | |
paul@47 | 50 | # Value parsing. |
paul@47 | 51 | |
paul@93 | 52 | country_code_regexp = re.compile(ur'(?:^|\W)(?P<code>[A-Z]{2})(?:$|\W+$)', re.UNICODE) |
paul@92 | 53 | |
paul@92 | 54 | month_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})' |
paul@92 | 55 | date_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})' |
paul@92 | 56 | time_regexp_str = ur'(?P<hour>[0-2][0-9]):(?P<minute>[0-5][0-9])(?::(?P<second>[0-6][0-9]))?' |
paul@98 | 57 | timezone_offset_str = ur'(?P<offset>(UTC)?(?:(?P<sign>[-+])(?P<hours>[0-9]{2})(?::?(?P<minutes>[0-9]{2}))?))' |
paul@98 | 58 | timezone_olson_str = ur'(?P<olson>[a-zA-Z]+(?:/[-_a-zA-Z]+){1,2})' |
paul@98 | 59 | timezone_utc_str = ur'UTC' |
paul@98 | 60 | timezone_regexp_str = ur'(?P<zone>' + timezone_offset_str + '|' + timezone_olson_str + '|' + timezone_utc_str + ')' |
paul@92 | 61 | datetime_regexp_str = date_regexp_str + ur'(?:\s+' + time_regexp_str + ur'(?:\s+' + timezone_regexp_str + ur')?)?' |
paul@92 | 62 | |
paul@92 | 63 | month_regexp = re.compile(month_regexp_str, re.UNICODE) |
paul@92 | 64 | date_regexp = re.compile(date_regexp_str, re.UNICODE) |
paul@92 | 65 | time_regexp = re.compile(time_regexp_str, re.UNICODE) |
paul@92 | 66 | datetime_regexp = re.compile(datetime_regexp_str, re.UNICODE) |
paul@100 | 67 | timezone_olson_regexp = re.compile(timezone_olson_str, re.UNICODE) |
paul@94 | 68 | timezone_offset_regexp = re.compile(timezone_offset_str, re.UNICODE) |
paul@92 | 69 | |
paul@19 | 70 | verbatim_regexp = re.compile(ur'(?:' |
paul@19 | 71 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@19 | 72 | ur'|' |
paul@19 | 73 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@19 | 74 | ur'|' |
paul@19 | 75 | ur'`(?P<monospace>.*?)`' |
paul@19 | 76 | ur'|' |
paul@19 | 77 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@19 | 78 | ur')', re.UNICODE) |
paul@10 | 79 | |
paul@10 | 80 | # Utility functions. |
paul@10 | 81 | |
paul@10 | 82 | def getCategoryPattern(request): |
paul@10 | 83 | global category_regexp |
paul@10 | 84 | |
paul@10 | 85 | try: |
paul@10 | 86 | return request.cfg.cache.page_category_regexact |
paul@10 | 87 | except AttributeError: |
paul@10 | 88 | |
paul@10 | 89 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 90 | |
paul@10 | 91 | if category_regexp is None: |
paul@10 | 92 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 93 | return category_regexp |
paul@10 | 94 | |
paul@92 | 95 | def int_or_none(x): |
paul@92 | 96 | if x is None: |
paul@92 | 97 | return x |
paul@92 | 98 | else: |
paul@92 | 99 | return int(x) |
paul@92 | 100 | |
paul@118 | 101 | def sort_none_first(x, y): |
paul@118 | 102 | if x is None: |
paul@118 | 103 | return -1 |
paul@118 | 104 | elif y is None: |
paul@118 | 105 | return 1 |
paul@118 | 106 | else: |
paul@118 | 107 | return cmp(x, y) |
paul@118 | 108 | |
paul@109 | 109 | # Utility classes and associated functions. |
paul@109 | 110 | |
paul@109 | 111 | class Form: |
paul@109 | 112 | |
paul@109 | 113 | """ |
paul@109 | 114 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@109 | 115 | environment. |
paul@109 | 116 | """ |
paul@109 | 117 | |
paul@109 | 118 | def __init__(self, form): |
paul@109 | 119 | self.form = form |
paul@109 | 120 | |
paul@110 | 121 | def get(self, name, default=None): |
paul@109 | 122 | values = self.form.getlist(name) |
paul@109 | 123 | if not values: |
paul@110 | 124 | return default |
paul@109 | 125 | else: |
paul@109 | 126 | return values |
paul@109 | 127 | |
paul@109 | 128 | def __getitem__(self, name): |
paul@109 | 129 | return self.form.getlist(name) |
paul@109 | 130 | |
paul@109 | 131 | class ActionSupport: |
paul@109 | 132 | |
paul@112 | 133 | """ |
paul@112 | 134 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@112 | 135 | convenience methods. |
paul@112 | 136 | """ |
paul@109 | 137 | |
paul@109 | 138 | def get_form(self): |
paul@109 | 139 | return get_form(self.request) |
paul@109 | 140 | |
paul@112 | 141 | def _get_selected(self, value, input_value): |
paul@112 | 142 | |
paul@112 | 143 | """ |
paul@112 | 144 | Return the HTML attribute text indicating selection of an option (or |
paul@112 | 145 | otherwise) if 'value' matches 'input_value'. |
paul@112 | 146 | """ |
paul@112 | 147 | |
paul@112 | 148 | return input_value is not None and value == input_value and 'selected="selected"' or '' |
paul@112 | 149 | |
paul@112 | 150 | def _get_selected_for_list(self, value, input_values): |
paul@112 | 151 | |
paul@112 | 152 | """ |
paul@112 | 153 | Return the HTML attribute text indicating selection of an option (or |
paul@112 | 154 | otherwise) if 'value' matches one of the 'input_values'. |
paul@112 | 155 | """ |
paul@112 | 156 | |
paul@112 | 157 | return value in input_values and 'selected="selected"' or '' |
paul@112 | 158 | |
paul@112 | 159 | def _get_input(self, form, name, default=None): |
paul@112 | 160 | |
paul@112 | 161 | """ |
paul@112 | 162 | Return the input from 'form' having the given 'name', returning either |
paul@112 | 163 | the input converted to an integer or the given 'default' (optional, None |
paul@112 | 164 | if not specified). |
paul@112 | 165 | """ |
paul@112 | 166 | |
paul@112 | 167 | value = form.get(name, [None])[0] |
paul@112 | 168 | if not value: # true if 0 obtained |
paul@112 | 169 | return default |
paul@112 | 170 | else: |
paul@112 | 171 | return int(value) |
paul@112 | 172 | |
paul@114 | 173 | def get_month_lists(self, default_as_current=0): |
paul@112 | 174 | |
paul@112 | 175 | """ |
paul@112 | 176 | Return two lists of HTML element definitions corresponding to the start |
paul@112 | 177 | and end month selection controls, with months selected according to any |
paul@112 | 178 | values that have been specified via request parameters. |
paul@112 | 179 | """ |
paul@112 | 180 | |
paul@112 | 181 | _ = self._ |
paul@112 | 182 | form = self.get_form() |
paul@112 | 183 | |
paul@112 | 184 | # Initialise month lists. |
paul@112 | 185 | |
paul@112 | 186 | start_month_list = [] |
paul@112 | 187 | end_month_list = [] |
paul@112 | 188 | |
paul@114 | 189 | start_month = self._get_input(form, "start-month", default_as_current and getCurrentMonth().month() or None) |
paul@112 | 190 | end_month = self._get_input(form, "end-month", start_month) |
paul@112 | 191 | |
paul@112 | 192 | # Prepare month lists, selecting specified months. |
paul@112 | 193 | |
paul@114 | 194 | if not default_as_current: |
paul@114 | 195 | start_month_list.append('<option value=""></option>') |
paul@114 | 196 | end_month_list.append('<option value=""></option>') |
paul@114 | 197 | |
paul@112 | 198 | for month in range(1, 13): |
paul@136 | 199 | month_label = escape(_(getMonthLabel(month))) |
paul@112 | 200 | selected = self._get_selected(month, start_month) |
paul@112 | 201 | start_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 202 | selected = self._get_selected(month, end_month) |
paul@112 | 203 | end_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 204 | |
paul@112 | 205 | return start_month_list, end_month_list |
paul@112 | 206 | |
paul@114 | 207 | def get_year_defaults(self, default_as_current=0): |
paul@112 | 208 | |
paul@112 | 209 | "Return defaults for the start and end years." |
paul@112 | 210 | |
paul@112 | 211 | form = self.get_form() |
paul@112 | 212 | |
paul@114 | 213 | start_year_default = form.get("start-year", [default_as_current and getCurrentYear() or ""])[0] |
paul@114 | 214 | end_year_default = form.get("end-year", [default_as_current and start_year_default or ""])[0] |
paul@112 | 215 | |
paul@112 | 216 | return start_year_default, end_year_default |
paul@112 | 217 | |
paul@127 | 218 | def get_day_defaults(self, default_as_current=0): |
paul@127 | 219 | |
paul@127 | 220 | "Return defaults for the start and end days." |
paul@127 | 221 | |
paul@127 | 222 | form = self.get_form() |
paul@127 | 223 | |
paul@127 | 224 | start_day_default = form.get("start-day", [default_as_current and getCurrentDate().day() or ""])[0] |
paul@127 | 225 | end_day_default = form.get("end-day", [default_as_current and start_day_default or ""])[0] |
paul@127 | 226 | |
paul@127 | 227 | return start_day_default, end_day_default |
paul@127 | 228 | |
paul@109 | 229 | def get_form(request): |
paul@109 | 230 | |
paul@109 | 231 | "Work around disruptive MoinMoin changes in 1.9." |
paul@109 | 232 | |
paul@109 | 233 | if hasattr(request, "values"): |
paul@109 | 234 | return Form(request.values) |
paul@109 | 235 | else: |
paul@109 | 236 | return request.form |
paul@109 | 237 | |
paul@110 | 238 | class send_headers: |
paul@110 | 239 | |
paul@110 | 240 | """ |
paul@110 | 241 | A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a |
paul@110 | 242 | 1.9.x environment. |
paul@110 | 243 | """ |
paul@110 | 244 | |
paul@110 | 245 | def __init__(self, request): |
paul@110 | 246 | self.request = request |
paul@110 | 247 | |
paul@110 | 248 | def __call__(self, headers): |
paul@110 | 249 | for header in headers: |
paul@110 | 250 | parts = header.split(":") |
paul@110 | 251 | self.request.headers.add(parts[0], ":".join(parts[1:])) |
paul@110 | 252 | |
paul@136 | 253 | def escattr(s): |
paul@136 | 254 | return escape(s, 1) |
paul@136 | 255 | |
paul@67 | 256 | # Textual representations. |
paul@67 | 257 | |
paul@67 | 258 | def getHTTPTimeString(tmtuple): |
paul@67 | 259 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( |
paul@129 | 260 | getDayLabel(tmtuple.tm_wday), |
paul@67 | 261 | tmtuple.tm_mday, |
paul@129 | 262 | getMonthLabel(tmtuple.tm_mon), |
paul@67 | 263 | tmtuple.tm_year, |
paul@67 | 264 | tmtuple.tm_hour, |
paul@67 | 265 | tmtuple.tm_min, |
paul@67 | 266 | tmtuple.tm_sec |
paul@67 | 267 | ) |
paul@67 | 268 | |
paul@67 | 269 | def getSimpleWikiText(text): |
paul@67 | 270 | |
paul@67 | 271 | """ |
paul@67 | 272 | Return the plain text representation of the given 'text' which may employ |
paul@67 | 273 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@67 | 274 | text. |
paul@67 | 275 | """ |
paul@67 | 276 | |
paul@67 | 277 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@67 | 278 | |
paul@67 | 279 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@67 | 280 | |
paul@67 | 281 | def getEncodedWikiText(text): |
paul@67 | 282 | |
paul@67 | 283 | "Encode the given 'text' in a verbatim representation." |
paul@67 | 284 | |
paul@67 | 285 | return "<<Verbatim(%s)>>" % text |
paul@67 | 286 | |
paul@67 | 287 | def getPrettyTitle(title): |
paul@67 | 288 | |
paul@67 | 289 | "Return a nicely formatted version of the given 'title'." |
paul@67 | 290 | |
paul@67 | 291 | return title.replace("_", " ").replace("/", u" ? ") |
paul@67 | 292 | |
paul@67 | 293 | def getMonthLabel(month): |
paul@67 | 294 | |
paul@67 | 295 | "Return an unlocalised label for the given 'month'." |
paul@67 | 296 | |
paul@67 | 297 | return month_labels[month - 1] # zero-based labels |
paul@67 | 298 | |
paul@67 | 299 | def getDayLabel(weekday): |
paul@67 | 300 | |
paul@67 | 301 | "Return an unlocalised label for the given 'weekday'." |
paul@67 | 302 | |
paul@67 | 303 | return weekday_labels[weekday] |
paul@67 | 304 | |
paul@19 | 305 | # Action support functions. |
paul@19 | 306 | |
paul@67 | 307 | def getPageRevision(page): |
paul@67 | 308 | |
paul@67 | 309 | "Return the revision details dictionary for the given 'page'." |
paul@67 | 310 | |
paul@67 | 311 | # From Page.edit_info... |
paul@67 | 312 | |
paul@67 | 313 | if hasattr(page, "editlog_entry"): |
paul@67 | 314 | line = page.editlog_entry() |
paul@67 | 315 | else: |
paul@67 | 316 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@67 | 317 | |
paul@89 | 318 | # Similar to Page.mtime_usecs behaviour... |
paul@89 | 319 | |
paul@89 | 320 | if line: |
paul@89 | 321 | timestamp = line.ed_time_usecs |
paul@89 | 322 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@89 | 323 | comment = line.comment |
paul@89 | 324 | else: |
paul@89 | 325 | mtime = 0 |
paul@89 | 326 | comment = "" |
paul@89 | 327 | |
paul@89 | 328 | return {"timestamp" : time.gmtime(mtime), "comment" : comment} |
paul@67 | 329 | |
paul@67 | 330 | # Category discovery and searching. |
paul@67 | 331 | |
paul@19 | 332 | def getCategories(request): |
paul@19 | 333 | |
paul@19 | 334 | """ |
paul@19 | 335 | From the AdvancedSearch macro, return a list of category page names using |
paul@19 | 336 | the given 'request'. |
paul@19 | 337 | """ |
paul@19 | 338 | |
paul@19 | 339 | # This will return all pages with "Category" in the title. |
paul@19 | 340 | |
paul@19 | 341 | cat_filter = getCategoryPattern(request).search |
paul@19 | 342 | return request.rootpage.getPageList(filter=cat_filter) |
paul@19 | 343 | |
paul@19 | 344 | def getCategoryMapping(category_pagenames, request): |
paul@19 | 345 | |
paul@19 | 346 | """ |
paul@19 | 347 | For the given 'category_pagenames' return a list of tuples of the form |
paul@19 | 348 | (category name, category page name) using the given 'request'. |
paul@19 | 349 | """ |
paul@19 | 350 | |
paul@19 | 351 | cat_pattern = getCategoryPattern(request) |
paul@19 | 352 | mapping = [] |
paul@19 | 353 | for pagename in category_pagenames: |
paul@19 | 354 | name = cat_pattern.match(pagename).group("key") |
paul@19 | 355 | if name != "Category": |
paul@19 | 356 | mapping.append((name, pagename)) |
paul@19 | 357 | mapping.sort() |
paul@19 | 358 | return mapping |
paul@19 | 359 | |
paul@67 | 360 | def getCategoryPages(pagename, request): |
paul@29 | 361 | |
paul@67 | 362 | """ |
paul@67 | 363 | Return the pages associated with the given category 'pagename' using the |
paul@67 | 364 | 'request'. |
paul@67 | 365 | """ |
paul@10 | 366 | |
paul@10 | 367 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@117 | 368 | results = search.searchPages(request, query, "page_name") |
paul@10 | 369 | |
paul@10 | 370 | cat_pattern = getCategoryPattern(request) |
paul@10 | 371 | pages = [] |
paul@10 | 372 | for page in results.hits: |
paul@10 | 373 | if not cat_pattern.match(page.page_name): |
paul@10 | 374 | pages.append(page) |
paul@10 | 375 | return pages |
paul@10 | 376 | |
paul@168 | 377 | def getAllCategoryPages(category_names, request): |
paul@168 | 378 | |
paul@168 | 379 | """ |
paul@168 | 380 | Return all pages belonging to the categories having the given |
paul@168 | 381 | 'category_names', using the given 'request'. |
paul@168 | 382 | """ |
paul@168 | 383 | |
paul@168 | 384 | pages = [] |
paul@168 | 385 | pagenames = set() |
paul@168 | 386 | |
paul@168 | 387 | for category_name in category_names: |
paul@168 | 388 | |
paul@168 | 389 | # Get the pages and page names in the category. |
paul@168 | 390 | |
paul@168 | 391 | pages_in_category = getCategoryPages(category_name, request) |
paul@168 | 392 | |
paul@168 | 393 | # Visit each page in the category. |
paul@168 | 394 | |
paul@168 | 395 | for page_in_category in pages_in_category: |
paul@168 | 396 | pagename = page_in_category.page_name |
paul@168 | 397 | |
paul@168 | 398 | # Only process each page once. |
paul@168 | 399 | |
paul@168 | 400 | if pagename in pagenames: |
paul@168 | 401 | continue |
paul@168 | 402 | else: |
paul@168 | 403 | pagenames.add(pagename) |
paul@168 | 404 | |
paul@168 | 405 | pages.append(page_in_category) |
paul@168 | 406 | |
paul@168 | 407 | return pages |
paul@168 | 408 | |
paul@168 | 409 | def getPagesFromResults(result_pages, request): |
paul@168 | 410 | |
paul@168 | 411 | "Return genuine pages for the given 'result_pages' using the 'request'." |
paul@168 | 412 | |
paul@168 | 413 | return [Page(request, page.page_name) for page in result_pages] |
paul@168 | 414 | |
paul@168 | 415 | # Interfaces. |
paul@168 | 416 | |
paul@168 | 417 | class ActsAsTimespan: |
paul@168 | 418 | pass |
paul@168 | 419 | |
paul@67 | 420 | # The main activity functions. |
paul@67 | 421 | |
paul@67 | 422 | class EventPage: |
paul@67 | 423 | |
paul@67 | 424 | "An event page." |
paul@67 | 425 | |
paul@67 | 426 | def __init__(self, page): |
paul@67 | 427 | self.page = page |
paul@69 | 428 | self.events = None |
paul@67 | 429 | self.body = None |
paul@67 | 430 | self.categories = None |
paul@67 | 431 | |
paul@67 | 432 | def copyPage(self, page): |
paul@67 | 433 | |
paul@67 | 434 | "Copy the body of the given 'page'." |
paul@67 | 435 | |
paul@67 | 436 | self.body = page.getBody() |
paul@67 | 437 | |
paul@67 | 438 | def getPageURL(self, request): |
paul@67 | 439 | |
paul@67 | 440 | "Using 'request', return the URL of this page." |
paul@24 | 441 | |
paul@117 | 442 | return request.getQualifiedURL(self.page.url(request, relative=0)) |
paul@67 | 443 | |
paul@67 | 444 | def getFormat(self): |
paul@67 | 445 | |
paul@67 | 446 | "Get the format used on this page." |
paul@24 | 447 | |
paul@117 | 448 | return self.page.pi["format"] |
paul@67 | 449 | |
paul@67 | 450 | def getRevisions(self): |
paul@67 | 451 | |
paul@67 | 452 | "Return a list of page revisions." |
paul@67 | 453 | |
paul@67 | 454 | return self.page.getRevList() |
paul@67 | 455 | |
paul@67 | 456 | def getPageRevision(self): |
paul@24 | 457 | |
paul@67 | 458 | "Return the revision details dictionary for this page." |
paul@67 | 459 | |
paul@67 | 460 | return getPageRevision(self.page) |
paul@67 | 461 | |
paul@67 | 462 | def getPageName(self): |
paul@67 | 463 | |
paul@67 | 464 | "Return the page name." |
paul@67 | 465 | |
paul@67 | 466 | return self.page.page_name |
paul@24 | 467 | |
paul@67 | 468 | def getPrettyPageName(self): |
paul@67 | 469 | |
paul@67 | 470 | "Return a nicely formatted title/name for this page." |
paul@67 | 471 | |
paul@67 | 472 | return getPrettyPageName(self.page) |
paul@67 | 473 | |
paul@67 | 474 | def getBody(self): |
paul@67 | 475 | |
paul@67 | 476 | "Get the current page body." |
paul@47 | 477 | |
paul@67 | 478 | if self.body is None: |
paul@67 | 479 | self.body = self.page.get_raw_body() |
paul@67 | 480 | return self.body |
paul@67 | 481 | |
paul@69 | 482 | def getEvents(self): |
paul@69 | 483 | |
paul@69 | 484 | "Return a list of events from this page." |
paul@67 | 485 | |
paul@69 | 486 | if self.events is None: |
paul@69 | 487 | details = {} |
paul@69 | 488 | self.events = [Event(self, details)] |
paul@47 | 489 | |
paul@67 | 490 | if self.getFormat() == "wiki": |
paul@67 | 491 | for match in definition_list_regexp.finditer(self.getBody()): |
paul@67 | 492 | |
paul@67 | 493 | # Skip commented-out items. |
paul@47 | 494 | |
paul@67 | 495 | if match.group("optcomment"): |
paul@67 | 496 | continue |
paul@67 | 497 | |
paul@67 | 498 | # Permit case-insensitive list terms. |
paul@67 | 499 | |
paul@67 | 500 | term = match.group("term").lower() |
paul@67 | 501 | desc = match.group("desc") |
paul@67 | 502 | |
paul@67 | 503 | # Special value type handling. |
paul@27 | 504 | |
paul@67 | 505 | # Dates. |
paul@67 | 506 | |
paul@67 | 507 | if term in ("start", "end"): |
paul@123 | 508 | desc = getDateTime(desc) |
paul@67 | 509 | |
paul@67 | 510 | # Lists (whose elements may be quoted). |
paul@67 | 511 | |
paul@67 | 512 | elif term in ("topics", "categories"): |
paul@84 | 513 | desc = [getSimpleWikiText(value.strip()) for value in desc.split(",") if value.strip()] |
paul@67 | 514 | |
paul@67 | 515 | # Labels which may well be quoted. |
paul@67 | 516 | |
paul@92 | 517 | elif term in ("title", "summary", "description", "location"): |
paul@67 | 518 | desc = getSimpleWikiText(desc) |
paul@67 | 519 | |
paul@67 | 520 | if desc is not None: |
paul@69 | 521 | |
paul@69 | 522 | # Handle apparent duplicates by creating a new set of |
paul@69 | 523 | # details. |
paul@69 | 524 | |
paul@69 | 525 | if details.has_key(term): |
paul@92 | 526 | |
paul@92 | 527 | # Make a new event. |
paul@92 | 528 | |
paul@69 | 529 | details = {} |
paul@69 | 530 | self.events.append(Event(self, details)) |
paul@67 | 531 | |
paul@69 | 532 | details[term] = desc |
paul@69 | 533 | |
paul@69 | 534 | return self.events |
paul@69 | 535 | |
paul@69 | 536 | def setEvents(self, events): |
paul@69 | 537 | |
paul@69 | 538 | "Set the given 'events' on this page." |
paul@69 | 539 | |
paul@69 | 540 | self.events = events |
paul@67 | 541 | |
paul@67 | 542 | def getCategoryMembership(self): |
paul@27 | 543 | |
paul@67 | 544 | "Get the category names from this page." |
paul@67 | 545 | |
paul@67 | 546 | if self.categories is None: |
paul@67 | 547 | body = self.getBody() |
paul@67 | 548 | match = category_membership_regexp.search(body) |
paul@158 | 549 | self.categories = match and [x for x in match.groups() if x] or [] |
paul@67 | 550 | |
paul@67 | 551 | return self.categories |
paul@67 | 552 | |
paul@67 | 553 | def setCategoryMembership(self, category_names): |
paul@10 | 554 | |
paul@67 | 555 | """ |
paul@67 | 556 | Set the category membership for the page using the specified |
paul@67 | 557 | 'category_names'. |
paul@67 | 558 | """ |
paul@67 | 559 | |
paul@67 | 560 | self.categories = category_names |
paul@67 | 561 | |
paul@67 | 562 | def flushEventDetails(self): |
paul@67 | 563 | |
paul@67 | 564 | "Flush the current event details to this page's body text." |
paul@10 | 565 | |
paul@67 | 566 | new_body_parts = [] |
paul@67 | 567 | end_of_last_match = 0 |
paul@67 | 568 | body = self.getBody() |
paul@69 | 569 | |
paul@69 | 570 | events = iter(self.getEvents()) |
paul@69 | 571 | |
paul@69 | 572 | event = events.next() |
paul@69 | 573 | event_details = event.getDetails() |
paul@69 | 574 | replaced_terms = set() |
paul@67 | 575 | |
paul@67 | 576 | for match in definition_list_regexp.finditer(body): |
paul@47 | 577 | |
paul@10 | 578 | # Permit case-insensitive list terms. |
paul@10 | 579 | |
paul@10 | 580 | term = match.group("term").lower() |
paul@10 | 581 | desc = match.group("desc") |
paul@10 | 582 | |
paul@69 | 583 | # Check that the term has not already been substituted. If so, |
paul@69 | 584 | # get the next event. |
paul@69 | 585 | |
paul@69 | 586 | if term in replaced_terms: |
paul@69 | 587 | try: |
paul@69 | 588 | event = events.next() |
paul@69 | 589 | |
paul@69 | 590 | # No more events. |
paul@69 | 591 | |
paul@69 | 592 | except StopIteration: |
paul@69 | 593 | break |
paul@69 | 594 | |
paul@69 | 595 | event_details = event.getDetails() |
paul@69 | 596 | replaced_terms = set() |
paul@69 | 597 | |
paul@75 | 598 | # Add preceding text to the new body. |
paul@75 | 599 | |
paul@75 | 600 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@75 | 601 | |
paul@75 | 602 | # Get the matching regions, adding the term to the new body. |
paul@75 | 603 | |
paul@75 | 604 | new_body_parts.append(match.group("wholeterm")) |
paul@75 | 605 | |
paul@10 | 606 | # Special value type handling. |
paul@10 | 607 | |
paul@67 | 608 | if event_details.has_key(term): |
paul@19 | 609 | |
paul@67 | 610 | # Dates. |
paul@47 | 611 | |
paul@67 | 612 | if term in ("start", "end"): |
paul@67 | 613 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@47 | 614 | |
paul@67 | 615 | # Lists (whose elements may be quoted). |
paul@47 | 616 | |
paul@67 | 617 | elif term in ("topics", "categories"): |
paul@92 | 618 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@47 | 619 | |
paul@92 | 620 | # Labels which must be quoted. |
paul@47 | 621 | |
paul@67 | 622 | elif term in ("title", "summary"): |
paul@67 | 623 | desc = getEncodedWikiText(event_details[term]) |
paul@47 | 624 | |
paul@67 | 625 | # Text which need not be quoted, but it will be Wiki text. |
paul@55 | 626 | |
paul@92 | 627 | elif term in ("description", "link", "location"): |
paul@67 | 628 | desc = event_details[term] |
paul@55 | 629 | |
paul@69 | 630 | replaced_terms.add(term) |
paul@69 | 631 | |
paul@75 | 632 | # Add the replaced value. |
paul@75 | 633 | |
paul@67 | 634 | new_body_parts.append(desc) |
paul@10 | 635 | |
paul@69 | 636 | # Remember where in the page has been processed. |
paul@69 | 637 | |
paul@69 | 638 | end_of_last_match = match.end() |
paul@69 | 639 | |
paul@69 | 640 | # Write the rest of the page. |
paul@69 | 641 | |
paul@69 | 642 | new_body_parts.append(body[end_of_last_match:]) |
paul@10 | 643 | |
paul@67 | 644 | self.body = "".join(new_body_parts) |
paul@11 | 645 | |
paul@67 | 646 | def flushCategoryMembership(self): |
paul@17 | 647 | |
paul@67 | 648 | "Flush the category membership to the page body." |
paul@11 | 649 | |
paul@67 | 650 | body = self.getBody() |
paul@67 | 651 | category_names = self.getCategoryMembership() |
paul@67 | 652 | match = category_membership_regexp.search(body) |
paul@10 | 653 | |
paul@67 | 654 | if match: |
paul@67 | 655 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@10 | 656 | |
paul@67 | 657 | def saveChanges(self): |
paul@10 | 658 | |
paul@67 | 659 | "Save changes to the event." |
paul@10 | 660 | |
paul@67 | 661 | self.flushEventDetails() |
paul@67 | 662 | self.flushCategoryMembership() |
paul@67 | 663 | self.page.saveText(self.getBody(), 0) |
paul@10 | 664 | |
paul@67 | 665 | def linkToPage(self, request, text, query_string=None): |
paul@11 | 666 | |
paul@67 | 667 | """ |
paul@67 | 668 | Using 'request', return a link to this page with the given link 'text' |
paul@67 | 669 | and optional 'query_string'. |
paul@67 | 670 | """ |
paul@11 | 671 | |
paul@67 | 672 | return linkToPage(request, self.page, text, query_string) |
paul@13 | 673 | |
paul@130 | 674 | class Event(ActsAsTimespan): |
paul@69 | 675 | |
paul@69 | 676 | "A description of an event." |
paul@69 | 677 | |
paul@69 | 678 | def __init__(self, page, details): |
paul@69 | 679 | self.page = page |
paul@69 | 680 | self.details = details |
paul@69 | 681 | |
paul@128 | 682 | def __hash__(self): |
paul@128 | 683 | return hash(self.getSummary()) |
paul@69 | 684 | |
paul@69 | 685 | def getPage(self): |
paul@69 | 686 | |
paul@69 | 687 | "Return the page describing this event." |
paul@69 | 688 | |
paul@69 | 689 | return self.page |
paul@69 | 690 | |
paul@69 | 691 | def setPage(self, page): |
paul@69 | 692 | |
paul@69 | 693 | "Set the 'page' describing this event." |
paul@69 | 694 | |
paul@69 | 695 | self.page = page |
paul@69 | 696 | |
paul@69 | 697 | def getSummary(self, event_parent=None): |
paul@69 | 698 | |
paul@69 | 699 | """ |
paul@69 | 700 | Return either the given title or summary of the event according to the |
paul@69 | 701 | event details, or a summary made from using the pretty version of the |
paul@69 | 702 | page name. |
paul@69 | 703 | |
paul@69 | 704 | If the optional 'event_parent' is specified, any page beneath the given |
paul@69 | 705 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@69 | 706 | if its name is used as the summary. |
paul@69 | 707 | """ |
paul@69 | 708 | |
paul@69 | 709 | event_details = self.details |
paul@69 | 710 | |
paul@69 | 711 | if event_details.has_key("title"): |
paul@69 | 712 | return event_details["title"] |
paul@69 | 713 | elif event_details.has_key("summary"): |
paul@69 | 714 | return event_details["summary"] |
paul@69 | 715 | else: |
paul@69 | 716 | # If appropriate, remove the parent details and "/" character. |
paul@69 | 717 | |
paul@69 | 718 | title = self.page.getPageName() |
paul@69 | 719 | |
paul@74 | 720 | if event_parent and title.startswith(event_parent): |
paul@69 | 721 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@69 | 722 | |
paul@69 | 723 | return getPrettyTitle(title) |
paul@69 | 724 | |
paul@69 | 725 | def getDetails(self): |
paul@69 | 726 | |
paul@69 | 727 | "Return the details for this event." |
paul@69 | 728 | |
paul@69 | 729 | return self.details |
paul@69 | 730 | |
paul@69 | 731 | def setDetails(self, event_details): |
paul@69 | 732 | |
paul@69 | 733 | "Set the 'event_details' for this event." |
paul@69 | 734 | |
paul@69 | 735 | self.details = event_details |
paul@69 | 736 | |
paul@123 | 737 | # Timespan-related methods. |
paul@123 | 738 | |
paul@123 | 739 | def __contains__(self, other): |
paul@123 | 740 | return self == other |
paul@123 | 741 | |
paul@123 | 742 | def __cmp__(self, other): |
paul@123 | 743 | if isinstance(other, Event): |
paul@123 | 744 | return cmp(self.as_timespan(), other.as_timespan()) |
paul@123 | 745 | else: |
paul@123 | 746 | return cmp(self.as_timespan(), other) |
paul@123 | 747 | |
paul@123 | 748 | def as_timespan(self): |
paul@123 | 749 | details = self.details |
paul@123 | 750 | if details.has_key("start") and details.has_key("end"): |
paul@123 | 751 | return Timespan(details["start"], details["end"]) |
paul@123 | 752 | else: |
paul@123 | 753 | return None |
paul@123 | 754 | |
paul@128 | 755 | def as_limits(self): |
paul@128 | 756 | return self.as_timespan().as_limits() |
paul@123 | 757 | |
paul@168 | 758 | def getEventsFromPages(pages): |
paul@168 | 759 | |
paul@168 | 760 | "Return a list of events found on the given 'pages'." |
paul@12 | 761 | |
paul@10 | 762 | events = [] |
paul@168 | 763 | |
paul@168 | 764 | for page in pages: |
paul@168 | 765 | |
paul@168 | 766 | # Get a real page, not a result page. |
paul@168 | 767 | |
paul@168 | 768 | event_page = EventPage(page) |
paul@168 | 769 | |
paul@168 | 770 | # Get all events described in the page. |
paul@168 | 771 | |
paul@168 | 772 | for event in event_page.getEvents(): |
paul@168 | 773 | |
paul@168 | 774 | # Remember the event. |
paul@168 | 775 | |
paul@168 | 776 | events.append(event) |
paul@168 | 777 | |
paul@168 | 778 | return events |
paul@168 | 779 | |
paul@168 | 780 | def getEventsInPeriod(events, calendar_period, resolution): |
paul@168 | 781 | |
paul@168 | 782 | """ |
paul@168 | 783 | Return a collection containing those of the given 'events' which occur |
paul@168 | 784 | within the given 'calendar_period' at the given 'resolution'. |
paul@168 | 785 | """ |
paul@168 | 786 | |
paul@168 | 787 | all_shown_events = TimespanCollection(resolution) |
paul@168 | 788 | |
paul@168 | 789 | for event in events: |
paul@168 | 790 | |
paul@168 | 791 | # Test for the suitability of the event. |
paul@168 | 792 | |
paul@168 | 793 | if event.as_timespan() is not None: |
paul@168 | 794 | |
paul@168 | 795 | # Compare the dates to the requested calendar window, if any. |
paul@168 | 796 | |
paul@168 | 797 | if event in calendar_period: |
paul@168 | 798 | all_shown_events.insert_in_order(event) |
paul@168 | 799 | |
paul@168 | 800 | return all_shown_events |
paul@10 | 801 | |
paul@29 | 802 | def setEventTimestamps(request, events): |
paul@29 | 803 | |
paul@29 | 804 | """ |
paul@29 | 805 | Using 'request', set timestamp details in the details dictionary of each of |
paul@67 | 806 | the 'events'. |
paul@29 | 807 | |
paul@111 | 808 | Return the latest timestamp found. |
paul@29 | 809 | """ |
paul@29 | 810 | |
paul@29 | 811 | latest = None |
paul@29 | 812 | |
paul@69 | 813 | for event in events: |
paul@69 | 814 | event_details = event.getDetails() |
paul@69 | 815 | event_page = event.getPage() |
paul@29 | 816 | |
paul@29 | 817 | # Get the initial revision of the page. |
paul@29 | 818 | |
paul@67 | 819 | revisions = event_page.getRevisions() |
paul@67 | 820 | event_page_initial = Page(request, event_page.getPageName(), rev=revisions[-1]) |
paul@29 | 821 | |
paul@29 | 822 | # Get the created and last modified times. |
paul@29 | 823 | |
paul@30 | 824 | initial_revision = getPageRevision(event_page_initial) |
paul@30 | 825 | event_details["created"] = initial_revision["timestamp"] |
paul@67 | 826 | latest_revision = event_page.getPageRevision() |
paul@30 | 827 | event_details["last-modified"] = latest_revision["timestamp"] |
paul@29 | 828 | event_details["sequence"] = len(revisions) - 1 |
paul@30 | 829 | event_details["last-comment"] = latest_revision["comment"] |
paul@29 | 830 | |
paul@29 | 831 | if latest is None or latest < event_details["last-modified"]: |
paul@29 | 832 | latest = event_details["last-modified"] |
paul@29 | 833 | |
paul@29 | 834 | return latest |
paul@29 | 835 | |
paul@26 | 836 | def getOrderedEvents(events): |
paul@26 | 837 | |
paul@26 | 838 | """ |
paul@26 | 839 | Return a list with the given 'events' ordered according to their start and |
paul@67 | 840 | end dates. |
paul@26 | 841 | """ |
paul@26 | 842 | |
paul@26 | 843 | ordered_events = events[:] |
paul@68 | 844 | ordered_events.sort() |
paul@26 | 845 | return ordered_events |
paul@26 | 846 | |
paul@168 | 847 | def getCalendarPeriod(calendar_start, calendar_end, resolution): |
paul@168 | 848 | |
paul@168 | 849 | """ |
paul@168 | 850 | Return a calendar period for the given 'calendar_start' and 'calendar_end', |
paul@168 | 851 | employing the given 'resolution'. The 'calendar_start' and 'calendar_end' |
paul@168 | 852 | parameters can be given as None. |
paul@168 | 853 | """ |
paul@168 | 854 | |
paul@168 | 855 | # Re-order the window, if appropriate. |
paul@168 | 856 | |
paul@168 | 857 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@168 | 858 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@168 | 859 | |
paul@168 | 860 | # Return a timespan at the given resolution. |
paul@168 | 861 | |
paul@168 | 862 | return Timespan(calendar_start, calendar_end).convert(resolution) |
paul@168 | 863 | |
paul@13 | 864 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest): |
paul@13 | 865 | |
paul@13 | 866 | """ |
paul@13 | 867 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 868 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 869 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 870 | specific period of interest. |
paul@13 | 871 | """ |
paul@13 | 872 | |
paul@13 | 873 | # Define the period as starting with any specified start month or the |
paul@13 | 874 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 875 | # event known. |
paul@13 | 876 | |
paul@13 | 877 | first = calendar_start or earliest |
paul@13 | 878 | last = calendar_end or latest |
paul@13 | 879 | |
paul@13 | 880 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 881 | # events in the requested period, and there was no start or end month |
paul@13 | 882 | # specified, show only the month indicated by the start or end of the |
paul@13 | 883 | # requested period. If all events were to be shown but none were found show |
paul@13 | 884 | # the current month. |
paul@13 | 885 | |
paul@122 | 886 | if isinstance(first, Date): |
paul@122 | 887 | get_current = getCurrentDate |
paul@122 | 888 | else: |
paul@122 | 889 | get_current = getCurrentMonth |
paul@122 | 890 | |
paul@13 | 891 | if first is None: |
paul@122 | 892 | first = last or get_current() |
paul@13 | 893 | if last is None: |
paul@122 | 894 | last = first or get_current() |
paul@13 | 895 | |
paul@13 | 896 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 897 | |
paul@13 | 898 | return min(first, last), last |
paul@13 | 899 | |
paul@168 | 900 | def getCoverage(events, resolution="date"): |
paul@15 | 901 | |
paul@15 | 902 | """ |
paul@168 | 903 | Determine the coverage of the given 'events', returning a collection of |
paul@168 | 904 | timespans, along with a dictionary mapping locations to collections of |
paul@168 | 905 | slots, where each slot contains a tuple of the form (timespans, events). |
paul@15 | 906 | """ |
paul@15 | 907 | |
paul@118 | 908 | all_events = {} |
paul@123 | 909 | full_coverage = TimespanCollection(resolution) |
paul@15 | 910 | |
paul@15 | 911 | # Get event details. |
paul@15 | 912 | |
paul@69 | 913 | for event in events: |
paul@69 | 914 | event_details = event.getDetails() |
paul@15 | 915 | |
paul@168 | 916 | # Find the coverage of this period for the event. |
paul@168 | 917 | |
paul@168 | 918 | # For day views, each location has its own slot, but for month |
paul@168 | 919 | # views, all locations are pooled together since having separate |
paul@168 | 920 | # slots for each location can lead to poor usage of vertical space. |
paul@168 | 921 | |
paul@168 | 922 | if resolution == "datetime": |
paul@168 | 923 | event_location = event_details.get("location") |
paul@168 | 924 | else: |
paul@168 | 925 | event_location = None |
paul@168 | 926 | |
paul@168 | 927 | # Update the overall coverage. |
paul@168 | 928 | |
paul@168 | 929 | full_coverage.insert_in_order(event) |
paul@168 | 930 | |
paul@168 | 931 | # Add a new events list for a new location. |
paul@168 | 932 | # Locations can be unspecified, thus None refers to all unlocalised |
paul@168 | 933 | # events. |
paul@168 | 934 | |
paul@168 | 935 | if not all_events.has_key(event_location): |
paul@168 | 936 | all_events[event_location] = [TimespanCollection(resolution, [event])] |
paul@168 | 937 | |
paul@168 | 938 | # Try and fit the event into an events list. |
paul@168 | 939 | |
paul@168 | 940 | else: |
paul@168 | 941 | slot = all_events[event_location] |
paul@168 | 942 | |
paul@168 | 943 | for slot_events in slot: |
paul@168 | 944 | |
paul@168 | 945 | # Where the event does not overlap with the events in the |
paul@168 | 946 | # current collection, add it alongside these events. |
paul@168 | 947 | |
paul@168 | 948 | if not event in slot_events: |
paul@168 | 949 | slot_events.insert_in_order(event) |
paul@168 | 950 | break |
paul@168 | 951 | |
paul@168 | 952 | # Make a new element in the list if the event cannot be |
paul@168 | 953 | # marked alongside existing events. |
paul@15 | 954 | |
paul@15 | 955 | else: |
paul@168 | 956 | slot.append(TimespanCollection(resolution, [event])) |
paul@15 | 957 | |
paul@15 | 958 | return full_coverage, all_events |
paul@15 | 959 | |
paul@122 | 960 | def getCoverageScale(coverage): |
paul@126 | 961 | |
paul@126 | 962 | """ |
paul@126 | 963 | Return a scale for the given coverage so that the times involved are |
paul@126 | 964 | exposed. The scale consists of a list of non-overlapping timespans forming |
paul@126 | 965 | a contiguous period of time. |
paul@126 | 966 | """ |
paul@126 | 967 | |
paul@122 | 968 | times = set() |
paul@122 | 969 | for timespan in coverage: |
paul@128 | 970 | start, end = timespan.as_limits() |
paul@128 | 971 | |
paul@128 | 972 | # Add either genuine times or dates converted to times. |
paul@128 | 973 | |
paul@128 | 974 | if isinstance(start, DateTime): |
paul@128 | 975 | times.add(start) |
paul@140 | 976 | else: |
paul@140 | 977 | times.add(start.as_datetime(None, None, None, None)) |
paul@128 | 978 | |
paul@128 | 979 | if isinstance(end, DateTime): |
paul@140 | 980 | times.add(end) |
paul@140 | 981 | else: |
paul@140 | 982 | times.add(end.as_date().next_day()) |
paul@128 | 983 | |
paul@122 | 984 | times = list(times) |
paul@135 | 985 | times.sort(cmp_dates_as_day_start) |
paul@122 | 986 | |
paul@122 | 987 | scale = [] |
paul@122 | 988 | first = 1 |
paul@122 | 989 | start = None |
paul@122 | 990 | for time in times: |
paul@122 | 991 | if not first: |
paul@123 | 992 | scale.append(Timespan(start, time)) |
paul@122 | 993 | else: |
paul@122 | 994 | first = 0 |
paul@122 | 995 | start = time |
paul@126 | 996 | |
paul@122 | 997 | return scale |
paul@122 | 998 | |
paul@67 | 999 | # Date-related functions. |
paul@67 | 1000 | |
paul@135 | 1001 | def cmp_dates_as_day_start(a, b): |
paul@135 | 1002 | |
paul@135 | 1003 | """ |
paul@135 | 1004 | Compare dates/datetimes 'a' and 'b' treating dates without time information |
paul@135 | 1005 | as the earliest time in a particular day. |
paul@135 | 1006 | """ |
paul@135 | 1007 | |
paul@135 | 1008 | are_equal = a == b |
paul@135 | 1009 | |
paul@135 | 1010 | if are_equal: |
paul@135 | 1011 | a2 = a.as_datetime_or_date() |
paul@135 | 1012 | b2 = b.as_datetime_or_date() |
paul@135 | 1013 | |
paul@135 | 1014 | if isinstance(a2, Date) and isinstance(b2, DateTime): |
paul@135 | 1015 | return -1 |
paul@135 | 1016 | elif isinstance(a2, DateTime) and isinstance(b2, Date): |
paul@135 | 1017 | return 1 |
paul@135 | 1018 | |
paul@135 | 1019 | return cmp(a, b) |
paul@135 | 1020 | |
paul@67 | 1021 | class Period: |
paul@67 | 1022 | |
paul@67 | 1023 | "A simple period of time." |
paul@67 | 1024 | |
paul@67 | 1025 | def __init__(self, data): |
paul@67 | 1026 | self.data = data |
paul@67 | 1027 | |
paul@67 | 1028 | def months(self): |
paul@67 | 1029 | return self.data[0] * 12 + self.data[1] |
paul@67 | 1030 | |
paul@168 | 1031 | class Convertible: |
paul@168 | 1032 | |
paul@168 | 1033 | "Support for converting temporal objects." |
paul@168 | 1034 | |
paul@168 | 1035 | def _get_converter(self, resolution): |
paul@168 | 1036 | if resolution == "month": |
paul@168 | 1037 | return lambda x: x and x.as_month() |
paul@168 | 1038 | elif resolution == "date": |
paul@168 | 1039 | return lambda x: x and x.as_date() |
paul@168 | 1040 | elif resolution == "datetime": |
paul@168 | 1041 | return lambda x: x and x.as_datetime_or_date() |
paul@168 | 1042 | else: |
paul@168 | 1043 | return lambda x: x |
paul@168 | 1044 | |
paul@168 | 1045 | class Temporal(Convertible): |
paul@67 | 1046 | |
paul@92 | 1047 | "A simple temporal representation, common to dates and times." |
paul@67 | 1048 | |
paul@67 | 1049 | def __init__(self, data): |
paul@92 | 1050 | self.data = list(data) |
paul@67 | 1051 | |
paul@67 | 1052 | def __repr__(self): |
paul@67 | 1053 | return "%s(%r)" % (self.__class__.__name__, self.data) |
paul@67 | 1054 | |
paul@67 | 1055 | def __hash__(self): |
paul@67 | 1056 | return hash(self.as_tuple()) |
paul@67 | 1057 | |
paul@67 | 1058 | def as_tuple(self): |
paul@92 | 1059 | return tuple(self.data) |
paul@92 | 1060 | |
paul@168 | 1061 | def convert(self, resolution): |
paul@168 | 1062 | return self._get_converter(resolution)(self) |
paul@168 | 1063 | |
paul@92 | 1064 | def __cmp__(self, other): |
paul@129 | 1065 | |
paul@129 | 1066 | """ |
paul@129 | 1067 | The result of comparing this instance with 'other' is derived from a |
paul@129 | 1068 | comparison of the instances' date(time) data at the highest common |
paul@129 | 1069 | resolution, meaning that if a date is compared to a datetime, the |
paul@129 | 1070 | datetime will be considered as a date. Thus, a date and a datetime |
paul@129 | 1071 | referring to the same date will be considered equal. |
paul@129 | 1072 | """ |
paul@129 | 1073 | |
paul@122 | 1074 | if not isinstance(other, Temporal): |
paul@122 | 1075 | return NotImplemented |
paul@122 | 1076 | else: |
paul@122 | 1077 | data = self.as_tuple() |
paul@122 | 1078 | other_data = other.as_tuple() |
paul@122 | 1079 | length = min(len(data), len(other_data)) |
paul@122 | 1080 | return cmp(data[:length], other_data[:length]) |
paul@92 | 1081 | |
paul@103 | 1082 | def until(self, start, end, nextfn, prevfn): |
paul@103 | 1083 | |
paul@103 | 1084 | """ |
paul@103 | 1085 | Return a collection of units of time by starting from the given 'start' |
paul@103 | 1086 | and stepping across intervening units until 'end' is reached, using the |
paul@103 | 1087 | given 'nextfn' and 'prevfn' to step from one unit to the next. |
paul@103 | 1088 | """ |
paul@103 | 1089 | |
paul@103 | 1090 | current = start |
paul@103 | 1091 | units = [current] |
paul@103 | 1092 | if current < end: |
paul@103 | 1093 | while current < end: |
paul@103 | 1094 | current = nextfn(current) |
paul@103 | 1095 | units.append(current) |
paul@103 | 1096 | elif current > end: |
paul@103 | 1097 | while current > end: |
paul@103 | 1098 | current = prevfn(current) |
paul@103 | 1099 | units.append(current) |
paul@103 | 1100 | return units |
paul@103 | 1101 | |
paul@140 | 1102 | def ambiguous(self): |
paul@140 | 1103 | |
paul@140 | 1104 | "Only times can be ambiguous." |
paul@140 | 1105 | |
paul@140 | 1106 | return 0 |
paul@140 | 1107 | |
paul@92 | 1108 | class Month(Temporal): |
paul@92 | 1109 | |
paul@92 | 1110 | "A simple year-month representation." |
paul@92 | 1111 | |
paul@92 | 1112 | def __str__(self): |
paul@92 | 1113 | return "%04d-%02d" % self.as_tuple()[:2] |
paul@67 | 1114 | |
paul@93 | 1115 | def as_datetime(self, day, hour, minute, second, zone): |
paul@93 | 1116 | return DateTime(self.as_tuple() + (day, hour, minute, second, zone)) |
paul@93 | 1117 | |
paul@67 | 1118 | def as_date(self, day): |
paul@67 | 1119 | return Date(self.as_tuple() + (day,)) |
paul@67 | 1120 | |
paul@92 | 1121 | def as_month(self): |
paul@92 | 1122 | return self |
paul@92 | 1123 | |
paul@67 | 1124 | def year(self): |
paul@67 | 1125 | return self.data[0] |
paul@67 | 1126 | |
paul@67 | 1127 | def month(self): |
paul@67 | 1128 | return self.data[1] |
paul@67 | 1129 | |
paul@67 | 1130 | def month_properties(self): |
paul@67 | 1131 | |
paul@67 | 1132 | """ |
paul@67 | 1133 | Return the weekday of the 1st of the month, along with the number of |
paul@67 | 1134 | days, as a tuple. |
paul@67 | 1135 | """ |
paul@67 | 1136 | |
paul@92 | 1137 | year, month = self.as_tuple()[:2] |
paul@67 | 1138 | return calendar.monthrange(year, month) |
paul@67 | 1139 | |
paul@67 | 1140 | def month_update(self, n=1): |
paul@67 | 1141 | |
paul@67 | 1142 | "Return the month updated by 'n' months." |
paul@67 | 1143 | |
paul@92 | 1144 | year, month = self.as_tuple()[:2] |
paul@67 | 1145 | return Month((year + (month - 1 + n) / 12, (month - 1 + n) % 12 + 1)) |
paul@67 | 1146 | |
paul@67 | 1147 | def next_month(self): |
paul@67 | 1148 | |
paul@67 | 1149 | "Return the month following this one." |
paul@67 | 1150 | |
paul@67 | 1151 | return self.month_update(1) |
paul@67 | 1152 | |
paul@67 | 1153 | def previous_month(self): |
paul@67 | 1154 | |
paul@67 | 1155 | "Return the month preceding this one." |
paul@67 | 1156 | |
paul@67 | 1157 | return self.month_update(-1) |
paul@67 | 1158 | |
paul@67 | 1159 | def __sub__(self, start): |
paul@67 | 1160 | |
paul@67 | 1161 | """ |
paul@67 | 1162 | Return the difference in years and months between this month and the |
paul@67 | 1163 | 'start' month as a period. |
paul@67 | 1164 | """ |
paul@67 | 1165 | |
paul@67 | 1166 | return Period([(x - y) for x, y in zip(self.data, start.data)]) |
paul@67 | 1167 | |
paul@67 | 1168 | def months_until(self, end): |
paul@92 | 1169 | |
paul@92 | 1170 | "Return the collection of months from this month until 'end'." |
paul@92 | 1171 | |
paul@92 | 1172 | return self.until(self.as_month(), end.as_month(), Month.next_month, Month.previous_month) |
paul@67 | 1173 | |
paul@67 | 1174 | class Date(Month): |
paul@67 | 1175 | |
paul@67 | 1176 | "A simple year-month-day representation." |
paul@67 | 1177 | |
paul@96 | 1178 | def constrain(self): |
paul@96 | 1179 | year, month, day = self.as_tuple()[:3] |
paul@96 | 1180 | |
paul@96 | 1181 | month = max(min(month, 12), 1) |
paul@96 | 1182 | wd, last_day = calendar.monthrange(year, month) |
paul@96 | 1183 | day = max(min(day, last_day), 1) |
paul@96 | 1184 | |
paul@96 | 1185 | self.data[1:3] = month, day |
paul@96 | 1186 | |
paul@67 | 1187 | def __str__(self): |
paul@67 | 1188 | return "%04d-%02d-%02d" % self.as_tuple()[:3] |
paul@67 | 1189 | |
paul@93 | 1190 | def as_datetime(self, hour, minute, second, zone): |
paul@93 | 1191 | return DateTime(self.as_tuple() + (hour, minute, second, zone)) |
paul@93 | 1192 | |
paul@92 | 1193 | def as_date(self): |
paul@92 | 1194 | return self |
paul@92 | 1195 | |
paul@130 | 1196 | def as_datetime_or_date(self): |
paul@130 | 1197 | return self |
paul@130 | 1198 | |
paul@67 | 1199 | def as_month(self): |
paul@67 | 1200 | return Month(self.data[:2]) |
paul@67 | 1201 | |
paul@67 | 1202 | def day(self): |
paul@67 | 1203 | return self.data[2] |
paul@67 | 1204 | |
paul@122 | 1205 | def day_update(self, n=1): |
paul@122 | 1206 | |
paul@145 | 1207 | "Return the month updated by 'n' days." |
paul@122 | 1208 | |
paul@122 | 1209 | delta = datetime.timedelta(n) |
paul@122 | 1210 | dt = datetime.date(*self.as_tuple()[:3]) |
paul@122 | 1211 | dt_new = dt + delta |
paul@122 | 1212 | return Date((dt_new.year, dt_new.month, dt_new.day)) |
paul@122 | 1213 | |
paul@67 | 1214 | def next_day(self): |
paul@67 | 1215 | |
paul@67 | 1216 | "Return the date following this one." |
paul@67 | 1217 | |
paul@92 | 1218 | year, month, day = self.as_tuple()[:3] |
paul@67 | 1219 | _wd, end_day = calendar.monthrange(year, month) |
paul@67 | 1220 | if day == end_day: |
paul@67 | 1221 | if month == 12: |
paul@67 | 1222 | return Date((year + 1, 1, 1)) |
paul@67 | 1223 | else: |
paul@67 | 1224 | return Date((year, month + 1, 1)) |
paul@67 | 1225 | else: |
paul@67 | 1226 | return Date((year, month, day + 1)) |
paul@67 | 1227 | |
paul@67 | 1228 | def previous_day(self): |
paul@67 | 1229 | |
paul@67 | 1230 | "Return the date preceding this one." |
paul@67 | 1231 | |
paul@92 | 1232 | year, month, day = self.as_tuple()[:3] |
paul@67 | 1233 | if day == 1: |
paul@67 | 1234 | if month == 1: |
paul@67 | 1235 | return Date((year - 1, 12, 31)) |
paul@67 | 1236 | else: |
paul@67 | 1237 | _wd, end_day = calendar.monthrange(year, month - 1) |
paul@67 | 1238 | return Date((year, month - 1, end_day)) |
paul@67 | 1239 | else: |
paul@67 | 1240 | return Date((year, month, day - 1)) |
paul@67 | 1241 | |
paul@67 | 1242 | def days_until(self, end): |
paul@92 | 1243 | |
paul@92 | 1244 | "Return the collection of days from this date until 'end'." |
paul@92 | 1245 | |
paul@92 | 1246 | return self.until(self.as_date(), end.as_date(), Date.next_day, Date.previous_day) |
paul@92 | 1247 | |
paul@92 | 1248 | class DateTime(Date): |
paul@92 | 1249 | |
paul@92 | 1250 | "A simple date plus time representation." |
paul@92 | 1251 | |
paul@96 | 1252 | def constrain(self): |
paul@96 | 1253 | Date.constrain(self) |
paul@96 | 1254 | |
paul@96 | 1255 | hour, minute, second = self.as_tuple()[3:6] |
paul@96 | 1256 | |
paul@96 | 1257 | if self.has_time(): |
paul@96 | 1258 | hour = max(min(hour, 23), 0) |
paul@96 | 1259 | minute = max(min(minute, 59), 0) |
paul@96 | 1260 | |
paul@96 | 1261 | if second is not None: |
paul@96 | 1262 | second = max(min(second, 60), 0) # support leap seconds |
paul@96 | 1263 | |
paul@96 | 1264 | self.data[3:6] = hour, minute, second |
paul@92 | 1265 | |
paul@92 | 1266 | def __str__(self): |
paul@128 | 1267 | return Date.__str__(self) + self.time_string() |
paul@128 | 1268 | |
paul@128 | 1269 | def time_string(self): |
paul@92 | 1270 | if self.has_time(): |
paul@92 | 1271 | data = self.as_tuple() |
paul@92 | 1272 | time_str = " %02d:%02d" % data[3:5] |
paul@92 | 1273 | if data[5] is not None: |
paul@92 | 1274 | time_str += ":%02d" % data[5] |
paul@92 | 1275 | if data[6] is not None: |
paul@92 | 1276 | time_str += " %s" % data[6] |
paul@128 | 1277 | return time_str |
paul@92 | 1278 | else: |
paul@128 | 1279 | return "" |
paul@92 | 1280 | |
paul@93 | 1281 | def as_datetime(self): |
paul@93 | 1282 | return self |
paul@93 | 1283 | |
paul@92 | 1284 | def as_date(self): |
paul@92 | 1285 | return Date(self.data[:3]) |
paul@92 | 1286 | |
paul@128 | 1287 | def as_datetime_or_date(self): |
paul@128 | 1288 | |
paul@128 | 1289 | """ |
paul@128 | 1290 | Return a date for this datetime if fields are missing. Otherwise, return |
paul@128 | 1291 | this datetime itself. |
paul@128 | 1292 | """ |
paul@128 | 1293 | |
paul@128 | 1294 | if not self.has_time(): |
paul@128 | 1295 | return self.as_date() |
paul@128 | 1296 | else: |
paul@128 | 1297 | return self |
paul@128 | 1298 | |
paul@126 | 1299 | def __cmp__(self, other): |
paul@129 | 1300 | |
paul@129 | 1301 | """ |
paul@129 | 1302 | The result of comparing this instance with 'other' is, if both instances |
paul@129 | 1303 | are datetime instances, derived from a comparison of the datetimes |
paul@129 | 1304 | converted to UTC. If one or both datetimes cannot be converted to UTC, |
paul@129 | 1305 | the datetimes are compared using the basic temporal comparison which |
paul@129 | 1306 | compares their raw time data. |
paul@129 | 1307 | """ |
paul@129 | 1308 | |
paul@140 | 1309 | this = self |
paul@140 | 1310 | |
paul@140 | 1311 | if this.has_time(): |
paul@128 | 1312 | if isinstance(other, DateTime): |
paul@140 | 1313 | if other.has_time(): |
paul@140 | 1314 | this_utc = this.to_utc() |
paul@140 | 1315 | other_utc = other.to_utc() |
paul@140 | 1316 | if this_utc is not None and other_utc is not None: |
paul@140 | 1317 | return cmp(this_utc.as_tuple(), other_utc.as_tuple()) |
paul@140 | 1318 | else: |
paul@140 | 1319 | other = other.padded() |
paul@140 | 1320 | else: |
paul@140 | 1321 | this = this.padded() |
paul@128 | 1322 | |
paul@128 | 1323 | return Date.__cmp__(this, other) |
paul@126 | 1324 | |
paul@92 | 1325 | def has_time(self): |
paul@140 | 1326 | |
paul@140 | 1327 | """ |
paul@140 | 1328 | Return whether this object has any time information. Objects without |
paul@140 | 1329 | time information can refer to the very start of a day. |
paul@140 | 1330 | """ |
paul@140 | 1331 | |
paul@92 | 1332 | return self.data[3] is not None and self.data[4] is not None |
paul@92 | 1333 | |
paul@128 | 1334 | def time(self): |
paul@128 | 1335 | return self.data[3:] |
paul@128 | 1336 | |
paul@92 | 1337 | def seconds(self): |
paul@92 | 1338 | return self.data[5] |
paul@92 | 1339 | |
paul@92 | 1340 | def time_zone(self): |
paul@92 | 1341 | return self.data[6] |
paul@92 | 1342 | |
paul@93 | 1343 | def set_time_zone(self, value): |
paul@92 | 1344 | self.data[6] = value |
paul@92 | 1345 | |
paul@140 | 1346 | def padded(self, empty_value=0): |
paul@140 | 1347 | |
paul@140 | 1348 | """ |
paul@140 | 1349 | Return a datetime with missing fields defined as being the given |
paul@140 | 1350 | 'empty_value' or 0 if not specified. |
paul@140 | 1351 | """ |
paul@140 | 1352 | |
paul@140 | 1353 | data = [] |
paul@140 | 1354 | for x in self.data[:6]: |
paul@140 | 1355 | if x is None: |
paul@140 | 1356 | data.append(empty_value) |
paul@140 | 1357 | else: |
paul@140 | 1358 | data.append(x) |
paul@140 | 1359 | |
paul@140 | 1360 | data += self.data[6:] |
paul@92 | 1361 | return DateTime(data) |
paul@92 | 1362 | |
paul@93 | 1363 | def to_utc(self): |
paul@93 | 1364 | |
paul@93 | 1365 | """ |
paul@93 | 1366 | Return this object converted to UTC, or None if such a conversion is not |
paul@93 | 1367 | defined. |
paul@93 | 1368 | """ |
paul@93 | 1369 | |
paul@128 | 1370 | if not self.has_time(): |
paul@128 | 1371 | return None |
paul@128 | 1372 | |
paul@93 | 1373 | offset = self.utc_offset() |
paul@93 | 1374 | if offset: |
paul@93 | 1375 | hours, minutes = offset |
paul@93 | 1376 | |
paul@93 | 1377 | # Invert the offset to get the correction. |
paul@93 | 1378 | |
paul@93 | 1379 | hours, minutes = -hours, -minutes |
paul@93 | 1380 | |
paul@93 | 1381 | # Get the components. |
paul@93 | 1382 | |
paul@128 | 1383 | hour, minute, second, zone = self.time() |
paul@93 | 1384 | date = self.as_date() |
paul@93 | 1385 | |
paul@93 | 1386 | # Add the minutes and hours. |
paul@93 | 1387 | |
paul@93 | 1388 | minute += minutes |
paul@103 | 1389 | if minute < 0 or minute > 59: |
paul@103 | 1390 | hour += minute / 60 |
paul@103 | 1391 | minute = minute % 60 |
paul@103 | 1392 | |
paul@103 | 1393 | # NOTE: This makes various assumptions and probably would not work |
paul@103 | 1394 | # NOTE: for general arithmetic. |
paul@93 | 1395 | |
paul@93 | 1396 | hour += hours |
paul@93 | 1397 | if hour < 0: |
paul@93 | 1398 | date = date.previous_day() |
paul@93 | 1399 | hour += 24 |
paul@93 | 1400 | elif hour > 23: |
paul@93 | 1401 | date = date.next_day() |
paul@93 | 1402 | hour -= 24 |
paul@93 | 1403 | |
paul@93 | 1404 | return date.as_datetime(hour, minute, second, "UTC") |
paul@100 | 1405 | |
paul@100 | 1406 | # Cannot convert. |
paul@100 | 1407 | |
paul@93 | 1408 | else: |
paul@93 | 1409 | return None |
paul@93 | 1410 | |
paul@93 | 1411 | def utc_offset(self): |
paul@93 | 1412 | |
paul@93 | 1413 | "Return the UTC offset in hours and minutes." |
paul@93 | 1414 | |
paul@93 | 1415 | zone = self.time_zone() |
paul@95 | 1416 | if not zone: |
paul@95 | 1417 | return None |
paul@93 | 1418 | |
paul@96 | 1419 | # Support explicit UTC zones. |
paul@96 | 1420 | |
paul@96 | 1421 | if zone == "UTC": |
paul@96 | 1422 | return 0, 0 |
paul@96 | 1423 | |
paul@94 | 1424 | # Attempt to return a UTC offset where an explicit offset has been set. |
paul@93 | 1425 | |
paul@94 | 1426 | match = timezone_offset_regexp.match(zone) |
paul@94 | 1427 | if match: |
paul@94 | 1428 | if match.group("sign") == "-": |
paul@93 | 1429 | sign = -1 |
paul@93 | 1430 | else: |
paul@93 | 1431 | sign = 1 |
paul@93 | 1432 | |
paul@94 | 1433 | hours = int(match.group("hours")) * sign |
paul@94 | 1434 | minutes = int(match.group("minutes") or 0) * sign |
paul@94 | 1435 | return hours, minutes |
paul@94 | 1436 | |
paul@102 | 1437 | # Attempt to handle Olson time zone identifiers. |
paul@102 | 1438 | |
paul@102 | 1439 | dt = self.as_olson_datetime() |
paul@102 | 1440 | if dt: |
paul@102 | 1441 | seconds = dt.utcoffset().seconds |
paul@102 | 1442 | hours = seconds / 3600 |
paul@102 | 1443 | minutes = (seconds % 3600) / 60 |
paul@102 | 1444 | return hours, minutes |
paul@102 | 1445 | |
paul@102 | 1446 | # Otherwise return None. |
paul@102 | 1447 | |
paul@93 | 1448 | return None |
paul@93 | 1449 | |
paul@100 | 1450 | def olson_identifier(self): |
paul@100 | 1451 | |
paul@100 | 1452 | "Return the Olson identifier from any zone information." |
paul@100 | 1453 | |
paul@100 | 1454 | zone = self.time_zone() |
paul@100 | 1455 | if not zone: |
paul@100 | 1456 | return None |
paul@100 | 1457 | |
paul@100 | 1458 | # Attempt to match an identifier. |
paul@100 | 1459 | |
paul@100 | 1460 | match = timezone_olson_regexp.match(zone) |
paul@100 | 1461 | if match: |
paul@100 | 1462 | return match.group("olson") |
paul@100 | 1463 | else: |
paul@100 | 1464 | return None |
paul@100 | 1465 | |
paul@102 | 1466 | def _as_olson_datetime(self, hours=None): |
paul@102 | 1467 | |
paul@102 | 1468 | """ |
paul@102 | 1469 | Return a Python datetime object for this datetime interpreted using any |
paul@102 | 1470 | Olson time zone identifier and the given 'hours' offset, raising one of |
paul@102 | 1471 | the pytz exceptions in case of ambiguity. |
paul@102 | 1472 | """ |
paul@102 | 1473 | |
paul@102 | 1474 | olson = self.olson_identifier() |
paul@102 | 1475 | if olson and pytz: |
paul@102 | 1476 | tz = pytz.timezone(olson) |
paul@102 | 1477 | data = self.padded().as_tuple()[:6] |
paul@102 | 1478 | dt = datetime.datetime(*data) |
paul@102 | 1479 | |
paul@102 | 1480 | # With an hours offset, find a time probably in a previously |
paul@102 | 1481 | # applicable time zone. |
paul@102 | 1482 | |
paul@102 | 1483 | if hours is not None: |
paul@102 | 1484 | td = datetime.timedelta(0, hours * 3600) |
paul@102 | 1485 | dt += td |
paul@102 | 1486 | |
paul@102 | 1487 | ldt = tz.localize(dt, None) |
paul@102 | 1488 | |
paul@102 | 1489 | # With an hours offset, adjust the time to define it within the |
paul@102 | 1490 | # previously applicable time zone but at the presumably intended |
paul@102 | 1491 | # position. |
paul@102 | 1492 | |
paul@102 | 1493 | if hours is not None: |
paul@102 | 1494 | ldt -= td |
paul@102 | 1495 | |
paul@102 | 1496 | return ldt |
paul@102 | 1497 | else: |
paul@102 | 1498 | return None |
paul@102 | 1499 | |
paul@102 | 1500 | def as_olson_datetime(self): |
paul@102 | 1501 | |
paul@102 | 1502 | """ |
paul@102 | 1503 | Return a Python datetime object for this datetime interpreted using any |
paul@102 | 1504 | Olson time zone identifier, choosing the time from the zone before the |
paul@102 | 1505 | period of ambiguity. |
paul@102 | 1506 | """ |
paul@102 | 1507 | |
paul@102 | 1508 | try: |
paul@102 | 1509 | return self._as_olson_datetime() |
paul@102 | 1510 | except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError): |
paul@103 | 1511 | |
paul@103 | 1512 | # Try again, using an earlier local time and then stepping forward |
paul@103 | 1513 | # in the chosen zone. |
paul@103 | 1514 | # NOTE: Four hours earlier seems reasonable. |
paul@103 | 1515 | |
paul@103 | 1516 | return self._as_olson_datetime(-4) |
paul@102 | 1517 | |
paul@100 | 1518 | def ambiguous(self): |
paul@100 | 1519 | |
paul@100 | 1520 | "Return whether the time is local and ambiguous." |
paul@100 | 1521 | |
paul@102 | 1522 | try: |
paul@102 | 1523 | self._as_olson_datetime() |
paul@102 | 1524 | except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError): |
paul@102 | 1525 | return 1 |
paul@100 | 1526 | |
paul@100 | 1527 | return 0 |
paul@100 | 1528 | |
paul@168 | 1529 | class Timespan(ActsAsTimespan, Convertible): |
paul@122 | 1530 | |
paul@122 | 1531 | """ |
paul@122 | 1532 | A period of time which can be compared against others to check for overlaps. |
paul@122 | 1533 | """ |
paul@122 | 1534 | |
paul@122 | 1535 | def __init__(self, start, end): |
paul@122 | 1536 | self.start = start |
paul@122 | 1537 | self.end = end |
paul@122 | 1538 | |
paul@122 | 1539 | def __repr__(self): |
paul@122 | 1540 | return "%s(%r, %r)" % (self.__class__.__name__, self.start, self.end) |
paul@122 | 1541 | |
paul@122 | 1542 | def __hash__(self): |
paul@122 | 1543 | return hash((self.start, self.end)) |
paul@122 | 1544 | |
paul@130 | 1545 | def as_timespan(self): |
paul@130 | 1546 | return self |
paul@130 | 1547 | |
paul@128 | 1548 | def as_limits(self): |
paul@123 | 1549 | return self.start, self.end |
paul@123 | 1550 | |
paul@168 | 1551 | def convert(self, resolution): |
paul@168 | 1552 | return Timespan(*map(self._get_converter(resolution), self.as_limits())) |
paul@168 | 1553 | |
paul@122 | 1554 | def is_before(self, a, b): |
paul@129 | 1555 | |
paul@129 | 1556 | """ |
paul@129 | 1557 | Return whether 'a' is before 'b'. Since the end datetime of one period |
paul@129 | 1558 | may be the same as the start datetime of another period, and yet the |
paul@129 | 1559 | first period is intended to be concluded by the end datetime and not |
paul@129 | 1560 | overlap with the other period, a different test is employed for datetime |
paul@129 | 1561 | comparisons. |
paul@129 | 1562 | """ |
paul@129 | 1563 | |
paul@140 | 1564 | # Datetimes without times can be equal to dates and be considered as |
paul@140 | 1565 | # occurring before those dates. |
paul@140 | 1566 | |
paul@140 | 1567 | if isinstance(a, DateTime) and (isinstance(b, DateTime) or not a.has_time()): |
paul@122 | 1568 | return a <= b |
paul@122 | 1569 | else: |
paul@122 | 1570 | return a < b |
paul@122 | 1571 | |
paul@122 | 1572 | def __contains__(self, other): |
paul@129 | 1573 | |
paul@129 | 1574 | """ |
paul@129 | 1575 | This instance is considered to contain 'other' if one is not before or |
paul@129 | 1576 | after the other. If this instance overlaps or coincides with 'other', |
paul@129 | 1577 | then 'other' is regarded as belonging to this instance's time period. |
paul@129 | 1578 | """ |
paul@129 | 1579 | |
paul@123 | 1580 | return self == other |
paul@122 | 1581 | |
paul@122 | 1582 | def __cmp__(self, other): |
paul@122 | 1583 | |
paul@122 | 1584 | """ |
paul@122 | 1585 | Return whether this timespan occupies the same period of time as the |
paul@129 | 1586 | 'other'. Timespans are considered less than others if their end points |
paul@129 | 1587 | precede the other's start point, and are considered greater than others |
paul@129 | 1588 | if their start points follow the other's end point. |
paul@122 | 1589 | """ |
paul@122 | 1590 | |
paul@130 | 1591 | if isinstance(other, ActsAsTimespan): |
paul@130 | 1592 | other = other.as_timespan() |
paul@130 | 1593 | |
paul@123 | 1594 | if self.end is not None and other.start is not None and self.is_before(self.end, other.start): |
paul@122 | 1595 | return -1 |
paul@123 | 1596 | elif self.start is not None and other.end is not None and self.is_before(other.end, self.start): |
paul@122 | 1597 | return 1 |
paul@122 | 1598 | else: |
paul@122 | 1599 | return 0 |
paul@122 | 1600 | |
paul@122 | 1601 | else: |
paul@123 | 1602 | if self.end is not None and self.is_before(self.end, other): |
paul@122 | 1603 | return -1 |
paul@140 | 1604 | elif self.start is not None and self.is_before(other, self.start): |
paul@122 | 1605 | return 1 |
paul@122 | 1606 | else: |
paul@122 | 1607 | return 0 |
paul@122 | 1608 | |
paul@123 | 1609 | class TimespanCollection: |
paul@123 | 1610 | |
paul@129 | 1611 | """ |
paul@130 | 1612 | A collection of timespans providing a list-like interface supporting |
paul@130 | 1613 | membership tests at a particular resolution. |
paul@129 | 1614 | """ |
paul@123 | 1615 | |
paul@123 | 1616 | def __init__(self, resolution, values=None): |
paul@168 | 1617 | self.resolution = resolution |
paul@168 | 1618 | self.values = values or [] |
paul@168 | 1619 | |
paul@168 | 1620 | def as_timespan(self): |
paul@168 | 1621 | return Timespan(*self.as_limits()) |
paul@168 | 1622 | |
paul@168 | 1623 | def as_limits(self): |
paul@168 | 1624 | |
paul@168 | 1625 | "Return the earliest and latest points in time for this collection." |
paul@168 | 1626 | |
paul@168 | 1627 | if not self.values: |
paul@168 | 1628 | return None, None |
paul@123 | 1629 | else: |
paul@168 | 1630 | first, last = self.values[0], self.values[-1] |
paul@168 | 1631 | if isinstance(first, ActsAsTimespan): |
paul@168 | 1632 | first = first.as_timespan().start |
paul@168 | 1633 | if isinstance(last, ActsAsTimespan): |
paul@168 | 1634 | last = last.as_timespan().end |
paul@168 | 1635 | return first, last |
paul@123 | 1636 | |
paul@123 | 1637 | def convert(self, value): |
paul@130 | 1638 | if isinstance(value, ActsAsTimespan): |
paul@168 | 1639 | return value.as_timespan().convert(self.resolution) |
paul@123 | 1640 | else: |
paul@168 | 1641 | return value.convert(self.resolution) |
paul@123 | 1642 | |
paul@123 | 1643 | def __iter__(self): |
paul@123 | 1644 | return iter(self.values) |
paul@123 | 1645 | |
paul@123 | 1646 | def __len__(self): |
paul@123 | 1647 | return len(self.values) |
paul@123 | 1648 | |
paul@123 | 1649 | def __getitem__(self, i): |
paul@123 | 1650 | return self.values[i] |
paul@123 | 1651 | |
paul@123 | 1652 | def __setitem__(self, i, value): |
paul@123 | 1653 | self.values[i] = value |
paul@123 | 1654 | |
paul@123 | 1655 | def __contains__(self, value): |
paul@123 | 1656 | test_value = self.convert(value) |
paul@123 | 1657 | return test_value in self.values |
paul@123 | 1658 | |
paul@123 | 1659 | def append(self, value): |
paul@123 | 1660 | self.values.append(value) |
paul@123 | 1661 | |
paul@123 | 1662 | def insert(self, i, value): |
paul@123 | 1663 | self.values.insert(i, value) |
paul@123 | 1664 | |
paul@123 | 1665 | def pop(self): |
paul@123 | 1666 | return self.values.pop() |
paul@123 | 1667 | |
paul@124 | 1668 | def insert_in_order(self, value): |
paul@124 | 1669 | bisect.insort_left(self, value) |
paul@124 | 1670 | |
paul@168 | 1671 | def items_in_range(self, start, end): |
paul@168 | 1672 | slice_start = bisect.bisect_left(self, start) |
paul@168 | 1673 | slice_end = bisect.bisect_right(self, end, slice_start) |
paul@168 | 1674 | return self.values[slice_start:slice_end] |
paul@168 | 1675 | |
paul@93 | 1676 | def getCountry(s): |
paul@92 | 1677 | |
paul@93 | 1678 | "Find a country code in the given string 's'." |
paul@93 | 1679 | |
paul@93 | 1680 | match = country_code_regexp.search(s) |
paul@92 | 1681 | |
paul@93 | 1682 | if match: |
paul@93 | 1683 | return match.group("code") |
paul@93 | 1684 | else: |
paul@93 | 1685 | return None |
paul@67 | 1686 | |
paul@67 | 1687 | def getDate(s): |
paul@67 | 1688 | |
paul@127 | 1689 | "Parse the string 's', extracting and returning a date object." |
paul@127 | 1690 | |
paul@127 | 1691 | dt = getDateTime(s) |
paul@127 | 1692 | if dt is not None: |
paul@127 | 1693 | return dt.as_date() |
paul@127 | 1694 | else: |
paul@127 | 1695 | return None |
paul@123 | 1696 | |
paul@123 | 1697 | def getDateTime(s): |
paul@67 | 1698 | |
paul@140 | 1699 | """ |
paul@140 | 1700 | Parse the string 's', extracting and returning a datetime object where time |
paul@140 | 1701 | information has been given or a date object where time information is |
paul@140 | 1702 | absent. |
paul@140 | 1703 | """ |
paul@67 | 1704 | |
paul@92 | 1705 | m = datetime_regexp.search(s) |
paul@67 | 1706 | if m: |
paul@92 | 1707 | groups = list(m.groups()) |
paul@92 | 1708 | |
paul@94 | 1709 | # Convert date and time data to integer or None. |
paul@92 | 1710 | |
paul@140 | 1711 | return DateTime(map(int_or_none, groups[:6]) + [m.group("zone")]).as_datetime_or_date() |
paul@67 | 1712 | else: |
paul@67 | 1713 | return None |
paul@67 | 1714 | |
paul@88 | 1715 | def getDateStrings(s): |
paul@88 | 1716 | |
paul@88 | 1717 | "Parse the string 's', extracting and returning all date strings." |
paul@88 | 1718 | |
paul@88 | 1719 | start = 0 |
paul@88 | 1720 | m = date_regexp.search(s, start) |
paul@88 | 1721 | l = [] |
paul@88 | 1722 | while m: |
paul@88 | 1723 | l.append("-".join(m.groups())) |
paul@88 | 1724 | m = date_regexp.search(s, m.end()) |
paul@88 | 1725 | return l |
paul@88 | 1726 | |
paul@67 | 1727 | def getMonth(s): |
paul@67 | 1728 | |
paul@88 | 1729 | "Parse the string 's', extracting and returning a month object." |
paul@67 | 1730 | |
paul@67 | 1731 | m = month_regexp.search(s) |
paul@67 | 1732 | if m: |
paul@67 | 1733 | return Month(map(int, m.groups())) |
paul@67 | 1734 | else: |
paul@67 | 1735 | return None |
paul@67 | 1736 | |
paul@122 | 1737 | def getCurrentDate(): |
paul@122 | 1738 | |
paul@122 | 1739 | "Return the current date as a (year, month, day) tuple." |
paul@122 | 1740 | |
paul@122 | 1741 | today = datetime.date.today() |
paul@122 | 1742 | return Date((today.year, today.month, today.day)) |
paul@122 | 1743 | |
paul@67 | 1744 | def getCurrentMonth(): |
paul@67 | 1745 | |
paul@67 | 1746 | "Return the current month as a (year, month) tuple." |
paul@67 | 1747 | |
paul@67 | 1748 | today = datetime.date.today() |
paul@67 | 1749 | return Month((today.year, today.month)) |
paul@67 | 1750 | |
paul@67 | 1751 | def getCurrentYear(): |
paul@67 | 1752 | |
paul@67 | 1753 | "Return the current year." |
paul@67 | 1754 | |
paul@67 | 1755 | today = datetime.date.today() |
paul@67 | 1756 | return today.year |
paul@67 | 1757 | |
paul@19 | 1758 | # User interface functions. |
paul@19 | 1759 | |
paul@55 | 1760 | def getParameter(request, name, default=None): |
paul@92 | 1761 | |
paul@92 | 1762 | """ |
paul@92 | 1763 | Using the given 'request', return the value of the parameter with the given |
paul@92 | 1764 | 'name', returning the optional 'default' (or None) if no value was supplied |
paul@92 | 1765 | in the 'request'. |
paul@92 | 1766 | """ |
paul@92 | 1767 | |
paul@110 | 1768 | return get_form(request).get(name, [default])[0] |
paul@23 | 1769 | |
paul@58 | 1770 | def getQualifiedParameter(request, calendar_name, argname, default=None): |
paul@92 | 1771 | |
paul@92 | 1772 | """ |
paul@92 | 1773 | Using the given 'request', 'calendar_name' and 'argname', retrieve the |
paul@92 | 1774 | value of the qualified parameter, returning the optional 'default' (or None) |
paul@92 | 1775 | if no value was supplied in the 'request'. |
paul@92 | 1776 | """ |
paul@92 | 1777 | |
paul@58 | 1778 | argname = getQualifiedParameterName(calendar_name, argname) |
paul@58 | 1779 | return getParameter(request, argname, default) |
paul@58 | 1780 | |
paul@58 | 1781 | def getQualifiedParameterName(calendar_name, argname): |
paul@92 | 1782 | |
paul@92 | 1783 | """ |
paul@92 | 1784 | Return the qualified parameter name using the given 'calendar_name' and |
paul@92 | 1785 | 'argname'. |
paul@92 | 1786 | """ |
paul@92 | 1787 | |
paul@58 | 1788 | if calendar_name is None: |
paul@58 | 1789 | return argname |
paul@58 | 1790 | else: |
paul@58 | 1791 | return "%s-%s" % (calendar_name, argname) |
paul@58 | 1792 | |
paul@122 | 1793 | def getParameterDate(arg): |
paul@122 | 1794 | |
paul@122 | 1795 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@122 | 1796 | |
paul@122 | 1797 | n = None |
paul@122 | 1798 | |
paul@128 | 1799 | if arg is None: |
paul@128 | 1800 | return None |
paul@128 | 1801 | |
paul@128 | 1802 | elif arg.startswith("current"): |
paul@122 | 1803 | date = getCurrentDate() |
paul@122 | 1804 | if len(arg) > 8: |
paul@122 | 1805 | n = int(arg[7:]) |
paul@122 | 1806 | |
paul@122 | 1807 | elif arg.startswith("yearstart"): |
paul@122 | 1808 | date = Date((getCurrentYear(), 1, 1)) |
paul@122 | 1809 | if len(arg) > 10: |
paul@122 | 1810 | n = int(arg[9:]) |
paul@122 | 1811 | |
paul@122 | 1812 | elif arg.startswith("yearend"): |
paul@122 | 1813 | date = Date((getCurrentYear(), 12, 31)) |
paul@122 | 1814 | if len(arg) > 8: |
paul@122 | 1815 | n = int(arg[7:]) |
paul@122 | 1816 | |
paul@122 | 1817 | else: |
paul@122 | 1818 | date = getDate(arg) |
paul@122 | 1819 | |
paul@122 | 1820 | if n is not None: |
paul@122 | 1821 | date = date.day_update(n) |
paul@122 | 1822 | |
paul@122 | 1823 | return date |
paul@122 | 1824 | |
paul@19 | 1825 | def getParameterMonth(arg): |
paul@67 | 1826 | |
paul@67 | 1827 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@67 | 1828 | |
paul@19 | 1829 | n = None |
paul@19 | 1830 | |
paul@128 | 1831 | if arg is None: |
paul@128 | 1832 | return None |
paul@128 | 1833 | |
paul@128 | 1834 | elif arg.startswith("current"): |
paul@19 | 1835 | date = getCurrentMonth() |
paul@19 | 1836 | if len(arg) > 8: |
paul@19 | 1837 | n = int(arg[7:]) |
paul@19 | 1838 | |
paul@19 | 1839 | elif arg.startswith("yearstart"): |
paul@67 | 1840 | date = Month((getCurrentYear(), 1)) |
paul@19 | 1841 | if len(arg) > 10: |
paul@19 | 1842 | n = int(arg[9:]) |
paul@19 | 1843 | |
paul@19 | 1844 | elif arg.startswith("yearend"): |
paul@67 | 1845 | date = Month((getCurrentYear(), 12)) |
paul@19 | 1846 | if len(arg) > 8: |
paul@19 | 1847 | n = int(arg[7:]) |
paul@19 | 1848 | |
paul@19 | 1849 | else: |
paul@19 | 1850 | date = getMonth(arg) |
paul@19 | 1851 | |
paul@19 | 1852 | if n is not None: |
paul@67 | 1853 | date = date.month_update(n) |
paul@19 | 1854 | |
paul@19 | 1855 | return date |
paul@19 | 1856 | |
paul@122 | 1857 | def getFormDate(request, calendar_name, argname): |
paul@122 | 1858 | |
paul@122 | 1859 | """ |
paul@122 | 1860 | Return the date from the 'request' for the calendar with the given |
paul@122 | 1861 | 'calendar_name' using the parameter having the given 'argname'. |
paul@122 | 1862 | """ |
paul@122 | 1863 | |
paul@122 | 1864 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@128 | 1865 | return getParameterDate(arg) |
paul@122 | 1866 | |
paul@19 | 1867 | def getFormMonth(request, calendar_name, argname): |
paul@67 | 1868 | |
paul@67 | 1869 | """ |
paul@67 | 1870 | Return the month from the 'request' for the calendar with the given |
paul@67 | 1871 | 'calendar_name' using the parameter having the given 'argname'. |
paul@67 | 1872 | """ |
paul@67 | 1873 | |
paul@58 | 1874 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@128 | 1875 | return getParameterMonth(arg) |
paul@19 | 1876 | |
paul@127 | 1877 | def getFormDateTriple(request, yeararg, montharg, dayarg): |
paul@127 | 1878 | |
paul@127 | 1879 | """ |
paul@127 | 1880 | Return the date from the 'request' for the calendar with the given |
paul@127 | 1881 | 'calendar_name' using the parameters having the given 'yeararg', 'montharg' |
paul@127 | 1882 | and 'dayarg' names. |
paul@127 | 1883 | """ |
paul@127 | 1884 | |
paul@127 | 1885 | year = getParameter(request, yeararg) |
paul@127 | 1886 | month = getParameter(request, montharg) |
paul@127 | 1887 | day = getParameter(request, dayarg) |
paul@127 | 1888 | if year and month and day: |
paul@127 | 1889 | return Date((int(year), int(month), int(day))) |
paul@19 | 1890 | else: |
paul@19 | 1891 | return None |
paul@19 | 1892 | |
paul@23 | 1893 | def getFormMonthPair(request, yeararg, montharg): |
paul@67 | 1894 | |
paul@67 | 1895 | """ |
paul@67 | 1896 | Return the month from the 'request' for the calendar with the given |
paul@67 | 1897 | 'calendar_name' using the parameters having the given 'yeararg' and |
paul@67 | 1898 | 'montharg' names. |
paul@67 | 1899 | """ |
paul@67 | 1900 | |
paul@23 | 1901 | year = getParameter(request, yeararg) |
paul@23 | 1902 | month = getParameter(request, montharg) |
paul@23 | 1903 | if year and month: |
paul@67 | 1904 | return Month((int(year), int(month))) |
paul@23 | 1905 | else: |
paul@23 | 1906 | return None |
paul@23 | 1907 | |
paul@122 | 1908 | def getFullDateLabel(request, date): |
paul@122 | 1909 | |
paul@122 | 1910 | """ |
paul@122 | 1911 | Return the full month plus year label using the given 'request' and |
paul@122 | 1912 | 'year_month'. |
paul@122 | 1913 | """ |
paul@122 | 1914 | |
paul@128 | 1915 | if not date: |
paul@128 | 1916 | return "" |
paul@128 | 1917 | |
paul@122 | 1918 | _ = request.getText |
paul@122 | 1919 | year, month, day = date.as_tuple()[:3] |
paul@125 | 1920 | start_weekday, number_of_days = date.month_properties() |
paul@125 | 1921 | weekday = (start_weekday + day - 1) % 7 |
paul@125 | 1922 | day_label = _(getDayLabel(weekday)) |
paul@122 | 1923 | month_label = _(getMonthLabel(month)) |
paul@122 | 1924 | return "%s %s %s %s" % (day_label, day, month_label, year) |
paul@122 | 1925 | |
paul@114 | 1926 | def getFullMonthLabel(request, year_month): |
paul@114 | 1927 | |
paul@114 | 1928 | """ |
paul@114 | 1929 | Return the full month plus year label using the given 'request' and |
paul@114 | 1930 | 'year_month'. |
paul@114 | 1931 | """ |
paul@114 | 1932 | |
paul@128 | 1933 | if not year_month: |
paul@128 | 1934 | return "" |
paul@128 | 1935 | |
paul@114 | 1936 | _ = request.getText |
paul@122 | 1937 | year, month = year_month.as_tuple()[:2] |
paul@114 | 1938 | month_label = _(getMonthLabel(month)) |
paul@114 | 1939 | return "%s %s" % (month_label, year) |
paul@114 | 1940 | |
paul@67 | 1941 | # Page-related functions. |
paul@67 | 1942 | |
paul@19 | 1943 | def getPrettyPageName(page): |
paul@19 | 1944 | |
paul@19 | 1945 | "Return a nicely formatted title/name for the given 'page'." |
paul@19 | 1946 | |
paul@117 | 1947 | title = page.split_title(force=1) |
paul@55 | 1948 | return getPrettyTitle(title) |
paul@55 | 1949 | |
paul@27 | 1950 | def linkToPage(request, page, text, query_string=None): |
paul@27 | 1951 | |
paul@27 | 1952 | """ |
paul@27 | 1953 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@27 | 1954 | optional 'query_string'. |
paul@27 | 1955 | """ |
paul@27 | 1956 | |
paul@27 | 1957 | text = wikiutil.escape(text) |
paul@117 | 1958 | return page.link_to_raw(request, text, query_string) |
paul@27 | 1959 | |
paul@88 | 1960 | def getFullPageName(parent, title): |
paul@88 | 1961 | |
paul@88 | 1962 | """ |
paul@88 | 1963 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@88 | 1964 | and 'title' (a simple page name). |
paul@88 | 1965 | """ |
paul@88 | 1966 | |
paul@88 | 1967 | if parent: |
paul@88 | 1968 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@88 | 1969 | else: |
paul@88 | 1970 | return title |
paul@88 | 1971 | |
paul@88 | 1972 | def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): |
paul@88 | 1973 | |
paul@88 | 1974 | """ |
paul@88 | 1975 | Using the given 'template_page', complete the 'new_page' by copying the |
paul@88 | 1976 | template and adding the given 'event_details' (a dictionary of event |
paul@88 | 1977 | fields), setting also the 'category_pagenames' to define category |
paul@88 | 1978 | membership. |
paul@88 | 1979 | """ |
paul@88 | 1980 | |
paul@88 | 1981 | event_page = EventPage(template_page) |
paul@88 | 1982 | new_event_page = EventPage(new_page) |
paul@88 | 1983 | new_event_page.copyPage(event_page) |
paul@88 | 1984 | |
paul@88 | 1985 | if new_event_page.getFormat() == "wiki": |
paul@88 | 1986 | new_event = Event(new_event_page, event_details) |
paul@88 | 1987 | new_event_page.setEvents([new_event]) |
paul@88 | 1988 | new_event_page.setCategoryMembership(category_pagenames) |
paul@88 | 1989 | new_event_page.saveChanges() |
paul@88 | 1990 | |
paul@10 | 1991 | # vim: tabstop=4 expandtab shiftwidth=4 |