Demonstrates how to read the response from a match_names query when peyotl's wrap_response option is
    used.

    If the context_name is not recognized, the attempt to match_names will generate a ValueError exception.
    Here this is caught, and we call the tnrs/contexts web service to get the list of valid context_names
        to provide the user of the script with some hints.

def match_and_print(name_list, context_name, do_approximate_matching, include_dubious, include_deprecated,
                    include_subtree, output):
    """Demonstrates how to read the response from a match_names query when peyotl's wrap_response option is
    used.

    If the context_name is not recognized, the attempt to match_names will generate a ValueError exception.
    Here this is caught, and we call the tnrs/contexts web service to get the list of valid context_names
        to provide the user of the script with some hints.
    """
    from peyotl.sugar import tnrs
    try:
        # Perform the match_names, and return the peyotl wrapper around the response.
        result = ot_tnrs_match_names(name_list,
                                     context_name=context_name,
                                     do_approximate_matching=do_approximate_matching,
                                     include_dubious=include_dubious,
                                     include_deprecated=include_deprecated,
                                     tnrs_wrapper=tnrs)
    except Exception as x:
        msg = str(x)
        if 'is not a valid context name' in msg and context_name is not None:
            # Here is a wrapper around the call to get the context names
            valid_contexts = tnrs.contexts()
            m = 'The valid context names are the strings in the values of the following "tnrs/contexts" dict:\n'
            sys.stderr.write(m)
            epp = pprint.PrettyPrinter(indent=4, stream=sys.stderr)
            epp.pprint(valid_contexts)
        raise RuntimeError('ot-tnrs-match-names: exception raised. {}'.format(x))
    # The code below demonstrates how to access the information from the response in the wrapper
    #   that is created by using the wrap_response option in the call
    output.write('A v2/tnrs/match_names query was performed using: {} \n'.format(tnrs.endpoint))
    output.write('The taxonomy being served by that server is:')
    output.write(' {}'.format(result.taxonomy.source))
    output.write(' by {}\n'.format(result.taxonomy.author))
    output.write('Information for the taxonomy can be found at {}\n'.format(result.taxonomy.weburl))
    output.write('{} out of {} queried name(s) were matched\n'.format(len(result.matched_name_ids), len(name_list)))
    output.write('{} out of {} queried name(s) were unambiguously matched\n'.format(len(result.unambiguous_name_ids),
                                                                                    len(name_list)))
    output.write('The context_name for the matched names was "{}"'.format(result.context))
    if result.context_inferred:
        output.write(' (this context was inferred based on the matches).\n')
    else:
        output.write(' (this context was supplied as an argument to speed up the name matching).\n')
    output.write('The name matching result(s) used approximate/fuzzy string matching? {}\n'.format(
        result.includes_approximate_matches))
    output.write('The name matching result(s) included dubious names? {}\n'.format(result.includes_dubious_names))
    output.write('The name matching result(s) included deprecated taxa? {}\n'.format(result.includes_deprecated_taxa))
    for name in name_list:
        match_tuple = result[name]
        output.write('The query name "{}" produced {} result(s):\n'.format(name, len(match_tuple)))
        for match_ind, match in enumerate(match_tuple):
            output.write('  Match #{}\n'.format(match_ind))
            output.write('    OTT ID (ot:ottId) = {}\n'.format(match.ott_id))
            output.write('    name (ot:ottTaxonName) = "{}"\n'.format(match.name))
            output.write('    query was matched using fuzzy/approximate string matching? {}\n'.format(
                match.is_approximate_match))
            output.write('    match score = {}\n'.format(match.score))
            output.write('    query name is a junior synonym of this match? {}\n'.format(match.is_synonym))
            output.write('    is deprecated from OTT? {}\n'.format(match.is_deprecated))
            output.write('    is dubious taxon? {}\n'.format(match.is_dubious))
            if match.synonyms:
                output.write('    known synonyms: "{}"\n'.format('", "'.join(match.synonyms)))
            else:
                output.write('    known synonyms: \n')
            output.write('    OTT flags for this taxon: {}\n'.format(match.flags))
            output.write('    The taxonomic rank associated with this name is: {}\n'.format(match.rank))
            output.write('    The nomenclatural code for this name is: {}\n'.format(match.nomenclature_code))
            output.write('    The (unstable) node ID in the current taxomachine instance is: {}\n'.format(
                match.taxomachine_node_id))
        if len(match_tuple) == 1:
            sys.stderr.write('\nOnly one match found, so we will request the info on the ancestors, too...\n')
            match = match_tuple[0]
            ott_id = match.ott_id
            fetch_and_write_taxon_info(id_list=[ott_id], include_anc=True, list_tips=False, output=output)
            if include_subtree:
                from peyotl.sugar import taxonomy
                subtree = taxonomy.subtree(ott_id)['subtree']
                output.write('The taxononmic subtree is:\n')
                output.write(subtree)
                output.write('\n')
        else:
            if include_subtree:
                sys.stderr.write(
                    '\nMultiple matches found - ancestor info and subtreesuppressed.\nSee ot-taxon-info.py and ot-taxon-subtree.py which can be called with an OTT ID\n')
            else:
                sys.stderr.write(
                    '\nMultiple matches found - ancestor info suppressed.\nSee ot-taxon-info.py which can be called with an OTT ID\n')