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

@ -10,14 +10,14 @@ import os, os.path
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
if os.path.isfile(os.getcwd() + '/events.ini'):
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'):
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'):
project_root = os.path.dirname(os.path.realpath(__file__)) + '/..'
# OK no luck
@ -39,7 +39,7 @@ cal_category = "Veranstaltung"
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.
cal_properties = [ # taken from ical specification
# descriptive
# descriptive
"attach", "categories", "class", "comment", "description", "geo", "location", "percent-complete", "priority", "resources", "status", "summary",
# date and time
"completed", "dtend", "due", "dtstart", "duration", "freebusy", "transp",
@ -134,7 +134,7 @@ def handleAttachments(events: list) -> list:
parsed_url = urlparse(attachment)
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
# handle image link
if isImage:
# make internal link relative to "/termine"
@ -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)
# 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"])]
# 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)
# extract event data
events = extractEventData(events)
# 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)
# 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()