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