Using dateutil.rrule

This commit is contained in:
2018-02-27 00:42:38 +01:00
parent 2956827c72
commit 2f1c4d09dc
2 changed files with 60 additions and 65 deletions

View File

@@ -6,67 +6,48 @@ import calendar
from yaml import load from yaml import load
from icalendar.parser import Contentlines, Contentline from icalendar.parser import Contentlines, Contentline
from dateutil.rrule import rrule, FREQNAMES
import caldav import caldav
class Task: class Task:
def __init__(self, ref_date, name, data): def __init__(self, ref_date, name, data):
self.ref_date = ref_date self.ref_date = ref_date
self.name = name self.name = name
only = data['only'].split(',') if 'only' in data else None data['start']['freq'] = FREQNAMES.index(data['start']['freq'])
if data['periodicity'] == 'monthly': self.date_begin = rrule(count=1, dtstart=ref_date, **data['start'])[0]
self.date_begin = self.parse_date_monthly(data['date-begin'], only) self.date_end = self.date_begin + datetime.timedelta(microseconds=-1, **data['duration'])
self.date_end = self.parse_date_monthly(data['date-end'], only)
elif data['periodicity'] == 'weekly':
pass
self.title = data['title'] self.title = data['title']
self.body = data['body'] self.body = data['body']
self.priority = data['priority'] self.priority = data['priority']
def parse_date_monthly(self, day, only=None):
if day < 0:
last_day_month = calendar.monthrange(self.ref_date.year, self.ref_date.month)[1]
day = last_day_month + day + 1
if only is not None:
only.sort()
for selected in only:
if int(selected) >= self.ref_date.month:
month = int(selected)
break
if month is None:
month = only[0]
else:
month = self.ref_date.month
return datetime.date(year=self.ref_date.year, month=month, day=day)
def to_ical(self): def to_ical(self):
current_dtstamp = datetime.datetime.now().strftime('%Y%m%dT%H%M%SZ') current_dtstamp = datetime.datetime.now().strftime('%Y%m%dT%H%M%SZ')
uid = "DV-Taskmanager-%s-%s" % (self.name, self.date_end.strftime('%Y%m%d')) uid = "DV-Taskmanager-%s-%s" % (self.name, self.date_end.strftime('%Y%m%d'))
c = Contentlines([ c = Contentlines([
Contentline('BEGIN:VCALENDAR'), Contentline('BEGIN:VCALENDAR'),
Contentline('VERSION:2.0'), Contentline('VERSION:2.0'),
Contentline('PRODID:-//Example Corp.//CalDAV Client//EN'), Contentline('PRODID:-//Example Corp.//CalDAV Client//EN'),
Contentline('BEGIN:VTODO'), Contentline('BEGIN:VTODO'),
Contentline('DTSTAMP:%s' % current_dtstamp), Contentline('DTSTAMP:%s' % current_dtstamp),
Contentline('CREATED:%s' % current_dtstamp), Contentline('CREATED:%s' % current_dtstamp),
Contentline('UID:%s' % uid), Contentline('UID:%s' % uid),
Contentline('SEQUENCE:%s' % '4'), Contentline('SEQUENCE:%s' % '4'),
Contentline('LAST-MODIFIED:%s' % current_dtstamp), Contentline('LAST-MODIFIED:%s' % current_dtstamp),
Contentline('DESCRIPTION:%s' % self.body), Contentline('DESCRIPTION:%s' % self.body),
Contentline('SUMMARY:%s' % self.title), Contentline('SUMMARY:%s' % self.title),
Contentline('PRIORITY:%s' % 1), Contentline('PRIORITY:%s' % 1),
Contentline('DUE;VALUE=DATE:%s' % self.date_end.strftime('%Y%m%d')), Contentline('DUE;VALUE=DATE:%s' % self.date_end.strftime('%Y%m%d')),
Contentline('DTSTART;VALUE=DATE:%s' % self.date_begin.strftime('%Y%m%d')), Contentline('DTSTART;VALUE=DATE:%s' % self.date_begin.strftime('%Y%m%d')),
Contentline('PERCENT-COMPLETE:0'), Contentline('PERCENT-COMPLETE:0'),
Contentline('END:VTODO'), Contentline('END:VTODO'),
Contentline('END:VCALENDAR') Contentline('END:VCALENDAR')
]) ])
return c.to_ical() return c.to_ical()
class Client: class Client:
def __init__(self, url): def __init__(self, url):
self.client = caldav.DAVClient(url) self.client = caldav.DAVClient(url)
@@ -76,19 +57,19 @@ class Client:
def add_event(self, event): def add_event(self, event):
self.calendar.add_event(task.to_ical()) self.calendar.add_event(task.to_ical())
#conf = load('./configuration.yml')
ref_date = datetime.date.today() + datetime.timedelta(days=3) # conf = load('./configuration.yml')
ref_date = datetime.date.today() + datetime.timedelta(days=-3)
with open('./tasks.yml', 'r') as content_file: with open('./tasks.yml', 'r') as content_file:
tasks_conf = load(content_file.read()) tasks_conf = load(content_file.read())
task_list = [] task_list = []
for task_name, task_data in tasks_conf.items(): for task_name, task_data in tasks_conf.items():
t = Task(ref_date, task_name, task_data); t = Task(ref_date, task_name, task_data)
if (t.date_begin == ref_date): if t.date_begin.date() == ref_date:
task_list.append(t) task_list.append(t)
import ipdb; ipdb.set_trace()
url = "https://ggentile:ID0t'8h6@cloud.dorfsvald.net/webdav/ggentile/calendar.ics/" url = "https://ggentile:ID0t'8h6@cloud.dorfsvald.net/webdav/ggentile/calendar.ics/"
client = Client(url) client = Client(url)
for task in task_list: for task in task_list:

