Attachment 'ics.v4.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3    Some comments....
   4 """
   5 Dependencies = []
   6 import os,string,Image,StringIO
   7 from MoinMoin import config, wikiutil, version
   8 from MoinMoin.PageEditor import PageEditor
   9 from MoinMoin import user, util
  10 from MoinMoin.Page import Page
  11 from MoinMoin.action import AttachFile
  12 from MoinMoin.formatter.text_html import Formatter
  13 from MoinMoin.parser import wiki
  14 from MoinMoin.util import MoinMoinNoFooter, pysupport
  15 from datetime import datetime, timedelta, date, time
  16 from icalendar import Calendar, Event, UTC, Alarm
  17 
  18 from types import TupleType, ListType
  19 SequenceTypes = [TupleType, ListType]
  20 import re
  21 from string import atoi
  22 
  23 
  24 action_name = __name__.split('.')[-1]
  25 
  26 def execute(pagename, request):
  27     if not request.user.may.read(pagename):
  28         Page(request, pagename).send_page(request)
  29         return
  30     
  31     _ = request.getText
  32     
  33     debug = True
  34     
  35     page = Page(request, pagename)
  36     text = page.get_raw_body()
  37     
  38     request.http_headers(["Content-type: text/plain;charset=%s" % config.charset])
  39     
  40     try:
  41         alarm_minutes = int(request.form.get('alarm', [-1])[0])
  42     except ValueError:
  43         alarm_minutes = -1
  44     
  45     lines = re.findall('[^\n]+',text)
  46     
  47     cal = Calendar()
  48     cal.add('prodid', '-//My calendar product//mxm.dk//')
  49     cal.add('version', '2.0')
  50     
  51     errors = []
  52     
  53     for line in lines:
  54         if re.match('^#',line) != None:
  55             continue
  56         if debug:
  57             request.write('line=',line,'\n')
  58         words = re.findall('[^\s]+',line)
  59         indesc = False # remember when we have entered the description part of the line
  60         d1 = '-1'
  61         m1 = '-1'
  62         y1 = '-1'
  63         d2 = '-1'
  64         m2 = '-1'
  65         y2 = '-1'
  66         h1 = '-1'
  67         min1 = '-1'
  68         h2 = '-1'
  69         min2 = '-1'
  70         hasEnd = False
  71         hasTime = False
  72         hasDate = False
  73         summary = ''
  74         delta = None
  75         weekday = '' # if set -> weekly recurrence
  76         for word in words:
  77             if debug:
  78                 request.write('  word="',word,'"\n')
  79                 request.write('indesc=')
  80                 if indesc:
  81                     request.write('True')
  82                 else:
  83                     request.write('False')
  84                 request.write('\n')
  85             if indesc == False:
  86                 if word == 'Mondays':
  87                     weekday = 'MO'
  88                 elif word == 'Tuesdays':
  89                     weekday = 'TU'
  90                 elif word == 'Wednesdays':
  91                     weekday = 'WE'
  92                 elif word == 'Thursdays':
  93                     weekday = 'TH'
  94                 elif word == 'Fridays':
  95                     weekday = 'FR'
  96                 elif word == 'Saturdays':
  97                     weekday = 'SA'
  98                 elif word == 'Sundays':
  99                     weekday = 'SU'
 100                 # dates...
 101                 elif re.match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,4}$',word) != None:
 102                     if debug:
 103                         request.write('matched DD.MM.YYYY\n')
 104                     d1,m1,y1 = re.findall('[0-9]+',word)
 105                     hasEnd = False
 106                     delta = timedelta(1)
 107                 elif re.match('^[0-9]{1,2}\.{0,1}-[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,4}$',word) != None:
 108                     if debug:
 109                         request.write('matched DD-DD.MM.YYYY\n')
 110                     d1,d2,m1,y1 = re.findall('[0-9]+',word)
 111                     m2 = m1
 112                     y2 = y1
 113                     hasEnd = True
 114                     delta = timedelta(1)
 115                     indesc = True # don't allow time definition
 116                 elif re.match('^[0-9]{1,2}\.[0-9]{1,2}\.{0,1}-[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,4}$',word) != None:
 117                     if debug:
 118                         request.write('matched DD.MM-DD.MM.YYYY\n')
 119                     d1,m1,d2,m2,y1 = re.findall('[0-9]+',word)
 120                     y2 = y1
 121                     hasEnd = True
 122                     delta = timedelta(1)
 123                     indesc = True # don't allow time definition
 124                 elif re.match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,4}-[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,4}$',word) != None:
 125                     if debug:
 126                         request.write('matched DD.MM.YYYY-DD.MM.YYYY\n')
 127                     d1,m1,y1,d2,m2,y2 = re.findall('[0-9]+',word)
 128                     hasEnd = True
 129                     delta = timedelta(1)
 130                     indesc = True # don't allow time definition
 131                 # times...
 132                 elif re.match('^[0-9]{1,2}:[0-9]{1,2}$',word) != None:
 133                     if debug:
 134                         request.write('matched HH:MM\n')
 135                     h1,min1 = re.findall('[0-9]+',word)
 136                     hasEnd = False
 137                     hasTime = True
 138                     delta = timedelta(0,3600) # def evt len = 1h if time of day is given
 139                 elif re.match('^[0-9]{1,2}:[0-9]{1,2}-[0-9]{1,2}:[0-9]{1,2}$',word) != None:
 140                     if debug:
 141                         request.write('matched HH:MM-HH:MM\n')
 142                     h1,min1,h2,min2 = re.findall('[0-9]+',word)
 143                     y2 = y1
 144                     m2 = m1
 145                     d2 = d1
 146                     hasEnd = True
 147                     indesc = True
 148                     hasTime = True
 149                     delta = None
 150                 elif hasTime and re.match('^[0-9]+m$',word) != None:
 151                     if debug:
 152                         request.write('matched m\n')
 153                     mins = re.findall('[0-9]+',word)[0]
 154                     mins = atoi(mins) * 60
 155                     delta = timedelta(0,mins)
 156                     hasEnd = False
 157                     indesc = True
 158                 elif hasTime and re.match('^[0-9]+h$',word) != None:
 159                     if debug:
 160                         request.write('matched h\n')
 161                     h = re.findall('[0-9]+',word)[0]
 162                     delta = timedelta(0,atoi(h)*3600)
 163                     hasEnd = False
 164                     indesc = True
 165                 else:
 166                     indesc = True
 167                     summary += word
 168             else:
 169                 if len(summary)>0:
 170                     summary += ' '
 171                 summary += word
 172         if debug:
 173             request.write('d1=',d1,', m1=',m1,', y1=',y1,', summary=',summary,'\n')
 174             request.write('h1=',h1,', min1=',min1,'\n')
 175             request.write('d2=',d2,', m2=',m2,', y2=',y2,'\n')
 176             request.write('h2=',h2,', min2=',min2,', weekday=',weekday,'\n')
 177             request.write('hasTime=')
 178             if hasTime:
 179                 request.write('True')
 180             else:
 181                 request.write('False')
 182             request.write('\n')
 183             request.write('hasEnd=')
 184             if hasEnd:
 185                 request.write('True')
 186             else:
 187                 request.write('False')
 188             request.write('\n')
 189             request.write('delta')
 190             if delta == None:
 191                 request.write('==None')
 192             else:
 193                 request.write('!=None')
 194             request.write('\n')
 195         event = Event()
 196         event.add('summary', summary)
 197         #event.add('description', text)
 198         startdate = None
 199         enddate = None
 200         starttime = None
 201         endtime = None
 202         start = None
 203         end = None
 204         if d1 != '-1':
 205             d1 = atoi(d1)
 206             m1 = atoi(m1)
 207             y1 = atoi(y1)
 208             if y1 < 2000:
 209                 y1 += 2000
 210             startdate = date(y1,m1,d1)
 211         if d2 != '-1':
 212             d2 = atoi(d2)
 213             m2 = atoi(m2)
 214             y2 = atoi(y2)
 215             if y2 < 2000:
 216                 y2 += 2000
 217             enddate = date(y2,m2,d2)
 218         if min1 == '-1':
 219             min1 = '0'
 220         if h1 != '-1':
 221             h1 = atoi(h1)
 222             min1 = atoi(min1)
 223             starttime = time(h1,min1,tzinfo=UTC)
 224         if min2 == '-1':
 225             min2 = '0'
 226         if h2 != '-1':
 227             h2 = atoi(h2)
 228             min2 = atoi(min2)
 229             endtime = time(h2,min2,tzinfo=UTC)
 230         if weekday == '': # no recurrency
 231             if startdate == None:
 232                 errors += line
 233                 continue
 234             elif starttime == None:
 235                 start = startdate
 236             else:
 237                 start = datetime.combine(startdate,starttime)
 238             if endtime == None:
 239                 end = enddate
 240             else:
 241                 end = datetime.combine(enddate,endtime)
 242             if end == None:
 243                 end = start
 244             if delta != None:
 245                 end += delta
 246             event.add('dtstart', start)
 247             event.add('dtend', end)
 248             event.add('dtstamp', start)
 249         else: # weekly recurrency
 250             # when to start recurrences? (also includes starting time of event)
 251             if startdate == None:
 252                 startdate = date.today() - timedelta(7) # no date -> run along...
 253             if starttime == None:
 254                 start = startdate
 255             else:
 256                 start = datetime.combine(startdate,starttime)
 257             # when to stop recurrences? (no time)
 258             if enddate == None:
 259                 until = date.today() + timedelta(84)
 260             else:
 261                 until = enddate
 262             # end of each event, NOT end of recurrence!
 263             if endtime == None:
 264                 end = start
 265             else:
 266                 end = datetime.combine(startdate,endtime)
 267             if delta != None:
 268                 end += delta
 269             r = dict(freq='WEEKLY', interval=1, byday=weekday)
 270             r['UNTIL'] = until;
 271             event.add('rrule',r)
 272             event.add('dtstart', start)
 273             event.add('dtend', end)
 274             event.add('dtstamp', start)
 275         if alarm_minutes >= 0:
 276             alarm = Alarm()
 277             alarm.add('description', '')
 278             alarm.add('action', 'DISPLAY')
 279             if hasTime:
 280                 alarm.add('trigger', start-timedelta(0,alarm_minutes*60))
 281             else:
 282                 #alarm.add('trigger', datetime.combine(start,time())-timedelta(0,18*3600))
 283                 alarm.add('trigger', start-timedelta(1))
 284             event.add_component(alarm)
 285         #event['uid'] = '20050115T101010/27346262376@mxm.dk'
 286         event.add('priority', 5)
 287         cal.add_component(event)
 288         
 289     # wrap error message in event *g*
 290     errdesc = ''
 291     for line in errors:
 292         if errdesc != '':
 293             errdesc += '\n'
 294         errdesc += line
 295     if len(errors)>0:
 296         errdesc = 'The following lines contain errors:\n\n' + errdesc
 297         event = Event()
 298         event.add('summary', 'IcsProducer Errors')
 299         event.add('description', errdesc)
 300         event.add('dtstart', datetime.utcnow())
 301         event.add('dtend', datetime.utcnow() + timedelta(0,10800))
 302         event.add('dtstamp', datetime.utcnow())
 303         event.add('priority', 5)
 304         cal.add_component(event)
 305     
 306     request.write(cal.as_string())
 307     raise MoinMoinNoFooter

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-03-01 18:13:38, 10.8 KB) [[attachment:ics.v4.py]]
 All files | Selected Files: delete move to page copy to page

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