added import safeguard to refresh_events; heeded a deprecation warning

This commit is contained in:
eclipse 2025-09-03 16:42:33 +02:00
parent bf9731d521
commit 75f914dd16
2 changed files with 66 additions and 60 deletions

View File

@ -27,4 +27,5 @@ DELETE_OUTPUT_DIRECTORY = True
# get event list from calendar(s)
sys.path.append(os.curdir + "/utils")
import refresh_events
from refresh_events import refresh_events as r_events
r_events()

View File

@ -153,67 +153,72 @@ def handleAttachments(events: list) -> list:
# print(event) #DEBUG
return events
# create vobject calendar
vcal = vobject.newFromBehavior("vcalendar")
# establish connection to caldav server
with caldav.DAVClient(url=server_url, username=cal_user, password=cal_pass) as dav_client:
# establish connection to calendar
dav_cal = dav_client.calendar(url=cal_url)
def refresh_events():
# create vobject calendar
vcal = vobject.newFromBehavior("vcalendar")
# put all events from caldav calendar into vobject calendar
for e in dav_cal.events():
for ev in e.instance.contents["vevent"]:
vcal.add(ev)
# establish connection to caldav server
with caldav.DAVClient(url=server_url, username=cal_user, password=cal_pass) as dav_client:
# establish connection to calendar
dav_cal = dav_client.calendar(url=cal_url)
# we only want events belonging to a specific category
events = [e for e in vcal.getChildren() if "categories" in e.contents.keys() and cal_category in map(lambda x: x.value[0], e.contents["categories"])]
# put all events from caldav calendar into vobject calendar
for e in dav_cal.events():
for ev in e.vobject_instance.contents["vevent"]:
vcal.add(ev)
# extract event data
events = extractEventData(events)
# we only want events belonging to a specific category
events = [e for e in vcal.getChildren() if "categories" in e.contents.keys() and cal_category in map(lambda x: x.value[0], e.contents["categories"])]
# fix dates and datetimes
for e in events:
for k in e.keys():
if k in cal_prop_datetimes:
# fix a CalDAV/vobject/icalendar (?) bug where a dtend value which is a date only (no time) automatically moves one day into the future
if k == "dtend" and type(e[k]) == date:
e[k] = e[k] - timedelta(days=1)
# fix all datetimes so they can be compared and sorted
e[k] = fixDatetime(e[k])
# extract event data
events = extractEventData(events)
# keep only future events
events = [e for e in events if e["dtstart"] >= now]
# fix dates and datetimes
for e in events:
for k in e.keys():
if k in cal_prop_datetimes:
# fix a CalDAV/vobject/icalendar (?) bug where a dtend value which is a date only (no time) automatically moves one day into the future
if k == "dtend" and type(e[k]) == date:
e[k] = e[k] - timedelta(days=1)
# fix all datetimes so they can be compared and sorted
e[k] = fixDatetime(e[k])
# sort attached links into page links and image links
events = handleAttachments(events)
# keep only future events
events = [e for e in events if e["dtstart"] >= now]
# sort by start date
events.sort(key=lambda e: e["dtstart"])
# sort attached links into page links and image links
events = handleAttachments(events)
# add start date to metadata, also start time and/or end date
for e in events:
# start date
e["startdate"] = e["dtstart"].astimezone()
# start time (if not the default time)
if e["dtstart"].timetz() != time(23, 59, 59, tzinfo=now.tzinfo):
e["starttime"] = e["dtstart"].astimezone()
# end date (if different from start date)
if e["dtstart"].date() < e["dtend"].date():
e["enddate"] = e["dtend"].astimezone()
# sort by start date
events.sort(key=lambda e: e["dtstart"])
# read default metadata if present
default_metadata = None
if os.path.isfile(result_file + ".metadata"):
with open(result_file + ".metadata", "r") as f:
default_metadata = yaml.safe_load(f)
# add start date to metadata, also start time and/or end date
for e in events:
# start date
e["startdate"] = e["dtstart"].astimezone()
# start time (if not the default time)
if e["dtstart"].timetz() != time(23, 59, 59, tzinfo=now.tzinfo):
e["starttime"] = e["dtstart"].astimezone()
# end date (if different from start date)
if e["dtstart"].date() < e["dtend"].date():
e["enddate"] = e["dtend"].astimezone()
# write data as YAML
with open(result_file, 'w') as f:
f.write("---\n")
if default_metadata:
yaml.dump(default_metadata, f)
yaml.dump({"termine": events}, f)
# write current datetime to file
yaml.dump({"written_at": now}, f)
f.write("---\n")
# read default metadata if present
default_metadata = None
if os.path.isfile(result_file + ".metadata"):
with open(result_file + ".metadata", "r") as f:
default_metadata = yaml.safe_load(f)
# write data as YAML
with open(result_file, 'w') as f:
f.write("---\n")
if default_metadata:
yaml.dump(default_metadata, f)
yaml.dump({"termine": events}, f)
# write current datetime to file
yaml.dump({"written_at": now}, f)
f.write("---\n")
if __name__ == "__main__":
refresh_events()