Attachment 'Calendar.py'
Download 1 """
2 MoinMoin processor for calendars.
3 This is currently version 1.5 (nm 20040707)
4
5 $Id: Calendar.py,v 1.4 2004/07/07 14:12:10 nigel Exp $
6
7 This calendar module has been seriously refactored so that it outputs
8 wiki text format which is pushed through the formatter.
9 This means wiki markup and links within the cells work - however you are
10 restricted to keeping the per-date cell stuff to a single line, and its
11 going to be pretty easy to come up with combinations of markup in the
12 cells which conflict with the markup I am adding and break things.
13
14 This is very much a trial of the idea - comments welcome...
15
16 History:
17 20030328 ntd (1.0) - Original version
18 20040702 cpj - Modified output code to avoid stdout
19 20040707 nm (1.5) - Rewrote to output wikitext rather than HTML
20 and pass that text to the formatter.
21
22 Work to do:
23 Handle date ranges for events
24 Hook to stylesheets?
25 Can I make the title show up bigger?
26
27 Input Example
28
29 {{{
30 #!Calendar (-)
31 OPTION:bodyBGColor=#DDDDDD
32 OPTION:captionBGColor=#CCCCFF
33 OPTION:firstWeekDay=6
34 20030328: Calendar 1.0 released
35 20030301: First of the month
36 20030331: End of the month
37 20030331: Last day...
38 20030401: First day of next month
39 20030430: Link to FrontPage
40 }}}
41
42 """
43
44 from MoinMoin.parser import wiki
45 import string, sys
46 import calendar
47
48 class EventCalendar:
49 def __init__(self, year, month, **kwargs):
50 self.year = year
51 self.month = month
52 self.days = { }
53 self.captionBGColor = "#D0D0D0"
54 self.bodyBGColor = "#FFFFFF"
55 self.firstWeekDay = calendar.SUNDAY
56
57
58 #-- Process keyword arguments.
59 if kwargs.has_key("captionBGColor"):
60 self.captionBGColor = kwargs['captionBGColor']
61 if kwargs.has_key("bodyBGColor"):
62 self.bodyBGColor = kwargs['bodyBGColor']
63 if kwargs.has_key("firstWeekDay"):
64 self.firstWeekDay = int(kwargs['firstWeekDay'])
65
66
67 lastDay = calendar.monthrange(year, month)[1]
68 for i in xrange(1,lastDay+1):
69 self.days[i] = [ ]
70
71 def addEvent(self,day,event):
72 self.days[day].append(event)
73
74 def toWIKI(self):
75 EnglishMonthText = {
76 1: 'January',
77 2: 'February',
78 3: 'March',
79 4: 'April',
80 5: 'May',
81 6: 'June',
82 7: 'July',
83 8: 'August',
84 9: 'September',
85 10: 'October',
86 11: 'November',
87 12: 'December'
88 }
89
90 EnglishDayText = {
91 0: 'Monday',
92 1: 'Tuesday',
93 2: 'Wednesday',
94 3: 'Thursday',
95 4: 'Friday',
96 5: 'Saturday',
97 6: 'Sunday'
98 }
99
100 #-- Get the first weekday defined in the calendar module and save it temporarily
101 fwd = calendar.firstweekday( )
102
103 #-- Set the first weekday
104 calendar.setfirstweekday(self.firstWeekDay)
105
106
107 captionTemplate = """||<tablewidth="100%%" rowbgcolor="%(captionBGColor)s" :-7>'''%(month)s %(year)s'''||"""
108
109 dayCaptionTemplate = """||<%(captionBGColor)s 14%%)> '''`%(day)s`'''"""
110
111 dayBodyTemplate = """||<%(bodyBGColor)s 14%%> %(text)s"""
112
113 weekdayHeaderRowTemplate = """||<%(captionBGColor)s )14%%> '''`%(weekday)s`'''"""
114
115 # Build a weekday header row - could be reused if there is more than one calendar
116 weekdayHeaderRow = ""
117 for i in range(calendar.firstweekday( ), calendar.firstweekday( )+7):
118 d = i%7
119 weekdayHeaderRow += weekdayHeaderRowTemplate%{'weekday':EnglishDayText[d], 'captionBGColor':self.captionBGColor}
120 weekdayHeaderRow += '||'
121
122 calList = calendar.monthcalendar(self.year, self.month)
123 weekRows = len(calList)
124
125 outrows = [ ]
126 outrows.append(captionTemplate%{'year': self.year,
127 'month': EnglishMonthText[self.month],
128 'captionBGColor':self.captionBGColor})
129 outrows.append(weekdayHeaderRow)
130 for week in calList:
131 captions = ""
132 bodies = ""
133 for day in week:
134 if day == 0:
135 caption = " "
136 body = " [[BR]] "
137 captionBGColor = self.bodyBGColor
138 else:
139 captionBGColor = self.captionBGColor
140 caption = "%s" % day
141 if len(self.days[day]) ==0:
142 body = " [[BR]] "
143 else:
144 body = ""
145 for event in self.days[day]:
146 body += "%s[[BR]]" % event
147 captions += dayCaptionTemplate % {'day':caption,
148 'captionBGColor':captionBGColor}
149 bodies += dayBodyTemplate % {'text':body,
150 'bodyBGColor':self.bodyBGColor}
151 captions += "||"
152 bodies += "||"
153 outrows.append(captions)
154 outrows.append(bodies)
155
156 # add some forced space
157 outrows.append("[[BR]]")
158
159 #-- Restore the first week day that was previously defined in the calendar module.
160 calendar.setfirstweekday(fwd)
161
162 return str.join("\n", outrows)
163
164
165 def process(request, formatter, lines):
166 events = { }
167 kwargs = { }
168
169 for line in lines:
170 text = string.strip(line)
171
172 if len(text) > 0:
173 i = string.find(text,":")
174 if i >= 0:
175 if text[:i] == 'OPTION':
176 option = text[i+1:]
177 j = string.find(option,"=")
178 if j > 0:
179 keyword = string.strip(option[:j])
180 value = string.strip(option[j+1:])
181 kwargs[keyword] = value
182 else:
183 eventDate = text[:i]
184 eventText = text[i+1:]
185
186 eventYear = int(eventDate[:4])
187 eventMonth = int(eventDate[4:6])
188 eventDay = int(eventDate[6:8])
189
190 if not events.has_key(eventYear):
191 events[eventYear] = { }
192 if not events[eventYear].has_key(eventMonth):
193 events[eventYear][eventMonth] = { }
194 if not events[eventYear][eventMonth].has_key(eventDay):
195 events[eventYear][eventMonth][eventDay] = [ ]
196
197 events[eventYear][eventMonth][eventDay].append(eventText)
198
199 cals = []
200 for year in events.keys( ):
201 for month in events[year].keys( ):
202 cal = apply(EventCalendar, (year, month), kwargs)
203 for day in events[year][month].keys( ):
204 for event in events[year][month][day]:
205 cal.addEvent(day,event)
206 cals.append((year,month,cal))
207 cals.sort( )
208 output = []
209 for item in cals:
210 year, month, cal = item
211 output.append("")
212 output.append(cal.toWIKI( ))
213 output.append("")
214 output.append("")
215 wikiizer = wiki.Parser("\n".join(output), request)
216 wikiizer.format(formatter)
217
218 """
219 march = EventCalendar(2003,3)
220 march.addEvent(14, "St. Patrick's Day")
221 march.addEvent(20, "First Day of Spring")
222 march.addEvent(20, "Joe's Birthday")
223 print march.toWIKI( )
224 """
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.