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