import textwrap


def update_docstring(docstring, options_dict):
    """Update a method docstring by inserting option docstrings defined in
    the options dictionary.  The input docstring should define `{options}`
    at the location where the options docstring block should be inserted.

    Parameters
    ----------
    docstring : str
        Existing method docstring.

    options_dict : dict
        Dictionary defining the options that will be appended to the
        method docstring.  Dictionary keys are mapped to option names
        and each element of the dictionary should have the format
        (default value, docstring, type).

    Returns
    -------
    docstring : str
        Updated method docstring.
    """

    options_str = []
    for i, (k, v) in enumerate(sorted(options_dict.items())):

        option_str = ''
        if i == 0:
            option_str += '%s : %s\n' % (k, v[2].__name__)
        else:
            option_str += ' ' * 8 + '%s : %s\n' % (k, v[2].__name__)

        option_doc = v[1]
        option_doc += ' (default : %s)' % v[0]
        option_doc = textwrap.wrap(option_doc, 72 - 12)
        option_str += ' ' * 12 + ('\n' + ' ' * 12).join(option_doc)
        options_str += [option_str]

    options_str = '\n\n'.join(options_str)

    return docstring.format(options=options_str)