Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type object 'Calendar' has no attribute 'from_string' #1

Closed
oliviercailloux opened this issue Jan 2, 2017 · 4 comments
Closed

type object 'Calendar' has no attribute 'from_string' #1

oliviercailloux opened this issue Jan 2, 2017 · 4 comments

Comments

@oliviercailloux
Copy link

oliviercailloux commented Jan 2, 2017

Hi,
I can’t run sort_ics.py using version 3.8.1 of python-icalendar.
$ sudo apt-get install python-icalendar
$ python2 sort_ics.py 1.ics 2.ics

Traceback (most recent call last):
File "sort_ics.py", line 37, in
cal = Calendar.from_string(open(sys.argv[1], 'rb').read())
AttributeError: type object 'Calendar' has no attribute 'from_string'

I suspect this is due to a change in the API of Calendar related to v3 (but I might be wrong). Any idea whether I can solve this simply?
Thanks.

@pzystorm
Copy link

pzystorm commented Feb 7, 2021

Here is a fix that it's compiling (at least).
But honestly... I have a different understanding of "sorting". Maybe there are some more changes in the past on that icalender stuff...
My ics file before and after the sort script is not ordered at all
The events in my ics file have no UID, but a DTSTART, so that I commented out the two lines:

    if a.has_key("UID"):
        val = a["UID"]

My assumption was, there is a ordered list by using the DTSTART field of all events... but no... nothing...

Here is the full script so far:

#!/usr/bin/python

from icalendar import Calendar
import sys

def get_key_value(a):
    val = ""
    if a.has_key("UID"):
        val = a["UID"]
    elif a.has_key("DTSTART"):
        val = a["DTSTART"]
    elif a.has_key("DESCRIPTION"):
        val = a["DESCRIPTION"]
    elif a.has_key("SUMMARY"):
        val = a["SUMMARY"]
    elif a.has_key("SUMMARY"):
        val = a["SUMMARY"]

    return val

def uid_sort(a, b):
    # fixme: we could protect us here against (invalid) UID-less components
    a_val = get_key_value(a)
    b_val = get_key_value(b)

    if a_val > b_val:
        return 1
    elif a_val < b_val:
        return -1
    else:
        return 0

if len(sys.argv) < 3:
    print "Usage: sort_ics.py in.ics out.ics"
    sys.exit(1)

cal = Calendar.from_ical(open(sys.argv[1], 'rb').read())

cal.subcomponents.sort(uid_sort)

# comps = cal.subcomponents
# print comps
# comps.sort(uid_sort)
# print comps

f = open(sys.argv[2], 'wb')
f.write(cal.to_ical())
f.close()

@oliviercailloux
Copy link
Author

Thanks for the reply… but, I absolutely don’t recall what I was trying to do, and I am no longer interested in this feature. So this issue can be closed, from my point of view.

@pzystorm
Copy link

I am not the dev of this script, so i cant close it :)
But this problem is still open and I have encountered the same issue like you.
So I have just wanted to share the progress how we might bring back the script to a working state.

@martinp26
Copy link
Owner

Hi, I just made some changes for python and a simple comparison does work now. Please check if this solves your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants