# Buggy code
def import_event(vevent, collection, locale, batch, random_uid):
    """import one event into collection, let user choose the collection"""

    # print all sub-events
    for sub_event in vevent:
        if not batch:
            event = Event.fromVEvents(
                [sub_event], calendar=collection.default_calendar_name, locale=locale
            )
            echo(event.event_description)

    # get the calendar to insert into
    if batch or len(collection.writable_names) == 1:
        calendar_name = collection.default_calendar_name
    else:
        choice = list()
        for num, name in enumerate(collection.writable_names):
            choice.append("{}({})".format(name, num))
        choice = ", ".join(choice)
        while True:
            value = prompt(
                "Which calendar do you want to import to? \n{}".format(choice),
                default=collection.default_calendar_name,
            )
            try:
                number = int(value)
                calendar_name = collection.writable_names[number]
                break
            except (ValueError, IndexError):
                matches = filter(
                    lambda x: x.startswith(value), collection.writable_names
                )
                if len(matches) == 1:
                    calendar_name = matches[0]
                    break
            echo("invalid choice")

    if batch or confirm(
        "Do you want to import this event into `{}`?".format(calendar_name)
    ):
        ics = aux.ics_from_list(vevent, random_uid)
        try:
            collection.new(
                Item(ics.to_ical().decode("utf-8")), collection=calendar_name
            )
        except DuplicateUid:
            if batch or confirm(
                "An event with the same UID already exists. Do you want to update it?"
            ):
                collection.force_update(
                    Item(ics.to_ical().decode("utf-8")), collection=calendar_name
                )
            else:
                logger.warn("Not importing event with UID `{}`".format(event.uid))


# Traceback (AttributeError)
(vdir)gour@atmarama ~/p/vdir> khal import -a gour ~/Downloads/gour-calendar.ics
03.09.2015: Krema vedske mudrosti <2015-09-03 Thu +2w>
Repeat: FREQ=WEEKLY;INTERVAL=2
Do you want to import this event into `None`? [y/N]: y
Traceback (most recent call last):
File "/home/gour/prj/vdir/bin/khal", line 11, in <module>
sys.exit(main_khal())
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/click/core.py", line 700, in __call__
return self.main(*args, **kwargs)
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/click/core.py", line 680, in main
rv = self.invoke(ctx)
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/click/core.py", line 1027, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/click/core.py", line 873, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/click/core.py", line 508, in invoke
return callback(*args, **kwargs)
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/click/decorators.py", line 16, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/khal/cli.py", line 298, in import_ics
random_uid=random_uid
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/khal/controllers.py", line 216, in import_ics
import_event(vevent, collection, conf['locale'], batch, random_uid)
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/khal/controllers.py", line 257, in import_event
collection=calendar_name)
File "/home/gour/prj/vdir/local/lib/python2.7/site-packages/khal/khalendar/khalendar.py", line 330, in new
self._calnames[event.calendar].new(event)
AttributeError: 'Item' object has no attribute 'calendar'

# Fixed code
def import_event(vevent, collection, locale, batch, random_uid):
    """import one event into collection, let user choose the collection"""

    # print all sub-events
    for sub_event in vevent:
        if not batch:
            event = Event.fromVEvents(
                [sub_event], calendar=collection.default_calendar_name, locale=locale
            )
            echo(event.event_description)

    # get the calendar to insert into
    if batch or len(collection.writable_names) == 1:
        calendar_name = collection.writable_names[0]
    else:
        choice = list()
        for num, name in enumerate(collection.writable_names):
            choice.append("{}({})".format(name, num))
        choice = ", ".join(choice)
        while True:
            value = prompt(
                "Which calendar do you want to import to? \n{}".format(choice),
                default=collection.default_calendar_name,
            )
            try:
                number = int(value)
                calendar_name = collection.writable_names[number]
                break
            except (ValueError, IndexError):
                matches = filter(
                    lambda x: x.startswith(value), collection.writable_names
                )
                if len(matches) == 1:
                    calendar_name = matches[0]
                    break
            echo("invalid choice")

    if batch or confirm(
        "Do you want to import this event into `{}`?".format(calendar_name)
    ):
        ics = aux.ics_from_list(vevent, random_uid)
        try:
            collection.new(
                Item(ics.to_ical().decode("utf-8")), collection=calendar_name
            )
        except DuplicateUid:
            if batch or confirm(
                "An event with the same UID already exists. Do you want to update it?"
            ):
                collection.force_update(
                    Item(ics.to_ical().decode("utf-8")), collection=calendar_name
                )
            else:
                logger.warn("Not importing event with UID `{}`".format(event.uid))