# Copyright 2015-2017 ProfitBricks GmbH
# Author: Benjamin Drung <benjamin.drung@profitbricks.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re


def find_item_by_name(list_, namegetter, name):
    """
    Find a item a given list by a matching name.

    The search for the name is done in this relaxing way:

    - exact name match
    - case-insentive name match
    - attribute starts with the name
    - attribute starts with the name (case insensitive)
    - name appears in the attribute
    - name appears in the attribute (case insensitive)

    :param    list_: A list of elements
    :type     list_: ``list``

    :param    namegetter: Function that returns the name for a given
                          element in the list
    :type     namegetter: ``function``

    :param    name: Name to search for
    :type     name: ``str``

    """
    matching_items = [i for i in list_ if namegetter(i) == name]
    if len(matching_items) == 0:
        prog = re.compile(re.escape(name) + '$', re.IGNORECASE)
        matching_items = [i for i in list_ if prog.match(namegetter(i))]
    if len(matching_items) == 0:
        prog = re.compile(re.escape(name))
        matching_items = [i for i in list_ if prog.match(namegetter(i))]
    if len(matching_items) == 0:
        prog = re.compile(re.escape(name), re.IGNORECASE)
        matching_items = [i for i in list_ if prog.match(namegetter(i))]
    if len(matching_items) == 0:
        prog = re.compile(re.escape(name))
        matching_items = [i for i in list_ if prog.search(namegetter(i))]
    if len(matching_items) == 0:
        prog = re.compile(re.escape(name), re.IGNORECASE)
        matching_items = [i for i in list_ if prog.search(namegetter(i))]
    return matching_items

@@ -651,16 +651,1279 @@
 rt re%0A%0A%0A
+def ask(question, options, default):%0A    %22%22%22%0A    Ask the user a question with a list of allowed answers (like yes or no).%0A%0A    The user is presented with a question and asked to select an answer from%0A    the given options list. The default will be returned if the user enters%0A    nothing. The user is asked to repeat his answer if his answer does not%0A    match any of the allowed anwsers.%0A%0A    :param    question: Question to present to the user (without question mark)%0A    :type     question: %60%60str%60%60%0A%0A    :param    options: List of allowed anwsers%0A    :type     options: %60%60list%60%60%0A%0A    :param    default: Default answer (if the user enters no text)%0A    :type     default: %60%60str%60%60%0A%0A    %22%22%22%0A    assert default in options%0A%0A    question += %22 (%7B%7D)? %22.format(%22/%22.join(o.upper() if o == default else o for o in options))%0A    selected = None%0A    while selected not in options:%0A        selected = input(question).strip().lower()%0A        if selected == %22%22:%0A            selected = default%0A        else:%0A            if selected not in options:%0A                question = %22Please type '%7B%7D'%7Bcomma%7D or '%7B%7D': %22.format(%0A                    %22', '%22.join(options%5B:-1%5D), options%5B-1%5D,%0A                    comma=',' if len(options) %3E 2 else '',%0A                )%0A    return selected%0A%0A%0A
 def find

Add helper function ask()