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