def uidgroups_by_type(calendar, itemtype):
    """Selects all items of type *itemtype* in the *calendar* and groups
    them into a dictionary based on their UID."""
    items = {}
    for item in calendar.items:
        if item.type != itemtype:
            continue
        uid = item[b'uid'][0].value
        if not uid in items:
            items[uid] = []
        items[uid].append(item)
    return items
