import re


def parse_show_ip_bgp_route_map(raw_result):
    """
    Parse the 'show ip bgp route-map' command raw output.

    :param str raw_result: vtysh raw result string.
    :rtype: dict
    :return: The parsed result of the show ip bgp route-map command in a \
        dictionary of the form:

     ::

        {
            '1':
                {
                            'action': 'deny',
                            'set_parameters': '',
                            'as_path_exclude': '20 30 40',
                            'match_parameters': '',
                            'prefix_list': 'List2',
                            'ipv6_prefix_list': 'List2-6'
                },
            '2':
                {
                            'action': 'permit',
                            'set_parameters': '',
                            'match_parameters': '',
                            'prefix_list': None,
                }
            '3':
                {
                            'action': 'permit',
                            'set_parameters': '',
                            'match_parameters': '',
                            'prefix_list': 'List1',
                }
        }
    """

    rmap_re = (
        r'Entry\s(?P<entry_number>\d+):\n'
        r'\s+action\s:\s(?P<action>\w+) \n'
        r'\s+Set\sparameters\s:(?P<set_parameters>[\S]*)\n'
        r'(\s+as_path_exclude\s:\s(?P<as_path_exclude>[\d ]+))?'
        r'\s+Match\sparameters\s:(?P<match_parameters>[\S]*)\n?'
        r'(\s+prefix_list\s:\s(?P<prefix_list>[\w-]+) \n?)?'
        r'(\s+ipv6_prefix_list\s:\s(?P<ipv6_prefix_list>[\w_\-]+) \n?)?'
    )

    result = {}

    for output in re.finditer(rmap_re, raw_result):
        entry = output.groupdict()
        result[entry['entry_number']] = entry

        if result[entry['entry_number']]['prefix_list'] is None:
            del result[entry['entry_number']]['prefix_list']
        if result[entry['entry_number']]['ipv6_prefix_list'] is None:
            del result[entry['entry_number']]['ipv6_prefix_list']
        if result[entry['entry_number']]['as_path_exclude'] is None:
            del result[entry['entry_number']]['as_path_exclude']

        del result[entry['entry_number']]['entry_number']

    assert result
    return result