#!/usr/bin/python """Generate HTML calendars This module reads a text file containing event information and generates a calendar web page containing those event tags in each day in a nicely formatted table where each tag links to the event text listed under the calendar written by manny juan manny@jps.net 09/15/97 """ import sys from calend2 import * import cgi,string from calnames import * def go(): print "Content-type: text/html" # identify response as HTML print # end of headers print "Event Calendar Generator" try: # eg query = "M=April&Y=1987" try: # have the cgi module parse the data for you... form = cgi.FieldStorage() MField = form["M"] YField = form["Y"] EvtField = form["events"] except (KeyError, TypeError): print "Sorry, I need both Month and Year",Mfield,YField,EvtField raise SystemExit # get the values of the fields try: M = MField.value Y = YField.value events=string.split(EvtField.value,'\n') except AttributeError: print "Sorry, Multiple months or multiple years are not allowed." raise SystemExit # convert the month and year to a number try: month = string.atoi(M) year = string.atoi(Y) except ValueError: print "
OOPS: year or month isn't a number (!) " + `Y` + ',' + `M` raise SystemExit # get the values of the fields try: LangField = form["lang"] BegwField = form["begw"] except (KeyError): pass try: lang = string.atoi(LangField.value) except ValueError: lang = 1 try: begw = string.atoi(BegwField.value) except ValueError: begw = 0 prmonth(year, month, lang, begw, events) finally: print '
Event calendar automatically generated by Python script', print ' evt2htm.cgi', print " found at manny juan's python page" print '' def prmonth(year, month, lang, begw, events): evt,evtstore,maxdd,count = parse_events(year,month,events) print '' gen_cal(year,month,lang,begw,maxdd,count,evt,evtstore) emit_events(year,month,begw,maxdd,count,evt,evtstore) import regex cmtpat = regex.compile('^ *\#') datepat = regex.compile('^ *\([0-9*][0-9]?\)[/]\([0-9][0-9]?\)') wordpat = regex.compile('^ *\([^ *\t*]+\)') def parse_events(year,month,events): maxdd=0 evn=0 evt={} evtstore={} count={} daysinmonth=mdays[month] if (isleap(year) & (month==2)): daysinmonth=daysinmonth+1 for day in range(daysinmonth+1): count[day]=0 for line in events: if string.strip(line) == '': #skip blank lines continue if cmtpat.search(line) > -1: continue if datepat.search(line) > -1: xmonth , xday = datepat.group(1,2) mth=string.atoi(xmonth) # select only the days for the month selected if mth==month: day=string.atoi(xday) line=line[datepat.regs[0][1]:] # first token must be date eg. 12/31 if wordpat.search(line) > -1: tag = wordpat.group(1) line=string.strip(line[wordpat.regs[0][1]:]) if len(line)==0: continue evn=evn+1 count[day]=count[day]+1 # keep track of cell height if (count[day]>maxdd): maxdd=count[day] # store event number and (event tag, event text) evt[(day,count[day])]=evn evtstore[evn]=(tag,line) return (evt,evtstore,maxdd,count) def gen_cal(year,month,lang,begw,maxdd,count,evt,evtstore): weeks=monthcalendar(year,month,begw) month_name,week_name=month_lits[lang-1] evseq=0 print '
' print ''+month_name[month-1],str(year)+'' print '' print '' for week in weeks: print '' for day in week: print '0): print 'width=5>'+str(day), else: print '>', print '' print '' print '' for day in week: print '' print '' print '
%s' % (week_name[(0+begw)%7]) print '%s' % (week_name[(1+begw)%7]) print '%s' % (week_name[(2+begw)%7]) print '%s' % (week_name[(3+begw)%7]) print '%s' % (week_name[(4+begw)%7]) print '%s' % (week_name[(5+begw)%7]) print '%s' % (week_name[(6+begw)%7]) print '
', if(day>0): for i in range(maxdd): if i>count[day]-1: print ' 
' else: evn=evt[(day,i+1)] tag,event=evtstore[evn] # this will also be the listing sequence (and link) evseq=evseq+1 print ''+tag+'
' print '
' print '
' def emit_events(year,month,begw,maxdd,count,evt,evtstore): weeks=monthcalendar(year,month,begw) evseq=0 print '


' for week in weeks: for day in week: if(day>0): for i in range(maxdd): if i
go to top
' # use this sequence as anchor evseq=evseq+1 print '
'+tag+'
', print '%d'%month+'/'+'%02d'%day+'
' print event+'
' if __name__ == '__main__': go()