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