paul@347 | 1 | # -*- coding: iso-8859-1 -*- |
paul@347 | 2 | """ |
paul@347 | 3 | MoinMoin - EventAggregator object types |
paul@347 | 4 | |
paul@347 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@347 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@347 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@347 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@347 | 9 | """ |
paul@347 | 10 | |
paul@353 | 11 | from DateSupport import DateTime |
paul@347 | 12 | from GeneralSupport import to_list |
paul@347 | 13 | from LocationSupport import getMapReference |
paul@347 | 14 | from MoinSupport import * |
paul@347 | 15 | |
paul@353 | 16 | from email.utils import parsedate |
paul@347 | 17 | import re |
paul@347 | 18 | |
paul@347 | 19 | try: |
paul@347 | 20 | set |
paul@347 | 21 | except NameError: |
paul@347 | 22 | from sets import Set as set |
paul@347 | 23 | |
paul@347 | 24 | # Page parsing. |
paul@347 | 25 | |
paul@347 | 26 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@347 | 27 | category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE) |
paul@347 | 28 | |
paul@347 | 29 | # Event parsing from page texts. |
paul@347 | 30 | |
paul@347 | 31 | def parseEvents(text, event_page, fragment=None): |
paul@347 | 32 | |
paul@347 | 33 | """ |
paul@347 | 34 | Parse events in the given 'text', returning a list of event objects for the |
paul@347 | 35 | given 'event_page'. An optional 'fragment' can be specified to indicate a |
paul@347 | 36 | specific region of the event page. |
paul@347 | 37 | |
paul@347 | 38 | If the optional 'fragment' identifier is provided, the first heading may |
paul@347 | 39 | also be used to provide an event summary/title. |
paul@347 | 40 | """ |
paul@347 | 41 | |
paul@347 | 42 | template_details = {} |
paul@347 | 43 | if fragment: |
paul@347 | 44 | template_details["fragment"] = fragment |
paul@347 | 45 | |
paul@347 | 46 | details = {} |
paul@347 | 47 | details.update(template_details) |
paul@347 | 48 | raw_details = {} |
paul@347 | 49 | |
paul@347 | 50 | # Obtain a heading, if requested. |
paul@347 | 51 | |
paul@347 | 52 | if fragment: |
paul@347 | 53 | for level, title, (start, end) in getHeadings(text): |
paul@347 | 54 | raw_details["title"] = text[start:end] |
paul@347 | 55 | details["title"] = getSimpleWikiText(title.strip()) |
paul@347 | 56 | break |
paul@347 | 57 | |
paul@347 | 58 | # Start populating events. |
paul@347 | 59 | |
paul@347 | 60 | events = [Event(event_page, details, raw_details)] |
paul@347 | 61 | |
paul@347 | 62 | for match in definition_list_regexp.finditer(text): |
paul@347 | 63 | |
paul@347 | 64 | # Skip commented-out items. |
paul@347 | 65 | |
paul@347 | 66 | if match.group("optcomment"): |
paul@347 | 67 | continue |
paul@347 | 68 | |
paul@347 | 69 | # Permit case-insensitive list terms. |
paul@347 | 70 | |
paul@347 | 71 | term = match.group("term").lower() |
paul@347 | 72 | raw_desc = match.group("desc") |
paul@347 | 73 | |
paul@347 | 74 | # Special value type handling. |
paul@347 | 75 | |
paul@347 | 76 | # Dates. |
paul@347 | 77 | |
paul@347 | 78 | if term in Event.date_terms: |
paul@347 | 79 | desc = getDateTime(raw_desc) |
paul@347 | 80 | |
paul@347 | 81 | # Lists (whose elements may be quoted). |
paul@347 | 82 | |
paul@347 | 83 | elif term in Event.list_terms: |
paul@347 | 84 | desc = map(getSimpleWikiText, to_list(raw_desc, ",")) |
paul@347 | 85 | |
paul@347 | 86 | # Position details. |
paul@347 | 87 | |
paul@347 | 88 | elif term == "geo": |
paul@347 | 89 | try: |
paul@347 | 90 | desc = map(getMapReference, to_list(raw_desc, None)) |
paul@347 | 91 | if len(desc) != 2: |
paul@347 | 92 | continue |
paul@347 | 93 | except (KeyError, ValueError): |
paul@347 | 94 | continue |
paul@347 | 95 | |
paul@347 | 96 | # Labels which may well be quoted. |
paul@347 | 97 | |
paul@347 | 98 | elif term in Event.title_terms: |
paul@347 | 99 | desc = getSimpleWikiText(raw_desc.strip()) |
paul@347 | 100 | |
paul@347 | 101 | # Plain Wiki text terms. |
paul@347 | 102 | |
paul@347 | 103 | elif term in Event.other_terms: |
paul@347 | 104 | desc = raw_desc.strip() |
paul@347 | 105 | |
paul@347 | 106 | else: |
paul@347 | 107 | desc = raw_desc |
paul@347 | 108 | |
paul@347 | 109 | if desc is not None: |
paul@347 | 110 | |
paul@347 | 111 | # Handle apparent duplicates by creating a new set of |
paul@347 | 112 | # details. |
paul@347 | 113 | |
paul@347 | 114 | if details.has_key(term): |
paul@347 | 115 | |
paul@347 | 116 | # Make a new event. |
paul@347 | 117 | |
paul@347 | 118 | details = {} |
paul@347 | 119 | details.update(template_details) |
paul@347 | 120 | raw_details = {} |
paul@347 | 121 | events.append(Event(event_page, details, raw_details)) |
paul@347 | 122 | |
paul@347 | 123 | details[term] = desc |
paul@347 | 124 | raw_details[term] = raw_desc |
paul@347 | 125 | |
paul@347 | 126 | return events |
paul@347 | 127 | |
paul@347 | 128 | # Event resources providing collections of events. |
paul@347 | 129 | |
paul@347 | 130 | class EventResource: |
paul@347 | 131 | |
paul@347 | 132 | "A resource providing event information." |
paul@347 | 133 | |
paul@347 | 134 | def __init__(self, url): |
paul@347 | 135 | self.url = url |
paul@347 | 136 | |
paul@347 | 137 | def getPageURL(self): |
paul@347 | 138 | |
paul@347 | 139 | "Return the URL of this page." |
paul@347 | 140 | |
paul@347 | 141 | return self.url |
paul@347 | 142 | |
paul@347 | 143 | def getFormat(self): |
paul@347 | 144 | |
paul@347 | 145 | "Get the format used by this resource." |
paul@347 | 146 | |
paul@347 | 147 | return "plain" |
paul@347 | 148 | |
paul@347 | 149 | def getMetadata(self): |
paul@347 | 150 | |
paul@347 | 151 | """ |
paul@347 | 152 | Return a dictionary containing items describing the page's "created" |
paul@347 | 153 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 154 | "last-comment" made about the last edit. |
paul@347 | 155 | """ |
paul@347 | 156 | |
paul@347 | 157 | return {} |
paul@347 | 158 | |
paul@347 | 159 | def getEvents(self): |
paul@347 | 160 | |
paul@347 | 161 | "Return a list of events from this resource." |
paul@347 | 162 | |
paul@347 | 163 | return [] |
paul@347 | 164 | |
paul@347 | 165 | def linkToPage(self, request, text, query_string=None, anchor=None): |
paul@347 | 166 | |
paul@347 | 167 | """ |
paul@347 | 168 | Using 'request', return a link to this page with the given link 'text' |
paul@347 | 169 | and optional 'query_string' and 'anchor'. |
paul@347 | 170 | """ |
paul@347 | 171 | |
paul@347 | 172 | return linkToResource(self.url, request, text, query_string, anchor) |
paul@347 | 173 | |
paul@347 | 174 | # Formatting-related functions. |
paul@347 | 175 | |
paul@347 | 176 | def formatText(self, text, fmt): |
paul@347 | 177 | |
paul@347 | 178 | """ |
paul@347 | 179 | Format the given 'text' using the specified formatter 'fmt'. |
paul@347 | 180 | """ |
paul@347 | 181 | |
paul@347 | 182 | # Assume plain text which is then formatted appropriately. |
paul@347 | 183 | |
paul@347 | 184 | return fmt.text(text) |
paul@347 | 185 | |
paul@347 | 186 | class EventCalendar(EventResource): |
paul@347 | 187 | |
paul@347 | 188 | "An iCalendar resource." |
paul@347 | 189 | |
paul@353 | 190 | def __init__(self, url, calendar, metadata): |
paul@347 | 191 | EventResource.__init__(self, url) |
paul@347 | 192 | self.calendar = calendar |
paul@353 | 193 | self.metadata = metadata |
paul@347 | 194 | self.events = None |
paul@347 | 195 | |
paul@353 | 196 | if not self.metadata.has_key("created") and self.metadata.has_key("date"): |
paul@353 | 197 | self.metadata["created"] = DateTime(parsedate(self.metadata["date"])[:7]) |
paul@353 | 198 | |
paul@353 | 199 | if self.metadata.has_key("last-modified") and not isinstance(self.metadata["last-modified"], DateTime): |
paul@353 | 200 | self.metadata["last-modified"] = DateTime(parsedate(self.metadata["last-modified"])[:7]) |
paul@353 | 201 | |
paul@353 | 202 | def getMetadata(self): |
paul@353 | 203 | |
paul@353 | 204 | """ |
paul@353 | 205 | Return a dictionary containing items describing the page's "created" |
paul@353 | 206 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@353 | 207 | "last-comment" made about the last edit. |
paul@353 | 208 | """ |
paul@353 | 209 | |
paul@353 | 210 | return self.metadata |
paul@353 | 211 | |
paul@347 | 212 | def getEvents(self): |
paul@347 | 213 | |
paul@347 | 214 | "Return a list of events from this resource." |
paul@347 | 215 | |
paul@347 | 216 | if self.events is None: |
paul@347 | 217 | self.events = [] |
paul@347 | 218 | |
paul@347 | 219 | _calendar, _empty, calendar = self.calendar |
paul@347 | 220 | |
paul@347 | 221 | for objtype, attrs, obj in calendar: |
paul@347 | 222 | |
paul@347 | 223 | # Read events. |
paul@347 | 224 | |
paul@347 | 225 | if objtype == "VEVENT": |
paul@347 | 226 | details = {} |
paul@347 | 227 | |
paul@347 | 228 | for property, attrs, value in obj: |
paul@347 | 229 | |
paul@347 | 230 | # Convert dates. |
paul@347 | 231 | |
paul@347 | 232 | if property in ("DTSTART", "DTEND", "CREATED", "DTSTAMP", "LAST-MODIFIED"): |
paul@347 | 233 | if property in ("DTSTART", "DTEND"): |
paul@347 | 234 | property = property[2:] |
paul@347 | 235 | if attrs.get("VALUE") == "DATE": |
paul@347 | 236 | value = getDateFromCalendar(value) |
paul@347 | 237 | if value and property == "END": |
paul@347 | 238 | value = value.previous_day() |
paul@347 | 239 | else: |
paul@347 | 240 | value = getDateTimeFromCalendar(value) |
paul@347 | 241 | |
paul@347 | 242 | # Convert numeric data. |
paul@347 | 243 | |
paul@347 | 244 | elif property == "SEQUENCE": |
paul@347 | 245 | value = int(value) |
paul@347 | 246 | |
paul@347 | 247 | # Convert lists. |
paul@347 | 248 | |
paul@347 | 249 | elif property == "CATEGORIES": |
paul@347 | 250 | value = to_list(value, ",") |
paul@347 | 251 | |
paul@347 | 252 | # Convert positions (using decimal values). |
paul@347 | 253 | |
paul@347 | 254 | elif property == "GEO": |
paul@347 | 255 | try: |
paul@347 | 256 | value = map(getMapReferenceFromDecimal, to_list(value, ";")) |
paul@347 | 257 | if len(value) != 2: |
paul@347 | 258 | continue |
paul@347 | 259 | except (KeyError, ValueError): |
paul@347 | 260 | continue |
paul@347 | 261 | |
paul@347 | 262 | # Accept other textual data as it is. |
paul@347 | 263 | |
paul@347 | 264 | elif property in ("LOCATION", "SUMMARY", "URL"): |
paul@347 | 265 | value = value or None |
paul@347 | 266 | |
paul@347 | 267 | # Ignore other properties. |
paul@347 | 268 | |
paul@347 | 269 | else: |
paul@347 | 270 | continue |
paul@347 | 271 | |
paul@347 | 272 | property = property.lower() |
paul@347 | 273 | details[property] = value |
paul@347 | 274 | |
paul@347 | 275 | self.events.append(CalendarEvent(self, details)) |
paul@347 | 276 | |
paul@347 | 277 | return self.events |
paul@347 | 278 | |
paul@347 | 279 | class EventPage: |
paul@347 | 280 | |
paul@347 | 281 | "An event page acting as an event resource." |
paul@347 | 282 | |
paul@347 | 283 | def __init__(self, page): |
paul@347 | 284 | self.page = page |
paul@347 | 285 | self.events = None |
paul@347 | 286 | self.body = None |
paul@347 | 287 | self.categories = None |
paul@347 | 288 | self.metadata = None |
paul@347 | 289 | |
paul@347 | 290 | def copyPage(self, page): |
paul@347 | 291 | |
paul@347 | 292 | "Copy the body of the given 'page'." |
paul@347 | 293 | |
paul@347 | 294 | self.body = page.getBody() |
paul@347 | 295 | |
paul@347 | 296 | def getPageURL(self): |
paul@347 | 297 | |
paul@347 | 298 | "Return the URL of this page." |
paul@347 | 299 | |
paul@347 | 300 | return getPageURL(self.page) |
paul@347 | 301 | |
paul@347 | 302 | def getFormat(self): |
paul@347 | 303 | |
paul@347 | 304 | "Get the format used on this page." |
paul@347 | 305 | |
paul@347 | 306 | return getFormat(self.page) |
paul@347 | 307 | |
paul@347 | 308 | def getMetadata(self): |
paul@347 | 309 | |
paul@347 | 310 | """ |
paul@347 | 311 | Return a dictionary containing items describing the page's "created" |
paul@347 | 312 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 313 | "last-comment" made about the last edit. |
paul@347 | 314 | """ |
paul@347 | 315 | |
paul@347 | 316 | if self.metadata is None: |
paul@347 | 317 | self.metadata = getMetadata(self.page) |
paul@347 | 318 | return self.metadata |
paul@347 | 319 | |
paul@347 | 320 | def getRevisions(self): |
paul@347 | 321 | |
paul@347 | 322 | "Return a list of page revisions." |
paul@347 | 323 | |
paul@347 | 324 | return self.page.getRevList() |
paul@347 | 325 | |
paul@347 | 326 | def getPageRevision(self): |
paul@347 | 327 | |
paul@347 | 328 | "Return the revision details dictionary for this page." |
paul@347 | 329 | |
paul@347 | 330 | return getPageRevision(self.page) |
paul@347 | 331 | |
paul@347 | 332 | def getPageName(self): |
paul@347 | 333 | |
paul@347 | 334 | "Return the page name." |
paul@347 | 335 | |
paul@347 | 336 | return self.page.page_name |
paul@347 | 337 | |
paul@347 | 338 | def getPrettyPageName(self): |
paul@347 | 339 | |
paul@347 | 340 | "Return a nicely formatted title/name for this page." |
paul@347 | 341 | |
paul@347 | 342 | return getPrettyPageName(self.page) |
paul@347 | 343 | |
paul@347 | 344 | def getBody(self): |
paul@347 | 345 | |
paul@347 | 346 | "Get the current page body." |
paul@347 | 347 | |
paul@347 | 348 | if self.body is None: |
paul@347 | 349 | self.body = self.page.get_raw_body() |
paul@347 | 350 | return self.body |
paul@347 | 351 | |
paul@347 | 352 | def getEvents(self): |
paul@347 | 353 | |
paul@347 | 354 | "Return a list of events from this page." |
paul@347 | 355 | |
paul@347 | 356 | if self.events is None: |
paul@347 | 357 | self.events = [] |
paul@347 | 358 | if self.getFormat() == "wiki": |
paul@347 | 359 | for format, attributes, region in getFragments(self.getBody(), True): |
paul@347 | 360 | self.events += parseEvents(region, self, attributes.get("fragment")) |
paul@347 | 361 | |
paul@347 | 362 | return self.events |
paul@347 | 363 | |
paul@347 | 364 | def setEvents(self, events): |
paul@347 | 365 | |
paul@347 | 366 | "Set the given 'events' on this page." |
paul@347 | 367 | |
paul@347 | 368 | self.events = events |
paul@347 | 369 | |
paul@347 | 370 | def getCategoryMembership(self): |
paul@347 | 371 | |
paul@347 | 372 | "Get the category names from this page." |
paul@347 | 373 | |
paul@347 | 374 | if self.categories is None: |
paul@347 | 375 | body = self.getBody() |
paul@347 | 376 | match = category_membership_regexp.search(body) |
paul@347 | 377 | self.categories = match and [x for x in match.groups() if x] or [] |
paul@347 | 378 | |
paul@347 | 379 | return self.categories |
paul@347 | 380 | |
paul@347 | 381 | def setCategoryMembership(self, category_names): |
paul@347 | 382 | |
paul@347 | 383 | """ |
paul@347 | 384 | Set the category membership for the page using the specified |
paul@347 | 385 | 'category_names'. |
paul@347 | 386 | """ |
paul@347 | 387 | |
paul@347 | 388 | self.categories = category_names |
paul@347 | 389 | |
paul@347 | 390 | def flushEventDetails(self): |
paul@347 | 391 | |
paul@347 | 392 | "Flush the current event details to this page's body text." |
paul@347 | 393 | |
paul@347 | 394 | new_body_parts = [] |
paul@347 | 395 | end_of_last_match = 0 |
paul@347 | 396 | body = self.getBody() |
paul@347 | 397 | |
paul@347 | 398 | events = iter(self.getEvents()) |
paul@347 | 399 | |
paul@347 | 400 | event = events.next() |
paul@347 | 401 | event_details = event.getDetails() |
paul@347 | 402 | replaced_terms = set() |
paul@347 | 403 | |
paul@347 | 404 | for match in definition_list_regexp.finditer(body): |
paul@347 | 405 | |
paul@347 | 406 | # Permit case-insensitive list terms. |
paul@347 | 407 | |
paul@347 | 408 | term = match.group("term").lower() |
paul@347 | 409 | desc = match.group("desc") |
paul@347 | 410 | |
paul@347 | 411 | # Check that the term has not already been substituted. If so, |
paul@347 | 412 | # get the next event. |
paul@347 | 413 | |
paul@347 | 414 | if term in replaced_terms: |
paul@347 | 415 | try: |
paul@347 | 416 | event = events.next() |
paul@347 | 417 | |
paul@347 | 418 | # No more events. |
paul@347 | 419 | |
paul@347 | 420 | except StopIteration: |
paul@347 | 421 | break |
paul@347 | 422 | |
paul@347 | 423 | event_details = event.getDetails() |
paul@347 | 424 | replaced_terms = set() |
paul@347 | 425 | |
paul@347 | 426 | # Add preceding text to the new body. |
paul@347 | 427 | |
paul@347 | 428 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@347 | 429 | |
paul@347 | 430 | # Get the matching regions, adding the term to the new body. |
paul@347 | 431 | |
paul@347 | 432 | new_body_parts.append(match.group("wholeterm")) |
paul@347 | 433 | |
paul@347 | 434 | # Special value type handling. |
paul@347 | 435 | |
paul@347 | 436 | if event_details.has_key(term): |
paul@347 | 437 | |
paul@347 | 438 | # Dates. |
paul@347 | 439 | |
paul@347 | 440 | if term in event.date_terms: |
paul@347 | 441 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@347 | 442 | |
paul@347 | 443 | # Lists (whose elements may be quoted). |
paul@347 | 444 | |
paul@347 | 445 | elif term in event.list_terms: |
paul@347 | 446 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@347 | 447 | |
paul@347 | 448 | # Labels which must be quoted. |
paul@347 | 449 | |
paul@347 | 450 | elif term in event.title_terms: |
paul@347 | 451 | desc = getEncodedWikiText(event_details[term]) |
paul@347 | 452 | |
paul@347 | 453 | # Position details. |
paul@347 | 454 | |
paul@347 | 455 | elif term == "geo": |
paul@347 | 456 | desc = " ".join(map(str, event_details[term])) |
paul@347 | 457 | |
paul@347 | 458 | # Text which need not be quoted, but it will be Wiki text. |
paul@347 | 459 | |
paul@347 | 460 | elif term in event.other_terms: |
paul@347 | 461 | desc = event_details[term] |
paul@347 | 462 | |
paul@347 | 463 | replaced_terms.add(term) |
paul@347 | 464 | |
paul@347 | 465 | # Add the replaced value. |
paul@347 | 466 | |
paul@347 | 467 | new_body_parts.append(desc) |
paul@347 | 468 | |
paul@347 | 469 | # Remember where in the page has been processed. |
paul@347 | 470 | |
paul@347 | 471 | end_of_last_match = match.end() |
paul@347 | 472 | |
paul@347 | 473 | # Write the rest of the page. |
paul@347 | 474 | |
paul@347 | 475 | new_body_parts.append(body[end_of_last_match:]) |
paul@347 | 476 | |
paul@347 | 477 | self.body = "".join(new_body_parts) |
paul@347 | 478 | |
paul@347 | 479 | def flushCategoryMembership(self): |
paul@347 | 480 | |
paul@347 | 481 | "Flush the category membership to the page body." |
paul@347 | 482 | |
paul@347 | 483 | body = self.getBody() |
paul@347 | 484 | category_names = self.getCategoryMembership() |
paul@347 | 485 | match = category_membership_regexp.search(body) |
paul@347 | 486 | |
paul@347 | 487 | if match: |
paul@347 | 488 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@347 | 489 | |
paul@347 | 490 | def saveChanges(self): |
paul@347 | 491 | |
paul@347 | 492 | "Save changes to the event." |
paul@347 | 493 | |
paul@347 | 494 | self.flushEventDetails() |
paul@347 | 495 | self.flushCategoryMembership() |
paul@347 | 496 | self.page.saveText(self.getBody(), 0) |
paul@347 | 497 | |
paul@347 | 498 | def linkToPage(self, request, text, query_string=None, anchor=None): |
paul@347 | 499 | |
paul@347 | 500 | """ |
paul@347 | 501 | Using 'request', return a link to this page with the given link 'text' |
paul@347 | 502 | and optional 'query_string' and 'anchor'. |
paul@347 | 503 | """ |
paul@347 | 504 | |
paul@347 | 505 | return linkToPage(request, self.page, text, query_string, anchor) |
paul@347 | 506 | |
paul@347 | 507 | # Formatting-related functions. |
paul@347 | 508 | |
paul@347 | 509 | def getParserClass(self, format): |
paul@347 | 510 | |
paul@347 | 511 | """ |
paul@347 | 512 | Return a parser class for the given 'format', returning a plain text |
paul@347 | 513 | parser if no parser can be found for the specified 'format'. |
paul@347 | 514 | """ |
paul@347 | 515 | |
paul@347 | 516 | return getParserClass(self.page.request, format) |
paul@347 | 517 | |
paul@347 | 518 | def formatText(self, text, fmt): |
paul@347 | 519 | |
paul@347 | 520 | """ |
paul@347 | 521 | Format the given 'text' using the specified formatter 'fmt'. |
paul@347 | 522 | """ |
paul@347 | 523 | |
paul@347 | 524 | fmt.page = page = self.page |
paul@347 | 525 | request = page.request |
paul@347 | 526 | |
paul@347 | 527 | parser_cls = self.getParserClass(self.getFormat()) |
paul@347 | 528 | return formatText(text, request, fmt, parser_cls) |
paul@347 | 529 | |
paul@347 | 530 | # Event details. |
paul@347 | 531 | |
paul@347 | 532 | class Event(ActsAsTimespan): |
paul@347 | 533 | |
paul@347 | 534 | "A description of an event." |
paul@347 | 535 | |
paul@347 | 536 | title_terms = "title", "summary" |
paul@347 | 537 | date_terms = "start", "end" |
paul@347 | 538 | list_terms = "topics", "categories" |
paul@347 | 539 | other_terms = "description", "location", "link" |
paul@347 | 540 | geo_terms = "geo", |
paul@347 | 541 | all_terms = title_terms + date_terms + list_terms + other_terms + geo_terms |
paul@347 | 542 | |
paul@347 | 543 | def __init__(self, page, details, raw_details=None): |
paul@347 | 544 | self.page = page |
paul@347 | 545 | self.details = details |
paul@347 | 546 | self.raw_details = raw_details |
paul@347 | 547 | |
paul@347 | 548 | # Permit omission of the end of the event by duplicating the start. |
paul@347 | 549 | |
paul@347 | 550 | if self.details.has_key("start") and not self.details.get("end"): |
paul@347 | 551 | end = self.details["start"] |
paul@347 | 552 | |
paul@347 | 553 | # Make any end time refer to the day instead. |
paul@347 | 554 | |
paul@347 | 555 | if isinstance(end, DateTime): |
paul@347 | 556 | end = end.as_date() |
paul@347 | 557 | |
paul@347 | 558 | self.details["end"] = end |
paul@347 | 559 | |
paul@347 | 560 | def __repr__(self): |
paul@347 | 561 | return "<Event %r %r>" % (self.getSummary(), self.as_limits()) |
paul@347 | 562 | |
paul@347 | 563 | def __hash__(self): |
paul@347 | 564 | |
paul@347 | 565 | """ |
paul@347 | 566 | Return a dictionary hash, avoiding mistaken equality of events in some |
paul@347 | 567 | situations (notably membership tests) by including the URL as well as |
paul@347 | 568 | the summary. |
paul@347 | 569 | """ |
paul@347 | 570 | |
paul@347 | 571 | return hash(self.getSummary() + self.getEventURL()) |
paul@347 | 572 | |
paul@347 | 573 | def getPage(self): |
paul@347 | 574 | |
paul@347 | 575 | "Return the page describing this event." |
paul@347 | 576 | |
paul@347 | 577 | return self.page |
paul@347 | 578 | |
paul@347 | 579 | def setPage(self, page): |
paul@347 | 580 | |
paul@347 | 581 | "Set the 'page' describing this event." |
paul@347 | 582 | |
paul@347 | 583 | self.page = page |
paul@347 | 584 | |
paul@347 | 585 | def getEventURL(self): |
paul@347 | 586 | |
paul@347 | 587 | "Return the URL of this event." |
paul@347 | 588 | |
paul@347 | 589 | fragment = self.details.get("fragment") |
paul@347 | 590 | return self.page.getPageURL() + (fragment and "#" + fragment or "") |
paul@347 | 591 | |
paul@347 | 592 | def linkToEvent(self, request, text, query_string=None): |
paul@347 | 593 | |
paul@347 | 594 | """ |
paul@347 | 595 | Using 'request', return a link to this event with the given link 'text' |
paul@347 | 596 | and optional 'query_string'. |
paul@347 | 597 | """ |
paul@347 | 598 | |
paul@347 | 599 | return self.page.linkToPage(request, text, query_string, self.details.get("fragment")) |
paul@347 | 600 | |
paul@347 | 601 | def getMetadata(self): |
paul@347 | 602 | |
paul@347 | 603 | """ |
paul@347 | 604 | Return a dictionary containing items describing the event's "created" |
paul@347 | 605 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 606 | "last-comment" made about the last edit. |
paul@347 | 607 | """ |
paul@347 | 608 | |
paul@347 | 609 | # Delegate this to the page. |
paul@347 | 610 | |
paul@347 | 611 | return self.page.getMetadata() |
paul@347 | 612 | |
paul@347 | 613 | def getSummary(self, event_parent=None): |
paul@347 | 614 | |
paul@347 | 615 | """ |
paul@347 | 616 | Return either the given title or summary of the event according to the |
paul@347 | 617 | event details, or a summary made from using the pretty version of the |
paul@347 | 618 | page name. |
paul@347 | 619 | |
paul@347 | 620 | If the optional 'event_parent' is specified, any page beneath the given |
paul@347 | 621 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@347 | 622 | if its name is used as the summary. |
paul@347 | 623 | """ |
paul@347 | 624 | |
paul@347 | 625 | event_details = self.details |
paul@347 | 626 | |
paul@347 | 627 | if event_details.has_key("title"): |
paul@347 | 628 | return event_details["title"] |
paul@347 | 629 | elif event_details.has_key("summary"): |
paul@347 | 630 | return event_details["summary"] |
paul@347 | 631 | else: |
paul@347 | 632 | # If appropriate, remove the parent details and "/" character. |
paul@347 | 633 | |
paul@347 | 634 | title = self.page.getPageName() |
paul@347 | 635 | |
paul@347 | 636 | if event_parent and title.startswith(event_parent): |
paul@347 | 637 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@347 | 638 | |
paul@347 | 639 | return getPrettyTitle(title) |
paul@347 | 640 | |
paul@347 | 641 | def getDetails(self): |
paul@347 | 642 | |
paul@347 | 643 | "Return the details for this event." |
paul@347 | 644 | |
paul@347 | 645 | return self.details |
paul@347 | 646 | |
paul@347 | 647 | def setDetails(self, event_details): |
paul@347 | 648 | |
paul@347 | 649 | "Set the 'event_details' for this event." |
paul@347 | 650 | |
paul@347 | 651 | self.details = event_details |
paul@347 | 652 | |
paul@347 | 653 | def getRawDetails(self): |
paul@347 | 654 | |
paul@347 | 655 | "Return the details for this event as they were written in a page." |
paul@347 | 656 | |
paul@347 | 657 | return self.raw_details |
paul@347 | 658 | |
paul@347 | 659 | # Timespan-related methods. |
paul@347 | 660 | |
paul@347 | 661 | def __contains__(self, other): |
paul@347 | 662 | return self == other |
paul@347 | 663 | |
paul@347 | 664 | def __eq__(self, other): |
paul@347 | 665 | if isinstance(other, Event): |
paul@347 | 666 | return self.getSummary() == other.getSummary() and self.getEventURL() == other.getEventURL() and self._cmp(other) |
paul@347 | 667 | else: |
paul@347 | 668 | return self._cmp(other) == 0 |
paul@347 | 669 | |
paul@347 | 670 | def __ne__(self, other): |
paul@347 | 671 | return not self.__eq__(other) |
paul@347 | 672 | |
paul@347 | 673 | def __lt__(self, other): |
paul@347 | 674 | return self._cmp(other) == -1 |
paul@347 | 675 | |
paul@347 | 676 | def __le__(self, other): |
paul@347 | 677 | return self._cmp(other) in (-1, 0) |
paul@347 | 678 | |
paul@347 | 679 | def __gt__(self, other): |
paul@347 | 680 | return self._cmp(other) == 1 |
paul@347 | 681 | |
paul@347 | 682 | def __ge__(self, other): |
paul@347 | 683 | return self._cmp(other) in (0, 1) |
paul@347 | 684 | |
paul@347 | 685 | def _cmp(self, other): |
paul@347 | 686 | |
paul@347 | 687 | "Compare this event to an 'other' event purely by their timespans." |
paul@347 | 688 | |
paul@347 | 689 | if isinstance(other, Event): |
paul@347 | 690 | return cmp(self.as_timespan(), other.as_timespan()) |
paul@347 | 691 | else: |
paul@347 | 692 | return cmp(self.as_timespan(), other) |
paul@347 | 693 | |
paul@347 | 694 | def as_timespan(self): |
paul@347 | 695 | details = self.details |
paul@347 | 696 | if details.has_key("start") and details.has_key("end"): |
paul@347 | 697 | return Timespan(details["start"], details["end"]) |
paul@347 | 698 | else: |
paul@347 | 699 | return None |
paul@347 | 700 | |
paul@347 | 701 | def as_limits(self): |
paul@347 | 702 | ts = self.as_timespan() |
paul@347 | 703 | return ts and ts.as_limits() |
paul@347 | 704 | |
paul@347 | 705 | class CalendarEvent(Event): |
paul@347 | 706 | |
paul@347 | 707 | "An event from a remote calendar." |
paul@347 | 708 | |
paul@347 | 709 | def getEventURL(self): |
paul@347 | 710 | |
paul@354 | 711 | """ |
paul@354 | 712 | Return the URL of this event, fixing any misinterpreted or incorrectly |
paul@354 | 713 | formatted value in the event definition or returning the resource URL in |
paul@354 | 714 | the absence of any URL in the event details. |
paul@354 | 715 | """ |
paul@354 | 716 | |
paul@354 | 717 | return self.details.get("url") and \ |
paul@354 | 718 | self.valueToString(self.details["url"]) or \ |
paul@354 | 719 | self.page.getPageURL() |
paul@354 | 720 | |
paul@354 | 721 | def getSummary(self, event_parent=None): |
paul@347 | 722 | |
paul@354 | 723 | """ |
paul@354 | 724 | Return the event summary, fixing any misinterpreted or incorrectly |
paul@354 | 725 | formatted value in the event definition. |
paul@354 | 726 | """ |
paul@354 | 727 | |
paul@354 | 728 | return self.valueToString(self.details["summary"]) |
paul@354 | 729 | |
paul@354 | 730 | def valueToString(self, value): |
paul@354 | 731 | |
paul@354 | 732 | "Return the given 'value' converted to a string." |
paul@354 | 733 | |
paul@354 | 734 | if isinstance(value, list): |
paul@354 | 735 | return ",".join(value) |
paul@354 | 736 | elif isinstance(value, tuple): |
paul@354 | 737 | return ";".join(value) |
paul@354 | 738 | else: |
paul@354 | 739 | return value |
paul@347 | 740 | |
paul@347 | 741 | def linkToEvent(self, request, text, query_string=None, anchor=None): |
paul@347 | 742 | |
paul@347 | 743 | """ |
paul@347 | 744 | Using 'request', return a link to this event with the given link 'text' |
paul@347 | 745 | and optional 'query_string' and 'anchor'. |
paul@347 | 746 | """ |
paul@347 | 747 | |
paul@347 | 748 | return linkToResource(self.getEventURL(), request, text, query_string, anchor) |
paul@347 | 749 | |
paul@347 | 750 | def getMetadata(self): |
paul@347 | 751 | |
paul@347 | 752 | """ |
paul@347 | 753 | Return a dictionary containing items describing the event's "created" |
paul@347 | 754 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 755 | "last-comment" made about the last edit. |
paul@347 | 756 | """ |
paul@347 | 757 | |
paul@353 | 758 | metadata = self.page.getMetadata() |
paul@353 | 759 | |
paul@347 | 760 | return { |
paul@353 | 761 | "created" : self.details.get("created") or self.details.get("dtstamp") or metadata["created"], |
paul@353 | 762 | "last-modified" : self.details.get("last-modified") or self.details.get("dtstamp") or metadata["last-modified"], |
paul@347 | 763 | "sequence" : self.details.get("sequence") or 0, |
paul@347 | 764 | "last-comment" : "" |
paul@347 | 765 | } |
paul@347 | 766 | |
paul@347 | 767 | # vim: tabstop=4 expandtab shiftwidth=4 |