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