paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregator library |
paul@10 | 4 | |
paul@10 | 5 | @copyright: 2008, 2009 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@10 | 11 | from MoinMoin.Page import Page |
paul@10 | 12 | from MoinMoin import search, version |
paul@24 | 13 | from MoinMoin import wikiutil |
paul@10 | 14 | import calendar |
paul@11 | 15 | import datetime |
paul@24 | 16 | import time |
paul@10 | 17 | import re |
paul@10 | 18 | |
paul@10 | 19 | __version__ = "0.1" |
paul@10 | 20 | |
paul@22 | 21 | # Date labels. |
paul@22 | 22 | |
paul@22 | 23 | month_labels = ["January", "February", "March", "April", "May", "June", |
paul@22 | 24 | "July", "August", "September", "October", "November", "December"] |
paul@22 | 25 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@22 | 26 | |
paul@10 | 27 | # Regular expressions where MoinMoin does not provide the required support. |
paul@10 | 28 | |
paul@10 | 29 | category_regexp = None |
paul@10 | 30 | definition_list_regexp = re.compile(ur'^\s+(?P<term>.*?)::\s(?P<desc>.*?)$', re.UNICODE | re.MULTILINE) |
paul@10 | 31 | date_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})', re.UNICODE) |
paul@10 | 32 | month_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})', re.UNICODE) |
paul@19 | 33 | verbatim_regexp = re.compile(ur'(?:' |
paul@19 | 34 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@19 | 35 | ur'|' |
paul@19 | 36 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@19 | 37 | ur'|' |
paul@19 | 38 | ur'`(?P<monospace>.*?)`' |
paul@19 | 39 | ur'|' |
paul@19 | 40 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@19 | 41 | ur')', re.UNICODE) |
paul@10 | 42 | |
paul@10 | 43 | # Utility functions. |
paul@10 | 44 | |
paul@10 | 45 | def isMoin15(): |
paul@10 | 46 | return version.release.startswith("1.5.") |
paul@10 | 47 | |
paul@10 | 48 | def getCategoryPattern(request): |
paul@10 | 49 | global category_regexp |
paul@10 | 50 | |
paul@10 | 51 | try: |
paul@10 | 52 | return request.cfg.cache.page_category_regexact |
paul@10 | 53 | except AttributeError: |
paul@10 | 54 | |
paul@10 | 55 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@10 | 56 | |
paul@10 | 57 | if category_regexp is None: |
paul@10 | 58 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@10 | 59 | return category_regexp |
paul@10 | 60 | |
paul@19 | 61 | # Action support functions. |
paul@19 | 62 | |
paul@19 | 63 | def getCategories(request): |
paul@19 | 64 | |
paul@19 | 65 | """ |
paul@19 | 66 | From the AdvancedSearch macro, return a list of category page names using |
paul@19 | 67 | the given 'request'. |
paul@19 | 68 | """ |
paul@19 | 69 | |
paul@19 | 70 | # This will return all pages with "Category" in the title. |
paul@19 | 71 | |
paul@19 | 72 | cat_filter = getCategoryPattern(request).search |
paul@19 | 73 | return request.rootpage.getPageList(filter=cat_filter) |
paul@19 | 74 | |
paul@19 | 75 | def getCategoryMapping(category_pagenames, request): |
paul@19 | 76 | |
paul@19 | 77 | """ |
paul@19 | 78 | For the given 'category_pagenames' return a list of tuples of the form |
paul@19 | 79 | (category name, category page name) using the given 'request'. |
paul@19 | 80 | """ |
paul@19 | 81 | |
paul@19 | 82 | cat_pattern = getCategoryPattern(request) |
paul@19 | 83 | mapping = [] |
paul@19 | 84 | for pagename in category_pagenames: |
paul@19 | 85 | name = cat_pattern.match(pagename).group("key") |
paul@19 | 86 | if name != "Category": |
paul@19 | 87 | mapping.append((name, pagename)) |
paul@19 | 88 | mapping.sort() |
paul@19 | 89 | return mapping |
paul@19 | 90 | |
paul@24 | 91 | def getPageDate(page): |
paul@24 | 92 | |
paul@24 | 93 | # From MoinMoin.xmlrpc... |
paul@24 | 94 | |
paul@27 | 95 | if hasattr(page, "edit_info"): |
paul@27 | 96 | edit_info = page.edit_info() |
paul@27 | 97 | else: |
paul@27 | 98 | edit_info = page.last_edit(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@27 | 99 | |
paul@24 | 100 | mtime = wikiutil.version2timestamp(long(edit_info['timestamp'])) # must be long for py 2.2.x |
paul@24 | 101 | return tuple(time.gmtime(mtime)) |
paul@24 | 102 | |
paul@10 | 103 | # The main activity functions. |
paul@10 | 104 | |
paul@10 | 105 | def getPages(pagename, request): |
paul@10 | 106 | |
paul@10 | 107 | "Return the links minus category links for 'pagename' using the 'request'." |
paul@10 | 108 | |
paul@10 | 109 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@10 | 110 | if isMoin15(): |
paul@10 | 111 | results = search.searchPages(request, query) |
paul@10 | 112 | results.sortByPagename() |
paul@10 | 113 | else: |
paul@10 | 114 | results = search.searchPages(request, query, "page_name") |
paul@10 | 115 | |
paul@10 | 116 | cat_pattern = getCategoryPattern(request) |
paul@10 | 117 | pages = [] |
paul@10 | 118 | for page in results.hits: |
paul@10 | 119 | if not cat_pattern.match(page.page_name): |
paul@10 | 120 | pages.append(page) |
paul@10 | 121 | return pages |
paul@10 | 122 | |
paul@24 | 123 | def getSimpleWikiText(text): |
paul@24 | 124 | |
paul@24 | 125 | """ |
paul@24 | 126 | Return the plain text representation of the given 'text' which may employ |
paul@24 | 127 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@24 | 128 | text. |
paul@24 | 129 | """ |
paul@24 | 130 | |
paul@24 | 131 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@24 | 132 | |
paul@24 | 133 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@24 | 134 | |
paul@27 | 135 | def getFormat(page): |
paul@27 | 136 | |
paul@27 | 137 | "Get the format used on 'page'." |
paul@27 | 138 | |
paul@27 | 139 | if isMoin15(): |
paul@27 | 140 | return "wiki" # page.pi_format |
paul@27 | 141 | else: |
paul@27 | 142 | return page.pi["format"] |
paul@27 | 143 | |
paul@10 | 144 | def getEventDetails(page): |
paul@10 | 145 | |
paul@10 | 146 | "Return a dictionary of event details from the given 'page'." |
paul@10 | 147 | |
paul@10 | 148 | event_details = {} |
paul@10 | 149 | |
paul@27 | 150 | if getFormat(page) == "wiki": |
paul@27 | 151 | for match in definition_list_regexp.finditer(page.get_raw_body()): |
paul@10 | 152 | |
paul@10 | 153 | # Permit case-insensitive list terms. |
paul@10 | 154 | |
paul@10 | 155 | term = match.group("term").lower() |
paul@10 | 156 | desc = match.group("desc") |
paul@10 | 157 | |
paul@10 | 158 | # Special value type handling. |
paul@10 | 159 | |
paul@19 | 160 | # Dates. |
paul@19 | 161 | |
paul@10 | 162 | if term in ("start", "end"): |
paul@10 | 163 | desc = getDate(desc) |
paul@19 | 164 | |
paul@24 | 165 | # Lists (whose elements may be quoted). |
paul@19 | 166 | |
paul@24 | 167 | elif term in ("topics", "categories"): |
paul@24 | 168 | desc = [getSimpleWikiText(value.strip()) for value in desc.split(",")] |
paul@10 | 169 | |
paul@19 | 170 | # Labels which may well be quoted. |
paul@19 | 171 | |
paul@19 | 172 | elif term in ("title", "summary"): |
paul@24 | 173 | desc = getSimpleWikiText(desc) |
paul@19 | 174 | |
paul@10 | 175 | if desc is not None: |
paul@10 | 176 | event_details[term] = desc |
paul@10 | 177 | |
paul@10 | 178 | return event_details |
paul@10 | 179 | |
paul@19 | 180 | def getEventSummary(event_page, event_details): |
paul@19 | 181 | |
paul@19 | 182 | """ |
paul@19 | 183 | Return either the given title or summary of the event described by the given |
paul@19 | 184 | 'event_page', according to the given 'event_details', or return the pretty |
paul@19 | 185 | version of the page name. |
paul@19 | 186 | """ |
paul@19 | 187 | |
paul@19 | 188 | if event_details.has_key("title"): |
paul@19 | 189 | return event_details["title"] |
paul@19 | 190 | elif event_details.has_key("summary"): |
paul@19 | 191 | return event_details["summary"] |
paul@19 | 192 | else: |
paul@19 | 193 | return getPrettyPageName(event_page) |
paul@19 | 194 | |
paul@10 | 195 | def getDate(s): |
paul@10 | 196 | |
paul@10 | 197 | "Parse the string 's', extracting and returning a date string." |
paul@10 | 198 | |
paul@10 | 199 | m = date_regexp.search(s) |
paul@10 | 200 | if m: |
paul@10 | 201 | return tuple(map(int, m.groups())) |
paul@10 | 202 | else: |
paul@10 | 203 | return None |
paul@10 | 204 | |
paul@10 | 205 | def getMonth(s): |
paul@10 | 206 | |
paul@10 | 207 | "Parse the string 's', extracting and returning a month string." |
paul@10 | 208 | |
paul@10 | 209 | m = month_regexp.search(s) |
paul@10 | 210 | if m: |
paul@10 | 211 | return tuple(map(int, m.groups())) |
paul@10 | 212 | else: |
paul@10 | 213 | return None |
paul@10 | 214 | |
paul@11 | 215 | def getCurrentMonth(): |
paul@11 | 216 | |
paul@11 | 217 | "Return the current month as a (year, month) tuple." |
paul@11 | 218 | |
paul@11 | 219 | today = datetime.date.today() |
paul@11 | 220 | return (today.year, today.month) |
paul@11 | 221 | |
paul@17 | 222 | def getCurrentYear(): |
paul@17 | 223 | |
paul@17 | 224 | "Return the current year." |
paul@17 | 225 | |
paul@17 | 226 | today = datetime.date.today() |
paul@17 | 227 | return today.year |
paul@17 | 228 | |
paul@11 | 229 | def monthupdate(date, n): |
paul@11 | 230 | |
paul@11 | 231 | "Return 'date' updated by 'n' months." |
paul@11 | 232 | |
paul@11 | 233 | if n < 0: |
paul@11 | 234 | fn = prevmonth |
paul@11 | 235 | else: |
paul@11 | 236 | fn = nextmonth |
paul@11 | 237 | |
paul@11 | 238 | i = 0 |
paul@11 | 239 | while i < abs(n): |
paul@11 | 240 | date = fn(date) |
paul@11 | 241 | i += 1 |
paul@11 | 242 | |
paul@11 | 243 | return date |
paul@11 | 244 | |
paul@13 | 245 | def daterange(first, last, step=1): |
paul@11 | 246 | |
paul@13 | 247 | """ |
paul@13 | 248 | Get the range of dates starting at 'first' and ending on 'last', using the |
paul@13 | 249 | specified 'step'. |
paul@13 | 250 | """ |
paul@11 | 251 | |
paul@10 | 252 | results = [] |
paul@10 | 253 | |
paul@10 | 254 | months_only = len(first) == 2 |
paul@10 | 255 | start_year = first[0] |
paul@10 | 256 | end_year = last[0] |
paul@10 | 257 | |
paul@11 | 258 | for year in range(start_year, end_year + step, step): |
paul@11 | 259 | if step == 1 and year < end_year: |
paul@10 | 260 | end_month = 12 |
paul@11 | 261 | elif step == -1 and year > end_year: |
paul@11 | 262 | end_month = 1 |
paul@10 | 263 | else: |
paul@10 | 264 | end_month = last[1] |
paul@10 | 265 | |
paul@11 | 266 | if step == 1 and year > start_year: |
paul@10 | 267 | start_month = 1 |
paul@11 | 268 | elif step == -1 and year < start_year: |
paul@11 | 269 | start_month = 12 |
paul@10 | 270 | else: |
paul@10 | 271 | start_month = first[1] |
paul@10 | 272 | |
paul@11 | 273 | for month in range(start_month, end_month + step, step): |
paul@10 | 274 | if months_only: |
paul@10 | 275 | results.append((year, month)) |
paul@10 | 276 | else: |
paul@11 | 277 | if step == 1 and month < end_month: |
paul@10 | 278 | _wd, end_day = calendar.monthrange(year, month) |
paul@11 | 279 | elif step == -1 and month > end_month: |
paul@11 | 280 | end_day = 1 |
paul@10 | 281 | else: |
paul@10 | 282 | end_day = last[2] |
paul@10 | 283 | |
paul@11 | 284 | if step == 1 and month > start_month: |
paul@10 | 285 | start_day = 1 |
paul@11 | 286 | elif step == -1 and month < start_month: |
paul@11 | 287 | _wd, start_day = calendar.monthrange(year, month) |
paul@10 | 288 | else: |
paul@10 | 289 | start_day = first[2] |
paul@10 | 290 | |
paul@11 | 291 | for day in range(start_day, end_day + step, step): |
paul@10 | 292 | results.append((year, month, day)) |
paul@10 | 293 | |
paul@10 | 294 | return results |
paul@10 | 295 | |
paul@10 | 296 | def nextdate(date): |
paul@11 | 297 | |
paul@11 | 298 | "Return the date following the given 'date'." |
paul@11 | 299 | |
paul@10 | 300 | year, month, day = date |
paul@10 | 301 | _wd, end_day = calendar.monthrange(year, month) |
paul@10 | 302 | if day == end_day: |
paul@10 | 303 | if month == 12: |
paul@10 | 304 | return (year + 1, 1, 1) |
paul@10 | 305 | else: |
paul@10 | 306 | return (year, month + 1, 1) |
paul@10 | 307 | else: |
paul@10 | 308 | return (year, month, day + 1) |
paul@10 | 309 | |
paul@11 | 310 | def prevdate(date): |
paul@11 | 311 | |
paul@11 | 312 | "Return the date preceding the given 'date'." |
paul@11 | 313 | |
paul@11 | 314 | year, month, day = date |
paul@11 | 315 | if day == 1: |
paul@11 | 316 | if month == 1: |
paul@11 | 317 | return (year - 1, 12, 31) |
paul@11 | 318 | else: |
paul@11 | 319 | _wd, end_day = calendar.monthrange(year, month - 1) |
paul@11 | 320 | return (year, month - 1, end_day) |
paul@11 | 321 | else: |
paul@11 | 322 | return (year, month, day - 1) |
paul@11 | 323 | |
paul@11 | 324 | def nextmonth(date): |
paul@11 | 325 | |
paul@11 | 326 | "Return the (year, month) tuple following 'date'." |
paul@11 | 327 | |
paul@11 | 328 | year, month = date |
paul@11 | 329 | if month == 12: |
paul@11 | 330 | return (year + 1, 1) |
paul@11 | 331 | else: |
paul@11 | 332 | return year, month + 1 |
paul@11 | 333 | |
paul@11 | 334 | def prevmonth(date): |
paul@11 | 335 | |
paul@11 | 336 | "Return the (year, month) tuple preceding 'date'." |
paul@11 | 337 | |
paul@11 | 338 | year, month = date |
paul@11 | 339 | if month == 1: |
paul@11 | 340 | return (year - 1, 12) |
paul@11 | 341 | else: |
paul@11 | 342 | return year, month - 1 |
paul@11 | 343 | |
paul@13 | 344 | def span(start, end): |
paul@13 | 345 | |
paul@13 | 346 | "Return the difference between 'start' and 'end'." |
paul@13 | 347 | |
paul@13 | 348 | return end[0] - start[0], end[1] - start[1] |
paul@13 | 349 | |
paul@10 | 350 | def getEvents(request, category_names, calendar_start=None, calendar_end=None): |
paul@10 | 351 | |
paul@10 | 352 | """ |
paul@10 | 353 | Using the 'request', generate a list of events found on pages belonging to |
paul@10 | 354 | the specified 'category_names', using the optional 'calendar_start' and |
paul@10 | 355 | 'calendar_end' month tuples of the form (year, month) to indicate a window |
paul@10 | 356 | of interest. |
paul@10 | 357 | |
paul@10 | 358 | Return a list of events, a dictionary mapping months to event lists (within |
paul@10 | 359 | the window of interest), a list of all events within the window of interest, |
paul@10 | 360 | the earliest month of an event within the window of interest, and the latest |
paul@10 | 361 | month of an event within the window of interest. |
paul@10 | 362 | """ |
paul@10 | 363 | |
paul@12 | 364 | # Re-order the window, if appropriate. |
paul@12 | 365 | |
paul@12 | 366 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@12 | 367 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@12 | 368 | |
paul@10 | 369 | events = [] |
paul@10 | 370 | shown_events = {} |
paul@10 | 371 | all_shown_events = [] |
paul@17 | 372 | processed_pages = set() |
paul@10 | 373 | |
paul@10 | 374 | earliest = None |
paul@10 | 375 | latest = None |
paul@10 | 376 | |
paul@10 | 377 | for category_name in category_names: |
paul@10 | 378 | |
paul@10 | 379 | # Get the pages and page names in the category. |
paul@10 | 380 | |
paul@10 | 381 | pages_in_category = getPages(category_name, request) |
paul@10 | 382 | |
paul@10 | 383 | # Visit each page in the category. |
paul@10 | 384 | |
paul@10 | 385 | for page_in_category in pages_in_category: |
paul@10 | 386 | pagename = page_in_category.page_name |
paul@10 | 387 | |
paul@17 | 388 | # Only process each page once. |
paul@17 | 389 | |
paul@17 | 390 | if pagename in processed_pages: |
paul@17 | 391 | continue |
paul@17 | 392 | else: |
paul@17 | 393 | processed_pages.add(pagename) |
paul@17 | 394 | |
paul@10 | 395 | # Get a real page, not a result page. |
paul@10 | 396 | |
paul@10 | 397 | real_page_in_category = Page(request, pagename) |
paul@10 | 398 | event_details = getEventDetails(real_page_in_category) |
paul@10 | 399 | |
paul@10 | 400 | # Define the event as the page together with its details. |
paul@10 | 401 | |
paul@10 | 402 | event = (real_page_in_category, event_details) |
paul@10 | 403 | events.append(event) |
paul@10 | 404 | |
paul@10 | 405 | # Test for the suitability of the event. |
paul@10 | 406 | |
paul@10 | 407 | if event_details.has_key("start") and event_details.has_key("end"): |
paul@10 | 408 | |
paul@10 | 409 | start_month = event_details["start"][:2] |
paul@10 | 410 | end_month = event_details["end"][:2] |
paul@10 | 411 | |
paul@10 | 412 | # Compare the months of the dates to the requested calendar |
paul@10 | 413 | # window, if any. |
paul@10 | 414 | |
paul@10 | 415 | if (calendar_start is None or end_month >= calendar_start) and \ |
paul@10 | 416 | (calendar_end is None or start_month <= calendar_end): |
paul@10 | 417 | |
paul@10 | 418 | all_shown_events.append(event) |
paul@10 | 419 | |
paul@10 | 420 | if earliest is None or start_month < earliest: |
paul@10 | 421 | earliest = start_month |
paul@10 | 422 | if latest is None or end_month > latest: |
paul@10 | 423 | latest = end_month |
paul@10 | 424 | |
paul@10 | 425 | # Store the event in the month-specific dictionary. |
paul@10 | 426 | |
paul@10 | 427 | first = max(start_month, calendar_start or start_month) |
paul@10 | 428 | last = min(end_month, calendar_end or end_month) |
paul@10 | 429 | |
paul@10 | 430 | for event_month in daterange(first, last): |
paul@10 | 431 | if not shown_events.has_key(event_month): |
paul@10 | 432 | shown_events[event_month] = [] |
paul@10 | 433 | shown_events[event_month].append(event) |
paul@10 | 434 | |
paul@10 | 435 | return events, shown_events, all_shown_events, earliest, latest |
paul@10 | 436 | |
paul@26 | 437 | def compareEvents(event1, event2): |
paul@26 | 438 | |
paul@26 | 439 | """ |
paul@26 | 440 | Compare 'event1' and 'event2' by start and end date, where both parameters |
paul@26 | 441 | are of the following form: |
paul@26 | 442 | |
paul@26 | 443 | (event_page, event_details) |
paul@26 | 444 | """ |
paul@26 | 445 | |
paul@26 | 446 | event_page1, event_details1 = event1 |
paul@26 | 447 | event_page2, event_details2 = event2 |
paul@26 | 448 | return cmp( |
paul@26 | 449 | (event_details1["start"], event_details1["end"]), |
paul@26 | 450 | (event_details2["start"], event_details2["end"]) |
paul@26 | 451 | ) |
paul@26 | 452 | |
paul@26 | 453 | def getOrderedEvents(events): |
paul@26 | 454 | |
paul@26 | 455 | """ |
paul@26 | 456 | Return a list with the given 'events' ordered according to their start and |
paul@26 | 457 | end dates. Each list element must be of the following form: |
paul@26 | 458 | |
paul@26 | 459 | (event_page, event_details) |
paul@26 | 460 | """ |
paul@26 | 461 | |
paul@26 | 462 | ordered_events = events[:] |
paul@26 | 463 | ordered_events.sort(compareEvents) |
paul@26 | 464 | return ordered_events |
paul@26 | 465 | |
paul@13 | 466 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest): |
paul@13 | 467 | |
paul@13 | 468 | """ |
paul@13 | 469 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@13 | 470 | indicating that no restriction is imposed on the period for each of the |
paul@13 | 471 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@13 | 472 | specific period of interest. |
paul@13 | 473 | """ |
paul@13 | 474 | |
paul@13 | 475 | # Define the period as starting with any specified start month or the |
paul@13 | 476 | # earliest event known, ending with any specified end month or the latest |
paul@13 | 477 | # event known. |
paul@13 | 478 | |
paul@13 | 479 | first = calendar_start or earliest |
paul@13 | 480 | last = calendar_end or latest |
paul@13 | 481 | |
paul@13 | 482 | # If there is no range of months to show, perhaps because there are no |
paul@13 | 483 | # events in the requested period, and there was no start or end month |
paul@13 | 484 | # specified, show only the month indicated by the start or end of the |
paul@13 | 485 | # requested period. If all events were to be shown but none were found show |
paul@13 | 486 | # the current month. |
paul@13 | 487 | |
paul@13 | 488 | if first is None: |
paul@13 | 489 | first = last or getCurrentMonth() |
paul@13 | 490 | if last is None: |
paul@13 | 491 | last = first or getCurrentMonth() |
paul@13 | 492 | |
paul@13 | 493 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@13 | 494 | |
paul@13 | 495 | return min(first, last), last |
paul@13 | 496 | |
paul@15 | 497 | def getCoverage(start, end, events): |
paul@15 | 498 | |
paul@15 | 499 | """ |
paul@15 | 500 | Within the period defined by the 'start' and 'end' dates, determine the |
paul@15 | 501 | coverage of the days in the period by the given 'events', returning a set of |
paul@15 | 502 | covered days, along with a list of slots, where each slot contains a tuple |
paul@15 | 503 | of the form (set of covered days, events). |
paul@15 | 504 | """ |
paul@15 | 505 | |
paul@15 | 506 | all_events = [] |
paul@15 | 507 | full_coverage = set() |
paul@15 | 508 | |
paul@15 | 509 | # Get event details. |
paul@15 | 510 | |
paul@15 | 511 | for event in events: |
paul@15 | 512 | event_page, event_details = event |
paul@15 | 513 | |
paul@15 | 514 | # Test for the event in the period. |
paul@15 | 515 | |
paul@15 | 516 | if event_details["start"] <= end and event_details["end"] >= start: |
paul@15 | 517 | |
paul@15 | 518 | # Find the coverage of this period for the event. |
paul@15 | 519 | |
paul@15 | 520 | event_start = max(event_details["start"], start) |
paul@15 | 521 | event_end = min(event_details["end"], end) |
paul@15 | 522 | event_coverage = set(daterange(event_start, event_end)) |
paul@15 | 523 | |
paul@15 | 524 | # Update the overall coverage. |
paul@15 | 525 | |
paul@15 | 526 | full_coverage.update(event_coverage) |
paul@15 | 527 | |
paul@15 | 528 | # Try and fit the event into the events list. |
paul@15 | 529 | |
paul@15 | 530 | for i, (coverage, covered_events) in enumerate(all_events): |
paul@15 | 531 | |
paul@15 | 532 | # Where the event does not overlap with the current |
paul@15 | 533 | # element, add it alongside existing events. |
paul@15 | 534 | |
paul@15 | 535 | if not coverage.intersection(event_coverage): |
paul@15 | 536 | covered_events.append(event) |
paul@15 | 537 | all_events[i] = coverage.union(event_coverage), covered_events |
paul@15 | 538 | break |
paul@15 | 539 | |
paul@15 | 540 | # Make a new element in the list if the event cannot be |
paul@15 | 541 | # marked alongside existing events. |
paul@15 | 542 | |
paul@15 | 543 | else: |
paul@15 | 544 | all_events.append((event_coverage, [event])) |
paul@15 | 545 | |
paul@15 | 546 | return full_coverage, all_events |
paul@15 | 547 | |
paul@19 | 548 | # User interface functions. |
paul@19 | 549 | |
paul@23 | 550 | def getParameter(request, name): |
paul@23 | 551 | return request.form.get(name, [None])[0] |
paul@23 | 552 | |
paul@19 | 553 | def getParameterMonth(arg): |
paul@19 | 554 | n = None |
paul@19 | 555 | |
paul@19 | 556 | if arg.startswith("current"): |
paul@19 | 557 | date = getCurrentMonth() |
paul@19 | 558 | if len(arg) > 8: |
paul@19 | 559 | n = int(arg[7:]) |
paul@19 | 560 | |
paul@19 | 561 | elif arg.startswith("yearstart"): |
paul@19 | 562 | date = (getCurrentYear(), 1) |
paul@19 | 563 | if len(arg) > 10: |
paul@19 | 564 | n = int(arg[9:]) |
paul@19 | 565 | |
paul@19 | 566 | elif arg.startswith("yearend"): |
paul@19 | 567 | date = (getCurrentYear(), 12) |
paul@19 | 568 | if len(arg) > 8: |
paul@19 | 569 | n = int(arg[7:]) |
paul@19 | 570 | |
paul@19 | 571 | else: |
paul@19 | 572 | date = getMonth(arg) |
paul@19 | 573 | |
paul@19 | 574 | if n is not None: |
paul@19 | 575 | date = monthupdate(date, n) |
paul@19 | 576 | |
paul@19 | 577 | return date |
paul@19 | 578 | |
paul@19 | 579 | def getFormMonth(request, calendar_name, argname): |
paul@19 | 580 | if calendar_name is None: |
paul@19 | 581 | calendar_prefix = argname |
paul@19 | 582 | else: |
paul@19 | 583 | calendar_prefix = "%s-%s" % (calendar_name, argname) |
paul@19 | 584 | |
paul@23 | 585 | arg = getParameter(request, calendar_prefix) |
paul@19 | 586 | if arg is not None: |
paul@19 | 587 | return getParameterMonth(arg) |
paul@19 | 588 | else: |
paul@19 | 589 | return None |
paul@19 | 590 | |
paul@23 | 591 | def getFormMonthPair(request, yeararg, montharg): |
paul@23 | 592 | year = getParameter(request, yeararg) |
paul@23 | 593 | month = getParameter(request, montharg) |
paul@23 | 594 | if year and month: |
paul@23 | 595 | return (int(year), int(month)) |
paul@23 | 596 | else: |
paul@23 | 597 | return None |
paul@23 | 598 | |
paul@19 | 599 | def getPrettyPageName(page): |
paul@19 | 600 | |
paul@19 | 601 | "Return a nicely formatted title/name for the given 'page'." |
paul@19 | 602 | |
paul@27 | 603 | if isMoin15(): |
paul@27 | 604 | title = page.split_title(page.request, force=1) |
paul@27 | 605 | else: |
paul@27 | 606 | title = page.split_title(force=1) |
paul@27 | 607 | |
paul@27 | 608 | return title.replace("_", " ").replace("/", u" ? ") |
paul@19 | 609 | |
paul@22 | 610 | def getMonthLabel(month): |
paul@22 | 611 | |
paul@22 | 612 | "Return an unlocalised label for the given 'month'." |
paul@22 | 613 | |
paul@22 | 614 | return month_labels[month - 1] # zero-based labels |
paul@22 | 615 | |
paul@22 | 616 | def getDayLabel(weekday): |
paul@22 | 617 | |
paul@22 | 618 | "Return an unlocalised label for the given 'weekday'." |
paul@22 | 619 | |
paul@22 | 620 | return weekday_labels[weekday] |
paul@22 | 621 | |
paul@27 | 622 | def linkToPage(request, page, text, query_string=None): |
paul@27 | 623 | |
paul@27 | 624 | """ |
paul@27 | 625 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@27 | 626 | optional 'query_string'. |
paul@27 | 627 | """ |
paul@27 | 628 | |
paul@27 | 629 | text = wikiutil.escape(text) |
paul@27 | 630 | |
paul@27 | 631 | if isMoin15(): |
paul@27 | 632 | url = wikiutil.quoteWikinameURL(page.page_name) |
paul@27 | 633 | if query_string is not None: |
paul@27 | 634 | url = "%s?%s" % (url, query_string) |
paul@27 | 635 | return wikiutil.link_tag(request, url, text, getattr(page, "formatter", None)) |
paul@27 | 636 | else: |
paul@27 | 637 | return page.link_to_raw(request, text, query_string) |
paul@27 | 638 | |
paul@10 | 639 | # vim: tabstop=4 expandtab shiftwidth=4 |