Manage an existing collection node.

def mode_collection():
    """
    Manage an existing collection node.
    """
    print globals()['mode_collection'].__doc__
    collection_node_id = existing_node_input()
    value = render_value_for_node(collection_node_id)
    if not value:
        return None
    print "Collection length: {0}".format(len(value))
    print safe_dump(value, default_flow_style=False)

    item_attr_list = []
    if len(value):
        for key in value.items()[0][1].keys():
            m = re.match(r'(.*) \((\d+)\)', key)
            item_attr_list.append(m.group(1))

    selection = True
    while selection:
        selection = select([
            'View collection',
            'Add item',
            'Add attribute',
            'Remove item',
            'Remove attribute',
            'Purge collection'
            ])
        if selection == 'View collection':
            print safe_dump(value, default_flow_style=False)
        elif selection == 'Purge collection':
            confirm = raw_input("Delete all {0} items and their {1} attributes from the collection? y/n\n".format(len(value.keys()), len(item_attr_list)))
            if confirm == 'y':
                delete_node(node_id=collection_node_id)
                purge_collection(value.keys())
        elif selection == 'Remove item':
            item_node_id = existing_node_input()
            if item_node_id < 0:
                return
            value = render_value_for_node(item_node_id)
            print safe_dump(value, default_flow_style=False)
            confirm = raw_input("Delete this node and it's attributes? y/n\n").format(len(value.keys()), len(item_attr_list))
            if confirm == 'y':
                delete_node(node_id=item_node_id)
                purge_collection(value.keys())

        elif selection == 'Add item':
            result = select_node(node_id=collection_node_id)
            collection_name = result[0].get('name')

            add_item_with_attributes_to_collection(
                    collection_name=collection_name,
                    collection_node_id=collection_node_id,
                    item_attr_list=item_attr_list)
        elif selection == 'Remove attribute':
            print "Select the attribute that will be removed:"
            attribute_selection = select(item_attr_list)
            if attribute_selection:
                confirm = raw_input("Delete attribute '{0}' from all {1} items in the collection? y/n\n".format(attribute_selection, len(value.keys())))
                if confirm == 'y':
                    for item_key, item in value.items():
                        for key in item.keys():
                            m = re.match(r'(.*) \((\d+)\)', key)
                            if m.group(1) == attribute_selection:
                                delete_node(node_id=m.group(2))
                                break
        elif selection == 'Add attribute':
            item_attr = raw_input("Add a collection item attribute name: ")
            if item_attr:
                item_index = 0
                for item_key, item in value.items():
                    item_index += 1
                    m = re.match(r'(.*) \((\d+)\)', item_key)
                    item_value = render_value_for_node(m.group(2))
                    print "item {0} of {1} items".format(item_index, len(value))
                    print safe_dump(item_value, default_flow_style=False)

                    new_attr_value = raw_input("Enter item attribute value for '{0}': ".format(item_attr))
                    # set value to none if it's an empty string
                    new_attr_value = new_attr_value if len(new_attr_value) else None
                    item_attr_node_id = insert_node(name=item_attr, value=new_attr_value)

                    insert_node_node(node_id=m.group(2), target_node_id=item_attr_node_id)
        # Update the value after each operation
        value = render_value_for_node(collection_node_id)