def main(): usage="usage: %prog [options] calendar [calendar2 calendar3...]" optparser = OptionParser(usage=usage) optparser.add_option('-v','--verbose',dest='verbose', action="store_true", default=False, help='print info while running [default: %default]') optparser.add_option('-u','--username',dest='username', default=None, help='username for the DaviCal server') optparser.add_option('-p','--password',dest='password', default=None, help='password for the DaviCal server') optparser.add_option('-s','--server',dest='server', default='localhost', help='name of the DaviCal server [default: %default]') optparser.add_option('-f','--force',dest='force', action="store_true", default=False, help='force the upload') optparser.add_option('-z','--timezone',dest='tz', default='Pacific/Auckland', help='set the timezone [default: %default]') (opts,args) = optparser.parse_args() optparser.check_required('-u') optparser.check_required('-p') top_level_url = "http://" + opts.server global TZ TZ=pytz.timezone(opts.tz) # set a really long socket timeout in seconds socket.setdefaulttimeout(None) # build opener with HTTPCookieProcessor opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(), MultipartPostHandler.MultipartPostHandler ) urllib2.install_opener( opener ) login_ics(top_level_url + '/index.php', opts.username, opts.password) (userUrl, userId) = get_userUrl(top_level_url + '/users.php') editUrl=top_level_url + userUrl +'&edit=1' log('Logged into server %s. User is %s, Id is %s' %(opts.server,opts.username,userId),verbose=opts.verbose) for file in args: cal_name = splitext(basename(file))[0] cal_modified = get_lastModified(top_level_url + userUrl, '/'+opts.username+'/'+cal_name+'/') file_modified = get_fileModified(file) if (not opts.force) and (cal_modified != None) and (file_modified <= cal_modified): # skip the update if davical was updated more recently than the file log('Skipping file %s based on modifications times. File: %s DaviCal: %s' %(file,file_modified,cal_modified),verbose=opts.verbose) continue log('Processing file %s' %(file),verbose=opts.verbose) calendar = dayplan(file) temp = tempfile.mkstemp(suffix=".html") os.write(temp[0], calendar.pprint()) submit_ics(editUrl,open(temp[1], "rb"),cal_name,userId) os.remove(temp[1])
def loading_test(): fhandle = StringIO(test_calendar) p = dayplan(fhandle) i = 0 for event in p.calendar.getChildren(): i += 1 # the timezone information counts as an extra entry assert_equals(i, 7, 'Expect 7 entries. Got %s' % i)
def allday_test(): plan = """ 7/21/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 N All day event """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.dtstart.value, datetime.date(2009, 7, 21)) assert_equals(p.calendar.vevent.dtend.value, datetime.date(2009, 7, 22))
def weekly_test(): plan = """ 7/21/2009 16:0:0 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 0 4 0 0 N Weekly event. Tuesday at 4pm """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.rrule.value, 'FREQ=WEEKLY;BYDAY=TU')
def daily_test(): plan = """ 10/5/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 259200 1286323200 0 0 0 N Daily Event - every 3 days, end 2010 """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.rrule.value, 'FREQ=DAILY;INTERVAL=3;UNTIL=20101006')
def until_test(): plan = """ 9/11/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 1286323200 0 0 1 E 9/11/2010 N Yearly event """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert re.search('UNTIL=20101006', p.calendar.vevent.rrule.value)
def exception_test(): plan = """ 9/11/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 0 0 0 1 E 9/11/2010 N Yearly event """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.exdate.value, [datetime.date(2010, 9, 11)])
def translate_chars_test(): plan = """ 2/14/2009 8:30:0 2:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 1238198400 64 0 0 N this event has an 0xa0 char that kills unicode \xa0 """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() o = p.pprint() assert isinstance(o, unicode)
def monthly2_test(): plan = """ 9/11/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 0 8963 0 0 N Monthly event - first, second and last Sunday and Monday """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.rrule.value, 'FREQ=MONTHLY;BYDAY=1SU,1MO,2SU,2MO,-1SU,-1MO')
def monthly1_test(): plan = """ 9/11/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 0 0 15 0 N Monthly event - 1,2,3,LAST """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.rrule.value, 'FREQ=MONTHLY;BYMONTHDAY=1,2,3,-1')
def ensure_unicode_test(): # because the Ligthning/Sunbird calendar objects otherwise plan = """ 2/14/2009 8:30:0 2:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 1238198400 64 0 0 N Weekly event on Saturday, duration is 2hrs, until 2009/03/28 """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() o = p.pprint() assert isinstance(o, unicode)
def yearly_allday_test(): plan = """ 7/21/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 0 0 0 1 N All day event, repeat yearly """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.dtstart.value, datetime.date(2009, 7, 21)) assert_equals(p.calendar.vevent.dtend.value, datetime.date(2009, 7, 22)) assert_equals(p.calendar.vevent.rrule.value, 'FREQ=YEARLY')
def duration_test(): plan = """ 7/21/2009 16:0:0 1:30:0 0:0:0 0:0:0 ---------- 0 0 N Event at 4pm for 1hr 30min """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.dtstart.value, datetime.datetime(2009, 7, 21, 16, 0, tzinfo=defaultTZ)) assert_equals(p.calendar.vevent.dtend.value, datetime.datetime(2009, 7, 21, 17, 30, tzinfo=defaultTZ))
def yearly_test(): plan = """ 9/11/2009 99:99:99 0:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 0 0 0 1 E 9/11/2010 N Yearly event M This is the text M of my M YEARLY EVENT """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.rrule.value, 'FREQ=YEARLY')
def duration_and_until_test(): plan = """ 2/14/2009 8:30:0 2:0:0 0:0:0 0:0:0 ---------- 0 0 R 0 1238198400 64 0 0 N Weekly event on Saturday, duration is 2hrs, until 2009/03/28 """ fhandle = StringIO(plan) p = dayplan(fhandle) print p.pprint() assert_equals(p.calendar.vevent.dtstart.value, datetime.datetime(2009, 2, 14, 8, 30, tzinfo=defaultTZ)) assert_equals(p.calendar.vevent.dtend.value, datetime.datetime(2009, 2, 14, 10, 30, tzinfo=defaultTZ)) assert re.search('UNTIL=20090328', p.calendar.vevent.rrule.value)