paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregator library |
paul@10 | 4 | |
paul@277 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@10 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@10 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@10 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@10 | 9 | """ |
paul@10 | 10 | |
paul@277 | 11 | from LocationSupport import * |
paul@277 | 12 | from MoinDateSupport import * |
paul@277 | 13 | from MoinSupport import * |
paul@277 | 14 | |
paul@10 | 15 | from MoinMoin.Page import Page |
paul@225 | 16 | from MoinMoin.action import cache |
paul@225 | 17 | from MoinMoin import caching |
paul@277 | 18 | from MoinMoin import search |
paul@24 | 19 | from MoinMoin import wikiutil |
paul@277 | 20 | |
paul@217 | 21 | import codecs |
paul@24 | 22 | import time |
paul@10 | 23 | import re |
paul@265 | 24 | import urllib, urllib2 |
paul@10 | 25 | |
paul@69 | 26 | try: |
paul@225 | 27 | from cStringIO import StringIO |
paul@225 | 28 | except ImportError: |
paul@225 | 29 | from StringIO import StringIO |
paul@10 | 30 | |
paul@69 | 31 | try: |
paul@69 | 32 | set |
paul@69 | 33 | except NameError: |
paul@69 | 34 | from sets import Set as set |
paul@69 | 35 | |
paul@100 | 36 | try: |
paul@217 | 37 | import vCalendar |
paul@217 | 38 | except ImportError: |
paul@217 | 39 | vCalendar = None |
paul@217 | 40 | |
paul@137 | 41 | escape = wikiutil.escape |
paul@137 | 42 | |
paul@277 | 43 | __version__ = "0.9" |
paul@22 | 44 | |
paul@10 | 45 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 46 | |
paul@10 | 47 | category_regexp = None |
paul@47 | 48 | |
paul@47 | 49 | # Page parsing. |
paul@47 | 50 | |
paul@84 | 51 | definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?):: )(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@158 | 52 | category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE) |
paul@47 | 53 | |
paul@47 | 54 | # Value parsing. |
paul@47 | 55 | |
paul@93 | 56 | country_code_regexp = re.compile(ur'(?:^|\W)(?P<code>[A-Z]{2})(?:$|\W+$)', re.UNICODE) |
paul@219 | 57 | |
paul@217 | 58 | # Simple content parsing. |
paul@92 | 59 | |
paul@19 | 60 | verbatim_regexp = re.compile(ur'(?:' |
paul@19 | 61 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@19 | 62 | ur'|' |
paul@19 | 63 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@19 | 64 | ur'|' |
paul@191 | 65 | ur'!(?P<verbatim3>.*?)(\s|$)?' |
paul@191 | 66 | ur'|' |
paul@19 | 67 | ur'`(?P<monospace>.*?)`' |
paul@19 | 68 | ur'|' |
paul@19 | 69 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@19 | 70 | ur')', re.UNICODE) |
paul@10 | 71 | |
paul@10 | 72 | # Utility functions. |
paul@10 | 73 | |
paul@10 | 74 | def getCategoryPattern(request): |
paul@10 | 75 | global category_regexp |
paul@10 | 76 | |
paul@10 | 77 | try: |
paul@10 | 78 | return request.cfg.cache.page_category_regexact |
paul@10 | 79 | except AttributeError: |
paul@10 | 80 | |
paul@10 | 81 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 82 | |
paul@10 | 83 | if category_regexp is None: |
paul@10 | 84 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 85 | return category_regexp |
paul@10 | 86 | |
paul@233 | 87 | def getWikiDict(pagename, request): |
paul@233 | 88 | if Page(request, pagename).exists() and request.user.may.read(pagename): |
paul@233 | 89 | if hasattr(request.dicts, "dict"): |
paul@233 | 90 | return request.dicts.dict(pagename) |
paul@233 | 91 | else: |
paul@233 | 92 | return request.dicts[pagename] |
paul@233 | 93 | else: |
paul@233 | 94 | return None |
paul@233 | 95 | |
paul@229 | 96 | def to_list(s, sep): |
paul@229 | 97 | return [x.strip() for x in s.split(sep) if x.strip()] |
paul@229 | 98 | |
paul@118 | 99 | def sort_none_first(x, y): |
paul@118 | 100 | if x is None: |
paul@118 | 101 | return -1 |
paul@118 | 102 | elif y is None: |
paul@118 | 103 | return 1 |
paul@118 | 104 | else: |
paul@118 | 105 | return cmp(x, y) |
paul@118 | 106 | |
paul@195 | 107 | def sort_start_first(x, y): |
paul@195 | 108 | x_ts = x.as_limits() |
paul@195 | 109 | if x_ts is not None: |
paul@195 | 110 | x_start, x_end = x_ts |
paul@195 | 111 | y_ts = y.as_limits() |
paul@195 | 112 | if y_ts is not None: |
paul@195 | 113 | y_start, y_end = y_ts |
paul@195 | 114 | start_order = cmp(x_start, y_start) |
paul@195 | 115 | if start_order == 0: |
paul@195 | 116 | return cmp(x_end, y_end) |
paul@195 | 117 | else: |
paul@195 | 118 | return start_order |
paul@195 | 119 | return 0 |
paul@195 | 120 | |
paul@109 | 121 | # Utility classes and associated functions. |
paul@109 | 122 | |
paul@277 | 123 | class ActionSupport(ActionSupport): |
paul@109 | 124 | |
paul@277 | 125 | "Extend the generic action support." |
paul@112 | 126 | |
paul@114 | 127 | def get_month_lists(self, default_as_current=0): |
paul@112 | 128 | |
paul@112 | 129 | """ |
paul@112 | 130 | Return two lists of HTML element definitions corresponding to the start |
paul@112 | 131 | and end month selection controls, with months selected according to any |
paul@112 | 132 | values that have been specified via request parameters. |
paul@112 | 133 | """ |
paul@112 | 134 | |
paul@112 | 135 | _ = self._ |
paul@112 | 136 | form = self.get_form() |
paul@112 | 137 | |
paul@112 | 138 | # Initialise month lists. |
paul@112 | 139 | |
paul@112 | 140 | start_month_list = [] |
paul@112 | 141 | end_month_list = [] |
paul@112 | 142 | |
paul@114 | 143 | start_month = self._get_input(form, "start-month", default_as_current and getCurrentMonth().month() or None) |
paul@112 | 144 | end_month = self._get_input(form, "end-month", start_month) |
paul@112 | 145 | |
paul@112 | 146 | # Prepare month lists, selecting specified months. |
paul@112 | 147 | |
paul@114 | 148 | if not default_as_current: |
paul@114 | 149 | start_month_list.append('<option value=""></option>') |
paul@114 | 150 | end_month_list.append('<option value=""></option>') |
paul@114 | 151 | |
paul@112 | 152 | for month in range(1, 13): |
paul@136 | 153 | month_label = escape(_(getMonthLabel(month))) |
paul@112 | 154 | selected = self._get_selected(month, start_month) |
paul@112 | 155 | start_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 156 | selected = self._get_selected(month, end_month) |
paul@112 | 157 | end_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@112 | 158 | |
paul@112 | 159 | return start_month_list, end_month_list |
paul@112 | 160 | |
paul@114 | 161 | def get_year_defaults(self, default_as_current=0): |
paul@112 | 162 | |
paul@112 | 163 | "Return defaults for the start and end years." |
paul@112 | 164 | |
paul@112 | 165 | form = self.get_form() |
paul@112 | 166 | |
paul@114 | 167 | start_year_default = form.get("start-year", [default_as_current and getCurrentYear() or ""])[0] |
paul@114 | 168 | end_year_default = form.get("end-year", [default_as_current and start_year_default or ""])[0] |
paul@112 | 169 | |
paul@112 | 170 | return start_year_default, end_year_default |
paul@112 | 171 | |
paul@127 | 172 | def get_day_defaults(self, default_as_current=0): |
paul@127 | 173 | |
paul@127 | 174 | "Return defaults for the start and end days." |
paul@127 | 175 | |
paul@127 | 176 | form = self.get_form() |
paul@127 | 177 | |
paul@127 | 178 | start_day_default = form.get("start-day", [default_as_current and getCurrentDate().day() or ""])[0] |
paul@127 | 179 | end_day_default = form.get("end-day", [default_as_current and start_day_default or ""])[0] |
paul@127 | 180 | |
paul@127 | 181 | return start_day_default, end_day_default |
paul@127 | 182 | |
paul@67 | 183 | # Textual representations. |
paul@67 | 184 | |
paul@67 | 185 | def getSimpleWikiText(text): |
paul@67 | 186 | |
paul@67 | 187 | """ |
paul@67 | 188 | Return the plain text representation of the given 'text' which may employ |
paul@67 | 189 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@67 | 190 | text. |
paul@67 | 191 | """ |
paul@67 | 192 | |
paul@67 | 193 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@67 | 194 | |
paul@67 | 195 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@67 | 196 | |
paul@67 | 197 | def getEncodedWikiText(text): |
paul@67 | 198 | |
paul@67 | 199 | "Encode the given 'text' in a verbatim representation." |
paul@67 | 200 | |
paul@67 | 201 | return "<<Verbatim(%s)>>" % text |
paul@67 | 202 | |
paul@67 | 203 | def getPrettyTitle(title): |
paul@67 | 204 | |
paul@67 | 205 | "Return a nicely formatted version of the given 'title'." |
paul@67 | 206 | |
paul@67 | 207 | return title.replace("_", " ").replace("/", u" ? ") |
paul@67 | 208 | |
paul@67 | 209 | # Category discovery and searching. |
paul@67 | 210 | |
paul@19 | 211 | def getCategories(request): |
paul@19 | 212 | |
paul@19 | 213 | """ |
paul@19 | 214 | From the AdvancedSearch macro, return a list of category page names using |
paul@19 | 215 | the given 'request'. |
paul@19 | 216 | """ |
paul@19 | 217 | |
paul@19 | 218 | # This will return all pages with "Category" in the title. |
paul@19 | 219 | |
paul@19 | 220 | cat_filter = getCategoryPattern(request).search |
paul@19 | 221 | return request.rootpage.getPageList(filter=cat_filter) |
paul@19 | 222 | |
paul@19 | 223 | def getCategoryMapping(category_pagenames, request): |
paul@19 | 224 | |
paul@19 | 225 | """ |
paul@19 | 226 | For the given 'category_pagenames' return a list of tuples of the form |
paul@19 | 227 | (category name, category page name) using the given 'request'. |
paul@19 | 228 | """ |
paul@19 | 229 | |
paul@19 | 230 | cat_pattern = getCategoryPattern(request) |
paul@19 | 231 | mapping = [] |
paul@19 | 232 | for pagename in category_pagenames: |
paul@19 | 233 | name = cat_pattern.match(pagename).group("key") |
paul@19 | 234 | if name != "Category": |
paul@19 | 235 | mapping.append((name, pagename)) |
paul@19 | 236 | mapping.sort() |
paul@19 | 237 | return mapping |
paul@19 | 238 | |
paul@67 | 239 | def getCategoryPages(pagename, request): |
paul@29 | 240 | |
paul@67 | 241 | """ |
paul@67 | 242 | Return the pages associated with the given category 'pagename' using the |
paul@67 | 243 | 'request'. |
paul@67 | 244 | """ |
paul@10 | 245 | |
paul@10 | 246 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@117 | 247 | results = search.searchPages(request, query, "page_name") |
paul@10 | 248 | |
paul@10 | 249 | cat_pattern = getCategoryPattern(request) |
paul@10 | 250 | pages = [] |
paul@10 | 251 | for page in results.hits: |
paul@10 | 252 | if not cat_pattern.match(page.page_name): |
paul@10 | 253 | pages.append(page) |
paul@10 | 254 | return pages |
paul@10 | 255 | |
paul@168 | 256 | def getAllCategoryPages(category_names, request): |
paul@168 | 257 | |
paul@168 | 258 | """ |
paul@168 | 259 | Return all pages belonging to the categories having the given |
paul@168 | 260 | 'category_names', using the given 'request'. |
paul@168 | 261 | """ |
paul@168 | 262 | |
paul@168 | 263 | pages = [] |
paul@168 | 264 | pagenames = set() |
paul@168 | 265 | |
paul@168 | 266 | for category_name in category_names: |
paul@168 | 267 | |
paul@168 | 268 | # Get the pages and page names in the category. |
paul@168 | 269 | |
paul@168 | 270 | pages_in_category = getCategoryPages(category_name, request) |
paul@168 | 271 | |
paul@168 | 272 | # Visit each page in the category. |
paul@168 | 273 | |
paul@168 | 274 | for page_in_category in pages_in_category: |
paul@168 | 275 | pagename = page_in_category.page_name |
paul@168 | 276 | |
paul@168 | 277 | # Only process each page once. |
paul@168 | 278 | |
paul@168 | 279 | if pagename in pagenames: |
paul@168 | 280 | continue |
paul@168 | 281 | else: |
paul@168 | 282 | pagenames.add(pagename) |
paul@168 | 283 | |
paul@168 | 284 | pages.append(page_in_category) |
paul@168 | 285 | |
paul@168 | 286 | return pages |
paul@168 | 287 | |
paul@168 | 288 | def getPagesFromResults(result_pages, request): |
paul@168 | 289 | |
paul@168 | 290 | "Return genuine pages for the given 'result_pages' using the 'request'." |
paul@168 | 291 | |
paul@168 | 292 | return [Page(request, page.page_name) for page in result_pages] |
paul@168 | 293 | |
paul@217 | 294 | # Event resources providing collections of events. |
paul@217 | 295 | |
paul@217 | 296 | class EventResource: |
paul@217 | 297 | |
paul@217 | 298 | "A resource providing event information." |
paul@217 | 299 | |
paul@217 | 300 | def __init__(self, url): |
paul@217 | 301 | self.url = url |
paul@217 | 302 | |
paul@223 | 303 | def getPageURL(self): |
paul@223 | 304 | |
paul@223 | 305 | "Return the URL of this page." |
paul@217 | 306 | |
paul@217 | 307 | return self.url |
paul@217 | 308 | |
paul@217 | 309 | def getFormat(self): |
paul@217 | 310 | |
paul@219 | 311 | "Get the format used by this resource." |
paul@217 | 312 | |
paul@217 | 313 | return "plain" |
paul@217 | 314 | |
paul@219 | 315 | def getMetadata(self): |
paul@219 | 316 | |
paul@219 | 317 | """ |
paul@219 | 318 | Return a dictionary containing items describing the page's "created" |
paul@219 | 319 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 320 | "last-comment" made about the last edit. |
paul@219 | 321 | """ |
paul@219 | 322 | |
paul@219 | 323 | return {} |
paul@219 | 324 | |
paul@217 | 325 | def getEvents(self): |
paul@217 | 326 | |
paul@217 | 327 | "Return a list of events from this resource." |
paul@217 | 328 | |
paul@217 | 329 | return [] |
paul@217 | 330 | |
paul@217 | 331 | def linkToPage(self, request, text, query_string=None): |
paul@217 | 332 | |
paul@217 | 333 | """ |
paul@217 | 334 | Using 'request', return a link to this page with the given link 'text' |
paul@217 | 335 | and optional 'query_string'. |
paul@217 | 336 | """ |
paul@217 | 337 | |
paul@219 | 338 | return linkToResource(self.url, request, text, query_string) |
paul@217 | 339 | |
paul@217 | 340 | # Formatting-related functions. |
paul@217 | 341 | |
paul@217 | 342 | def formatText(self, text, request, fmt): |
paul@217 | 343 | |
paul@217 | 344 | """ |
paul@217 | 345 | Format the given 'text' using the specified 'request' and formatter |
paul@217 | 346 | 'fmt'. |
paul@217 | 347 | """ |
paul@217 | 348 | |
paul@217 | 349 | # Assume plain text which is then formatted appropriately. |
paul@217 | 350 | |
paul@217 | 351 | return fmt.text(text) |
paul@217 | 352 | |
paul@217 | 353 | class EventCalendar(EventResource): |
paul@217 | 354 | |
paul@217 | 355 | "An iCalendar resource." |
paul@217 | 356 | |
paul@217 | 357 | def __init__(self, url, calendar): |
paul@217 | 358 | EventResource.__init__(self, url) |
paul@217 | 359 | self.calendar = calendar |
paul@217 | 360 | self.events = None |
paul@217 | 361 | |
paul@217 | 362 | def getEvents(self): |
paul@217 | 363 | |
paul@217 | 364 | "Return a list of events from this resource." |
paul@217 | 365 | |
paul@217 | 366 | if self.events is None: |
paul@217 | 367 | self.events = [] |
paul@217 | 368 | |
paul@217 | 369 | _calendar, _empty, calendar = self.calendar |
paul@217 | 370 | |
paul@217 | 371 | for objtype, attrs, obj in calendar: |
paul@217 | 372 | |
paul@217 | 373 | # Read events. |
paul@217 | 374 | |
paul@217 | 375 | if objtype == "VEVENT": |
paul@217 | 376 | details = {} |
paul@217 | 377 | |
paul@217 | 378 | for property, attrs, value in obj: |
paul@217 | 379 | |
paul@217 | 380 | # Convert dates. |
paul@217 | 381 | |
paul@219 | 382 | if property in ("DTSTART", "DTEND", "CREATED", "DTSTAMP", "LAST-MODIFIED"): |
paul@219 | 383 | if property in ("DTSTART", "DTEND"): |
paul@219 | 384 | property = property[2:] |
paul@217 | 385 | if attrs.get("VALUE") == "DATE": |
paul@217 | 386 | value = getDateFromCalendar(value) |
paul@217 | 387 | else: |
paul@217 | 388 | value = getDateTimeFromCalendar(value) |
paul@217 | 389 | |
paul@219 | 390 | # Convert numeric data. |
paul@219 | 391 | |
paul@219 | 392 | elif property == "SEQUENCE": |
paul@219 | 393 | value = int(value) |
paul@219 | 394 | |
paul@219 | 395 | # Convert lists. |
paul@219 | 396 | |
paul@219 | 397 | elif property == "CATEGORIES": |
paul@229 | 398 | value = to_list(value, ",") |
paul@229 | 399 | |
paul@229 | 400 | # Convert positions (using decimal values). |
paul@229 | 401 | |
paul@229 | 402 | elif property == "GEO": |
paul@237 | 403 | try: |
paul@237 | 404 | value = map(getMapReferenceFromDecimal, to_list(value, ";")) |
paul@246 | 405 | if len(value) != 2: |
paul@246 | 406 | continue |
paul@237 | 407 | except (KeyError, ValueError): |
paul@237 | 408 | continue |
paul@219 | 409 | |
paul@217 | 410 | # Accept other textual data as it is. |
paul@217 | 411 | |
paul@219 | 412 | elif property in ("LOCATION", "SUMMARY", "URL"): |
paul@257 | 413 | value = value or None |
paul@219 | 414 | |
paul@219 | 415 | # Ignore other properties. |
paul@217 | 416 | |
paul@217 | 417 | else: |
paul@217 | 418 | continue |
paul@217 | 419 | |
paul@219 | 420 | property = property.lower() |
paul@217 | 421 | details[property] = value |
paul@217 | 422 | |
paul@219 | 423 | self.events.append(CalendarEvent(self, details)) |
paul@217 | 424 | |
paul@217 | 425 | return self.events |
paul@67 | 426 | |
paul@67 | 427 | class EventPage: |
paul@67 | 428 | |
paul@229 | 429 | "An event page acting as an event resource." |
paul@67 | 430 | |
paul@67 | 431 | def __init__(self, page): |
paul@67 | 432 | self.page = page |
paul@69 | 433 | self.events = None |
paul@67 | 434 | self.body = None |
paul@67 | 435 | self.categories = None |
paul@219 | 436 | self.metadata = None |
paul@67 | 437 | |
paul@67 | 438 | def copyPage(self, page): |
paul@67 | 439 | |
paul@67 | 440 | "Copy the body of the given 'page'." |
paul@67 | 441 | |
paul@67 | 442 | self.body = page.getBody() |
paul@67 | 443 | |
paul@223 | 444 | def getPageURL(self): |
paul@223 | 445 | |
paul@223 | 446 | "Return the URL of this page." |
paul@223 | 447 | |
paul@280 | 448 | return getPageURL(self.page) |
paul@67 | 449 | |
paul@67 | 450 | def getFormat(self): |
paul@67 | 451 | |
paul@67 | 452 | "Get the format used on this page." |
paul@24 | 453 | |
paul@280 | 454 | return getFormat(self.page) |
paul@67 | 455 | |
paul@219 | 456 | def getMetadata(self): |
paul@219 | 457 | |
paul@219 | 458 | """ |
paul@219 | 459 | Return a dictionary containing items describing the page's "created" |
paul@219 | 460 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 461 | "last-comment" made about the last edit. |
paul@219 | 462 | """ |
paul@219 | 463 | |
paul@219 | 464 | if self.metadata is None: |
paul@280 | 465 | self.metadata = getMetadata(self.page) |
paul@219 | 466 | return self.metadata |
paul@219 | 467 | |
paul@67 | 468 | def getRevisions(self): |
paul@67 | 469 | |
paul@67 | 470 | "Return a list of page revisions." |
paul@67 | 471 | |
paul@67 | 472 | return self.page.getRevList() |
paul@67 | 473 | |
paul@67 | 474 | def getPageRevision(self): |
paul@24 | 475 | |
paul@67 | 476 | "Return the revision details dictionary for this page." |
paul@67 | 477 | |
paul@67 | 478 | return getPageRevision(self.page) |
paul@67 | 479 | |
paul@67 | 480 | def getPageName(self): |
paul@67 | 481 | |
paul@67 | 482 | "Return the page name." |
paul@67 | 483 | |
paul@67 | 484 | return self.page.page_name |
paul@24 | 485 | |
paul@67 | 486 | def getPrettyPageName(self): |
paul@67 | 487 | |
paul@67 | 488 | "Return a nicely formatted title/name for this page." |
paul@67 | 489 | |
paul@67 | 490 | return getPrettyPageName(self.page) |
paul@67 | 491 | |
paul@67 | 492 | def getBody(self): |
paul@67 | 493 | |
paul@67 | 494 | "Get the current page body." |
paul@47 | 495 | |
paul@67 | 496 | if self.body is None: |
paul@67 | 497 | self.body = self.page.get_raw_body() |
paul@67 | 498 | return self.body |
paul@67 | 499 | |
paul@69 | 500 | def getEvents(self): |
paul@69 | 501 | |
paul@69 | 502 | "Return a list of events from this page." |
paul@67 | 503 | |
paul@69 | 504 | if self.events is None: |
paul@69 | 505 | details = {} |
paul@69 | 506 | self.events = [Event(self, details)] |
paul@47 | 507 | |
paul@67 | 508 | if self.getFormat() == "wiki": |
paul@67 | 509 | for match in definition_list_regexp.finditer(self.getBody()): |
paul@67 | 510 | |
paul@67 | 511 | # Skip commented-out items. |
paul@47 | 512 | |
paul@67 | 513 | if match.group("optcomment"): |
paul@67 | 514 | continue |
paul@67 | 515 | |
paul@67 | 516 | # Permit case-insensitive list terms. |
paul@67 | 517 | |
paul@67 | 518 | term = match.group("term").lower() |
paul@67 | 519 | desc = match.group("desc") |
paul@67 | 520 | |
paul@67 | 521 | # Special value type handling. |
paul@27 | 522 | |
paul@67 | 523 | # Dates. |
paul@67 | 524 | |
paul@67 | 525 | if term in ("start", "end"): |
paul@123 | 526 | desc = getDateTime(desc) |
paul@67 | 527 | |
paul@67 | 528 | # Lists (whose elements may be quoted). |
paul@67 | 529 | |
paul@67 | 530 | elif term in ("topics", "categories"): |
paul@229 | 531 | desc = map(getSimpleWikiText, to_list(desc, ",")) |
paul@229 | 532 | |
paul@230 | 533 | # Position details. |
paul@229 | 534 | |
paul@229 | 535 | elif term == "geo": |
paul@237 | 536 | try: |
paul@237 | 537 | desc = map(getMapReference, to_list(desc, None)) |
paul@246 | 538 | if len(desc) != 2: |
paul@246 | 539 | continue |
paul@237 | 540 | except (KeyError, ValueError): |
paul@237 | 541 | continue |
paul@67 | 542 | |
paul@67 | 543 | # Labels which may well be quoted. |
paul@67 | 544 | |
paul@92 | 545 | elif term in ("title", "summary", "description", "location"): |
paul@179 | 546 | desc = getSimpleWikiText(desc.strip()) |
paul@67 | 547 | |
paul@67 | 548 | if desc is not None: |
paul@69 | 549 | |
paul@69 | 550 | # Handle apparent duplicates by creating a new set of |
paul@69 | 551 | # details. |
paul@69 | 552 | |
paul@69 | 553 | if details.has_key(term): |
paul@92 | 554 | |
paul@92 | 555 | # Make a new event. |
paul@92 | 556 | |
paul@69 | 557 | details = {} |
paul@69 | 558 | self.events.append(Event(self, details)) |
paul@67 | 559 | |
paul@69 | 560 | details[term] = desc |
paul@69 | 561 | |
paul@69 | 562 | return self.events |
paul@69 | 563 | |
paul@69 | 564 | def setEvents(self, events): |
paul@69 | 565 | |
paul@69 | 566 | "Set the given 'events' on this page." |
paul@69 | 567 | |
paul@69 | 568 | self.events = events |
paul@67 | 569 | |
paul@67 | 570 | def getCategoryMembership(self): |
paul@27 | 571 | |
paul@67 | 572 | "Get the category names from this page." |
paul@67 | 573 | |
paul@67 | 574 | if self.categories is None: |
paul@67 | 575 | body = self.getBody() |
paul@67 | 576 | match = category_membership_regexp.search(body) |
paul@158 | 577 | self.categories = match and [x for x in match.groups() if x] or [] |
paul@67 | 578 | |
paul@67 | 579 | return self.categories |
paul@67 | 580 | |
paul@67 | 581 | def setCategoryMembership(self, category_names): |
paul@10 | 582 | |
paul@67 | 583 | """ |
paul@67 | 584 | Set the category membership for the page using the specified |
paul@67 | 585 | 'category_names'. |
paul@67 | 586 | """ |
paul@67 | 587 | |
paul@67 | 588 | self.categories = category_names |
paul@67 | 589 | |
paul@67 | 590 | def flushEventDetails(self): |
paul@67 | 591 | |
paul@67 | 592 | "Flush the current event details to this page's body text." |
paul@10 | 593 | |
paul@67 | 594 | new_body_parts = [] |
paul@67 | 595 | end_of_last_match = 0 |
paul@67 | 596 | body = self.getBody() |
paul@69 | 597 | |
paul@69 | 598 | events = iter(self.getEvents()) |
paul@69 | 599 | |
paul@69 | 600 | event = events.next() |
paul@69 | 601 | event_details = event.getDetails() |
paul@69 | 602 | replaced_terms = set() |
paul@67 | 603 | |
paul@67 | 604 | for match in definition_list_regexp.finditer(body): |
paul@47 | 605 | |
paul@10 | 606 | # Permit case-insensitive list terms. |
paul@10 | 607 | |
paul@10 | 608 | term = match.group("term").lower() |
paul@10 | 609 | desc = match.group("desc") |
paul@10 | 610 | |
paul@69 | 611 | # Check that the term has not already been substituted. If so, |
paul@69 | 612 | # get the next event. |
paul@69 | 613 | |
paul@69 | 614 | if term in replaced_terms: |
paul@69 | 615 | try: |
paul@69 | 616 | event = events.next() |
paul@69 | 617 | |
paul@69 | 618 | # No more events. |
paul@69 | 619 | |
paul@69 | 620 | except StopIteration: |
paul@69 | 621 | break |
paul@69 | 622 | |
paul@69 | 623 | event_details = event.getDetails() |
paul@69 | 624 | replaced_terms = set() |
paul@69 | 625 | |
paul@75 | 626 | # Add preceding text to the new body. |
paul@75 | 627 | |
paul@75 | 628 | new_body_parts.append(body[end_of_last_match:match.start()]) |
paul@75 | 629 | |
paul@75 | 630 | # Get the matching regions, adding the term to the new body. |
paul@75 | 631 | |
paul@75 | 632 | new_body_parts.append(match.group("wholeterm")) |
paul@75 | 633 | |
paul@10 | 634 | # Special value type handling. |
paul@10 | 635 | |
paul@67 | 636 | if event_details.has_key(term): |
paul@19 | 637 | |
paul@67 | 638 | # Dates. |
paul@47 | 639 | |
paul@67 | 640 | if term in ("start", "end"): |
paul@67 | 641 | desc = desc.replace("YYYY-MM-DD", str(event_details[term])) |
paul@47 | 642 | |
paul@67 | 643 | # Lists (whose elements may be quoted). |
paul@47 | 644 | |
paul@67 | 645 | elif term in ("topics", "categories"): |
paul@92 | 646 | desc = ", ".join([getEncodedWikiText(item) for item in event_details[term]]) |
paul@47 | 647 | |
paul@92 | 648 | # Labels which must be quoted. |
paul@47 | 649 | |
paul@67 | 650 | elif term in ("title", "summary"): |
paul@67 | 651 | desc = getEncodedWikiText(event_details[term]) |
paul@47 | 652 | |
paul@230 | 653 | # Position details. |
paul@230 | 654 | |
paul@230 | 655 | elif term == "geo": |
paul@230 | 656 | desc = " ".join(map(str, event_details[term])) |
paul@230 | 657 | |
paul@67 | 658 | # Text which need not be quoted, but it will be Wiki text. |
paul@55 | 659 | |
paul@92 | 660 | elif term in ("description", "link", "location"): |
paul@67 | 661 | desc = event_details[term] |
paul@55 | 662 | |
paul@69 | 663 | replaced_terms.add(term) |
paul@69 | 664 | |
paul@75 | 665 | # Add the replaced value. |
paul@75 | 666 | |
paul@67 | 667 | new_body_parts.append(desc) |
paul@10 | 668 | |
paul@69 | 669 | # Remember where in the page has been processed. |
paul@69 | 670 | |
paul@69 | 671 | end_of_last_match = match.end() |
paul@69 | 672 | |
paul@69 | 673 | # Write the rest of the page. |
paul@69 | 674 | |
paul@69 | 675 | new_body_parts.append(body[end_of_last_match:]) |
paul@10 | 676 | |
paul@67 | 677 | self.body = "".join(new_body_parts) |
paul@11 | 678 | |
paul@67 | 679 | def flushCategoryMembership(self): |
paul@17 | 680 | |
paul@67 | 681 | "Flush the category membership to the page body." |
paul@11 | 682 | |
paul@67 | 683 | body = self.getBody() |
paul@67 | 684 | category_names = self.getCategoryMembership() |
paul@67 | 685 | match = category_membership_regexp.search(body) |
paul@10 | 686 | |
paul@67 | 687 | if match: |
paul@67 | 688 | self.body = "".join([body[:match.start()], " ".join(category_names), body[match.end():]]) |
paul@10 | 689 | |
paul@67 | 690 | def saveChanges(self): |
paul@10 | 691 | |
paul@67 | 692 | "Save changes to the event." |
paul@10 | 693 | |
paul@67 | 694 | self.flushEventDetails() |
paul@67 | 695 | self.flushCategoryMembership() |
paul@67 | 696 | self.page.saveText(self.getBody(), 0) |
paul@10 | 697 | |
paul@67 | 698 | def linkToPage(self, request, text, query_string=None): |
paul@11 | 699 | |
paul@67 | 700 | """ |
paul@67 | 701 | Using 'request', return a link to this page with the given link 'text' |
paul@67 | 702 | and optional 'query_string'. |
paul@67 | 703 | """ |
paul@11 | 704 | |
paul@67 | 705 | return linkToPage(request, self.page, text, query_string) |
paul@13 | 706 | |
paul@217 | 707 | # Formatting-related functions. |
paul@217 | 708 | |
paul@217 | 709 | def getParserClass(self, request, format): |
paul@217 | 710 | |
paul@217 | 711 | """ |
paul@217 | 712 | Return a parser class using the 'request' for the given 'format', returning |
paul@217 | 713 | a plain text parser if no parser can be found for the specified 'format'. |
paul@217 | 714 | """ |
paul@217 | 715 | |
paul@217 | 716 | try: |
paul@217 | 717 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@217 | 718 | except wikiutil.PluginMissingError: |
paul@217 | 719 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@217 | 720 | |
paul@217 | 721 | def formatText(self, text, request, fmt): |
paul@217 | 722 | |
paul@217 | 723 | """ |
paul@217 | 724 | Format the given 'text' using the specified 'request' and formatter |
paul@217 | 725 | 'fmt'. |
paul@217 | 726 | """ |
paul@217 | 727 | |
paul@219 | 728 | fmt.page = self.page |
paul@219 | 729 | |
paul@217 | 730 | # Suppress line anchors. |
paul@217 | 731 | |
paul@217 | 732 | parser_cls = self.getParserClass(request, self.getFormat()) |
paul@217 | 733 | parser = parser_cls(text, request, line_anchors=False) |
paul@217 | 734 | |
paul@217 | 735 | # Fix lists by indicating that a paragraph is already started. |
paul@217 | 736 | |
paul@217 | 737 | return request.redirectedOutput(parser.format, fmt, inhibit_p=True) |
paul@217 | 738 | |
paul@217 | 739 | # Event details. |
paul@217 | 740 | |
paul@130 | 741 | class Event(ActsAsTimespan): |
paul@69 | 742 | |
paul@69 | 743 | "A description of an event." |
paul@69 | 744 | |
paul@69 | 745 | def __init__(self, page, details): |
paul@69 | 746 | self.page = page |
paul@69 | 747 | self.details = details |
paul@69 | 748 | |
paul@217 | 749 | # Permit omission of the end of the event by duplicating the start. |
paul@217 | 750 | |
paul@240 | 751 | if self.details.has_key("start") and not self.details.get("end"): |
paul@218 | 752 | end = self.details["start"] |
paul@218 | 753 | |
paul@218 | 754 | # Make any end time refer to the day instead. |
paul@218 | 755 | |
paul@218 | 756 | if isinstance(end, DateTime): |
paul@218 | 757 | end = end.as_date() |
paul@218 | 758 | |
paul@218 | 759 | self.details["end"] = end |
paul@217 | 760 | |
paul@170 | 761 | def __repr__(self): |
paul@170 | 762 | return "<Event %r %r>" % (self.getSummary(), self.as_limits()) |
paul@170 | 763 | |
paul@128 | 764 | def __hash__(self): |
paul@223 | 765 | |
paul@223 | 766 | """ |
paul@223 | 767 | Return a dictionary hash, avoiding mistaken equality of events in some |
paul@223 | 768 | situations (notably membership tests) by including the URL as well as |
paul@223 | 769 | the summary. |
paul@223 | 770 | """ |
paul@223 | 771 | |
paul@223 | 772 | return hash(self.getSummary() + self.getEventURL()) |
paul@69 | 773 | |
paul@69 | 774 | def getPage(self): |
paul@69 | 775 | |
paul@69 | 776 | "Return the page describing this event." |
paul@69 | 777 | |
paul@69 | 778 | return self.page |
paul@69 | 779 | |
paul@69 | 780 | def setPage(self, page): |
paul@69 | 781 | |
paul@69 | 782 | "Set the 'page' describing this event." |
paul@69 | 783 | |
paul@69 | 784 | self.page = page |
paul@69 | 785 | |
paul@223 | 786 | def getEventURL(self): |
paul@223 | 787 | |
paul@223 | 788 | "Return the URL of this event." |
paul@223 | 789 | |
paul@223 | 790 | return self.page.getPageURL() |
paul@219 | 791 | |
paul@219 | 792 | def linkToEvent(self, request, text, query_string=None): |
paul@219 | 793 | |
paul@219 | 794 | """ |
paul@219 | 795 | Using 'request', return a link to this event with the given link 'text' |
paul@219 | 796 | and optional 'query_string'. |
paul@219 | 797 | """ |
paul@219 | 798 | |
paul@219 | 799 | return self.page.linkToPage(request, text, query_string) |
paul@219 | 800 | |
paul@219 | 801 | def getMetadata(self): |
paul@219 | 802 | |
paul@219 | 803 | """ |
paul@219 | 804 | Return a dictionary containing items describing the event's "created" |
paul@219 | 805 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 806 | "last-comment" made about the last edit. |
paul@219 | 807 | """ |
paul@219 | 808 | |
paul@219 | 809 | # Delegate this to the page. |
paul@219 | 810 | |
paul@219 | 811 | return self.page.getMetadata() |
paul@219 | 812 | |
paul@69 | 813 | def getSummary(self, event_parent=None): |
paul@69 | 814 | |
paul@69 | 815 | """ |
paul@69 | 816 | Return either the given title or summary of the event according to the |
paul@69 | 817 | event details, or a summary made from using the pretty version of the |
paul@69 | 818 | page name. |
paul@69 | 819 | |
paul@69 | 820 | If the optional 'event_parent' is specified, any page beneath the given |
paul@69 | 821 | 'event_parent' page in the page hierarchy will omit this parent information |
paul@69 | 822 | if its name is used as the summary. |
paul@69 | 823 | """ |
paul@69 | 824 | |
paul@69 | 825 | event_details = self.details |
paul@69 | 826 | |
paul@69 | 827 | if event_details.has_key("title"): |
paul@69 | 828 | return event_details["title"] |
paul@69 | 829 | elif event_details.has_key("summary"): |
paul@69 | 830 | return event_details["summary"] |
paul@69 | 831 | else: |
paul@69 | 832 | # If appropriate, remove the parent details and "/" character. |
paul@69 | 833 | |
paul@69 | 834 | title = self.page.getPageName() |
paul@69 | 835 | |
paul@74 | 836 | if event_parent and title.startswith(event_parent): |
paul@69 | 837 | title = title[len(event_parent.rstrip("/")) + 1:] |
paul@69 | 838 | |
paul@69 | 839 | return getPrettyTitle(title) |
paul@69 | 840 | |
paul@69 | 841 | def getDetails(self): |
paul@69 | 842 | |
paul@69 | 843 | "Return the details for this event." |
paul@69 | 844 | |
paul@69 | 845 | return self.details |
paul@69 | 846 | |
paul@69 | 847 | def setDetails(self, event_details): |
paul@69 | 848 | |
paul@69 | 849 | "Set the 'event_details' for this event." |
paul@69 | 850 | |
paul@69 | 851 | self.details = event_details |
paul@69 | 852 | |
paul@123 | 853 | # Timespan-related methods. |
paul@123 | 854 | |
paul@123 | 855 | def __contains__(self, other): |
paul@123 | 856 | return self == other |
paul@123 | 857 | |
paul@224 | 858 | def __eq__(self, other): |
paul@224 | 859 | if isinstance(other, Event): |
paul@224 | 860 | return self.getSummary() == other.getSummary() and self.getEventURL() == other.getEventURL() and self._cmp(other) |
paul@224 | 861 | else: |
paul@224 | 862 | return self._cmp(other) == 0 |
paul@224 | 863 | |
paul@224 | 864 | def __ne__(self, other): |
paul@224 | 865 | return not self.__eq__(other) |
paul@224 | 866 | |
paul@224 | 867 | def __lt__(self, other): |
paul@224 | 868 | return self._cmp(other) == -1 |
paul@224 | 869 | |
paul@224 | 870 | def __le__(self, other): |
paul@224 | 871 | return self._cmp(other) in (-1, 0) |
paul@224 | 872 | |
paul@224 | 873 | def __gt__(self, other): |
paul@224 | 874 | return self._cmp(other) == 1 |
paul@224 | 875 | |
paul@224 | 876 | def __ge__(self, other): |
paul@224 | 877 | return self._cmp(other) in (0, 1) |
paul@224 | 878 | |
paul@224 | 879 | def _cmp(self, other): |
paul@223 | 880 | |
paul@223 | 881 | "Compare this event to an 'other' event purely by their timespans." |
paul@223 | 882 | |
paul@123 | 883 | if isinstance(other, Event): |
paul@123 | 884 | return cmp(self.as_timespan(), other.as_timespan()) |
paul@123 | 885 | else: |
paul@123 | 886 | return cmp(self.as_timespan(), other) |
paul@123 | 887 | |
paul@123 | 888 | def as_timespan(self): |
paul@123 | 889 | details = self.details |
paul@123 | 890 | if details.has_key("start") and details.has_key("end"): |
paul@123 | 891 | return Timespan(details["start"], details["end"]) |
paul@123 | 892 | else: |
paul@123 | 893 | return None |
paul@123 | 894 | |
paul@128 | 895 | def as_limits(self): |
paul@170 | 896 | ts = self.as_timespan() |
paul@170 | 897 | return ts and ts.as_limits() |
paul@123 | 898 | |
paul@219 | 899 | class CalendarEvent(Event): |
paul@219 | 900 | |
paul@219 | 901 | "An event from a remote calendar." |
paul@219 | 902 | |
paul@223 | 903 | def getEventURL(self): |
paul@223 | 904 | |
paul@223 | 905 | "Return the URL of this event." |
paul@223 | 906 | |
paul@223 | 907 | return self.details.get("url") or self.page.getPageURL() |
paul@219 | 908 | |
paul@219 | 909 | def linkToEvent(self, request, text, query_string=None): |
paul@219 | 910 | |
paul@219 | 911 | """ |
paul@219 | 912 | Using 'request', return a link to this event with the given link 'text' |
paul@219 | 913 | and optional 'query_string'. |
paul@219 | 914 | """ |
paul@219 | 915 | |
paul@223 | 916 | return linkToResource(self.getEventURL(), request, text, query_string) |
paul@219 | 917 | |
paul@219 | 918 | def getMetadata(self): |
paul@219 | 919 | |
paul@219 | 920 | """ |
paul@219 | 921 | Return a dictionary containing items describing the event's "created" |
paul@219 | 922 | time, "last-modified" time, "sequence" (or revision number) and the |
paul@219 | 923 | "last-comment" made about the last edit. |
paul@219 | 924 | """ |
paul@219 | 925 | |
paul@219 | 926 | return { |
paul@219 | 927 | "created" : self.details.get("created") or self.details["dtstamp"], |
paul@219 | 928 | "last-modified" : self.details.get("last-modified") or self.details["dtstamp"], |
paul@219 | 929 | "sequence" : self.details.get("sequence") or 0, |
paul@219 | 930 | "last-comment" : "" |
paul@219 | 931 | } |
paul@219 | 932 | |
paul@217 | 933 | # Obtaining event containers and events from such containers. |
paul@217 | 934 | |
paul@217 | 935 | def getEventPages(pages): |
paul@168 | 936 | |
paul@168 | 937 | "Return a list of events found on the given 'pages'." |
paul@12 | 938 | |
paul@217 | 939 | # Get real pages instead of result pages. |
paul@217 | 940 | |
paul@217 | 941 | return map(EventPage, pages) |
paul@217 | 942 | |
paul@219 | 943 | def getAllEventSources(request): |
paul@219 | 944 | |
paul@219 | 945 | "Return all event sources defined in the Wiki using the 'request'." |
paul@219 | 946 | |
paul@219 | 947 | sources_page = getattr(request.cfg, "event_aggregator_sources_page", "EventSourcesDict") |
paul@219 | 948 | |
paul@219 | 949 | # Remote sources are accessed via dictionary page definitions. |
paul@219 | 950 | |
paul@237 | 951 | return getWikiDict(sources_page, request) |
paul@219 | 952 | |
paul@217 | 953 | def getEventResources(sources, calendar_start, calendar_end, request): |
paul@217 | 954 | |
paul@217 | 955 | """ |
paul@217 | 956 | Return resource objects for the given 'sources' using the given |
paul@217 | 957 | 'calendar_start' and 'calendar_end' to parameterise requests to the sources, |
paul@217 | 958 | and the 'request' to access configuration settings in the Wiki. |
paul@217 | 959 | """ |
paul@217 | 960 | |
paul@219 | 961 | sources_dict = getAllEventSources(request) |
paul@219 | 962 | if not sources_dict: |
paul@217 | 963 | return [] |
paul@217 | 964 | |
paul@217 | 965 | # Use dates for the calendar limits. |
paul@217 | 966 | |
paul@218 | 967 | if isinstance(calendar_start, Date): |
paul@218 | 968 | pass |
paul@218 | 969 | elif isinstance(calendar_start, Month): |
paul@217 | 970 | calendar_start = calendar_start.as_date(1) |
paul@217 | 971 | |
paul@218 | 972 | if isinstance(calendar_end, Date): |
paul@218 | 973 | pass |
paul@218 | 974 | elif isinstance(calendar_end, Month): |
paul@217 | 975 | calendar_end = calendar_end.as_date(-1) |
paul@217 | 976 | |
paul@217 | 977 | resources = [] |
paul@217 | 978 | |
paul@217 | 979 | for source in sources: |
paul@217 | 980 | try: |
paul@265 | 981 | details = sources_dict[source].split() |
paul@265 | 982 | url = details[0] |
paul@265 | 983 | format = (details[1:] or ["ical"])[0] |
paul@237 | 984 | except (KeyError, ValueError): |
paul@237 | 985 | pass |
paul@237 | 986 | else: |
paul@217 | 987 | # Prevent local file access. |
paul@217 | 988 | |
paul@217 | 989 | if url.startswith("file:"): |
paul@217 | 990 | continue |
paul@217 | 991 | |
paul@217 | 992 | # Parameterise the URL. |
paul@221 | 993 | # Where other parameters are used, care must be taken to encode them |
paul@221 | 994 | # properly. |
paul@221 | 995 | |
paul@221 | 996 | url = url.replace("{start}", urllib.quote_plus(calendar_start and str(calendar_start) or "")) |
paul@221 | 997 | url = url.replace("{end}", urllib.quote_plus(calendar_end and str(calendar_end) or "")) |
paul@217 | 998 | |
paul@217 | 999 | # Get a parser. |
paul@265 | 1000 | # NOTE: This could be done reactively by choosing a parser based on |
paul@265 | 1001 | # NOTE: the content type provided by the URL. |
paul@217 | 1002 | |
paul@217 | 1003 | if format == "ical" and vCalendar is not None: |
paul@217 | 1004 | parser = vCalendar.parse |
paul@217 | 1005 | resource_cls = EventCalendar |
paul@265 | 1006 | required_content_type = "text/calendar" |
paul@217 | 1007 | else: |
paul@217 | 1008 | continue |
paul@217 | 1009 | |
paul@225 | 1010 | # See if the URL is cached. |
paul@225 | 1011 | |
paul@225 | 1012 | cache_key = cache.key(request, content=url) |
paul@225 | 1013 | cache_entry = caching.CacheEntry(request, "EventAggregator", cache_key, scope='wiki') |
paul@225 | 1014 | |
paul@226 | 1015 | # If no entry exists, or if the entry is older than a certain age |
paul@226 | 1016 | # (5 minutes by default), create one with the response from the URL. |
paul@226 | 1017 | |
paul@226 | 1018 | now = time.time() |
paul@226 | 1019 | mtime = cache_entry.mtime() |
paul@226 | 1020 | max_cache_age = int(getattr(request.cfg, "event_aggregator_max_cache_age", "300")) |
paul@226 | 1021 | |
paul@226 | 1022 | # NOTE: The URL could be checked and the 'If-Modified-Since' header |
paul@226 | 1023 | # NOTE: (see MoinMoin.action.pollsistersites) could be checked. |
paul@226 | 1024 | |
paul@226 | 1025 | if not cache_entry.exists() or now - mtime >= max_cache_age: |
paul@225 | 1026 | |
paul@225 | 1027 | # Access the remote data source. |
paul@225 | 1028 | |
paul@269 | 1029 | cache_entry.open(mode="w") |
paul@269 | 1030 | |
paul@225 | 1031 | try: |
paul@265 | 1032 | f = urllib2.urlopen(url) |
paul@265 | 1033 | try: |
paul@265 | 1034 | cache_entry.write(url + "\n") |
paul@265 | 1035 | cache_entry.write((f.headers.get("content-type") or "") + "\n") |
paul@265 | 1036 | cache_entry.write(f.read()) |
paul@265 | 1037 | finally: |
paul@265 | 1038 | cache_entry.close() |
paul@265 | 1039 | f.close() |
paul@265 | 1040 | |
paul@265 | 1041 | # In case of an exception, just ignore the remote source. |
paul@265 | 1042 | # NOTE: This could be reported somewhere. |
paul@265 | 1043 | |
paul@265 | 1044 | except IOError: |
paul@269 | 1045 | if cache_entry.exists(): |
paul@269 | 1046 | cache_entry.remove() |
paul@265 | 1047 | continue |
paul@225 | 1048 | |
paul@225 | 1049 | # Open the cache entry and read it. |
paul@225 | 1050 | |
paul@225 | 1051 | cache_entry.open() |
paul@217 | 1052 | try: |
paul@225 | 1053 | data = cache_entry.read() |
paul@225 | 1054 | finally: |
paul@225 | 1055 | cache_entry.close() |
paul@225 | 1056 | |
paul@225 | 1057 | # Process the entry, parsing the content. |
paul@225 | 1058 | |
paul@225 | 1059 | f = StringIO(data) |
paul@225 | 1060 | try: |
paul@225 | 1061 | url = f.readline() |
paul@265 | 1062 | |
paul@265 | 1063 | # Get the content type and encoding, making sure that the data |
paul@265 | 1064 | # can be parsed. |
paul@265 | 1065 | |
paul@265 | 1066 | content_type, encoding = getContentTypeAndEncoding(f.readline()) |
paul@265 | 1067 | if content_type != required_content_type: |
paul@265 | 1068 | continue |
paul@265 | 1069 | |
paul@265 | 1070 | # Send the data to the parser. |
paul@265 | 1071 | |
paul@225 | 1072 | uf = codecs.getreader(encoding or "utf-8")(f) |
paul@225 | 1073 | try: |
paul@225 | 1074 | resources.append(resource_cls(url, parser(uf))) |
paul@225 | 1075 | finally: |
paul@225 | 1076 | uf.close() |
paul@217 | 1077 | finally: |
paul@217 | 1078 | f.close() |
paul@217 | 1079 | |
paul@217 | 1080 | return resources |
paul@217 | 1081 | |
paul@217 | 1082 | def getEventsFromResources(resources): |
paul@217 | 1083 | |
paul@217 | 1084 | "Return a list of events supplied by the given event 'resources'." |
paul@217 | 1085 | |
paul@10 | 1086 | events = [] |
paul@168 | 1087 | |
paul@217 | 1088 | for resource in resources: |
paul@217 | 1089 | |
paul@217 | 1090 | # Get all events described by the resource. |
paul@217 | 1091 | |
paul@217 | 1092 | for event in resource.getEvents(): |
paul@168 | 1093 | |
paul@168 | 1094 | # Remember the event. |
paul@168 | 1095 | |
paul@168 | 1096 | events.append(event) |
paul@168 | 1097 | |
paul@168 | 1098 | return events |
paul@168 | 1099 | |
paul@217 | 1100 | # Event filtering and limits. |
paul@217 | 1101 | |
paul@171 | 1102 | def getEventsInPeriod(events, calendar_period): |
paul@168 | 1103 | |
paul@168 | 1104 | """ |
paul@168 | 1105 | Return a collection containing those of the given 'events' which occur |
paul@171 | 1106 | within the given 'calendar_period'. |
paul@168 | 1107 | """ |
paul@168 | 1108 | |
paul@171 | 1109 | all_shown_events = [] |
paul@168 | 1110 | |
paul@168 | 1111 | for event in events: |
paul@168 | 1112 | |
paul@168 | 1113 | # Test for the suitability of the event. |
paul@168 | 1114 | |
paul@168 | 1115 | if event.as_timespan() is not None: |
paul@168 | 1116 | |
paul@168 | 1117 | # Compare the dates to the requested calendar window, if any. |
paul@168 | 1118 | |
paul@168 | 1119 | if event in calendar_period: |
paul@171 | 1120 | all_shown_events.append(event) |
paul@168 | 1121 | |
paul@168 | 1122 | return all_shown_events |
paul@10 | 1123 | |
paul@171 | 1124 | def getEventLimits(events): |
paul@171 | 1125 | |
paul@171 | 1126 | "Return the earliest and latest of the given 'events'." |
paul@171 | 1127 | |
paul@171 | 1128 | earliest = None |
paul@171 | 1129 | latest = None |
paul@171 | 1130 | |
paul@171 | 1131 | for event in events: |
paul@171 | 1132 | |
paul@171 | 1133 | # Test for the suitability of the event. |
paul@171 | 1134 | |
paul@171 | 1135 | if event.as_timespan() is not None: |
paul@171 | 1136 | ts = event.as_timespan() |
paul@171 | 1137 | if earliest is None or ts.start < earliest: |
paul@171 | 1138 | earliest = ts.start |
paul@171 | 1139 | if latest is None or ts.end > latest: |
paul@171 | 1140 | latest = ts.end |
paul@171 | 1141 | |
paul@171 | 1142 | return earliest, latest |
paul@171 | 1143 | |
paul@29 | 1144 | def setEventTimestamps(request, events): |
paul@29 | 1145 | |
paul@29 | 1146 | """ |
paul@29 | 1147 | Using 'request', set timestamp details in the details dictionary of each of |
paul@67 | 1148 | the 'events'. |
paul@29 | 1149 | |
paul@111 | 1150 | Return the latest timestamp found. |
paul@29 | 1151 | """ |
paul@29 | 1152 | |
paul@29 | 1153 | latest = None |
paul@29 | 1154 | |
paul@69 | 1155 | for event in events: |
paul@69 | 1156 | event_details = event.getDetails() |
paul@219 | 1157 | |
paul@219 | 1158 | # Populate the details with event metadata. |
paul@219 | 1159 | |
paul@219 | 1160 | event_details.update(event.getMetadata()) |
paul@29 | 1161 | |
paul@29 | 1162 | if latest is None or latest < event_details["last-modified"]: |
paul@29 | 1163 | latest = event_details["last-modified"] |
paul@29 | 1164 | |
paul@29 | 1165 | return latest |
paul@29 | 1166 | |
paul@26 | 1167 | def getOrderedEvents(events): |
paul@26 | 1168 | |
paul@26 | 1169 | """ |
paul@26 | 1170 | Return a list with the given 'events' ordered according to their start and |
paul@67 | 1171 | end dates. |
paul@26 | 1172 | """ |
paul@26 | 1173 | |
paul@26 | 1174 | ordered_events = events[:] |
paul@68 | 1175 | ordered_events.sort() |
paul@26 | 1176 | return ordered_events |
paul@26 | 1177 | |
paul@171 | 1178 | def getCalendarPeriod(calendar_start, calendar_end): |
paul@168 | 1179 | |
paul@168 | 1180 | """ |
paul@171 | 1181 | Return a calendar period for the given 'calendar_start' and 'calendar_end'. |
paul@171 | 1182 | These parameters can be given as None. |
paul@168 | 1183 | """ |
paul@168 | 1184 | |
paul@168 | 1185 | # Re-order the window, if appropriate. |
paul@168 | 1186 | |
paul@168 | 1187 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@168 | 1188 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@168 | 1189 | |
paul@171 | 1190 | return Timespan(calendar_start, calendar_end) |
paul@168 | 1191 | |
paul@187 | 1192 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest, resolution): |
paul@13 | 1193 | |
paul@13 | 1194 | """ |
paul@13 | 1195 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 1196 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 1197 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 1198 | specific period of interest. |
paul@13 | 1199 | """ |
paul@13 | 1200 | |
paul@13 | 1201 | # Define the period as starting with any specified start month or the |
paul@13 | 1202 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 1203 | # event known. |
paul@13 | 1204 | |
paul@13 | 1205 | first = calendar_start or earliest |
paul@13 | 1206 | last = calendar_end or latest |
paul@13 | 1207 | |
paul@13 | 1208 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 1209 | # events in the requested period, and there was no start or end month |
paul@13 | 1210 | # specified, show only the month indicated by the start or end of the |
paul@13 | 1211 | # requested period. If all events were to be shown but none were found show |
paul@13 | 1212 | # the current month. |
paul@13 | 1213 | |
paul@187 | 1214 | if resolution == "date": |
paul@122 | 1215 | get_current = getCurrentDate |
paul@122 | 1216 | else: |
paul@122 | 1217 | get_current = getCurrentMonth |
paul@122 | 1218 | |
paul@13 | 1219 | if first is None: |
paul@122 | 1220 | first = last or get_current() |
paul@13 | 1221 | if last is None: |
paul@122 | 1222 | last = first or get_current() |
paul@13 | 1223 | |
paul@187 | 1224 | if resolution == "month": |
paul@187 | 1225 | first = first.as_month() |
paul@187 | 1226 | last = last.as_month() |
paul@187 | 1227 | |
paul@13 | 1228 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 1229 | |
paul@13 | 1230 | return min(first, last), last |
paul@13 | 1231 | |
paul@168 | 1232 | def getCoverage(events, resolution="date"): |
paul@15 | 1233 | |
paul@15 | 1234 | """ |
paul@168 | 1235 | Determine the coverage of the given 'events', returning a collection of |
paul@168 | 1236 | timespans, along with a dictionary mapping locations to collections of |
paul@168 | 1237 | slots, where each slot contains a tuple of the form (timespans, events). |
paul@15 | 1238 | """ |
paul@15 | 1239 | |
paul@118 | 1240 | all_events = {} |
paul@123 | 1241 | full_coverage = TimespanCollection(resolution) |
paul@15 | 1242 | |
paul@15 | 1243 | # Get event details. |
paul@15 | 1244 | |
paul@69 | 1245 | for event in events: |
paul@69 | 1246 | event_details = event.getDetails() |
paul@15 | 1247 | |
paul@168 | 1248 | # Find the coverage of this period for the event. |
paul@168 | 1249 | |
paul@168 | 1250 | # For day views, each location has its own slot, but for month |
paul@168 | 1251 | # views, all locations are pooled together since having separate |
paul@168 | 1252 | # slots for each location can lead to poor usage of vertical space. |
paul@168 | 1253 | |
paul@168 | 1254 | if resolution == "datetime": |
paul@168 | 1255 | event_location = event_details.get("location") |
paul@168 | 1256 | else: |
paul@168 | 1257 | event_location = None |
paul@168 | 1258 | |
paul@168 | 1259 | # Update the overall coverage. |
paul@168 | 1260 | |
paul@168 | 1261 | full_coverage.insert_in_order(event) |
paul@168 | 1262 | |
paul@168 | 1263 | # Add a new events list for a new location. |
paul@168 | 1264 | # Locations can be unspecified, thus None refers to all unlocalised |
paul@168 | 1265 | # events. |
paul@168 | 1266 | |
paul@168 | 1267 | if not all_events.has_key(event_location): |
paul@168 | 1268 | all_events[event_location] = [TimespanCollection(resolution, [event])] |
paul@168 | 1269 | |
paul@168 | 1270 | # Try and fit the event into an events list. |
paul@168 | 1271 | |
paul@168 | 1272 | else: |
paul@168 | 1273 | slot = all_events[event_location] |
paul@168 | 1274 | |
paul@168 | 1275 | for slot_events in slot: |
paul@168 | 1276 | |
paul@168 | 1277 | # Where the event does not overlap with the events in the |
paul@168 | 1278 | # current collection, add it alongside these events. |
paul@168 | 1279 | |
paul@168 | 1280 | if not event in slot_events: |
paul@168 | 1281 | slot_events.insert_in_order(event) |
paul@168 | 1282 | break |
paul@168 | 1283 | |
paul@168 | 1284 | # Make a new element in the list if the event cannot be |
paul@168 | 1285 | # marked alongside existing events. |
paul@15 | 1286 | |
paul@15 | 1287 | else: |
paul@168 | 1288 | slot.append(TimespanCollection(resolution, [event])) |
paul@15 | 1289 | |
paul@15 | 1290 | return full_coverage, all_events |
paul@15 | 1291 | |
paul@122 | 1292 | def getCoverageScale(coverage): |
paul@126 | 1293 | |
paul@126 | 1294 | """ |
paul@126 | 1295 | Return a scale for the given coverage so that the times involved are |
paul@126 | 1296 | exposed. The scale consists of a list of non-overlapping timespans forming |
paul@126 | 1297 | a contiguous period of time. |
paul@126 | 1298 | """ |
paul@126 | 1299 | |
paul@122 | 1300 | times = set() |
paul@122 | 1301 | for timespan in coverage: |
paul@128 | 1302 | start, end = timespan.as_limits() |
paul@128 | 1303 | |
paul@128 | 1304 | # Add either genuine times or dates converted to times. |
paul@128 | 1305 | |
paul@128 | 1306 | if isinstance(start, DateTime): |
paul@128 | 1307 | times.add(start) |
paul@140 | 1308 | else: |
paul@218 | 1309 | times.add(start.as_start_of_day()) |
paul@128 | 1310 | |
paul@128 | 1311 | if isinstance(end, DateTime): |
paul@140 | 1312 | times.add(end) |
paul@140 | 1313 | else: |
paul@140 | 1314 | times.add(end.as_date().next_day()) |
paul@128 | 1315 | |
paul@122 | 1316 | times = list(times) |
paul@135 | 1317 | times.sort(cmp_dates_as_day_start) |
paul@122 | 1318 | |
paul@122 | 1319 | scale = [] |
paul@122 | 1320 | first = 1 |
paul@122 | 1321 | start = None |
paul@122 | 1322 | for time in times: |
paul@122 | 1323 | if not first: |
paul@123 | 1324 | scale.append(Timespan(start, time)) |
paul@122 | 1325 | else: |
paul@122 | 1326 | first = 0 |
paul@122 | 1327 | start = time |
paul@126 | 1328 | |
paul@122 | 1329 | return scale |
paul@122 | 1330 | |
paul@93 | 1331 | def getCountry(s): |
paul@92 | 1332 | |
paul@93 | 1333 | "Find a country code in the given string 's'." |
paul@93 | 1334 | |
paul@93 | 1335 | match = country_code_regexp.search(s) |
paul@92 | 1336 | |
paul@93 | 1337 | if match: |
paul@93 | 1338 | return match.group("code") |
paul@93 | 1339 | else: |
paul@93 | 1340 | return None |
paul@67 | 1341 | |
paul@67 | 1342 | # Page-related functions. |
paul@67 | 1343 | |
paul@88 | 1344 | def fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames): |
paul@88 | 1345 | |
paul@88 | 1346 | """ |
paul@88 | 1347 | Using the given 'template_page', complete the 'new_page' by copying the |
paul@88 | 1348 | template and adding the given 'event_details' (a dictionary of event |
paul@88 | 1349 | fields), setting also the 'category_pagenames' to define category |
paul@88 | 1350 | membership. |
paul@88 | 1351 | """ |
paul@88 | 1352 | |
paul@88 | 1353 | event_page = EventPage(template_page) |
paul@88 | 1354 | new_event_page = EventPage(new_page) |
paul@88 | 1355 | new_event_page.copyPage(event_page) |
paul@88 | 1356 | |
paul@88 | 1357 | if new_event_page.getFormat() == "wiki": |
paul@88 | 1358 | new_event = Event(new_event_page, event_details) |
paul@88 | 1359 | new_event_page.setEvents([new_event]) |
paul@88 | 1360 | new_event_page.setCategoryMembership(category_pagenames) |
paul@255 | 1361 | new_event_page.flushEventDetails() |
paul@255 | 1362 | |
paul@255 | 1363 | return new_event_page.getBody() |
paul@88 | 1364 | |
paul@10 | 1365 | # vim: tabstop=4 expandtab shiftwidth=4 |