python-caldav / caldav

Apache License 2.0
313 stars 91 forks source link

enhance documentation by info how to deal with vCATEGORY object #376

Closed VinzenzStauner closed 5 months ago

VinzenzStauner commented 5 months ago

Hi there, I found this labrary very useful as I am currently writing a client app for Caldav and Webdav data. Really great work!

I had an issue how to read the vCATEGORY object using the follwoing code. It would be very useful to show the tweak how to convert ical objects to a string representation using to_ical().decode() in the documentation or example code. Would that be possible?

Here the code snippet:

elementsDateTime = ["DTSTART", "DTSTAMP", "CREATED", "DUE", "LAST-MODIFIED", "COMPLETED"]

tasks = calendar.search(todo=True,include_completed=False)

for task in tasks:
    for item in task.icalendar_component:
      if(item in elementsDateTime):
          print(item +  ": " + str(task.icalendar_component[item].dt))
      elif(item == "CATEGORIES"):
          print(item +  ": " + str(task.icalendar_component[item]))
      else:
          print(item +  ": " + str(task.icalendar_component[item]))

It always returned a vCATEGORY object instead of a string representation:

CATEGORIES: <icalendar.prop.vCategory object at 0x7fdf49131e10>

Solution: the line where I read the categories must be changed to the following code using the to_ical().decode() functions:

print(item + ": " + str(task.icalendar_component[item].to_ical().decode()))

With that tweak, the categories where printed fine like this:

CATEGORIES: Test

tobixen commented 5 months ago

For the purpose above, I think you should rather access task.data rather than task.icalendar_component. I also think the documentation for how to access task.icalendar_component.categories belongs to the icalendar library and not to the caldav library.