2.1 --- a/macros/EventAggregator.py Sat Mar 21 23:01:28 2009 +0100
2.2 +++ b/macros/EventAggregator.py Sun Mar 22 01:46:06 2009 +0100
2.3 @@ -111,6 +111,43 @@
2.4 else:
2.5 return None
2.6
2.7 +def monthrange(first, last):
2.8 + results = []
2.9 +
2.10 + end_year = last[0]
2.11 +
2.12 + for year in range(first[0], end_year + 1):
2.13 + if year < last[0]:
2.14 + end_month = 12
2.15 + else:
2.16 + end_month = last[1]
2.17 +
2.18 + if year > first[0]:
2.19 + start_month = 1
2.20 + else:
2.21 + start_month = first[1]
2.22 +
2.23 + for month in range(start_month, end_month + 1):
2.24 + results.append((year, month))
2.25 +
2.26 + return results
2.27 +
2.28 +def getColour(s):
2.29 + colour = [0, 0, 0]
2.30 + digit = 0
2.31 + for c in s:
2.32 + colour[digit] += ord(c)
2.33 + colour[digit] = colour[digit] % 256
2.34 + digit += 1
2.35 + digit = digit % 3
2.36 + return tuple(colour)
2.37 +
2.38 +def getBlackOrWhite(colour):
2.39 + if sum(colour) / 3.0 > 127:
2.40 + return (0, 0, 0)
2.41 + else:
2.42 + return (255, 255, 255)
2.43 +
2.44 def execute(macro, args):
2.45
2.46 """
2.47 @@ -156,7 +193,8 @@
2.48 # categories, as found in the macro arguments.
2.49
2.50 events = []
2.51 - shown_events = []
2.52 + shown_events = {}
2.53 +
2.54 earliest = None
2.55 latest = None
2.56
2.57 @@ -191,172 +229,204 @@
2.58 if (calendar_start is None or end_month >= calendar_start) and \
2.59 (calendar_end is None or start_month <= calendar_end):
2.60
2.61 - shown_events.append(event)
2.62 -
2.63 if earliest is None or start_month < earliest:
2.64 earliest = start_month
2.65 if latest is None or end_month > latest:
2.66 latest = end_month
2.67
2.68 + # Store the event in the month-specific dictionary.
2.69 +
2.70 + first = max(start_month, calendar_start or start_month)
2.71 + last = min(end_month, calendar_end or end_month)
2.72 +
2.73 + for event_month in monthrange(first, last):
2.74 + if not shown_events.has_key(event_month):
2.75 + shown_events[event_month] = []
2.76 + shown_events[event_month].append(event)
2.77 +
2.78 # Make a calendar.
2.79
2.80 output = []
2.81
2.82 - if mode == "calendar":
2.83 + if mode == "list":
2.84 + output.append(fmt.bullet_list(on=1, attr={"class" : "event-listings"}))
2.85
2.86 - first = calendar_start or earliest
2.87 - last = calendar_end or latest
2.88 + # Visit all months in the requested range, or across known events.
2.89
2.90 - end_year = last[0]
2.91 + first = calendar_start or earliest
2.92 + last = calendar_end or latest
2.93
2.94 - for year in range(first[0], end_year + 1):
2.95 - if year < last[0]:
2.96 - end_month = 12
2.97 - else:
2.98 - end_month = last[1]
2.99 + for year, month in monthrange(first, last):
2.100 +
2.101 + # Either output a calendar view...
2.102
2.103 - if year > first[0]:
2.104 - start_month = 1
2.105 - else:
2.106 - start_month = first[1]
2.107 + if mode == "calendar":
2.108 +
2.109 + # Output a month.
2.110 +
2.111 + output.append(fmt.table(on=1, attrs={"tableclass" : "event-month"}))
2.112
2.113 - for month in range(start_month, end_month + 1):
2.114 + output.append(fmt.table_row(on=1))
2.115 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "7"}))
2.116 + output.append(fmt.span(on=1))
2.117 + output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels
2.118 + output.append(fmt.span(on=0))
2.119 + output.append(fmt.text(" "))
2.120 + output.append(fmt.span(on=1))
2.121 + output.append(fmt.text(year))
2.122 + output.append(fmt.span(on=0))
2.123 + output.append(fmt.table_cell(on=0))
2.124 + output.append(fmt.table_row(on=0))
2.125
2.126 - # Output a month.
2.127 + # Weekday headings.
2.128
2.129 - output.append(fmt.table(on=1, attrs={"tableclass" : "event-month"}))
2.130 + output.append(fmt.table_row(on=1))
2.131
2.132 - output.append(fmt.table_row(on=1))
2.133 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "7"}))
2.134 - output.append(fmt.span(on=1))
2.135 - output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels
2.136 - output.append(fmt.span(on=0))
2.137 - output.append(fmt.text(" "))
2.138 - output.append(fmt.span(on=1))
2.139 - output.append(fmt.text(year))
2.140 - output.append(fmt.span(on=0))
2.141 + for weekday in range(0, 7):
2.142 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-heading"}))
2.143 + output.append(fmt.text(_(weekday_labels[weekday])))
2.144 output.append(fmt.table_cell(on=0))
2.145 - output.append(fmt.table_row(on=0))
2.146 +
2.147 + output.append(fmt.table_row(on=0))
2.148 +
2.149 + # Process the days of the month.
2.150 +
2.151 + start_weekday, number_of_days = calendar.monthrange(year, month)
2.152
2.153 - # Weekday headings.
2.154 + # The start weekday is the weekday of day number 1.
2.155 + # Find the first day of the week, counting from below zero, if
2.156 + # necessary, in order to land on the first day of the month as
2.157 + # day number 1.
2.158 +
2.159 + first_day = 1 - start_weekday
2.160 +
2.161 + while first_day <= number_of_days:
2.162 +
2.163 + # Output a week.
2.164
2.165 output.append(fmt.table_row(on=1))
2.166
2.167 for weekday in range(0, 7):
2.168 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-heading"}))
2.169 - output.append(fmt.text(_(weekday_labels[weekday])))
2.170 - output.append(fmt.table_cell(on=0))
2.171 + day = first_day + weekday
2.172 +
2.173 + # Output out-of-month days.
2.174 +
2.175 + if day < 1 or day > number_of_days:
2.176 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-excluded"}))
2.177 + output.append(fmt.table_cell(on=0))
2.178 +
2.179 + # Output normal days.
2.180 +
2.181 + else:
2.182 + # Get event details.
2.183 + # NOTE: Can be made more efficient.
2.184 +
2.185 + date = (year, month, day)
2.186 + day_events = []
2.187 +
2.188 + for event_page, event_details in shown_events.get((year, month), []):
2.189 +
2.190 + # Test for the event on the current day.
2.191 +
2.192 + if event_details["start"] <= date <= event_details["end"]:
2.193 + day_events.append((event_page, event_details))
2.194 +
2.195 + # Output the day.
2.196 +
2.197 + if day_events:
2.198 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-busy"}))
2.199 + else:
2.200 + output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-empty"}))
2.201 +
2.202 + output.append(fmt.div(on=1))
2.203 + output.append(fmt.span(on=1, css_class="event-day-number"))
2.204 + output.append(fmt.text(day))
2.205 + output.append(fmt.span(on=0))
2.206 + output.append(fmt.div(on=0))
2.207 +
2.208 + output.append(fmt.div(on=1, css_class="event-summaries"))
2.209 +
2.210 + # Show event details.
2.211 +
2.212 + for event_page, event_details in day_events:
2.213 +
2.214 + # Get a pretty version of the page name.
2.215 +
2.216 + pretty_pagename = getPrettyPageName(event_page)
2.217 +
2.218 + # Generate a colour for the event.
2.219 +
2.220 + bg = getColour(event_page.page_name)
2.221 + fg = getBlackOrWhite(bg)
2.222 +
2.223 + # Output the event.
2.224 +
2.225 + output.append(fmt.div(on=1, css_class="event-summary",
2.226 + style=("background-color: rgb(%d, %d, %d); color: rgb(%d, %d, %d);" % (bg + fg))))
2.227 + output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
2.228 + output.append(fmt.div(on=0))
2.229 +
2.230 + output.append(fmt.div(on=0))
2.231 +
2.232 + # End of day.
2.233 +
2.234 + output.append(fmt.table_cell(on=0))
2.235
2.236 output.append(fmt.table_row(on=0))
2.237
2.238 - # Process the days of the month.
2.239 -
2.240 - start_weekday, number_of_days = calendar.monthrange(year, month)
2.241 + first_day += 7
2.242
2.243 - # The start weekday is the weekday of day number 1.
2.244 - # Find the first day of the week, counting from below zero, if
2.245 - # necessary, in order to land on the first day of the month as
2.246 - # day number 1.
2.247 -
2.248 - first_day = 1 - start_weekday
2.249 -
2.250 - while first_day <= number_of_days:
2.251 + # End of month.
2.252
2.253 - # Output a week.
2.254 -
2.255 - output.append(fmt.table_row(on=1))
2.256 + output.append(fmt.table(on=0))
2.257
2.258 - for weekday in range(0, 7):
2.259 - day = first_day + weekday
2.260 + # Or output a summary view...
2.261
2.262 - # Output out-of-month days.
2.263 -
2.264 - if day < 1 or day > number_of_days:
2.265 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-excluded"}))
2.266 - output.append(fmt.table_cell(on=0))
2.267 + elif mode == "list":
2.268
2.269 - # Output normal days.
2.270 -
2.271 - else:
2.272 - # Get event details.
2.273 - # NOTE: Can be made more efficient.
2.274 -
2.275 - date = (year, month, day)
2.276 - day_events = []
2.277 -
2.278 - for event_page, event_details in shown_events:
2.279 -
2.280 - # Test for the event on the current day.
2.281 + output.append(fmt.listitem(on=1, attr={"class" : "event-listings-month"}))
2.282 + output.append(fmt.div(on=1, attr={"class" : "event-listings-month-heading"}))
2.283 + output.append(fmt.span(on=1))
2.284 + output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels
2.285 + output.append(fmt.span(on=0))
2.286 + output.append(fmt.text(" "))
2.287 + output.append(fmt.span(on=1))
2.288 + output.append(fmt.text(year))
2.289 + output.append(fmt.span(on=0))
2.290 + output.append(fmt.div(on=0))
2.291
2.292 - if event_details["start"] <= date <= event_details["end"]:
2.293 - day_events.append((event_page, event_details))
2.294 -
2.295 - # Output the day.
2.296 + output.append(fmt.bullet_list(on=1, attr={"class" : "event-month-listings"}))
2.297
2.298 - if day_events:
2.299 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-busy"}))
2.300 - else:
2.301 - output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-empty"}))
2.302 + for event_page, event_details in shown_events.get((year, month), []):
2.303
2.304 - output.append(fmt.div(on=1, css_class="event-day-number"))
2.305 - output.append(fmt.text(day))
2.306 - output.append(fmt.div(on=0))
2.307 + # Get a pretty version of the page name.
2.308
2.309 - # Show event details.
2.310 -
2.311 - for event_page, event_details in day_events:
2.312 -
2.313 - # Get a pretty version of the page name.
2.314 + pretty_pagename = getPrettyPageName(event_page)
2.315
2.316 - pretty_pagename = getPrettyPageName(event_page)
2.317 + output.append(fmt.listitem(on=1, attr={"class" : "event-listing"}))
2.318
2.319 - # Output the event.
2.320 -
2.321 - output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
2.322 - output.append(fmt.linebreak())
2.323 + # Link to the page using the pretty name.
2.324
2.325 - # End of day.
2.326 -
2.327 - output.append(fmt.table_cell(on=0))
2.328 -
2.329 - output.append(fmt.table_row(on=0))
2.330 + output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
2.331
2.332 - first_day += 7
2.333 -
2.334 - # End of month.
2.335 + # Add the event details.
2.336
2.337 - output.append(fmt.table(on=0))
2.338 -
2.339 - elif mode == "list":
2.340 - output.append(fmt.bullet_list(on=1, attr={"class" : "event-listings"}))
2.341 + output.append(fmt.definition_list(on=1, attr={"class" : "event-details"}))
2.342
2.343 - for event_page, event_details in shown_events:
2.344 -
2.345 - # Get a pretty version of the page name.
2.346 -
2.347 - pretty_pagename = getPrettyPageName(event_page)
2.348 -
2.349 - output.append(fmt.listitem(on=1, attr={"class" : "event-listing"}))
2.350 -
2.351 - # Link to the page using the pretty name.
2.352 -
2.353 - output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename)))
2.354 -
2.355 - # Add the event details.
2.356 + for key, value in event_details.items():
2.357 + output.append(fmt.definition_term(on=1))
2.358 + output.append(fmt.text(key))
2.359 + output.append(fmt.definition_term(on=0))
2.360 + output.append(fmt.definition_desc(on=1))
2.361 + output.append(fmt.text(value))
2.362 + output.append(fmt.definition_desc(on=0))
2.363
2.364 - output.append(fmt.definition_list(on=1, attr={"class" : "event-details"}))
2.365 + output.append(fmt.definition_list(on=0))
2.366 + output.append(fmt.listitem(on=0))
2.367
2.368 - for key, value in event_details.items():
2.369 - output.append(fmt.definition_term(on=1))
2.370 - output.append(fmt.text(key))
2.371 - output.append(fmt.definition_term(on=0))
2.372 - output.append(fmt.definition_desc(on=1))
2.373 - output.append(fmt.text(value))
2.374 - output.append(fmt.definition_desc(on=0))
2.375 + output.append(fmt.bullet_list(on=0))
2.376
2.377 - output.append(fmt.definition_list(on=0))
2.378 - output.append(fmt.listitem(on=0))
2.379 -
2.380 + if mode == "list":
2.381 output.append(fmt.bullet_list(on=0))
2.382
2.383 return ''.join(output)