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@347 | 484 | elif property in ("LOCATION", "SUMMARY", "URL"): |
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@408 | 528 | ] |
paul@408 | 529 | |
paul@408 | 530 | def __init__(self, url, doc, metadata): |
paul@408 | 531 | EventCalendarResource.__init__(self, url, metadata) |
paul@408 | 532 | self.doc = doc |
paul@437 | 533 | self.properties = self.getProperties(self.calendar_properties, doc) |
paul@437 | 534 | |
paul@437 | 535 | def getMethod(self): |
paul@437 | 536 | return self.properties.get("method") |
paul@408 | 537 | |
paul@408 | 538 | def getEvents(self): |
paul@408 | 539 | |
paul@408 | 540 | "Return a list of events from this resource." |
paul@408 | 541 | |
paul@408 | 542 | if self.events is None: |
paul@408 | 543 | self.events = [] |
paul@408 | 544 | |
paul@408 | 545 | for event in self.doc.xpath("//xcal:vevent", namespaces=self.XCAL): |
paul@437 | 546 | details = self.getProperties(self.event_properties, event) |
paul@408 | 547 | self.events.append(CalendarEvent(self, details)) |
paul@408 | 548 | |
paul@408 | 549 | return self.events |
paul@408 | 550 | |
paul@437 | 551 | # Retrieval methods. |
paul@437 | 552 | |
paul@437 | 553 | def getProperties(self, definitions, root): |
paul@437 | 554 | details = {} |
paul@437 | 555 | |
paul@437 | 556 | for property, path, converter in definitions: |
paul@437 | 557 | values = root.xpath(path, namespaces=self.XCAL) |
paul@437 | 558 | |
paul@437 | 559 | try: |
paul@437 | 560 | value = getattr(self, converter)(property, values) |
paul@437 | 561 | details[property] = value |
paul@437 | 562 | except (IndexError, ValueError), exc: |
paul@437 | 563 | pass |
paul@437 | 564 | |
paul@437 | 565 | return details |
paul@437 | 566 | |
paul@412 | 567 | # Parsing methods. |
paul@412 | 568 | |
paul@408 | 569 | def _getValue(self, values, type): |
paul@437 | 570 | if values: |
paul@437 | 571 | for element in values[0].xpath("xcal:%s" % type, namespaces=self.XCAL): |
paul@437 | 572 | return element.textContent |
paul@437 | 573 | return None |
paul@408 | 574 | |
paul@408 | 575 | def getText(self, property, values): |
paul@408 | 576 | return self._getValue(values, "text") |
paul@408 | 577 | |
paul@408 | 578 | def getDateTime(self, property, values): |
paul@408 | 579 | element = values[0] |
paul@408 | 580 | for dtelement in element.xpath("xcal:date-time|xcal:date", namespaces=self.XCAL): |
paul@408 | 581 | dt = getDateTimeFromISO8601(dtelement.textContent) |
paul@408 | 582 | break |
paul@408 | 583 | else: |
paul@408 | 584 | return None |
paul@408 | 585 | |
paul@408 | 586 | tzid = self._getValue(element.xpath("xcal:parameters", namespaces=self.XCAL), "tzid") |
paul@408 | 587 | if tzid and isinstance(dt, DateTime): |
paul@408 | 588 | zone = "/".join(tzid.rsplit("/", 2)[-2:]) |
paul@408 | 589 | dt.set_time_zone(zone) |
paul@408 | 590 | |
paul@408 | 591 | if dtelement.localName == "date" and property == "end": |
paul@408 | 592 | dt = dt.previous_day() |
paul@408 | 593 | |
paul@408 | 594 | return dt |
paul@408 | 595 | |
paul@408 | 596 | def getInteger(self, property, values): |
paul@408 | 597 | value = self._getValue(values, "integer") |
paul@408 | 598 | if value is not None: |
paul@408 | 599 | return int(value) |
paul@408 | 600 | else: |
paul@408 | 601 | return None |
paul@408 | 602 | |
paul@408 | 603 | def getCollection(self, property, values): |
paul@408 | 604 | return [n.textContent for n in values[0].xpath("xcal:text", namespaces=self.XCAL)] |
paul@408 | 605 | |
paul@408 | 606 | def getGeo(self, property, values): |
paul@408 | 607 | geo = [None, None] |
paul@408 | 608 | |
paul@408 | 609 | for geoelement in values[0].xpath("xcal:latitude|xcal:longitude", namespaces=self.XCAL): |
paul@408 | 610 | value = geoelement.textContent |
paul@408 | 611 | if geoelement.localName == "latitude": |
paul@408 | 612 | geo[0] = value |
paul@408 | 613 | else: |
paul@408 | 614 | geo[1] = value |
paul@408 | 615 | |
paul@408 | 616 | if None not in geo: |
paul@408 | 617 | return map(getMapReferenceFromDecimal, geo) |
paul@408 | 618 | else: |
paul@408 | 619 | return None |
paul@408 | 620 | |
paul@408 | 621 | def getURI(self, property, values): |
paul@408 | 622 | return self._getValue(values, "uri") |
paul@408 | 623 | |
paul@347 | 624 | class EventPage: |
paul@347 | 625 | |
paul@347 | 626 | "An event page acting as an event resource." |
paul@347 | 627 | |
paul@347 | 628 | def __init__(self, page): |
paul@347 | 629 | self.page = page |
paul@347 | 630 | self.events = None |
paul@347 | 631 | self.body = None |
paul@347 | 632 | self.categories = None |
paul@347 | 633 | self.metadata = None |
paul@347 | 634 | |
paul@347 | 635 | def copyPage(self, page): |
paul@347 | 636 | |
paul@347 | 637 | "Copy the body of the given 'page'." |
paul@347 | 638 | |
paul@347 | 639 | self.body = page.getBody() |
paul@347 | 640 | |
paul@347 | 641 | def getPageURL(self): |
paul@347 | 642 | |
paul@347 | 643 | "Return the URL of this page." |
paul@347 | 644 | |
paul@347 | 645 | return getPageURL(self.page) |
paul@347 | 646 | |
paul@347 | 647 | def getFormat(self): |
paul@347 | 648 | |
paul@347 | 649 | "Get the format used on this page." |
paul@347 | 650 | |
paul@347 | 651 | return getFormat(self.page) |
paul@347 | 652 | |
paul@347 | 653 | def getMetadata(self): |
paul@347 | 654 | |
paul@347 | 655 | """ |
paul@347 | 656 | Return a dictionary containing items describing the page's "created" |
paul@347 | 657 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 658 | "last-comment" made about the last edit. |
paul@347 | 659 | """ |
paul@347 | 660 | |
paul@347 | 661 | if self.metadata is None: |
paul@347 | 662 | self.metadata = getMetadata(self.page) |
paul@347 | 663 | return self.metadata |
paul@347 | 664 | |
paul@347 | 665 | def getRevisions(self): |
paul@347 | 666 | |
paul@347 | 667 | "Return a list of page revisions." |
paul@347 | 668 | |
paul@347 | 669 | return self.page.getRevList() |
paul@347 | 670 | |
paul@347 | 671 | def getPageRevision(self): |
paul@347 | 672 | |
paul@347 | 673 | "Return the revision details dictionary for this page." |
paul@347 | 674 | |
paul@347 | 675 | return getPageRevision(self.page) |
paul@347 | 676 | |
paul@347 | 677 | def getPageName(self): |
paul@347 | 678 | |
paul@347 | 679 | "Return the page name." |
paul@347 | 680 | |
paul@347 | 681 | return self.page.page_name |
paul@347 | 682 | |
paul@347 | 683 | def getPrettyPageName(self): |
paul@347 | 684 | |
paul@347 | 685 | "Return a nicely formatted title/name for this page." |
paul@347 | 686 | |
paul@347 | 687 | return getPrettyPageName(self.page) |
paul@347 | 688 | |
paul@347 | 689 | def getBody(self): |
paul@347 | 690 | |
paul@347 | 691 | "Get the current page body." |
paul@347 | 692 | |
paul@347 | 693 | if self.body is None: |
paul@347 | 694 | self.body = self.page.get_raw_body() |
paul@347 | 695 | return self.body |
paul@347 | 696 | |
paul@347 | 697 | def getEvents(self): |
paul@347 | 698 | |
paul@347 | 699 | "Return a list of events from this page." |
paul@347 | 700 | |
paul@347 | 701 | if self.events is None: |
paul@428 | 702 | self.events = parseEventsInPage(self.page.data, self).getEvents() |
paul@347 | 703 | |
paul@347 | 704 | return self.events |
paul@347 | 705 | |
paul@347 | 706 | def setEvents(self, events): |
paul@347 | 707 | |
paul@347 | 708 | "Set the given 'events' on this page." |
paul@347 | 709 | |
paul@347 | 710 | self.events = events |
paul@347 | 711 | |
paul@347 | 712 | def getCategoryMembership(self): |
paul@347 | 713 | |
paul@347 | 714 | "Get the category names from this page." |
paul@347 | 715 | |
paul@347 | 716 | if self.categories is None: |
paul@347 | 717 | body = self.getBody() |
paul@347 | 718 | match = category_membership_regexp.search(body) |
paul@347 | 719 | self.categories = match and [x for x in match.groups() if x] or [] |
paul@347 | 720 | |
paul@347 | 721 | return self.categories |
paul@347 | 722 | |
paul@347 | 723 | def setCategoryMembership(self, category_names): |
paul@347 | 724 | |
paul@347 | 725 | """ |
paul@347 | 726 | Set the category membership for the page using the specified |
paul@347 | 727 | 'category_names'. |
paul@347 | 728 | """ |
paul@347 | 729 | |
paul@347 | 730 | self.categories = category_names |
paul@347 | 731 | |
paul@347 | 732 | def flushEventDetails(self): |
paul@347 | 733 | |
paul@347 | 734 | "Flush the current event details to this page's body text." |
paul@347 | 735 | |
paul@347 | 736 | new_body_parts = [] |
paul@347 | 737 | end_of_last_match = 0 |
paul@347 | 738 | body = self.getBody() |
paul@347 | 739 | |
paul@347 | 740 | events = iter(self.getEvents()) |
paul@347 | 741 | |
paul@347 | 742 | event = events.next() |
paul@347 | 743 | event_details = event.getDetails() |
paul@347 | 744 | replaced_terms = set() |
paul@347 | 745 | |
paul@347 | 746 | for match in definition_list_regexp.finditer(body): |
paul@347 | 747 | |
paul@347 | 748 | # Permit case-insensitive list terms. |
paul@347 | 749 | |
paul@347 | 750 | term = match.group("term").lower() |
paul@347 | 751 | desc = match.group("desc") |
paul@347 | 752 | |
paul@347 | 753 | # Check that the term has not already been substituted. If so, |
paul@347 | 754 | # get the next event. |
paul@347 | 755 | |
paul@347 | 756 | if term in replaced_terms: |
paul@347 | 757 | try: |
paul@347 | 758 | event = events.next() |
paul@347 | 759 | |
paul@347 | 760 | # No more events. |
paul@347 | 761 | |
paul@347 | 762 | except StopIteration: |
paul@347 | 763 | break |
paul@347 | 764 | |
paul@347 | 765 | event_details = event.getDetails() |
paul@347 | 766 | replaced_terms = set() |
paul@347 | 767 | |
paul@347 | 768 | # Add preceding text to the new body. |
paul@347 | 769 | |
paul@347 | 770 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@347 | 771 | |
paul@347 | 772 | # Get the matching regions, adding the term to the new body. |
paul@347 | 773 | |
paul@347 | 774 | new_body_parts.append(match.group("wholeterm")) |
paul@347 | 775 | |
paul@347 | 776 | # Special value type handling. |
paul@347 | 777 | |
paul@347 | 778 | if event_details.has_key(term): |
paul@347 | 779 | |
paul@347 | 780 | # Dates. |
paul@347 | 781 | |
paul@347 | 782 | if term in event.date_terms: |
paul@347 | 783 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@347 | 784 | |
paul@347 | 785 | # Lists (whose elements may be quoted). |
paul@347 | 786 | |
paul@347 | 787 | elif term in event.list_terms: |
paul@347 | 788 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@347 | 789 | |
paul@347 | 790 | # Labels which must be quoted. |
paul@347 | 791 | |
paul@347 | 792 | elif term in event.title_terms: |
paul@347 | 793 | desc = getEncodedWikiText(event_details[term]) |
paul@347 | 794 | |
paul@347 | 795 | # Position details. |
paul@347 | 796 | |
paul@347 | 797 | elif term == "geo": |
paul@347 | 798 | desc = " ".join(map(str, event_details[term])) |
paul@347 | 799 | |
paul@347 | 800 | # Text which need not be quoted, but it will be Wiki text. |
paul@347 | 801 | |
paul@347 | 802 | elif term in event.other_terms: |
paul@347 | 803 | desc = event_details[term] |
paul@347 | 804 | |
paul@347 | 805 | replaced_terms.add(term) |
paul@347 | 806 | |
paul@347 | 807 | # Add the replaced value. |
paul@347 | 808 | |
paul@347 | 809 | new_body_parts.append(desc) |
paul@347 | 810 | |
paul@347 | 811 | # Remember where in the page has been processed. |
paul@347 | 812 | |
paul@347 | 813 | end_of_last_match = match.end() |
paul@347 | 814 | |
paul@347 | 815 | # Write the rest of the page. |
paul@347 | 816 | |
paul@347 | 817 | new_body_parts.append(body[end_of_last_match:]) |
paul@347 | 818 | |
paul@347 | 819 | self.body = "".join(new_body_parts) |
paul@347 | 820 | |
paul@347 | 821 | def flushCategoryMembership(self): |
paul@347 | 822 | |
paul@347 | 823 | "Flush the category membership to the page body." |
paul@347 | 824 | |
paul@347 | 825 | body = self.getBody() |
paul@347 | 826 | category_names = self.getCategoryMembership() |
paul@347 | 827 | match = category_membership_regexp.search(body) |
paul@347 | 828 | |
paul@347 | 829 | if match: |
paul@347 | 830 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@347 | 831 | |
paul@347 | 832 | def saveChanges(self): |
paul@347 | 833 | |
paul@347 | 834 | "Save changes to the event." |
paul@347 | 835 | |
paul@347 | 836 | self.flushEventDetails() |
paul@347 | 837 | self.flushCategoryMembership() |
paul@347 | 838 | self.page.saveText(self.getBody(), 0) |
paul@347 | 839 | |
paul@347 | 840 | def linkToPage(self, request, text, query_string=None, anchor=None): |
paul@347 | 841 | |
paul@347 | 842 | """ |
paul@347 | 843 | Using 'request', return a link to this page with the given link 'text' |
paul@347 | 844 | and optional 'query_string' and 'anchor'. |
paul@347 | 845 | """ |
paul@347 | 846 | |
paul@347 | 847 | return linkToPage(request, self.page, text, query_string, anchor) |
paul@347 | 848 | |
paul@347 | 849 | # Formatting-related functions. |
paul@347 | 850 | |
paul@347 | 851 | def getParserClass(self, format): |
paul@347 | 852 | |
paul@347 | 853 | """ |
paul@347 | 854 | Return a parser class for the given 'format', returning a plain text |
paul@347 | 855 | parser if no parser can be found for the specified 'format'. |
paul@347 | 856 | """ |
paul@347 | 857 | |
paul@347 | 858 | return getParserClass(self.page.request, format) |
paul@347 | 859 | |
paul@347 | 860 | def formatText(self, text, fmt): |
paul@347 | 861 | |
paul@347 | 862 | """ |
paul@347 | 863 | Format the given 'text' using the specified formatter 'fmt'. |
paul@347 | 864 | """ |
paul@347 | 865 | |
paul@347 | 866 | fmt.page = page = self.page |
paul@347 | 867 | request = page.request |
paul@347 | 868 | |
paul@395 | 869 | if self.getFormat() == "calendar": |
paul@395 | 870 | parser_cls = RawParser |
paul@395 | 871 | else: |
paul@395 | 872 | parser_cls = self.getParserClass(self.getFormat()) |
paul@395 | 873 | |
paul@347 | 874 | return formatText(text, request, fmt, parser_cls) |
paul@347 | 875 | |
paul@347 | 876 | # Event details. |
paul@347 | 877 | |
paul@347 | 878 | class Event(ActsAsTimespan): |
paul@347 | 879 | |
paul@347 | 880 | "A description of an event." |
paul@347 | 881 | |
paul@347 | 882 | title_terms = "title", "summary" |
paul@347 | 883 | date_terms = "start", "end" |
paul@347 | 884 | list_terms = "topics", "categories" |
paul@347 | 885 | other_terms = "description", "location", "link" |
paul@347 | 886 | geo_terms = "geo", |
paul@347 | 887 | all_terms = title_terms + date_terms + list_terms + other_terms + geo_terms |
paul@347 | 888 | |
paul@347 | 889 | def __init__(self, page, details, raw_details=None): |
paul@347 | 890 | self.page = page |
paul@347 | 891 | self.details = details |
paul@366 | 892 | self.raw_details = raw_details or {} |
paul@347 | 893 | |
paul@347 | 894 | # Permit omission of the end of the event by duplicating the start. |
paul@347 | 895 | |
paul@347 | 896 | if self.details.has_key("start") and not self.details.get("end"): |
paul@347 | 897 | end = self.details["start"] |
paul@347 | 898 | |
paul@347 | 899 | # Make any end time refer to the day instead. |
paul@347 | 900 | |
paul@347 | 901 | if isinstance(end, DateTime): |
paul@347 | 902 | end = end.as_date() |
paul@347 | 903 | |
paul@347 | 904 | self.details["end"] = end |
paul@347 | 905 | |
paul@347 | 906 | def __repr__(self): |
paul@347 | 907 | return "<Event %r %r>" % (self.getSummary(), self.as_limits()) |
paul@347 | 908 | |
paul@347 | 909 | def __hash__(self): |
paul@347 | 910 | |
paul@347 | 911 | """ |
paul@347 | 912 | Return a dictionary hash, avoiding mistaken equality of events in some |
paul@347 | 913 | situations (notably membership tests) by including the URL as well as |
paul@347 | 914 | the summary. |
paul@347 | 915 | """ |
paul@347 | 916 | |
paul@347 | 917 | return hash(self.getSummary() + self.getEventURL()) |
paul@347 | 918 | |
paul@347 | 919 | def getPage(self): |
paul@347 | 920 | |
paul@347 | 921 | "Return the page describing this event." |
paul@347 | 922 | |
paul@347 | 923 | return self.page |
paul@347 | 924 | |
paul@347 | 925 | def setPage(self, page): |
paul@347 | 926 | |
paul@347 | 927 | "Set the 'page' describing this event." |
paul@347 | 928 | |
paul@347 | 929 | self.page = page |
paul@347 | 930 | |
paul@347 | 931 | def getEventURL(self): |
paul@347 | 932 | |
paul@347 | 933 | "Return the URL of this event." |
paul@347 | 934 | |
paul@347 | 935 | fragment = self.details.get("fragment") |
paul@347 | 936 | return self.page.getPageURL() + (fragment and "#" + fragment or "") |
paul@347 | 937 | |
paul@347 | 938 | def linkToEvent(self, request, text, query_string=None): |
paul@347 | 939 | |
paul@347 | 940 | """ |
paul@347 | 941 | Using 'request', return a link to this event with the given link 'text' |
paul@347 | 942 | and optional 'query_string'. |
paul@347 | 943 | """ |
paul@347 | 944 | |
paul@347 | 945 | return self.page.linkToPage(request, text, query_string, self.details.get("fragment")) |
paul@347 | 946 | |
paul@347 | 947 | def getMetadata(self): |
paul@347 | 948 | |
paul@347 | 949 | """ |
paul@347 | 950 | Return a dictionary containing items describing the event's "created" |
paul@347 | 951 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 952 | "last-comment" made about the last edit. |
paul@347 | 953 | """ |
paul@347 | 954 | |
paul@347 | 955 | # Delegate this to the page. |
paul@347 | 956 | |
paul@347 | 957 | return self.page.getMetadata() |
paul@347 | 958 | |
paul@347 | 959 | def getSummary(self, event_parent=None): |
paul@347 | 960 | |
paul@347 | 961 | """ |
paul@347 | 962 | Return either the given title or summary of the event according to the |
paul@347 | 963 | event details, or a summary made from using the pretty version of the |
paul@347 | 964 | page name. |
paul@347 | 965 | |
paul@347 | 966 | If the optional 'event_parent' is specified, any page beneath the given |
paul@347 | 967 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@347 | 968 | if its name is used as the summary. |
paul@347 | 969 | """ |
paul@347 | 970 | |
paul@347 | 971 | event_details = self.details |
paul@347 | 972 | |
paul@347 | 973 | if event_details.has_key("title"): |
paul@347 | 974 | return event_details["title"] |
paul@347 | 975 | elif event_details.has_key("summary"): |
paul@347 | 976 | return event_details["summary"] |
paul@347 | 977 | else: |
paul@347 | 978 | # If appropriate, remove the parent details and "/" character. |
paul@347 | 979 | |
paul@347 | 980 | title = self.page.getPageName() |
paul@347 | 981 | |
paul@347 | 982 | if event_parent and title.startswith(event_parent): |
paul@347 | 983 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@347 | 984 | |
paul@347 | 985 | return getPrettyTitle(title) |
paul@347 | 986 | |
paul@347 | 987 | def getDetails(self): |
paul@347 | 988 | |
paul@347 | 989 | "Return the details for this event." |
paul@347 | 990 | |
paul@347 | 991 | return self.details |
paul@347 | 992 | |
paul@347 | 993 | def setDetails(self, event_details): |
paul@347 | 994 | |
paul@347 | 995 | "Set the 'event_details' for this event." |
paul@347 | 996 | |
paul@347 | 997 | self.details = event_details |
paul@347 | 998 | |
paul@347 | 999 | def getRawDetails(self): |
paul@347 | 1000 | |
paul@347 | 1001 | "Return the details for this event as they were written in a page." |
paul@347 | 1002 | |
paul@347 | 1003 | return self.raw_details |
paul@347 | 1004 | |
paul@347 | 1005 | # Timespan-related methods. |
paul@347 | 1006 | |
paul@347 | 1007 | def __contains__(self, other): |
paul@347 | 1008 | return self == other |
paul@347 | 1009 | |
paul@347 | 1010 | def __eq__(self, other): |
paul@347 | 1011 | if isinstance(other, Event): |
paul@347 | 1012 | return self.getSummary() == other.getSummary() and self.getEventURL() == other.getEventURL() and self._cmp(other) |
paul@347 | 1013 | else: |
paul@347 | 1014 | return self._cmp(other) == 0 |
paul@347 | 1015 | |
paul@347 | 1016 | def __ne__(self, other): |
paul@347 | 1017 | return not self.__eq__(other) |
paul@347 | 1018 | |
paul@347 | 1019 | def __lt__(self, other): |
paul@347 | 1020 | return self._cmp(other) == -1 |
paul@347 | 1021 | |
paul@347 | 1022 | def __le__(self, other): |
paul@347 | 1023 | return self._cmp(other) in (-1, 0) |
paul@347 | 1024 | |
paul@347 | 1025 | def __gt__(self, other): |
paul@347 | 1026 | return self._cmp(other) == 1 |
paul@347 | 1027 | |
paul@347 | 1028 | def __ge__(self, other): |
paul@347 | 1029 | return self._cmp(other) in (0, 1) |
paul@347 | 1030 | |
paul@347 | 1031 | def _cmp(self, other): |
paul@347 | 1032 | |
paul@347 | 1033 | "Compare this event to an 'other' event purely by their timespans." |
paul@347 | 1034 | |
paul@347 | 1035 | if isinstance(other, Event): |
paul@347 | 1036 | return cmp(self.as_timespan(), other.as_timespan()) |
paul@347 | 1037 | else: |
paul@347 | 1038 | return cmp(self.as_timespan(), other) |
paul@347 | 1039 | |
paul@347 | 1040 | def as_timespan(self): |
paul@347 | 1041 | details = self.details |
paul@347 | 1042 | if details.has_key("start") and details.has_key("end"): |
paul@347 | 1043 | return Timespan(details["start"], details["end"]) |
paul@347 | 1044 | else: |
paul@347 | 1045 | return None |
paul@347 | 1046 | |
paul@347 | 1047 | def as_limits(self): |
paul@347 | 1048 | ts = self.as_timespan() |
paul@347 | 1049 | return ts and ts.as_limits() |
paul@347 | 1050 | |
paul@347 | 1051 | class CalendarEvent(Event): |
paul@347 | 1052 | |
paul@347 | 1053 | "An event from a remote calendar." |
paul@347 | 1054 | |
paul@347 | 1055 | def getEventURL(self): |
paul@347 | 1056 | |
paul@354 | 1057 | """ |
paul@354 | 1058 | Return the URL of this event, fixing any misinterpreted or incorrectly |
paul@354 | 1059 | formatted value in the event definition or returning the resource URL in |
paul@354 | 1060 | the absence of any URL in the event details. |
paul@354 | 1061 | """ |
paul@354 | 1062 | |
paul@413 | 1063 | # NOTE: Redirect empty URLs to an action showing the resource details. |
paul@413 | 1064 | |
paul@354 | 1065 | return self.details.get("url") and \ |
paul@354 | 1066 | self.valueToString(self.details["url"]) or \ |
paul@354 | 1067 | self.page.getPageURL() |
paul@354 | 1068 | |
paul@354 | 1069 | def getSummary(self, event_parent=None): |
paul@347 | 1070 | |
paul@354 | 1071 | """ |
paul@354 | 1072 | Return the event summary, fixing any misinterpreted or incorrectly |
paul@354 | 1073 | formatted value in the event definition. |
paul@354 | 1074 | """ |
paul@354 | 1075 | |
paul@354 | 1076 | return self.valueToString(self.details["summary"]) |
paul@354 | 1077 | |
paul@354 | 1078 | def valueToString(self, value): |
paul@354 | 1079 | |
paul@354 | 1080 | "Return the given 'value' converted to a string." |
paul@354 | 1081 | |
paul@354 | 1082 | if isinstance(value, list): |
paul@354 | 1083 | return ",".join(value) |
paul@354 | 1084 | elif isinstance(value, tuple): |
paul@354 | 1085 | return ";".join(value) |
paul@354 | 1086 | else: |
paul@354 | 1087 | return value |
paul@347 | 1088 | |
paul@347 | 1089 | def linkToEvent(self, request, text, query_string=None, anchor=None): |
paul@347 | 1090 | |
paul@347 | 1091 | """ |
paul@347 | 1092 | Using 'request', return a link to this event with the given link 'text' |
paul@347 | 1093 | and optional 'query_string' and 'anchor'. |
paul@347 | 1094 | """ |
paul@347 | 1095 | |
paul@347 | 1096 | return linkToResource(self.getEventURL(), request, text, query_string, anchor) |
paul@347 | 1097 | |
paul@347 | 1098 | def getMetadata(self): |
paul@347 | 1099 | |
paul@347 | 1100 | """ |
paul@347 | 1101 | Return a dictionary containing items describing the event's "created" |
paul@347 | 1102 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@347 | 1103 | "last-comment" made about the last edit. |
paul@347 | 1104 | """ |
paul@347 | 1105 | |
paul@353 | 1106 | metadata = self.page.getMetadata() |
paul@353 | 1107 | |
paul@347 | 1108 | return { |
paul@353 | 1109 | "created" : self.details.get("created") or self.details.get("dtstamp") or metadata["created"], |
paul@353 | 1110 | "last-modified" : self.details.get("last-modified") or self.details.get("dtstamp") or metadata["last-modified"], |
paul@347 | 1111 | "sequence" : self.details.get("sequence") or 0, |
paul@347 | 1112 | "last-comment" : "" |
paul@347 | 1113 | } |
paul@347 | 1114 | |
paul@347 | 1115 | # vim: tabstop=4 expandtab shiftwidth=4 |