<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def _result_to_dict(data, result, conf, source):
    '''
    Aggregates LDAP search result based on rules, returns a dictionary.

    Rules:
    Attributes tagged in the pillar config as 'attrs' or 'lists' are
    scanned for a 'key=value' format (non matching entries are ignored.

    Entries matching the 'attrs' tag overwrite previous values where
    the key matches a previous result.

    Entries matching the 'lists' tag are appended to list of values where
    the key matches a previous result.

    All Matching entries are then written directly to the pillar data
    dictionary as data[key] = value.

    For example, search result:

        { saltKeyValue': ['ntpserver=ntp.acme.local', 'foo=myfoo'],
          'saltList': ['vhost=www.acme.net', 'vhost=www.acme.local'] }

    is written to the pillar data dictionary as:

        { 'ntpserver': 'ntp.acme.local', 'foo': 'myfoo',
           'vhost': ['www.acme.net', 'www.acme.local'] }
    '''
    attrs = _config('attrs', conf) or []
    lists = _config('lists', conf) or []
    dict_key_attr = _config('dict_key_attr', conf) or 'dn'
    # TODO:
    # deprecate the default 'mode: split' and make the more
    # straightforward 'mode: map' the new default
    mode = _config('mode', conf) or 'split'
    if mode == 'map':
        data[source] = []
        for record in result:
            ret = {}
            if 'dn' in attrs or 'distinguishedName' in attrs:
                log.debug('dn: %s', record[0])
                ret['dn'] = record[0]
            record = record[1]
            log.debug('record: %s', record)
            for key in record:
                if key in attrs:
                    for item in record.get(key):
                        ret[key] = item
                if key in lists:
                    ret[key] = record.get(key)
            data[source].append(ret)
    elif mode == 'dict':
        data[source] = {}
        for record in result:
            ret = {}
            distinguished_name = record[0]
            log.debug('dn: %s', distinguished_name)
            if 'dn' in attrs or 'distinguishedName' in attrs:
                ret['dn'] = distinguished_name
            record = record[1]
            log.debug('record: %s', record)
            for key in record:
                if key in attrs:
                    for item in record.get(key):
                        ret[key] = item
                if key in lists:
                    ret[key] = record.get(key)
            if dict_key_attr in ['dn', 'distinguishedName']:
                dict_key = distinguished_name
            else:
                dict_key = ','.join(sorted(record.get(dict_key_attr, [])))
            try:
                data[source][dict_key].append(ret)
            except KeyError:
                data[source][dict_key] = [ret]
    elif mode == 'split':
        for key in result[0][1]:
            if key in attrs:
                for item in result.get(key):
                    skey, sval = item.split('=', 1)
                    data[skey] = sval
            elif key in lists:
                for item in result.get(key):
                    if '=' in item:
                        skey, sval = item.split('=', 1)
                        if skey not in data:
                            data[skey] = [sval]
                        else:
                            data[skey].append(sval)
    return data