View File

@@ -1,38 +1,52 @@
#see http://dateutil.readthedocs.io/en/stable/rrule.html # see http://dateutil.readthedocs.io/en/stable/rrule.html
# freq, dtstart=None, interval=1, wkst=None, count=None, until=None, bysetpos=None,
# bymonth=None, bymonthday=None, byyearday=None, byeaster=None, byweekno=None, byweekday=None,
# byhour=None, byminute=None, bysecond=None, cache=False
# Where freq must be one of YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, or SECONDLY.
reglement_piano: reglement_piano:
periodicity: monthly start:
date-begin: -4 freq: MONTHLY
date-end: -1 bymonthday: -5
duration:
days: 5
title: Règlement Piano title: Règlement Piano
body: 578236 body: 578236
priority: 1 priority: 1
loyer: loyer:
periodicity: monthly start:
date-begin: 1 freq: MONTHLY
date-end: 10 bymonthday: 1
duration:
days: 10
title: Loyer title: Loyer
body: https://fr.foncia.com/login\nA797GEA4EP23F body: https://fr.foncia.com/login\nA797GEA4EP23F
priority: 1 priority: 1
declaration_polemploi: declaration_polemploi:
periodicity: monthly start:
date-begin: 1 freq: MONTHLY
date-end: 10 bymonthday: 1
duration:
days: 10
title: Déclaration Pôle Emploi title: Déclaration Pôle Emploi
body: N/A body: N/A
priority: 1 priority: 1
declaration_ae: declaration_ae:
periodicity: monthly start:
only: 1,4,7,10 freq: MONTHLY
date-begin: 1 bymonthday: 1
date-end: 10 bymonth: [1,4,7,10]
duration:
days: 30
title: Déclaration AE title: Déclaration AE
body: N/A body: N/A
priority: 1 priority: 1
facturation_x2i: facturation_x2i:
periodicity: monthly start:
date-begin: -4 freq: MONTHLY
date-end: -1 bymonthday: -4
duration:
days: 4
title: Facture X2I title: Facture X2I
body: N/A body: N/A
priority: 1 priority: 1