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,42 +6,22 @@ import calendar
from yaml import load
from icalendar.parser import Contentlines, Contentline
from dateutil.rrule import rrule, FREQNAMES
import caldav
class Task:
def __init__(self, ref_date, name, data):
self.ref_date = ref_date
self.name = name
only = data['only'].split(',') if 'only' in data else None
if data['periodicity'] == 'monthly':
self.date_begin = self.parse_date_monthly(data['date-begin'], only)
self.date_end = self.parse_date_monthly(data['date-end'], only)
elif data['periodicity'] == 'weekly':
pass
data['start']['freq'] = FREQNAMES.index(data['start']['freq'])
self.date_begin = rrule(count=1, dtstart=ref_date, **data['start'])[0]
self.date_end = self.date_begin + datetime.timedelta(microseconds=-1, **data['duration'])
self.title = data['title']
self.body = data['body']
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):
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'))
@@ -67,6 +47,7 @@ class Task:
return c.to_ical()
class Client:
def __init__(self, url):
self.client = caldav.DAVClient(url)
@@ -76,19 +57,19 @@ class Client:
def add_event(self, event):
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:
tasks_conf = load(content_file.read())
task_list = []
for task_name, task_data in tasks_conf.items():
t = Task(ref_date, task_name, task_data);
if (t.date_begin == ref_date):
t = Task(ref_date, task_name, task_data)
if t.date_begin.date() == ref_date:
task_list.append(t)
import ipdb; ipdb.set_trace()
url = "https://ggentile:ID0t'8h6@cloud.dorfsvald.net/webdav/ggentile/calendar.ics/"
client = Client(url)
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:
periodicity: monthly
date-begin: -4
date-end: -1
start:
freq: MONTHLY
bymonthday: -5
duration:
days: 5
title: Règlement Piano
body: 578236
priority: 1
loyer:
periodicity: monthly
date-begin: 1
date-end: 10
start:
freq: MONTHLY
bymonthday: 1
duration:
days: 10
title: Loyer
body: https://fr.foncia.com/login\nA797GEA4EP23F
priority: 1
declaration_polemploi:
periodicity: monthly
date-begin: 1
date-end: 10
start:
freq: MONTHLY
bymonthday: 1
duration:
days: 10
title: Déclaration Pôle Emploi
body: N/A
priority: 1
declaration_ae:
periodicity: monthly
only: 1,4,7,10
date-begin: 1
date-end: 10
start:
freq: MONTHLY
bymonthday: 1
bymonth: [1,4,7,10]
duration:
days: 30
title: Déclaration AE
body: N/A
priority: 1
facturation_x2i:
periodicity: monthly
date-begin: -4
date-end: -1
start:
freq: MONTHLY
bymonthday: -4
duration:
days: 4
title: Facture X2I
body: N/A
priority: 1