1.1 --- a/macros/EventAggregator.py Sat Mar 21 23:01:28 2009 +0100
1.2 +++ b/macros/EventAggregator.py Sun Mar 22 01:46:06 2009 +0100
1.3 @@ -111,6 +111,43 @@
1.4 else:
1.5 return None
1.6
1.7 +def monthrange(first, last):
1.8 + results = []
1.9 +
1.10 + end_year = last[0]
1.11 +
1.12 + for year in range(first[0], end_year + 1):
1.13 + if year < last[0]:
1.14 + end_month = 12
1.15 + else:
1.16 + end_month = last[1]
1.17 +
1.18 + if year > first[0]:
1.19 + start_month = 1
1.20 + else:
1.21 + start_month = first[1]
1.22 +
1.23 + for month in range(start_month, end_month + 1):
1.24 + results.append((year, month))
1.25 +
1.26 + return results
1.27 +
1.28 +def getColour(s):
1.29 + colour = [0, 0, 0]
1.30 + digit = 0
1.31 + for c in s:
1.32 + colour[digit] += ord(c)
1.33 + colour[digit] = colour[digit] % 256
1.34 + digit += 1
1.35 + digit = digit % 3
1.36 + return tuple(colour)
1.37 +
1.38 +def getBlackOrWhite(colour):
1.39 + if sum(colour) / 3.0 > 127:
1.40 + return (0, 0, 0)
1.41 + else:
1.42 + return (255, 255, 255)
1.43 +
1.44 def execute(macro, args):
1.45
1.46 """
1.47 @@ -156,7 +193,8 @@
1.48 # categories, as found in the macro arguments.
1.49
1.50 events = []
1.51 - shown_events = []
1.52 + shown_events = {}
1.53 +
1.54 earliest = None
1.55 latest = None
1.56
1.57 @@ -191,172 +229,204 @@
1.58 if (calendar_start is None or end_month >= calendar_start) and \
1.59 (calendar_end is None or start_month <= calendar_end):
1.60
1.61 - shown_events.append(event)
1.62 -
1.63 if earliest is None or start_month < earliest:
1.64 earliest = start_month
1.65 if latest is None or end_month > latest:
1.66 latest = end_month
1.67
1.68 + # Store the event in the month-specific dictionary.
1.69 +
1.70 + first = max(start_month, calendar_start or start_month)
1.71 + last = min(end_month, calendar_end or end_month)
1.72 +
1.73 + for event_month in monthrange(first, last):
1.74 + if not shown_events.has_key(event_month):
1.75 + shown_events[event_month] = []
1.76 + shown_events[event_month].append(event)
1.77 +
1.78 # Make a calendar.
1.79
1.80 output = []
1.81
1.82 - if mode == "calendar":
1.83 + if mode == "list":
1.84 + output.append(fmt.bullet_list(on=1, attr={"class" : "event-listings"}))
1.85
1.86 - first = calendar_start or earliest
1.87 - last = calendar_end or latest
1.88 + # Visit all months in the requested range, or across known events.
1.89
1.90 - end_year = last[0]
1.91 + first = calendar_start or earliest
1.92 + last = calendar_end or latest
1.93
1.94 - for year in range(first[0], end_year + 1):
1.95 - if year < last[0]:
1.96 - end_month = 12
1.97 - else:
1.98 - end_month = last[1]
1.99 + for year, month in monthrange(first, last):
1.100 +
1.101 + # Either output a calendar view...
1.102
1.103 - if year > first[0]:
1.104 - start_month = 1
1.105 - else:
1.106 - start_month = first[1]
1.107 + if mode == "calendar":
1.108 +
1.109 + # Output a month.
1.110 +
1.111 + output.append(fmt.table(on=1, attrs={"tableclass" : "event-month"}))
1.112
1.113 - for month in range(start_month, end_month + 1):
1.114 + output.append(fmt.table_row(on=1))
1.115 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "7"}))
1.116 + output.append(fmt.span(on=1))
1.117 + output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels
1.118 + output.append(fmt.span(on=0))
1.119 + output.append(fmt.text(" "))
1.120 + output.append(fmt.span(on=1))
1.121 + output.append(fmt.text(year))
1.122 + output.append(fmt.span(on=0))
1.123 + output.append(fmt.table_cell(on=0))
1.124 + output.append(fmt.table_row(on=0))
1.125
1.126 - # Output a month.
1.127 + # Weekday headings.
1.128
1.129 - output.append(fmt.table(on=1, attrs={"tableclass" : "event-month"}))
1.130 + output.append(fmt.table_row(on=1))
1.131
1.132 - output.append(fmt.table_row(on=1))
1.133 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "7"}))
1.134 - output.append(fmt.span(on=1))
1.135 - output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels
1.136 - output.append(fmt.span(on=0))
1.137 - output.append(fmt.text(" "))
1.138 - output.append(fmt.span(on=1))
1.139 - output.append(fmt.text(year))
1.140 - output.append(fmt.span(on=0))
1.141 + for weekday in range(0, 7):
1.142 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-heading"}))
1.143 + output.append(fmt.text(_(weekday_labels[weekday])))
1.144 output.append(fmt.table_cell(on=0))
1.145 - output.append(fmt.table_row(on=0))
1.146 +
1.147 + output.append(fmt.table_row(on=0))
1.148 +
1.149 + # Process the days of the month.
1.150 +
1.151 + start_weekday, number_of_days = calendar.monthrange(year, month)
1.152
1.153 - # Weekday headings.
1.154 + # The start weekday is the weekday of day number 1.
1.155 + # Find the first day of the week, counting from below zero, if
1.156 + # necessary, in order to land on the first day of the month as
1.157 + # day number 1.
1.158 +
1.159 + first_day = 1 - start_weekday
1.160 +
1.161 + while first_day <= number_of_days:
1.162 +
1.163 + # Output a week.
1.164
1.165 output.append(fmt.table_row(on=1))
1.166
1.167 for weekday in range(0, 7):
1.168 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-heading"}))
1.169 - output.append(fmt.text(_(weekday_labels[weekday])))
1.170 - output.append(fmt.table_cell(on=0))
1.171 + day = first_day + weekday
1.172 +
1.173 + # Output out-of-month days.
1.174 +
1.175 + if day < 1 or day > number_of_days:
1.176 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-excluded"}))
1.177 + output.append(fmt.table_cell(on=0))
1.178 +
1.179 + # Output normal days.
1.180 +
1.181 + else:
1.182 + # Get event details.
1.183 + # NOTE: Can be made more efficient.
1.184 +
1.185 + date = (year, month, day)
1.186 + day_events = []
1.187 +
1.188 + for event_page, event_details in shown_events.get((year, month), []):
1.189 +
1.190 + # Test for the event on the current day.
1.191 +
1.192 + if event_details["start"] <= date <= event_details["end"]:
1.193 + day_events.append((event_page, event_details))
1.194 +
1.195 + # Output the day.
1.196 +
1.197 + if day_events:
1.198 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-busy"}))
1.199 + else:
1.200 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-empty"}))
1.201 +
1.202 + output.append(fmt.div(on=1))
1.203 + output.append(fmt.span(on=1, css_class="event-day-number"))
1.204 + output.append(fmt.text(day))
1.205 + output.append(fmt.span(on=0))
1.206 + output.append(fmt.div(on=0))
1.207 +
1.208 + output.append(fmt.div(on=1, css_class="event-summaries"))
1.209 +
1.210 + # Show event details.
1.211 +
1.212 + for event_page, event_details in day_events:
1.213 +
1.214 + # Get a pretty version of the page name.
1.215 +
1.216 + pretty_pagename = getPrettyPageName(event_page)
1.217 +
1.218 + # Generate a colour for the event.
1.219 +
1.220 + bg = getColour(event_page.page_name)
1.221 + fg = getBlackOrWhite(bg)
1.222 +
1.223 + # Output the event.
1.224 +
1.225 + output.append(fmt.div(on=1, css_class="event-summary",
1.226 + style=("background-color: rgb(%d, %d, %d); color: rgb(%d, %d, %d);" % (bg + fg))))
1.227 + output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
1.228 + output.append(fmt.div(on=0))
1.229 +
1.230 + output.append(fmt.div(on=0))
1.231 +
1.232 + # End of day.
1.233 +
1.234 + output.append(fmt.table_cell(on=0))
1.235
1.236 output.append(fmt.table_row(on=0))
1.237
1.238 - # Process the days of the month.
1.239 -
1.240 - start_weekday, number_of_days = calendar.monthrange(year, month)
1.241 + first_day += 7
1.242
1.243 - # The start weekday is the weekday of day number 1.
1.244 - # Find the first day of the week, counting from below zero, if
1.245 - # necessary, in order to land on the first day of the month as
1.246 - # day number 1.
1.247 -
1.248 - first_day = 1 - start_weekday
1.249 -
1.250 - while first_day <= number_of_days:
1.251 + # End of month.
1.252
1.253 - # Output a week.
1.254 -
1.255 - output.append(fmt.table_row(on=1))
1.256 + output.append(fmt.table(on=0))
1.257
1.258 - for weekday in range(0, 7):
1.259 - day = first_day + weekday
1.260 + # Or output a summary view...
1.261
1.262 - # Output out-of-month days.
1.263 -
1.264 - if day < 1 or day > number_of_days:
1.265 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-excluded"}))
1.266 - output.append(fmt.table_cell(on=0))
1.267 + elif mode == "list":
1.268
1.269 - # Output normal days.
1.270 -
1.271 - else:
1.272 - # Get event details.
1.273 - # NOTE: Can be made more efficient.
1.274 -
1.275 - date = (year, month, day)
1.276 - day_events = []
1.277 -
1.278 - for event_page, event_details in shown_events:
1.279 -
1.280 - # Test for the event on the current day.
1.281 + output.append(fmt.listitem(on=1, attr={"class" : "event-listings-month"}))
1.282 + output.append(fmt.div(on=1, attr={"class" : "event-listings-month-heading"}))
1.283 + output.append(fmt.span(on=1))
1.284 + output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels
1.285 + output.append(fmt.span(on=0))
1.286 + output.append(fmt.text(" "))
1.287 + output.append(fmt.span(on=1))
1.288 + output.append(fmt.text(year))
1.289 + output.append(fmt.span(on=0))
1.290 + output.append(fmt.div(on=0))
1.291
1.292 - if event_details["start"] <= date <= event_details["end"]:
1.293 - day_events.append((event_page, event_details))
1.294 -
1.295 - # Output the day.
1.296 + output.append(fmt.bullet_list(on=1, attr={"class" : "event-month-listings"}))
1.297
1.298 - if day_events:
1.299 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-busy"}))
1.300 - else:
1.301 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-empty"}))
1.302 + for event_page, event_details in shown_events.get((year, month), []):
1.303
1.304 - output.append(fmt.div(on=1, css_class="event-day-number"))
1.305 - output.append(fmt.text(day))
1.306 - output.append(fmt.div(on=0))
1.307 + # Get a pretty version of the page name.
1.308
1.309 - # Show event details.
1.310 -
1.311 - for event_page, event_details in day_events:
1.312 -
1.313 - # Get a pretty version of the page name.
1.314 + pretty_pagename = getPrettyPageName(event_page)
1.315
1.316 - pretty_pagename = getPrettyPageName(event_page)
1.317 + output.append(fmt.listitem(on=1, attr={"class" : "event-listing"}))
1.318
1.319 - # Output the event.
1.320 -
1.321 - output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
1.322 - output.append(fmt.linebreak())
1.323 + # Link to the page using the pretty name.
1.324
1.325 - # End of day.
1.326 -
1.327 - output.append(fmt.table_cell(on=0))
1.328 -
1.329 - output.append(fmt.table_row(on=0))
1.330 + output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
1.331
1.332 - first_day += 7
1.333 -
1.334 - # End of month.
1.335 + # Add the event details.
1.336
1.337 - output.append(fmt.table(on=0))
1.338 -
1.339 - elif mode == "list":
1.340 - output.append(fmt.bullet_list(on=1, attr={"class" : "event-listings"}))
1.341 + output.append(fmt.definition_list(on=1, attr={"class" : "event-details"}))
1.342
1.343 - for event_page, event_details in shown_events:
1.344 -
1.345 - # Get a pretty version of the page name.
1.346 -
1.347 - pretty_pagename = getPrettyPageName(event_page)
1.348 -
1.349 - output.append(fmt.listitem(on=1, attr={"class" : "event-listing"}))
1.350 -
1.351 - # Link to the page using the pretty name.
1.352 -
1.353 - output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
1.354 -
1.355 - # Add the event details.
1.356 + for key, value in event_details.items():
1.357 + output.append(fmt.definition_term(on=1))
1.358 + output.append(fmt.text(key))
1.359 + output.append(fmt.definition_term(on=0))
1.360 + output.append(fmt.definition_desc(on=1))
1.361 + output.append(fmt.text(value))
1.362 + output.append(fmt.definition_desc(on=0))
1.363
1.364 - output.append(fmt.definition_list(on=1, attr={"class" : "event-details"}))
1.365 + output.append(fmt.definition_list(on=0))
1.366 + output.append(fmt.listitem(on=0))
1.367
1.368 - for key, value in event_details.items():
1.369 - output.append(fmt.definition_term(on=1))
1.370 - output.append(fmt.text(key))
1.371 - output.append(fmt.definition_term(on=0))
1.372 - output.append(fmt.definition_desc(on=1))
1.373 - output.append(fmt.text(value))
1.374 - output.append(fmt.definition_desc(on=0))
1.375 + output.append(fmt.bullet_list(on=0))
1.376
1.377 - output.append(fmt.definition_list(on=0))
1.378 - output.append(fmt.listitem(on=0))
1.379 -
1.380 + if mode == "list":
1.381 output.append(fmt.bullet_list(on=0))
1.382
1.383 return ''.join(output)