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) # get event list from calendar(s)
sys.path.append(os.curdir + "/utils") sys.path.append(os.curdir + "/utils")
import refresh_events from refresh_events import refresh_events as r_events
r_events()

View File

@ -10,14 +10,14 @@ import os, os.path
from urllib.parse import urlparse from urllib.parse import urlparse
# find out where events.ini lives; that's the project root dir # find out where events.ini lives; that's the project root dir
# check current working directory # check current working directory
if os.path.isfile(os.getcwd() + '/events.ini'): if os.path.isfile(os.getcwd() + '/events.ini'):
project_root = os.getcwd() project_root = os.getcwd()
# check this script's location dir # check this script's location dir
elif os.path.isfile(os.path.dirname(os.path.realpath(__file__)) + '/events.ini'): elif os.path.isfile(os.path.dirname(os.path.realpath(__file__)) + '/events.ini'):
project_root = os.path.dirname(os.path.realpath(__file__)) project_root = os.path.dirname(os.path.realpath(__file__))
# check parent dir of this script's dir # check parent dir of this script's dir
elif os.path.isfile(os.path.dirname(os.path.realpath(__file__)) + '/../events.ini'): elif os.path.isfile(os.path.dirname(os.path.realpath(__file__)) + '/../events.ini'):
project_root = os.path.dirname(os.path.realpath(__file__)) + '/..' project_root = os.path.dirname(os.path.realpath(__file__)) + '/..'
# OK no luck # OK no luck
@ -39,7 +39,7 @@ cal_category = "Veranstaltung"
result_file = project_root + '/content/pages/termine.md' result_file = project_root + '/content/pages/termine.md'
# List of icalendar properties that will be passed on to Pelican. At this time, all properties are used except those regarding alarms. # List of icalendar properties that will be passed on to Pelican. At this time, all properties are used except those regarding alarms.
cal_properties = [ # taken from ical specification cal_properties = [ # taken from ical specification
# descriptive # descriptive
"attach", "categories", "class", "comment", "description", "geo", "location", "percent-complete", "priority", "resources", "status", "summary", "attach", "categories", "class", "comment", "description", "geo", "location", "percent-complete", "priority", "resources", "status", "summary",
# date and time # date and time
"completed", "dtend", "due", "dtstart", "duration", "freebusy", "transp", "completed", "dtend", "due", "dtstart", "duration", "freebusy", "transp",
@ -134,7 +134,7 @@ def handleAttachments(events: list) -> list:
parsed_url = urlparse(attachment) parsed_url = urlparse(attachment)
isImage = True if parsed_url.path.split(".")[-1] in image_suffixes else False isImage = True if parsed_url.path.split(".")[-1] in image_suffixes else False
# print(f"attachment is {attachment}, parsed_url is {parsed_url} and isImage is {isImage}") #DEBUG # print(f"attachment is {attachment}, parsed_url is {parsed_url} and isImage is {isImage}") #DEBUG
# handle image link # handle image link
if isImage: if isImage:
# make internal link relative to "/termine" # make internal link relative to "/termine"
@ -153,67 +153,72 @@ def handleAttachments(events: list) -> list:
# print(event) #DEBUG # print(event) #DEBUG
return events return events
# create vobject calendar
vcal = vobject.newFromBehavior("vcalendar")
# establish connection to caldav server def refresh_events():
with caldav.DAVClient(url=server_url, username=cal_user, password=cal_pass) as dav_client: # create vobject calendar
# establish connection to calendar vcal = vobject.newFromBehavior("vcalendar")
dav_cal = dav_client.calendar(url=cal_url)
# put all events from caldav calendar into vobject calendar # establish connection to caldav server
for e in dav_cal.events(): with caldav.DAVClient(url=server_url, username=cal_user, password=cal_pass) as dav_client:
for ev in e.instance.contents["vevent"]: # establish connection to calendar
vcal.add(ev) 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"])]
# extract event data # put all events from caldav calendar into vobject calendar
events = extractEventData(events) for e in dav_cal.events():
for ev in e.vobject_instance.contents["vevent"]:
vcal.add(ev)
# 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 # extract event data
for e in events: events = extractEventData(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])
# keep only future events # fix dates and datetimes
events = [e for e in events if e["dtstart"] >= now] 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 # keep only future events
events = handleAttachments(events) events = [e for e in events if e["dtstart"] >= now]
# sort by start date # sort attached links into page links and image links
events.sort(key=lambda e: e["dtstart"]) events = handleAttachments(events)
# add start date to metadata, also start time and/or end date # sort by start date
for e in events: events.sort(key=lambda e: e["dtstart"])
# 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()
# read default metadata if present # add start date to metadata, also start time and/or end date
default_metadata = None for e in events:
if os.path.isfile(result_file + ".metadata"): # start date
with open(result_file + ".metadata", "r") as f: e["startdate"] = e["dtstart"].astimezone()
default_metadata = yaml.safe_load(f) # 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 # read default metadata if present
with open(result_file, 'w') as f: default_metadata = None
f.write("---\n") if os.path.isfile(result_file + ".metadata"):
if default_metadata: with open(result_file + ".metadata", "r") as f:
yaml.dump(default_metadata, f) default_metadata = yaml.safe_load(f)
yaml.dump({"termine": events}, f)
# write current datetime to file # write data as YAML
yaml.dump({"written_at": now}, f) with open(result_file, 'w') as f:
f.write("---\n") 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()