Adding a last run tracking file to be able to created skipped days tasks

This commit is contained in:
2023-09-26 11:06:05 +02:00
parent 2f9dd0f98f
commit 7cb394f804
2 changed files with 42 additions and 25 deletions

3
.gitignore vendored
View File

@@ -1,4 +1,5 @@
.idea/ .idea/
.last.run
configuration.yml configuration.yml
tasks.yml tasks.yml
test/ test/

64
main.py
View File

@@ -1,12 +1,11 @@
import datetime import datetime
import os
from caldav.lib.error import NotFoundError
from caldav.objects import Todo
from yaml import load, FullLoader from yaml import load, FullLoader
import icalendar
from icalendar.parser import Contentlines, Contentline from icalendar.parser import Contentlines, Contentline
from dateutil.rrule import rrule, FREQNAMES from dateutil.rrule import rrule, FREQNAMES
import caldav import caldav
from caldav.lib.error import NotFoundError
APP_ID = 'CaldavRecurringTask' APP_ID = 'CaldavRecurringTask'
@@ -66,6 +65,9 @@ class Client:
if self.calendar is None: if self.calendar is None:
raise LookupError('No calendar named "{}" found'.format(calendar_name)) raise LookupError('No calendar named "{}" found'.format(calendar_name))
def todos(self, include_completed=True):
return self.calendar.todos(include_completed=include_completed)
def add_todo(self, new_task): def add_todo(self, new_task):
self.calendar.add_todo(new_task.to_ical()) self.calendar.add_todo(new_task.to_ical())
@@ -82,28 +84,42 @@ if __name__ == "__main__":
with open('./configuration.yml', 'r') as configuration_file: with open('./configuration.yml', 'r') as configuration_file:
conf = load(configuration_file.read(), Loader=FullLoader) conf = load(configuration_file.read(), Loader=FullLoader)
ref_date = datetime.date.today() + datetime.timedelta(days=2) last_run_file_path = './.last.run'
with open('tasks.yml', 'r') as content_file: if os.path.isfile(last_run_file_path):
tasks_conf = load(content_file.read(), Loader=FullLoader) with open(last_run_file_path, 'r') as last_run_file:
run_date_str = last_run_file.readline()
run_date = datetime.datetime.strptime(run_date_str, '%Y-%m-%d') + datetime.timedelta(days=1)
run_date = run_date.date()
else:
run_date = datetime.date.today()
task_list = [] while run_date <= datetime.date.today():
for task_name, task_data in tasks_conf.items(): ref_date = run_date + datetime.timedelta(days=2)
t = Task(ref_date, task_name, task_data) with open('tasks.yml', 'r') as content_file:
if t.date_begin.date() == ref_date: tasks_conf = load(content_file.read(), Loader=FullLoader)
task_list.append(t)
if task_list: task_list = []
server = conf['dav_server'] for task_name, task_data in tasks_conf.items():
client = Client(server['url'], server['user'], server['pass'], server['calendar']) t = Task(ref_date, task_name, task_data)
previous_tasks = client.todos(include_completed=True) if t.date_begin.date() == ref_date:
for task in task_list: task_list.append(t)
if not client.get_todo_by_uid(task.uid):
# Removing previous completed tasks
for t in previous_tasks:
if t.icalendar_component.get('uid').startswith(f"{APP_ID}-{task.name}")\
and t.icalendar_component.get('status') == 'COMPLETED':
t.delete()
client.add_todo(task) if task_list:
server = conf['dav_server']
client = Client(server['url'], server['user'], server['pass'], server['calendar'])
previous_tasks = client.todos(include_completed=True)
for task in task_list:
if not client.get_todo_by_uid(task.uid):
# Removing previous completed tasks
for t in previous_tasks:
if t.icalendar_component.get('uid').startswith(f"{APP_ID}-{task.name}")\
and t.icalendar_component.get('status') == 'COMPLETED':
t.delete()
print('process finished: {} tasks created'.format(len(task_list))) client.add_todo(task)
print('process finished for date {}: {} tasks created\n'.format(run_date, len(task_list)))
run_date = run_date + datetime.timedelta(days=1)
with open(last_run_file_path, 'w') as last_run_file:
last_run_file.write(str(datetime.date.today()))