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@412 | 18 | from email.parser import Parser |
paul@353 | 19 | from email.utils import parsedate |
paul@347 | 20 | import re |
paul@347 | 21 | |
paul@347 | 22 | try: |
paul@395 | 23 | from cStringIO import StringIO |
paul@395 | 24 | except ImportError: |
paul@395 | 25 | from StringIO import StringIO |
paul@395 | 26 | |
paul@395 | 27 | try: |
paul@347 | 28 | set |
paul@347 | 29 | except NameError: |
paul@347 | 30 | from sets import Set as set |
paul@347 | 31 | |
paul@408 | 32 | # Import libxml2dom for xCalendar parsing. |
paul@408 | 33 | |
paul@408 | 34 | try: |
paul@408 | 35 | import libxml2dom |
paul@408 | 36 | except ImportError: |
paul@408 | 37 | libxml2dom = None |
paul@408 | 38 | |
paul@428 | 39 | # Import MoinMessage for decryption. |
paul@428 | 40 | |
paul@428 | 41 | try: |
paul@428 | 42 | import MoinMessageSupport |
paul@428 | 43 | import MoinMessage |
paul@428 | 44 | except ImportError: |
paul@428 | 45 | MoinMessageSupport = None |
paul@428 | 46 | MoinMessage = None |
paul@428 | 47 | |
paul@347 | 48 | # Page parsing. |
paul@347 | 49 | |
paul@347 | 50 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@347 | 51 | category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE) |
paul@347 | 52 | |
paul@347 | 53 | # Event parsing from page texts. |
paul@347 | 54 | |
paul@395 | 55 | def parseEventsInPage(text, page, fragment=None): |
paul@395 | 56 | |
paul@395 | 57 | """ |
paul@395 | 58 | Parse events in the given 'text' from the given 'page'. |
paul@395 | 59 | """ |
paul@395 | 60 | |
paul@395 | 61 | # Calendar-format pages are parsed directly by the iCalendar parser. |
paul@395 | 62 | |
paul@395 | 63 | if page.getFormat() == "calendar": |
paul@428 | 64 | return parseEventsInCalendar(text, page.getPageURL()) |
paul@395 | 65 | |
paul@408 | 66 | # xCalendar-format pages are parsed directly by the iCalendar parser. |
paul@408 | 67 | |
paul@408 | 68 | elif page.getFormat() == "xcalendar": |
paul@428 | 69 | return parseEventsInXMLCalendar(text, page.getPageURL()) |
paul@408 | 70 | |
paul@395 | 71 | # Wiki-format pages are parsed region-by-region using the special markup. |
paul@395 | 72 | |
paul@395 | 73 | elif page.getFormat() == "wiki": |
paul@395 | 74 | |
paul@395 | 75 | # Where a page contains events, potentially in regions, identify the page |
paul@395 | 76 | # regions and obtain the events within them. |
paul@395 | 77 | |
paul@428 | 78 | events = EventResourceCollection() |
paul@395 | 79 | for format, attributes, region in getFragments(text, True): |
paul@395 | 80 | if format == "calendar": |
paul@428 | 81 | events.append(parseEventsInCalendar(region, page.getPageURL())) |
paul@395 | 82 | else: |
paul@428 | 83 | events.append(parseEvents(region, page, attributes.get("fragment") or fragment)) |
paul@395 | 84 | return events |
paul@395 | 85 | |
paul@395 | 86 | # Unsupported format pages return no events. |
paul@395 | 87 | |
paul@395 | 88 | else: |
paul@428 | 89 | return EventResourceCollection() |
paul@395 | 90 | |
paul@428 | 91 | def parseEventsInCalendar(text, url=None): |
paul@395 | 92 | |
paul@405 | 93 | """ |
paul@405 | 94 | Parse events in iCalendar format from the given 'text'. |
paul@405 | 95 | """ |
paul@405 | 96 | |
paul@405 | 97 | # Fill the StringIO with encoded plain string data. |
paul@405 | 98 | |
paul@405 | 99 | encoding = "utf-8" |
paul@428 | 100 | return parseEventsInCalendarFromResource(StringIO(text.encode(encoding)), encoding, url) |
paul@405 | 101 | |
paul@428 | 102 | def parseEventsInXMLCalendar(text, url): |
paul@408 | 103 | |
paul@408 | 104 | """ |
paul@408 | 105 | Parse events in xCalendar format from the given 'text'. |
paul@408 | 106 | """ |
paul@408 | 107 | |
paul@408 | 108 | # Fill the StringIO with encoded plain string data. |
paul@408 | 109 | |
paul@408 | 110 | encoding = "utf-8" |
paul@428 | 111 | return parseEventsInXMLCalendarFromResource(StringIO(text.encode(encoding)), encoding, url) |
paul@428 | 112 | |
paul@428 | 113 | def parseEventsInStore(store, url=None, request=None): |
paul@428 | 114 | |
paul@428 | 115 | """ |
paul@428 | 116 | Parse events in the given item 'store'. |
paul@428 | 117 | """ |
paul@428 | 118 | |
paul@428 | 119 | calendar = EventResourceCollection() |
paul@428 | 120 | for text in store: |
paul@428 | 121 | calendar.append(parseEventsInMessage(text, url, request)) |
paul@428 | 122 | return calendar |
paul@428 | 123 | |
paul@428 | 124 | def parseEventsInMessage(text, url=None, request=None): |
paul@428 | 125 | |
paul@428 | 126 | """ |
paul@428 | 127 | Parse events in the given 'text' in MIME e-mail message format. |
paul@428 | 128 | """ |
paul@428 | 129 | |
paul@428 | 130 | message = Parser().parsestr(text) |
paul@428 | 131 | return parseEventsInMessagePart(message, url=url, request=request) |
paul@428 | 132 | |
paul@428 | 133 | # Events originating from remote sources are typically made available within |
paul@428 | 134 | # calendar objects, with collection objects used to group multiple calendars. |
paul@408 | 135 | |
paul@405 | 136 | def parseEventsInCalendarFromResource(f, encoding=None, url=None, metadata=None): |
paul@395 | 137 | |
paul@405 | 138 | """ |
paul@405 | 139 | Parse events in iCalendar format from the given file-like object 'f', with |
paul@405 | 140 | content having any specified 'encoding' and being described by the given |
paul@405 | 141 | 'url' and 'metadata'. |
paul@405 | 142 | """ |
paul@405 | 143 | |
paul@405 | 144 | # Read Unicode from the resource. |
paul@405 | 145 | |
paul@405 | 146 | uf = getreader(encoding or "utf-8")(f) |
paul@405 | 147 | try: |
paul@405 | 148 | return EventCalendar(url or "", vCalendar.parse(uf), metadata or {}) |
paul@405 | 149 | finally: |
paul@405 | 150 | uf.close() |
paul@395 | 151 | |
paul@408 | 152 | def parseEventsInXMLCalendarFromResource(f, encoding=None, url=None, metadata=None): |
paul@408 | 153 | |
paul@408 | 154 | """ |
paul@408 | 155 | Parse events in xCalendar format from the given file-like object 'f', with |
paul@408 | 156 | content having any specified 'encoding' and being described by the given |
paul@408 | 157 | 'url' and 'metadata'. |
paul@408 | 158 | """ |
paul@408 | 159 | |
paul@408 | 160 | if libxml2dom is not None: |
paul@408 | 161 | return EventXMLCalendar(url or "", libxml2dom.parse(f), metadata or {}) |
paul@408 | 162 | else: |
paul@408 | 163 | return None |
paul@408 | 164 | |
paul@428 | 165 | def parseEventsInMailboxFromResource(f, encoding=None, url=None, metadata=None): |
paul@412 | 166 | |
paul@412 | 167 | """ |
paul@428 | 168 | Parse a compound message containing events in iCalendar or xCalendar format |
paul@428 | 169 | from the given file-like object 'f', with content having any specified |
paul@428 | 170 | 'encoding' and being described by the given 'url' and 'metadata'. |
paul@412 | 171 | """ |
paul@412 | 172 | |
paul@412 | 173 | message = Parser().parse(f) |
paul@428 | 174 | new_url = "" # hide the IMAP URL |
paul@428 | 175 | return parseEventsInMessagePart(message, new_url, metadata) |
paul@428 | 176 | |
paul@428 | 177 | def parseEventsInMessagePart(part, url=None, metadata=None, request=None): |
paul@412 | 178 | |
paul@428 | 179 | """ |
paul@428 | 180 | Parse events in the given message 'part'. |
paul@428 | 181 | """ |
paul@412 | 182 | |
paul@428 | 183 | if part.is_multipart(): |
paul@428 | 184 | resources = EventResourceCollection() |
paul@428 | 185 | |
paul@428 | 186 | # Attempt to decrypt any encrypted message parts. |
paul@412 | 187 | |
paul@428 | 188 | if MoinMessageSupport is not None: |
paul@428 | 189 | try: |
paul@428 | 190 | if MoinMessage.is_encrypted(part): |
paul@428 | 191 | homedir = MoinMessageSupport.get_homedir(request) |
paul@428 | 192 | gpg = MoinMessage.GPG(homedir) |
paul@428 | 193 | text = gpg.decryptMessage(part) |
paul@428 | 194 | part = Parser().parsestr(text) |
paul@428 | 195 | except MoinMessage.MoinMessageError: |
paul@428 | 196 | return resources |
paul@428 | 197 | |
paul@428 | 198 | for subpart in part.get_payload(): |
paul@428 | 199 | resources.append(parseEventsInMessagePart(subpart, url, metadata, request)) |
paul@428 | 200 | |
paul@428 | 201 | return resources |
paul@428 | 202 | |
paul@428 | 203 | else: |
paul@428 | 204 | content_type = part.get_content_type() |
paul@428 | 205 | text = part.get_payload(decode=True) |
paul@428 | 206 | |
paul@428 | 207 | # Handle standard calendar formats. |
paul@428 | 208 | |
paul@428 | 209 | if content_type == "application/calendar+xml": |
paul@428 | 210 | return parseEventsInXMLCalendarFromResource(StringIO(text), part.get_charset(), url) |
paul@428 | 211 | elif content_type == "text/calendar": |
paul@428 | 212 | return parseEventsInCalendarFromResource(StringIO(text), part.get_charset(), url) |
paul@412 | 213 | else: |
paul@428 | 214 | return EventResourceCollection() |
paul@412 | 215 | |
paul@428 | 216 | # Wiki-specific event parsing. |
paul@412 | 217 | |
paul@347 | 218 | def parseEvents(text, event_page, fragment=None): |
paul@347 | 219 | |
paul@347 | 220 | """ |
paul@347 | 221 | Parse events in the given 'text', returning a list of event objects for the |
paul@347 | 222 | given 'event_page'. An optional 'fragment' can be specified to indicate a |
paul@347 | 223 | specific region of the event page. |
paul@347 | 224 | |
paul@347 | 225 | If the optional 'fragment' identifier is provided, the first heading may |
paul@347 | 226 | also be used to provide an event summary/title. |
paul@347 | 227 | """ |
paul@347 | 228 | |
paul@347 | 229 | template_details = {} |
paul@347 | 230 | if fragment: |
paul@347 | 231 | template_details["fragment"] = fragment |
paul@347 | 232 | |
paul@347 | 233 | details = {} |
paul@347 | 234 | details.update(template_details) |
paul@347 | 235 | raw_details = {} |
paul@347 | 236 | |
paul@347 | 237 | # Obtain a heading, if requested. |
paul@347 | 238 | |
paul@347 | 239 | if fragment: |
paul@347 | 240 | for level, title, (start, end) in getHeadings(text): |
paul@347 | 241 | raw_details["title"] = text[start:end] |
paul@347 | 242 | details["title"] = getSimpleWikiText(title.strip()) |
paul@347 | 243 | break |
paul@347 | 244 | |
paul@347 | 245 | # Start populating events. |
paul@347 | 246 | |
paul@347 | 247 | events = [Event(event_page, details, raw_details)] |
paul@347 | 248 | |
paul@380 | 249 | # Get any default raw details to modify. |
paul@380 | 250 | |
paul@380 | 251 | raw_details = events[-1].getRawDetails() |
paul@380 | 252 | |
paul@347 | 253 | for match in definition_list_regexp.finditer(text): |
paul@347 | 254 | |
paul@347 | 255 | # Skip commented-out items. |
paul@347 | 256 | |
paul@347 | 257 | if match.group("optcomment"): |
paul@347 | 258 | continue |
paul@347 | 259 | |
paul@347 | 260 | # Permit case-insensitive list terms. |
paul@347 | 261 | |
paul@347 | 262 | term = match.group("term").lower() |
paul@347 | 263 | raw_desc = match.group("desc") |
paul@347 | 264 | |
paul@347 | 265 | # Special value type handling. |
paul@347 | 266 | |
paul@347 | 267 | # Dates. |
paul@347 | 268 | |
paul@347 | 269 | if term in Event.date_terms: |
paul@347 | 270 | desc = getDateTime(raw_desc) |
paul@347 | 271 | |
paul@347 | 272 | # Lists (whose elements may be quoted). |
paul@347 | 273 | |
paul@347 | 274 | elif term in Event.list_terms: |
paul@347 | 275 | desc = map(getSimpleWikiText, to_list(raw_desc, ",")) |
paul@347 | 276 | |
paul@347 | 277 | # Position details. |
paul@347 | 278 | |
paul@347 | 279 | elif term == "geo": |
paul@347 | 280 | try: |
paul@347 | 281 | desc = map(getMapReference, to_list(raw_desc, None)) |
paul@347 | 282 | if len(desc) != 2: |
paul@347 | 283 | continue |
paul@347 | 284 | except (KeyError, ValueError): |
paul@347 | 285 | continue |
paul@347 | 286 | |
paul@347 | 287 | # Labels which may well be quoted. |
paul@347 | 288 | |
paul@347 | 289 | elif term in Event.title_terms: |
paul@347 | 290 | desc = getSimpleWikiText(raw_desc.strip()) |
paul@347 | 291 | |
paul@347 | 292 | # Plain Wiki text terms. |
paul@347 | 293 | |
paul@347 | 294 | elif term in Event.other_terms: |
paul@347 | 295 | desc = raw_desc.strip() |
paul@347 | 296 | |
paul@347 | 297 | else: |
paul@347 | 298 | desc = raw_desc |
paul@347 | 299 | |
paul@347 | 300 | if desc is not None: |
paul@347 | 301 | |
paul@347 | 302 | # Handle apparent duplicates by creating a new set of |
paul@347 | 303 | # details. |
paul@347 | 304 | |
paul@347 | 305 | if details.has_key(term): |
paul@347 | 306 | |
paul@347 | 307 | # Make a new event. |
paul@347 | 308 | |
paul@347 | 309 | details = {} |
paul@347 | 310 | details.update(template_details) |
paul@347 | 311 | raw_details = {} |
paul@347 | 312 | events.append(Event(event_page, details, raw_details)) |
paul@380 | 313 | raw_details = events[-1].getRawDetails() |
paul@347 | 314 | |
paul@347 | 315 | details[term] = desc |
paul@347 | 316 | raw_details[term] = raw_desc |
paul@347 | 317 | |
paul@427 | 318 | # Filter out incomplete events. |
paul@427 | 319 | |
paul@427 | 320 | return EventResource(event_page.getPageURL(), events=[e for e in events if e.as_timespan()]) |
paul@347 | 321 | |
paul@347 | 322 | # Event resources providing collections of events. |
paul@347 | 323 | |
paul@347 | 324 | class EventResource: |
paul@347 | 325 | |
paul@347 | 326 | "A resource providing event information." |
paul@347 | 327 | |
paul@428 | 328 | def __init__(self, url, metadata=None, events=None): |
paul@347 | 329 | self.url = url |
paul@412 | 330 | self.metadata = metadata |
paul@428 | 331 | self.events = events |
paul@347 | 332 | |
paul@347 | 333 | def getPageURL(self): |
paul@347 | 334 | |
paul@347 | 335 | "Return the URL of this page." |
paul@347 | 336 | |
paul@347 | 337 | return self.url |
paul@347 | 338 | |
paul@347 | 339 | def getFormat(self): |
paul@347 | 340 | |
paul@347 | 341 | "Get the format used by this resource." |
paul@347 | 342 | |
paul@347 | 343 | return "plain" |
paul@347 | 344 | |
paul@347 | 345 | def getMetadata(self): |
paul@347 | 346 | |
paul@347 | 347 | """ |
paul@347 | 348 | Return a dictionary containing items describing the page's "created" |
paul@347 | 349 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 350 | "last-comment" made about the last edit. |
paul@347 | 351 | """ |
paul@347 | 352 | |
paul@412 | 353 | return self.metadata or {} |
paul@347 | 354 | |
paul@437 | 355 | def getMethod(self): |
paul@437 | 356 | |
paul@437 | 357 | "Return the iTIP-related method associated with this resource." |
paul@437 | 358 | |
paul@437 | 359 | return None |
paul@437 | 360 | |
paul@347 | 361 | def getEvents(self): |
paul@347 | 362 | |
paul@347 | 363 | "Return a list of events from this resource." |
paul@347 | 364 | |
paul@412 | 365 | return self.events or [] |
paul@347 | 366 | |
paul@347 | 367 | def linkToPage(self, request, text, query_string=None, anchor=None): |
paul@347 | 368 | |
paul@347 | 369 | """ |
paul@347 | 370 | Using 'request', return a link to this page with the given link 'text' |
paul@347 | 371 | and optional 'query_string' and 'anchor'. |
paul@347 | 372 | """ |
paul@347 | 373 | |
paul@347 | 374 | return linkToResource(self.url, request, text, query_string, anchor) |
paul@347 | 375 | |
paul@347 | 376 | # Formatting-related functions. |
paul@347 | 377 | |
paul@347 | 378 | def formatText(self, text, fmt): |
paul@347 | 379 | |
paul@347 | 380 | """ |
paul@347 | 381 | Format the given 'text' using the specified formatter 'fmt'. |
paul@347 | 382 | """ |
paul@347 | 383 | |
paul@347 | 384 | # Assume plain text which is then formatted appropriately. |
paul@347 | 385 | |
paul@347 | 386 | return fmt.text(text) |
paul@347 | 387 | |
paul@412 | 388 | class EventResourceCollection(EventResource): |
paul@412 | 389 | |
paul@412 | 390 | "A collection of resources." |
paul@412 | 391 | |
paul@428 | 392 | def __init__(self, resources=None): |
paul@428 | 393 | self.resources = resources or [] |
paul@412 | 394 | |
paul@412 | 395 | def append(self, resource): |
paul@412 | 396 | self.resources.append(resource) |
paul@412 | 397 | |
paul@412 | 398 | def getEvents(self): |
paul@412 | 399 | events = [] |
paul@412 | 400 | for resource in self.resources: |
paul@412 | 401 | events += resource.getEvents() |
paul@412 | 402 | return events |
paul@412 | 403 | |
paul@408 | 404 | class EventCalendarResource(EventResource): |
paul@347 | 405 | |
paul@408 | 406 | "A generic calendar resource." |
paul@408 | 407 | |
paul@408 | 408 | def __init__(self, url, metadata): |
paul@412 | 409 | EventResource.__init__(self, url, metadata) |
paul@347 | 410 | |
paul@353 | 411 | if not self.metadata.has_key("created") and self.metadata.has_key("date"): |
paul@353 | 412 | self.metadata["created"] = DateTime(parsedate(self.metadata["date"])[:7]) |
paul@353 | 413 | |
paul@353 | 414 | if self.metadata.has_key("last-modified") and not isinstance(self.metadata["last-modified"], DateTime): |
paul@353 | 415 | self.metadata["last-modified"] = DateTime(parsedate(self.metadata["last-modified"])[:7]) |
paul@353 | 416 | |
paul@408 | 417 | class EventCalendar(EventCalendarResource): |
paul@408 | 418 | |
paul@408 | 419 | "An iCalendar resource." |
paul@408 | 420 | |
paul@437 | 421 | calendar_properties = ("METHOD",) |
paul@437 | 422 | |
paul@408 | 423 | def __init__(self, url, calendar, metadata): |
paul@408 | 424 | EventCalendarResource.__init__(self, url, metadata) |
paul@408 | 425 | self.calendar = calendar |
paul@437 | 426 | self.properties = {} |
paul@437 | 427 | |
paul@437 | 428 | def getMethod(self): |
paul@437 | 429 | return self.properties.get("METHOD") |
paul@408 | 430 | |
paul@347 | 431 | def getEvents(self): |
paul@347 | 432 | |
paul@347 | 433 | "Return a list of events from this resource." |
paul@347 | 434 | |
paul@347 | 435 | if self.events is None: |
paul@347 | 436 | self.events = [] |
paul@347 | 437 | |
paul@347 | 438 | _calendar, _empty, calendar = self.calendar |
paul@347 | 439 | |
paul@347 | 440 | for objtype, attrs, obj in calendar: |
paul@347 | 441 | |
paul@347 | 442 | # Read events. |
paul@347 | 443 | |
paul@347 | 444 | if objtype == "VEVENT": |
paul@347 | 445 | details = {} |
paul@347 | 446 | |
paul@347 | 447 | for property, attrs, value in obj: |
paul@347 | 448 | |
paul@347 | 449 | # Convert dates. |
paul@347 | 450 | |
paul@347 | 451 | if property in ("DTSTART", "DTEND", "CREATED", "DTSTAMP", "LAST-MODIFIED"): |
paul@347 | 452 | if property in ("DTSTART", "DTEND"): |
paul@347 | 453 | property = property[2:] |
paul@347 | 454 | if attrs.get("VALUE") == "DATE": |
paul@347 | 455 | value = getDateFromCalendar(value) |
paul@347 | 456 | if value and property == "END": |
paul@347 | 457 | value = value.previous_day() |
paul@347 | 458 | else: |
paul@347 | 459 | value = getDateTimeFromCalendar(value) |
paul@347 | 460 | |
paul@347 | 461 | # Convert numeric data. |
paul@347 | 462 | |
paul@347 | 463 | elif property == "SEQUENCE": |
paul@347 | 464 | value = int(value) |
paul@347 | 465 | |
paul@347 | 466 | # Convert lists. |
paul@347 | 467 | |
paul@347 | 468 | elif property == "CATEGORIES": |
paul@391 | 469 | if not isinstance(value, list): |
paul@391 | 470 | value = to_list(value, ",") |
paul@347 | 471 | |
paul@347 | 472 | # Convert positions (using decimal values). |
paul@347 | 473 | |
paul@347 | 474 | elif property == "GEO": |
paul@347 | 475 | try: |
paul@347 | 476 | value = map(getMapReferenceFromDecimal, to_list(value, ";")) |
paul@347 | 477 | if len(value) != 2: |
paul@347 | 478 | continue |
paul@347 | 479 | except (KeyError, ValueError): |
paul@347 | 480 | continue |
paul@347 | 481 | |
paul@347 | 482 | # Accept other textual data as it is. |
paul@347 | 483 | |
paul@438 | 484 | elif property in ("LOCATION", "SUMMARY", "URL", "UID"): |
paul@347 | 485 | value = value or None |
paul@347 | 486 | |
paul@347 | 487 | # Ignore other properties. |
paul@347 | 488 | |
paul@347 | 489 | else: |
paul@347 | 490 | continue |
paul@347 | 491 | |
paul@347 | 492 | property = property.lower() |
paul@347 | 493 | details[property] = value |
paul@347 | 494 | |
paul@347 | 495 | self.events.append(CalendarEvent(self, details)) |
paul@347 | 496 | |
paul@437 | 497 | # Obtain calendar-level information. |
paul@437 | 498 | |
paul@437 | 499 | elif objtype in self.calendar_properties: |
paul@437 | 500 | self.properties[objtype] = obj |
paul@437 | 501 | |
paul@347 | 502 | return self.events |
paul@347 | 503 | |
paul@408 | 504 | class EventXMLCalendar(EventCalendarResource): |
paul@408 | 505 | |
paul@408 | 506 | "An xCalendar resource." |
paul@408 | 507 | |
paul@408 | 508 | XCAL = {"xcal" : "urn:ietf:params:xml:ns:icalendar-2.0"} |
paul@408 | 509 | |
paul@408 | 510 | # See: http://tools.ietf.org/html/draft-daboo-et-al-icalendar-in-xml-11#section-3.4 |
paul@408 | 511 | |
paul@437 | 512 | calendar_properties = [ |
paul@437 | 513 | ("method", "xcal:properties/xcal:method", "getText"), |
paul@437 | 514 | ] |
paul@437 | 515 | |
paul@437 | 516 | event_properties = [ |
paul@408 | 517 | ("summary", "xcal:properties/xcal:summary", "getText"), |
paul@408 | 518 | ("location", "xcal:properties/xcal:location", "getText"), |
paul@408 | 519 | ("start", "xcal:properties/xcal:dtstart", "getDateTime"), |
paul@408 | 520 | ("end", "xcal:properties/xcal:dtend", "getDateTime"), |
paul@408 | 521 | ("created", "xcal:properties/xcal:created", "getDateTime"), |
paul@408 | 522 | ("dtstamp", "xcal:properties/xcal:dtstamp", "getDateTime"), |
paul@408 | 523 | ("last-modified", "xcal:properties/xcal:last-modified", "getDateTime"), |
paul@408 | 524 | ("sequence", "xcal:properties/xcal:sequence", "getInteger"), |
paul@408 | 525 | ("categories", "xcal:properties/xcal:categories", "getCollection"), |
paul@408 | 526 | ("geo", "xcal:properties/xcal:geo", "getGeo"), |
paul@408 | 527 | ("url", "xcal:properties/xcal:url", "getURI"), |
paul@438 | 528 | ("uid", "xcal:properties/xcal:uid", "getText"), |
paul@408 | 529 | ] |
paul@408 | 530 | |
paul@408 | 531 | def __init__(self, url, doc, metadata): |
paul@408 | 532 | EventCalendarResource.__init__(self, url, metadata) |
paul@408 | 533 | self.doc = doc |
paul@437 | 534 | self.properties = self.getProperties(self.calendar_properties, doc) |
paul@437 | 535 | |
paul@437 | 536 | def getMethod(self): |
paul@437 | 537 | return self.properties.get("method") |
paul@408 | 538 | |
paul@408 | 539 | def getEvents(self): |
paul@408 | 540 | |
paul@408 | 541 | "Return a list of events from this resource." |
paul@408 | 542 | |
paul@408 | 543 | if self.events is None: |
paul@408 | 544 | self.events = [] |
paul@408 | 545 | |
paul@408 | 546 | for event in self.doc.xpath("//xcal:vevent", namespaces=self.XCAL): |
paul@437 | 547 | details = self.getProperties(self.event_properties, event) |
paul@408 | 548 | self.events.append(CalendarEvent(self, details)) |
paul@408 | 549 | |
paul@408 | 550 | return self.events |
paul@408 | 551 | |
paul@437 | 552 | # Retrieval methods. |
paul@437 | 553 | |
paul@437 | 554 | def getProperties(self, definitions, root): |
paul@437 | 555 | details = {} |
paul@437 | 556 | |
paul@437 | 557 | for property, path, converter in definitions: |
paul@437 | 558 | values = root.xpath(path, namespaces=self.XCAL) |
paul@437 | 559 | |
paul@437 | 560 | try: |
paul@437 | 561 | value = getattr(self, converter)(property, values) |
paul@437 | 562 | details[property] = value |
paul@437 | 563 | except (IndexError, ValueError), exc: |
paul@437 | 564 | pass |
paul@437 | 565 | |
paul@437 | 566 | return details |
paul@437 | 567 | |
paul@412 | 568 | # Parsing methods. |
paul@412 | 569 | |
paul@408 | 570 | def _getValue(self, values, type): |
paul@437 | 571 | if values: |
paul@437 | 572 | for element in values[0].xpath("xcal:%s" % type, namespaces=self.XCAL): |
paul@437 | 573 | return element.textContent |
paul@437 | 574 | return None |
paul@408 | 575 | |
paul@408 | 576 | def getText(self, property, values): |
paul@408 | 577 | return self._getValue(values, "text") |
paul@408 | 578 | |
paul@408 | 579 | def getDateTime(self, property, values): |
paul@408 | 580 | element = values[0] |
paul@408 | 581 | for dtelement in element.xpath("xcal:date-time|xcal:date", namespaces=self.XCAL): |
paul@408 | 582 | dt = getDateTimeFromISO8601(dtelement.textContent) |
paul@408 | 583 | break |
paul@408 | 584 | else: |
paul@408 | 585 | return None |
paul@408 | 586 | |
paul@408 | 587 | tzid = self._getValue(element.xpath("xcal:parameters", namespaces=self.XCAL), "tzid") |
paul@408 | 588 | if tzid and isinstance(dt, DateTime): |
paul@408 | 589 | zone = "/".join(tzid.rsplit("/", 2)[-2:]) |
paul@408 | 590 | dt.set_time_zone(zone) |
paul@408 | 591 | |
paul@408 | 592 | if dtelement.localName == "date" and property == "end": |
paul@408 | 593 | dt = dt.previous_day() |
paul@408 | 594 | |
paul@408 | 595 | return dt |
paul@408 | 596 | |
paul@408 | 597 | def getInteger(self, property, values): |
paul@408 | 598 | value = self._getValue(values, "integer") |
paul@408 | 599 | if value is not None: |
paul@408 | 600 | return int(value) |
paul@408 | 601 | else: |
paul@408 | 602 | return None |
paul@408 | 603 | |
paul@408 | 604 | def getCollection(self, property, values): |
paul@408 | 605 | return [n.textContent for n in values[0].xpath("xcal:text", namespaces=self.XCAL)] |
paul@408 | 606 | |
paul@408 | 607 | def getGeo(self, property, values): |
paul@408 | 608 | geo = [None, None] |
paul@408 | 609 | |
paul@408 | 610 | for geoelement in values[0].xpath("xcal:latitude|xcal:longitude", namespaces=self.XCAL): |
paul@408 | 611 | value = geoelement.textContent |
paul@408 | 612 | if geoelement.localName == "latitude": |
paul@408 | 613 | geo[0] = value |
paul@408 | 614 | else: |
paul@408 | 615 | geo[1] = value |
paul@408 | 616 | |
paul@408 | 617 | if None not in geo: |
paul@408 | 618 | return map(getMapReferenceFromDecimal, geo) |
paul@408 | 619 | else: |
paul@408 | 620 | return None |
paul@408 | 621 | |
paul@408 | 622 | def getURI(self, property, values): |
paul@408 | 623 | return self._getValue(values, "uri") |
paul@408 | 624 | |
paul@347 | 625 | class EventPage: |
paul@347 | 626 | |
paul@347 | 627 | "An event page acting as an event resource." |
paul@347 | 628 | |
paul@347 | 629 | def __init__(self, page): |
paul@347 | 630 | self.page = page |
paul@347 | 631 | self.events = None |
paul@347 | 632 | self.body = None |
paul@347 | 633 | self.categories = None |
paul@347 | 634 | self.metadata = None |
paul@347 | 635 | |
paul@347 | 636 | def copyPage(self, page): |
paul@347 | 637 | |
paul@347 | 638 | "Copy the body of the given 'page'." |
paul@347 | 639 | |
paul@347 | 640 | self.body = page.getBody() |
paul@347 | 641 | |
paul@347 | 642 | def getPageURL(self): |
paul@347 | 643 | |
paul@347 | 644 | "Return the URL of this page." |
paul@347 | 645 | |
paul@347 | 646 | return getPageURL(self.page) |
paul@347 | 647 | |
paul@347 | 648 | def getFormat(self): |
paul@347 | 649 | |
paul@347 | 650 | "Get the format used on this page." |
paul@347 | 651 | |
paul@347 | 652 | return getFormat(self.page) |
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 page'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 | if self.metadata is None: |
paul@347 | 663 | self.metadata = getMetadata(self.page) |
paul@347 | 664 | return self.metadata |
paul@347 | 665 | |
paul@347 | 666 | def getRevisions(self): |
paul@347 | 667 | |
paul@347 | 668 | "Return a list of page revisions." |
paul@347 | 669 | |
paul@347 | 670 | return self.page.getRevList() |
paul@347 | 671 | |
paul@347 | 672 | def getPageRevision(self): |
paul@347 | 673 | |
paul@347 | 674 | "Return the revision details dictionary for this page." |
paul@347 | 675 | |
paul@347 | 676 | return getPageRevision(self.page) |
paul@347 | 677 | |
paul@347 | 678 | def getPageName(self): |
paul@347 | 679 | |
paul@347 | 680 | "Return the page name." |
paul@347 | 681 | |
paul@347 | 682 | return self.page.page_name |
paul@347 | 683 | |
paul@347 | 684 | def getPrettyPageName(self): |
paul@347 | 685 | |
paul@347 | 686 | "Return a nicely formatted title/name for this page." |
paul@347 | 687 | |
paul@347 | 688 | return getPrettyPageName(self.page) |
paul@347 | 689 | |
paul@347 | 690 | def getBody(self): |
paul@347 | 691 | |
paul@347 | 692 | "Get the current page body." |
paul@347 | 693 | |
paul@347 | 694 | if self.body is None: |
paul@347 | 695 | self.body = self.page.get_raw_body() |
paul@347 | 696 | return self.body |
paul@347 | 697 | |
paul@347 | 698 | def getEvents(self): |
paul@347 | 699 | |
paul@347 | 700 | "Return a list of events from this page." |
paul@347 | 701 | |
paul@347 | 702 | if self.events is None: |
paul@428 | 703 | self.events = parseEventsInPage(self.page.data, self).getEvents() |
paul@347 | 704 | |
paul@347 | 705 | return self.events |
paul@347 | 706 | |
paul@347 | 707 | def setEvents(self, events): |
paul@347 | 708 | |
paul@347 | 709 | "Set the given 'events' on this page." |
paul@347 | 710 | |
paul@347 | 711 | self.events = events |
paul@347 | 712 | |
paul@347 | 713 | def getCategoryMembership(self): |
paul@347 | 714 | |
paul@347 | 715 | "Get the category names from this page." |
paul@347 | 716 | |
paul@347 | 717 | if self.categories is None: |
paul@347 | 718 | body = self.getBody() |
paul@347 | 719 | match = category_membership_regexp.search(body) |
paul@347 | 720 | self.categories = match and [x for x in match.groups() if x] or [] |
paul@347 | 721 | |
paul@347 | 722 | return self.categories |
paul@347 | 723 | |
paul@347 | 724 | def setCategoryMembership(self, category_names): |
paul@347 | 725 | |
paul@347 | 726 | """ |
paul@347 | 727 | Set the category membership for the page using the specified |
paul@347 | 728 | 'category_names'. |
paul@347 | 729 | """ |
paul@347 | 730 | |
paul@347 | 731 | self.categories = category_names |
paul@347 | 732 | |
paul@347 | 733 | def flushEventDetails(self): |
paul@347 | 734 | |
paul@347 | 735 | "Flush the current event details to this page's body text." |
paul@347 | 736 | |
paul@347 | 737 | new_body_parts = [] |
paul@347 | 738 | end_of_last_match = 0 |
paul@347 | 739 | body = self.getBody() |
paul@347 | 740 | |
paul@347 | 741 | events = iter(self.getEvents()) |
paul@347 | 742 | |
paul@347 | 743 | event = events.next() |
paul@347 | 744 | event_details = event.getDetails() |
paul@347 | 745 | replaced_terms = set() |
paul@347 | 746 | |
paul@347 | 747 | for match in definition_list_regexp.finditer(body): |
paul@347 | 748 | |
paul@347 | 749 | # Permit case-insensitive list terms. |
paul@347 | 750 | |
paul@347 | 751 | term = match.group("term").lower() |
paul@347 | 752 | desc = match.group("desc") |
paul@347 | 753 | |
paul@347 | 754 | # Check that the term has not already been substituted. If so, |
paul@347 | 755 | # get the next event. |
paul@347 | 756 | |
paul@347 | 757 | if term in replaced_terms: |
paul@347 | 758 | try: |
paul@347 | 759 | event = events.next() |
paul@347 | 760 | |
paul@347 | 761 | # No more events. |
paul@347 | 762 | |
paul@347 | 763 | except StopIteration: |
paul@347 | 764 | break |
paul@347 | 765 | |
paul@347 | 766 | event_details = event.getDetails() |
paul@347 | 767 | replaced_terms = set() |
paul@347 | 768 | |
paul@347 | 769 | # Add preceding text to the new body. |
paul@347 | 770 | |
paul@347 | 771 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@347 | 772 | |
paul@347 | 773 | # Get the matching regions, adding the term to the new body. |
paul@347 | 774 | |
paul@347 | 775 | new_body_parts.append(match.group("wholeterm")) |
paul@347 | 776 | |
paul@347 | 777 | # Special value type handling. |
paul@347 | 778 | |
paul@347 | 779 | if event_details.has_key(term): |
paul@347 | 780 | |
paul@347 | 781 | # Dates. |
paul@347 | 782 | |
paul@347 | 783 | if term in event.date_terms: |
paul@347 | 784 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@347 | 785 | |
paul@347 | 786 | # Lists (whose elements may be quoted). |
paul@347 | 787 | |
paul@347 | 788 | elif term in event.list_terms: |
paul@347 | 789 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@347 | 790 | |
paul@347 | 791 | # Labels which must be quoted. |
paul@347 | 792 | |
paul@347 | 793 | elif term in event.title_terms: |
paul@347 | 794 | desc = getEncodedWikiText(event_details[term]) |
paul@347 | 795 | |
paul@347 | 796 | # Position details. |
paul@347 | 797 | |
paul@347 | 798 | elif term == "geo": |
paul@347 | 799 | desc = " ".join(map(str, event_details[term])) |
paul@347 | 800 | |
paul@347 | 801 | # Text which need not be quoted, but it will be Wiki text. |
paul@347 | 802 | |
paul@347 | 803 | elif term in event.other_terms: |
paul@347 | 804 | desc = event_details[term] |
paul@347 | 805 | |
paul@347 | 806 | replaced_terms.add(term) |
paul@347 | 807 | |
paul@347 | 808 | # Add the replaced value. |
paul@347 | 809 | |
paul@347 | 810 | new_body_parts.append(desc) |
paul@347 | 811 | |
paul@347 | 812 | # Remember where in the page has been processed. |
paul@347 | 813 | |
paul@347 | 814 | end_of_last_match = match.end() |
paul@347 | 815 | |
paul@347 | 816 | # Write the rest of the page. |
paul@347 | 817 | |
paul@347 | 818 | new_body_parts.append(body[end_of_last_match:]) |
paul@347 | 819 | |
paul@347 | 820 | self.body = "".join(new_body_parts) |
paul@347 | 821 | |
paul@347 | 822 | def flushCategoryMembership(self): |
paul@347 | 823 | |
paul@347 | 824 | "Flush the category membership to the page body." |
paul@347 | 825 | |
paul@347 | 826 | body = self.getBody() |
paul@347 | 827 | category_names = self.getCategoryMembership() |
paul@347 | 828 | match = category_membership_regexp.search(body) |
paul@347 | 829 | |
paul@347 | 830 | if match: |
paul@347 | 831 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@347 | 832 | |
paul@347 | 833 | def saveChanges(self): |
paul@347 | 834 | |
paul@347 | 835 | "Save changes to the event." |
paul@347 | 836 | |
paul@347 | 837 | self.flushEventDetails() |
paul@347 | 838 | self.flushCategoryMembership() |
paul@347 | 839 | self.page.saveText(self.getBody(), 0) |
paul@347 | 840 | |
paul@347 | 841 | def linkToPage(self, request, text, query_string=None, anchor=None): |
paul@347 | 842 | |
paul@347 | 843 | """ |
paul@347 | 844 | Using 'request', return a link to this page with the given link 'text' |
paul@347 | 845 | and optional 'query_string' and 'anchor'. |
paul@347 | 846 | """ |
paul@347 | 847 | |
paul@347 | 848 | return linkToPage(request, self.page, text, query_string, anchor) |
paul@347 | 849 | |
paul@347 | 850 | # Formatting-related functions. |
paul@347 | 851 | |
paul@347 | 852 | def getParserClass(self, format): |
paul@347 | 853 | |
paul@347 | 854 | """ |
paul@347 | 855 | Return a parser class for the given 'format', returning a plain text |
paul@347 | 856 | parser if no parser can be found for the specified 'format'. |
paul@347 | 857 | """ |
paul@347 | 858 | |
paul@347 | 859 | return getParserClass(self.page.request, format) |
paul@347 | 860 | |
paul@347 | 861 | def formatText(self, text, fmt): |
paul@347 | 862 | |
paul@347 | 863 | """ |
paul@347 | 864 | Format the given 'text' using the specified formatter 'fmt'. |
paul@347 | 865 | """ |
paul@347 | 866 | |
paul@347 | 867 | fmt.page = page = self.page |
paul@347 | 868 | request = page.request |
paul@347 | 869 | |
paul@395 | 870 | if self.getFormat() == "calendar": |
paul@395 | 871 | parser_cls = RawParser |
paul@395 | 872 | else: |
paul@395 | 873 | parser_cls = self.getParserClass(self.getFormat()) |
paul@395 | 874 | |
paul@347 | 875 | return formatText(text, request, fmt, parser_cls) |
paul@347 | 876 | |
paul@347 | 877 | # Event details. |
paul@347 | 878 | |
paul@347 | 879 | class Event(ActsAsTimespan): |
paul@347 | 880 | |
paul@347 | 881 | "A description of an event." |
paul@347 | 882 | |
paul@347 | 883 | title_terms = "title", "summary" |
paul@347 | 884 | date_terms = "start", "end" |
paul@347 | 885 | list_terms = "topics", "categories" |
paul@347 | 886 | other_terms = "description", "location", "link" |
paul@347 | 887 | geo_terms = "geo", |
paul@347 | 888 | all_terms = title_terms + date_terms + list_terms + other_terms + geo_terms |
paul@347 | 889 | |
paul@347 | 890 | def __init__(self, page, details, raw_details=None): |
paul@347 | 891 | self.page = page |
paul@347 | 892 | self.details = details |
paul@366 | 893 | self.raw_details = raw_details or {} |
paul@347 | 894 | |
paul@347 | 895 | # Permit omission of the end of the event by duplicating the start. |
paul@347 | 896 | |
paul@347 | 897 | if self.details.has_key("start") and not self.details.get("end"): |
paul@347 | 898 | end = self.details["start"] |
paul@347 | 899 | |
paul@347 | 900 | # Make any end time refer to the day instead. |
paul@347 | 901 | |
paul@347 | 902 | if isinstance(end, DateTime): |
paul@347 | 903 | end = end.as_date() |
paul@347 | 904 | |
paul@347 | 905 | self.details["end"] = end |
paul@347 | 906 | |
paul@347 | 907 | def __repr__(self): |
paul@347 | 908 | return "<Event %r %r>" % (self.getSummary(), self.as_limits()) |
paul@347 | 909 | |
paul@347 | 910 | def __hash__(self): |
paul@347 | 911 | |
paul@347 | 912 | """ |
paul@347 | 913 | Return a dictionary hash, avoiding mistaken equality of events in some |
paul@347 | 914 | situations (notably membership tests) by including the URL as well as |
paul@347 | 915 | the summary. |
paul@347 | 916 | """ |
paul@347 | 917 | |
paul@347 | 918 | return hash(self.getSummary() + self.getEventURL()) |
paul@347 | 919 | |
paul@347 | 920 | def getPage(self): |
paul@347 | 921 | |
paul@347 | 922 | "Return the page describing this event." |
paul@347 | 923 | |
paul@347 | 924 | return self.page |
paul@347 | 925 | |
paul@347 | 926 | def setPage(self, page): |
paul@347 | 927 | |
paul@347 | 928 | "Set the 'page' describing this event." |
paul@347 | 929 | |
paul@347 | 930 | self.page = page |
paul@347 | 931 | |
paul@438 | 932 | def getEventUID(self): |
paul@438 | 933 | |
paul@438 | 934 | "Return the UID of this event." |
paul@438 | 935 | |
paul@438 | 936 | return self.details.get("uid") or self.getEventURL() |
paul@438 | 937 | |
paul@438 | 938 | def getSequence(self): |
paul@438 | 939 | |
paul@438 | 940 | "Return the sequence number of this event." |
paul@438 | 941 | |
paul@438 | 942 | return self.details.get("sequence") or 0 |
paul@438 | 943 | |
paul@347 | 944 | def getEventURL(self): |
paul@347 | 945 | |
paul@347 | 946 | "Return the URL of this event." |
paul@347 | 947 | |
paul@347 | 948 | fragment = self.details.get("fragment") |
paul@347 | 949 | return self.page.getPageURL() + (fragment and "#" + fragment or "") |
paul@347 | 950 | |
paul@347 | 951 | def linkToEvent(self, request, text, query_string=None): |
paul@347 | 952 | |
paul@347 | 953 | """ |
paul@347 | 954 | Using 'request', return a link to this event with the given link 'text' |
paul@347 | 955 | and optional 'query_string'. |
paul@347 | 956 | """ |
paul@347 | 957 | |
paul@347 | 958 | return self.page.linkToPage(request, text, query_string, self.details.get("fragment")) |
paul@347 | 959 | |
paul@347 | 960 | def getMetadata(self): |
paul@347 | 961 | |
paul@347 | 962 | """ |
paul@347 | 963 | Return a dictionary containing items describing the event's "created" |
paul@347 | 964 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 965 | "last-comment" made about the last edit. |
paul@347 | 966 | """ |
paul@347 | 967 | |
paul@347 | 968 | # Delegate this to the page. |
paul@347 | 969 | |
paul@347 | 970 | return self.page.getMetadata() |
paul@347 | 971 | |
paul@347 | 972 | def getSummary(self, event_parent=None): |
paul@347 | 973 | |
paul@347 | 974 | """ |
paul@347 | 975 | Return either the given title or summary of the event according to the |
paul@347 | 976 | event details, or a summary made from using the pretty version of the |
paul@347 | 977 | page name. |
paul@347 | 978 | |
paul@347 | 979 | If the optional 'event_parent' is specified, any page beneath the given |
paul@347 | 980 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@347 | 981 | if its name is used as the summary. |
paul@347 | 982 | """ |
paul@347 | 983 | |
paul@347 | 984 | event_details = self.details |
paul@347 | 985 | |
paul@347 | 986 | if event_details.has_key("title"): |
paul@347 | 987 | return event_details["title"] |
paul@347 | 988 | elif event_details.has_key("summary"): |
paul@347 | 989 | return event_details["summary"] |
paul@347 | 990 | else: |
paul@347 | 991 | # If appropriate, remove the parent details and "/" character. |
paul@347 | 992 | |
paul@347 | 993 | title = self.page.getPageName() |
paul@347 | 994 | |
paul@347 | 995 | if event_parent and title.startswith(event_parent): |
paul@347 | 996 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@347 | 997 | |
paul@347 | 998 | return getPrettyTitle(title) |
paul@347 | 999 | |
paul@347 | 1000 | def getDetails(self): |
paul@347 | 1001 | |
paul@347 | 1002 | "Return the details for this event." |
paul@347 | 1003 | |
paul@347 | 1004 | return self.details |
paul@347 | 1005 | |
paul@347 | 1006 | def setDetails(self, event_details): |
paul@347 | 1007 | |
paul@347 | 1008 | "Set the 'event_details' for this event." |
paul@347 | 1009 | |
paul@347 | 1010 | self.details = event_details |
paul@347 | 1011 | |
paul@347 | 1012 | def getRawDetails(self): |
paul@347 | 1013 | |
paul@347 | 1014 | "Return the details for this event as they were written in a page." |
paul@347 | 1015 | |
paul@347 | 1016 | return self.raw_details |
paul@347 | 1017 | |
paul@347 | 1018 | # Timespan-related methods. |
paul@347 | 1019 | |
paul@347 | 1020 | def __contains__(self, other): |
paul@347 | 1021 | return self == other |
paul@347 | 1022 | |
paul@347 | 1023 | def __eq__(self, other): |
paul@347 | 1024 | if isinstance(other, Event): |
paul@347 | 1025 | return self.getSummary() == other.getSummary() and self.getEventURL() == other.getEventURL() and self._cmp(other) |
paul@347 | 1026 | else: |
paul@347 | 1027 | return self._cmp(other) == 0 |
paul@347 | 1028 | |
paul@347 | 1029 | def __ne__(self, other): |
paul@347 | 1030 | return not self.__eq__(other) |
paul@347 | 1031 | |
paul@347 | 1032 | def __lt__(self, other): |
paul@347 | 1033 | return self._cmp(other) == -1 |
paul@347 | 1034 | |
paul@347 | 1035 | def __le__(self, other): |
paul@347 | 1036 | return self._cmp(other) in (-1, 0) |
paul@347 | 1037 | |
paul@347 | 1038 | def __gt__(self, other): |
paul@347 | 1039 | return self._cmp(other) == 1 |
paul@347 | 1040 | |
paul@347 | 1041 | def __ge__(self, other): |
paul@347 | 1042 | return self._cmp(other) in (0, 1) |
paul@347 | 1043 | |
paul@347 | 1044 | def _cmp(self, other): |
paul@347 | 1045 | |
paul@347 | 1046 | "Compare this event to an 'other' event purely by their timespans." |
paul@347 | 1047 | |
paul@347 | 1048 | if isinstance(other, Event): |
paul@347 | 1049 | return cmp(self.as_timespan(), other.as_timespan()) |
paul@347 | 1050 | else: |
paul@347 | 1051 | return cmp(self.as_timespan(), other) |
paul@347 | 1052 | |
paul@347 | 1053 | def as_timespan(self): |
paul@347 | 1054 | details = self.details |
paul@347 | 1055 | if details.has_key("start") and details.has_key("end"): |
paul@347 | 1056 | return Timespan(details["start"], details["end"]) |
paul@347 | 1057 | else: |
paul@347 | 1058 | return None |
paul@347 | 1059 | |
paul@347 | 1060 | def as_limits(self): |
paul@347 | 1061 | ts = self.as_timespan() |
paul@347 | 1062 | return ts and ts.as_limits() |
paul@347 | 1063 | |
paul@347 | 1064 | class CalendarEvent(Event): |
paul@347 | 1065 | |
paul@347 | 1066 | "An event from a remote calendar." |
paul@347 | 1067 | |
paul@347 | 1068 | def getEventURL(self): |
paul@347 | 1069 | |
paul@354 | 1070 | """ |
paul@354 | 1071 | Return the URL of this event, fixing any misinterpreted or incorrectly |
paul@354 | 1072 | formatted value in the event definition or returning the resource URL in |
paul@354 | 1073 | the absence of any URL in the event details. |
paul@354 | 1074 | """ |
paul@354 | 1075 | |
paul@413 | 1076 | # NOTE: Redirect empty URLs to an action showing the resource details. |
paul@413 | 1077 | |
paul@354 | 1078 | return self.details.get("url") and \ |
paul@354 | 1079 | self.valueToString(self.details["url"]) or \ |
paul@354 | 1080 | self.page.getPageURL() |
paul@354 | 1081 | |
paul@354 | 1082 | def getSummary(self, event_parent=None): |
paul@347 | 1083 | |
paul@354 | 1084 | """ |
paul@354 | 1085 | Return the event summary, fixing any misinterpreted or incorrectly |
paul@354 | 1086 | formatted value in the event definition. |
paul@354 | 1087 | """ |
paul@354 | 1088 | |
paul@354 | 1089 | return self.valueToString(self.details["summary"]) |
paul@354 | 1090 | |
paul@354 | 1091 | def valueToString(self, value): |
paul@354 | 1092 | |
paul@354 | 1093 | "Return the given 'value' converted to a string." |
paul@354 | 1094 | |
paul@354 | 1095 | if isinstance(value, list): |
paul@354 | 1096 | return ",".join(value) |
paul@354 | 1097 | elif isinstance(value, tuple): |
paul@354 | 1098 | return ";".join(value) |
paul@354 | 1099 | else: |
paul@354 | 1100 | return value |
paul@347 | 1101 | |
paul@347 | 1102 | def linkToEvent(self, request, text, query_string=None, anchor=None): |
paul@347 | 1103 | |
paul@347 | 1104 | """ |
paul@347 | 1105 | Using 'request', return a link to this event with the given link 'text' |
paul@347 | 1106 | and optional 'query_string' and 'anchor'. |
paul@347 | 1107 | """ |
paul@347 | 1108 | |
paul@347 | 1109 | return linkToResource(self.getEventURL(), request, text, query_string, anchor) |
paul@347 | 1110 | |
paul@347 | 1111 | def getMetadata(self): |
paul@347 | 1112 | |
paul@347 | 1113 | """ |
paul@347 | 1114 | Return a dictionary containing items describing the event's "created" |
paul@347 | 1115 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 1116 | "last-comment" made about the last edit. |
paul@347 | 1117 | """ |
paul@347 | 1118 | |
paul@353 | 1119 | metadata = self.page.getMetadata() |
paul@353 | 1120 | |
paul@347 | 1121 | return { |
paul@353 | 1122 | "created" : self.details.get("created") or self.details.get("dtstamp") or metadata["created"], |
paul@353 | 1123 | "last-modified" : self.details.get("last-modified") or self.details.get("dtstamp") or metadata["last-modified"], |
paul@438 | 1124 | "sequence" : self.getSequence(), |
paul@347 | 1125 | "last-comment" : "" |
paul@347 | 1126 | } |
paul@347 | 1127 | |
paul@347 | 1128 | # vim: tabstop=4 expandtab shiftwidth=4 |