Attachment 'FIXEDCalendar.py'

Download

   1 """
   2  MoinMoin parser for calendars.
   3  Basically a bug fixed version of http://moinmoin.wikiwikiweb.de/MoinMoinCalendarExample
   4  Converted from a processor into a parser.
   5  Tested with MoinMoin 1.5.3
   6 
   7 clach04 - 06-May-2006
   8 """
   9 
  10 #
  11 # clach04 06-May-2006
  12 # shamelessly stolen parser example from 
  13 # http://moinmoin.wikiwikiweb.de/UnifyParsersAndProcessors?highlight=%28Processors%29
  14 # Added format_args to init as without, MoinMoin doesn't accept it
  15 
  16 Dependencies = ["time"]
  17 
  18 #####
  19 ##### from http://moinmoin.wikiwikiweb.de/MoinMoinCalendarExample
  20 ##### sys.stdout.write() calls replaced with request.write() calls
  21 """
  22  MoinMoin processor for calendars.
  23  This is currently version 1.0 (ntd 20030328)
  24 
  25  Work to do:
  26  v1.1 handle date ranges for events
  27  v2.0 full wiki processing in each cell of calendar
  28 """
  29 
  30 import string, sys
  31 import calendar
  32 
  33 class EventCalendar:
  34     def __init__(self, year, month, **kwargs):
  35         self.year = year
  36         self.month = month
  37         self.days = { }
  38         self.captionBGColor = "#D0D0D0"
  39         self.bodyBGColor = "#FFFFFF"
  40         self.firstWeekDay = calendar.SUNDAY
  41 
  42 
  43         #-- Process keyword arguments.
  44         if kwargs.has_key("captionBGColor"):
  45             self.captionBGColor = kwargs['captionBGColor']
  46         if kwargs.has_key("bodyBGColor"):
  47             self.bodyBGColor = kwargs['bodyBGColor']
  48         if kwargs.has_key("firstWeekDay"):
  49             self.firstWeekDay = int(kwargs['firstWeekDay'])
  50 
  51 
  52         lastDay = calendar.monthrange(year, month)[1]
  53         for i in xrange(1,lastDay+1):
  54             self.days[i] = [ ]
  55 
  56     def addEvent(self,day,event):
  57         self.days[day].append(event)
  58 
  59     def toHTML(self):
  60         EnglishMonthText = {
  61             1: 'January',
  62             2: 'February',
  63             3: 'March',
  64             4: 'April',
  65             5: 'May',
  66             6: 'June',
  67             7: 'July',
  68             8: 'August',
  69             9: 'September',
  70             10: 'October',
  71             11: 'November',
  72             12: 'December'
  73         }
  74 
  75         EnglishDayText = {
  76             0: 'Monday',
  77             1: 'Tuesday',
  78             2: 'Wednesday',
  79             3: 'Thursday',
  80             4: 'Friday',
  81             5: 'Saturday',
  82             6: 'Sunday'
  83         }
  84 
  85         #-- Get the first weekday defined in the calendar module and save it temporarily
  86         fwd = calendar.firstweekday( )
  87 
  88         #-- Set the first weekday
  89         calendar.setfirstweekday(self.firstWeekDay)
  90 
  91 
  92         captionTemplate = """
  93 <table width="100%%" border="1" cellspacing="0" cellpadding="2">
  94     <tr>
  95         <td bgcolor="%(captionBGColor)s" align="center" valign="middle">
  96             <font size="+2"><tt><b>%(month)s %(year)s</b></tt></font>
  97         </td>
  98     </tr>
  99 </table>
 100         """
 101 
 102 
 103         dayCaptionTemplate = """
 104     <td align="right" valign="middle" bgcolor="%(captionBGColor)s" width="14%%"><tt><b>%(day)s</b></tt></td>
 105         """
 106 
 107 
 108         dayBodyTemplate = """
 109     <td valign="top" bgcolor="%(bodyBGColor)s" width="14%%"><p><tt>%(text)s</tt></p></td>
 110         """
 111 
 112 
 113         weekdayHeaderRowTemplate = """
 114         <td bgcolor="%(captionBGColor)s" align="center" valign="middle" width="14%%"><tt><b>%(weekday)s</b></tt></td>
 115         """
 116 
 117 
 118         weekdayHeaderRow = """
 119 <table width="100%%" border="1" cellspacing="0" cellpadding="2">
 120     <tr>
 121         """
 122 
 123         for i in range(calendar.firstweekday( ), calendar.firstweekday( )+7):
 124             d = i%7
 125             weekdayHeaderRow += weekdayHeaderRowTemplate%{'weekday':EnglishDayText[d], 'captionBGColor':self.captionBGColor}
 126         weekdayHeaderRow += """
 127     </tr>
 128 </table>
 129         """
 130 
 131         calList = calendar.monthcalendar(self.year, self.month)
 132         weekRows = len(calList)
 133 
 134         weeks = [ ]
 135         for week in calList:
 136             captions = "<tr>"
 137             bodies = "<tr>"
 138             for day in week:
 139                 if day == 0:
 140                     caption = "&nbsp;"
 141                     body = "&nbsp;"
 142                     captionBGColor = self.bodyBGColor
 143                 else:
 144                     captionBGColor = self.captionBGColor
 145                     caption = "%s"%day
 146                     if len(self.days[day]) ==0:
 147                         body = "&nbsp;"
 148                     else:
 149                         body = ""
 150                         for event in self.days[day]:
 151                             body += "%s<br>"%event
 152                 captions += dayCaptionTemplate%{'day':caption, 'captionBGColor':captionBGColor}
 153                 bodies += dayBodyTemplate%{'text':body, 'bodyBGColor':self.bodyBGColor}
 154             captions += "</tr>"
 155             bodies += "</tr>"
 156             week = captions + bodies
 157             weeks.append(week)
 158 
 159         #-- Put it all together...
 160         html = captionTemplate%{'year': self.year, 'month': EnglishMonthText[self.month], 'captionBGColor':self.captionBGColor}
 161         html += weekdayHeaderRow
 162         html += '<table width="100%%" border="1" cellspacing="0" cellpadding="2">%s</table>'%''.join(weeks)
 163 
 164         #-- Restore the first week day that was previously defined in the calendar module.
 165         calendar.setfirstweekday(fwd)
 166 
 167         return html
 168 
 169 
 170 def process(request, formatter, lines):
 171     events = { }
 172     kwargs = { }
 173 
 174     lines=lines.split('\n') ## clach04 lines is a string not a list, etc. (using moin 1.5.3 src tarball as of 06-May-2006)
 175     for line in lines:
 176         text = string.strip(line)
 177 
 178         if len(text) > 0:
 179             i = string.find(text,":")
 180             if i >= 0:
 181                 if text[:i] == 'OPTION':
 182                     option = text[i+1:]
 183                     j = string.find(option,"=")
 184                     if j > 0:
 185                         keyword = string.strip(option[:j])
 186                         ## clach04, convert from Unicode to single byte,
 187                         ## (Python 2.3) apply() only accepts single byte strings as keys!
 188                         keyword = str(keyword ) 
 189                         value = string.strip(option[j+1:])
 190                         kwargs[keyword] = value
 191                 else:
 192                     eventDate = text[:i]
 193                     eventText = text[i+1:]
 194 
 195                     eventYear = int(eventDate[:4])
 196                     eventMonth = int(eventDate[4:6])
 197                     eventDay = int(eventDate[6:8])
 198 
 199                     if not events.has_key(eventYear):
 200                         events[eventYear] = { }
 201                     if not events[eventYear].has_key(eventMonth):
 202                         events[eventYear][eventMonth] = { }
 203                     if not events[eventYear][eventMonth].has_key(eventDay):
 204                         events[eventYear][eventMonth][eventDay] = [ ]
 205 
 206                     events[eventYear][eventMonth][eventDay].append(eventText)
 207 
 208     cals = [ ]
 209     for year in events.keys( ):
 210         for month in events[year].keys( ):
 211             cal = apply(EventCalendar, (year, month), kwargs)
 212             for day in events[year][month].keys( ):
 213                 for event in events[year][month][day]:
 214                     cal.addEvent(day,event)
 215             cals.append((year,month,cal))
 216     cals.sort( )
 217 
 218     for item in cals:
 219         year, month, cal = item
 220         request.write("<p>")
 221         request.write(cal.toHTML( ))
 222         request.write("</p>")
 223 
 224 """
 225 march = EventCalendar(2003,3)
 226 march.addEvent(14, "St. Patrick's Day")
 227 march.addEvent(20, "First Day of Spring")
 228 march.addEvent(20, "Joe's Birthday")
 229 print march.toHTML( )
 230 """
 231 
 232 
 233 
 234 """
 235 parser code, template taken from
 236     http://moinmoin.wikiwikiweb.de/UnifyParsersAndProcessors?highlight=%28Processors%29
 237     
 238 Added format_args to __init__. Without init(), MoinMoin doesn't accept it
 239 and raises an exception.
 240 """
 241 class Parser:
 242     """ a Calendar parser """
 243 
 244     def __init__(self, raw, request, format_args=None):
 245         # save call arguments for later use in format
 246         self.raw = raw
 247         self.request = request
 248         self.format_args = format_args
 249 
 250     def format(self, formatter):
 251         # this has the function of the old processor method
 252         # just use self.raw and self.request
 253         process(self.request, formatter, self.raw)

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.
  • [get | view] (2006-05-07 18:04:51, 8.0 KB) [[attachment:FIXEDCalendar.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.